使用比较器提升序列化映射
Boost serialize map with comparator
有人可以帮助我了解如何使用比较器序列化地图。保存地图后知道比较器 class,但地图不知道比较器字段。
bool operator() (const ScalarT &a, const ScalarT &b) const{
return (a - someField < b);
}
啊。很好的问题。这是boost中序列化的一个限制。
在有状态比较器的情况下,它不会序列化比较器状态。
在 SSCCE 中演示该问题:Live On Coliru,打印出
====== original order
3 -> three
2 -> two
1 -> one
====== deserialized order
1 -> one
2 -> two
3 -> three
这是有道理的,因为否则将要求所有比较器都可序列化。这会带来 hard 问题(如果 Cmp 是 std::function<bool(K)>
怎么办?如果实际值是 lambda 怎么办?)。
所以,长话短说:硬着头皮把你的比较器也序列化:
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <iostream>
#include <sstream>
template <typename T>
struct StateFullCmp {
bool invert_;
StateFullCmp(bool invert) : invert_(invert) {}
bool operator()(T const&a, T const& b) const {
return invert_? (b<a) : (a<b);
}
};
namespace boost { namespace serialization {
template <typename Ar, typename T>
void serialize(Ar& ar, StateFullCmp<T>& cmp, unsigned) {
ar & cmp.invert_;
}
} }
int main() {
using MyMap = std::map<int, std::string, StateFullCmp<int> >;
std::stringstream ss;
{
MyMap const m {
{ { 1, "one" }, { 2, "two" }, { 3, "three" } },
StateFullCmp<int> { true } // inverted order
};
std::cout << "====== original order\n";
for (auto& e : m)
std::cout << e.first << " -> " << e.second << "\n";
boost::archive::text_oarchive oa(ss);
auto cmp_copy = m.key_comp();
oa << cmp_copy << m;
}
{
boost::archive::text_iarchive ia(ss);
MyMap::key_compare cmp_state { false }; // default initialization
ia >> cmp_state;
MyMap deserialized(cmp_state);
ia >> deserialized;
std::cout << "====== deserialized order\n";
for (auto& e : deserialized)
std::cout << e.first << " -> " << e.second << "\n";
}
}
现在打印:
====== original order
3 -> three
2 -> two
1 -> one
====== deserialized order
3 -> three
2 -> two
1 -> one
有人可以帮助我了解如何使用比较器序列化地图。保存地图后知道比较器 class,但地图不知道比较器字段。
bool operator() (const ScalarT &a, const ScalarT &b) const{
return (a - someField < b);
}
啊。很好的问题。这是boost中序列化的一个限制。
在有状态比较器的情况下,它不会序列化比较器状态。
在 SSCCE 中演示该问题:Live On Coliru,打印出
====== original order
3 -> three
2 -> two
1 -> one
====== deserialized order
1 -> one
2 -> two
3 -> three
这是有道理的,因为否则将要求所有比较器都可序列化。这会带来 hard 问题(如果 Cmp 是 std::function<bool(K)>
怎么办?如果实际值是 lambda 怎么办?)。
所以,长话短说:硬着头皮把你的比较器也序列化:
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
#include <iostream>
#include <sstream>
template <typename T>
struct StateFullCmp {
bool invert_;
StateFullCmp(bool invert) : invert_(invert) {}
bool operator()(T const&a, T const& b) const {
return invert_? (b<a) : (a<b);
}
};
namespace boost { namespace serialization {
template <typename Ar, typename T>
void serialize(Ar& ar, StateFullCmp<T>& cmp, unsigned) {
ar & cmp.invert_;
}
} }
int main() {
using MyMap = std::map<int, std::string, StateFullCmp<int> >;
std::stringstream ss;
{
MyMap const m {
{ { 1, "one" }, { 2, "two" }, { 3, "three" } },
StateFullCmp<int> { true } // inverted order
};
std::cout << "====== original order\n";
for (auto& e : m)
std::cout << e.first << " -> " << e.second << "\n";
boost::archive::text_oarchive oa(ss);
auto cmp_copy = m.key_comp();
oa << cmp_copy << m;
}
{
boost::archive::text_iarchive ia(ss);
MyMap::key_compare cmp_state { false }; // default initialization
ia >> cmp_state;
MyMap deserialized(cmp_state);
ia >> deserialized;
std::cout << "====== deserialized order\n";
for (auto& e : deserialized)
std::cout << e.first << " -> " << e.second << "\n";
}
}
现在打印:
====== original order
3 -> three
2 -> two
1 -> one
====== deserialized order
3 -> three
2 -> two
1 -> one