【发布时间】:2014-12-28 21:26:42
【问题描述】:
我一直在调查一些关于使用自定义键的 std::tr1::unordered_map 的 boost 序列化的奇怪行为。在序列化一个密钥和序列化包含该密钥的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。这来自this boost ticket。将其包含在 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[&newKey]是如何找到东西的。&newKey是怎么进去的? -
newKey是key的反序列化版本。HashKey*类型的哈希运算符被覆盖。因此,当访问&newKey的哈希函数时,它返回的结果与&key的结果相同。 -
哈希值相同,但值不同。反序列化
newKey不会更改其地址。 -
@Barry 我知道
newKey的地址和key的地址不同,但是hash函数没有解决吗? -
不,哈希只是找到一个密钥将在哪个桶中,如果它存在的话。一旦它缩小到桶,它使用键相等来找到键。
标签: c++ serialization boost hash unordered-map