使用boost序列化、std::tr1::unordered_map和自定义键的奇怪行为

Odd behavior using boost serialization, std::tr1::unordered_map, and a custom key

本文关键字:自定义 序列化 boost std tr1 使用 unordered map      更新时间:2023-10-16

我一直在研究一些关于使用自定义键对std::tr1::unordered_map进行增强序列化的奇怪行为。在序列化密钥和序列化包含密钥的unordered_map之间,四个成员有4种不同的情况:反序列化密钥、反序列化unordered_map、原始密钥、原始unordered_map

  • 使用原始密钥和原始unordered_map
  • 使用反序列化的密钥和反序列化的unordered_map
  • 使用具有原始unordered_map的反序列化密钥
  • 使用具有反序列化unordered_map的原始密钥

前两个案例正如您所期望的那样工作,但后两个案例没有正确映射。我在下面创建了一个最低限度的工作示例。请注意,unordered_map需要boost标头才能序列化。我已经把它附在底部了。

#include <cstdlib>
#include <unordered_map>
#include <string>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include "unordered_map.hpp"
class HashKey {
public:
    HashKey() = default;
    HashKey(const HashKey& orig) = default;
    virtual ~HashKey() = default;
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & const_cast<unsigned long &>(id);
    }
    inline bool operator==(const HashKey& key) const {
        return this->id == key.id;
    }
    struct KeyHasher {
        std::size_t operator()(const HashKey* key) const {
            return boost::hash<unsigned long>()(key->id);
        }
    };
private:
    static unsigned long int idCounter;
    const unsigned long int id = HashKey::idCounter;
};
unsigned long int HashKey::idCounter = 0;
int main(int argc, char** argv) {
    std::tr1::unordered_map<HashKey*,std::string,HashKey::KeyHasher> map;
    HashKey key;
    map[&key]="works!";
    {
        std::ofstream ofs("key.save");
        boost::archive::text_oarchive oa(ofs);
        oa << key;
        oa << map;
    }
    HashKey newKey;
    std::tr1::unordered_map<HashKey*,std::string,HashKey::KeyHasher> newMap;
    {
        std::ifstream ifs("key.save");
        boost::archive::text_iarchive ia(ifs);
        ia >> newKey;
        ia >> newMap;
    }
    std::cout<<"Result: "<<map[&key]<<"n";
    std::cout<<"Result: "<<newMap[&newKey]<<"n";
    std::cout<<"Result: "<<map[&newKey]<<"n";
    std::cout<<"Result: "<<newMap[&key]<<"n";

    return 0;
}

运行时此代码的输出为:

Result: works!
Result: works!
Result: 
Result: 

我不明白为什么最后两个案例不起作用。我检查了散列函数输出的值,它们是正确的。我怀疑这与用作键的指针的operator()==有关,但我不确定如何检查。我希望能够在我的代码中使用所有4种情况。有没有说明为什么这不起作用?谢谢


这是用于序列化哈希映射的unordered_map.hpp。这来自于这张助推票。将其包含在MWE:中

#ifndef  BOOST_SERIALIZATION_UNORDERED_MAP_HPP
#define BOOST_SERIALIZATION_UNORDERED_MAP_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// serialization/unordered_map.hpp:
// serialization for stl unordered_map templates
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . 
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//  See http://www.boost.org for updates, documentation, and revision history.
#include <boost/tr1/unordered_map.hpp>
#include <boost/config.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/unordered_collections_save_imp.hpp>
#include <boost/serialization/unordered_collections_load_imp.hpp>
#include <boost/serialization/split_free.hpp>
namespace boost { 
namespace serialization {
namespace stl {
// map input
template<class Archive, class Container>
struct archive_input_unordered_map
{
    inline void operator()(
        Archive &ar, 
        Container &s, 
        const unsigned int v
    ){
        typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
        detail::stack_construct<Archive, type> t(ar, v);
        // borland fails silently w/o full namespace
        ar >> boost::serialization::make_nvp("item", t.reference());
        std::pair<BOOST_DEDUCED_TYPENAME Container::const_iterator, bool> result = 
            s.insert(t.reference());
        // note: the following presumes that the map::value_type was NOT tracked
        // in the archive.  This is the usual case, but here there is no way
        // to determine that.  
        if(result.second){
            ar.reset_object_address(
                & (result.first->second),
                & t.reference().second
            );
        }
    }
};
// multimap input
template<class Archive, class Container>
struct archive_input_unordered_multimap
{
    inline void operator()(
        Archive &ar, 
        Container &s, 
        const unsigned int v
    ){
        typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
        detail::stack_construct<Archive, type> t(ar, v);
        // borland fails silently w/o full namespace
        ar >> boost::serialization::make_nvp("item", t.reference());
        BOOST_DEDUCED_TYPENAME Container::const_iterator result 
            = s.insert(t.reference());
        // note: the following presumes that the map::value_type was NOT tracked
        // in the archive.  This is the usual case, but here there is no way
        // to determine that.  
        ar.reset_object_address(
            & result->second,
            & t.reference()
        );
    }
};
} // stl
template<
    class Archive, 
    class Key, 
    class HashFcn, 
    class EqualKey,
    class Allocator
>
inline void save(
    Archive & ar,
    const std::tr1::unordered_map<
        Key, HashFcn, EqualKey, Allocator
    > &t,
    const unsigned int /*file_version*/
){
    boost::serialization::stl::save_unordered_collection<
        Archive, 
        std::tr1::unordered_map<
            Key, HashFcn, EqualKey, Allocator
        >
    >(ar, t);
}
template<
    class Archive, 
    class Key, 
    class HashFcn, 
    class EqualKey,
    class Allocator
>
inline void load(
    Archive & ar,
    std::tr1::unordered_map<
        Key, HashFcn, EqualKey, Allocator
    > &t,
    const unsigned int /*file_version*/
){
    boost::serialization::stl::load_unordered_collection<
        Archive,
        std::tr1::unordered_map<
            Key, HashFcn, EqualKey, Allocator
        >,
        boost::serialization::stl::archive_input_unordered_map<
            Archive, 
            std::tr1::unordered_map<
                Key, HashFcn, EqualKey, Allocator
            >
        >
    >(ar, t);
}
// split non-intrusive serialization function member into separate
// non intrusive save/load member functions
template<
    class Archive, 
    class Key, 
    class HashFcn, 
    class EqualKey,
    class Allocator
>
inline void serialize(
    Archive & ar,
    std::tr1::unordered_map<
        Key, HashFcn, EqualKey, Allocator
    > &t,
    const unsigned int file_version
){
    boost::serialization::split_free(ar, t, file_version);
}
// unordered_multimap
template<
    class Archive, 
    class Key, 
    class HashFcn, 
    class EqualKey,
    class Allocator
>
inline void save(
    Archive & ar,
    const std::tr1::unordered_multimap<
        Key, HashFcn, EqualKey, Allocator
    > &t,
    const unsigned int /*file_version*/
){
    boost::serialization::stl::save_unordered_collection<
        Archive, 
        std::tr1::unordered_multimap<
            Key, HashFcn, EqualKey, Allocator
        >
    >(ar, t);
}
template<
    class Archive, 
    class Key, 
    class HashFcn, 
    class EqualKey,
    class Allocator
>
inline void load(
    Archive & ar,
    std::tr1::unordered_multimap<
        Key, HashFcn, EqualKey, Allocator
    > &t,
    const unsigned int /*file_version*/
){
    boost::serialization::stl::load_unordered_collection<
        Archive,
        std::tr1::unordered_multimap<
            Key, HashFcn, EqualKey, Allocator
        >,
        boost::serialization::stl::archive_input_unordered_multimap<
            Archive, 
            std::tr1::unordered_multimap<
                Key, HashFcn, EqualKey, Allocator
            >
        >
    >(ar, t);
}
// split non-intrusive serialization function member into separate
// non intrusive save/load member functions
template<
    class Archive, 
    class Key, 
    class HashFcn, 
    class EqualKey,
    class Allocator
>
inline void serialize(
    Archive & ar,
    std::tr1::unordered_multimap<
        Key, HashFcn, EqualKey, Allocator
    > &t,
    const unsigned int file_version
){
    boost::serialization::split_free(ar, t, file_version);
}
} // namespace serialization
} // namespace boost
#endif // BOOST_SERIALIZATION_UNORDERED_MAP_HPP

所以说实话,我不完全理解发生了什么,因为我不知道如何:

newMap.contains(&newKey)

可能是真的。由于您将指针存储到键,所以在反序列化newKey时,它的地址不应该更改,因此它不可能在新映射中。也就是说,为了让一切变得更简单,你可能想做的只是。。。不使用指针:

typedef std::tr1::unordered_map<HashKey,
                                std::string,
                                HashKey::KeyHasher> MapType;
HashKey key;
MapType map;
map[key] = "works";
// serialize stuff...
HashKey newKey;
MapType newMap;
// deserialize stuff...
assert(map.contains(key));
assert(map.contains(newKey));
assert(newMap.contains(key));
assert(newMap.contains(newKey));

这需要将KeyHasher固定为使用HashKey&而不是HashKey*。如果两把钥匙相等,它们应该是可互换的。如果使用指针,则最终依赖于指针相等,这与值相等完全无关(p1 == p2表示*p1 == *p2,但不是相反)。

如果你真的坚持让KeyType是HashKey*,那么你可以做:

struct Pred {
    bool operator()(const HashKey* a, const HashKey* b) const {
        return (*a) == (*b);
    }
};
typedef std::tr1::unordered_map<HashKey*,
                                std::string,
                                HashKey::KeyHasher,
                                Pred> MapType;

但我建议第一种方式。