From f8ffdc9d8ceb1dc575df4acbb4fd66ceabd802cb Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 27 Mar 2020 21:52:47 -0400 Subject: [PATCH] Add example demonstrating saving nested hashmaps to binary file. --- examples/dump_nested.cc | 88 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 examples/dump_nested.cc diff --git a/examples/dump_nested.cc b/examples/dump_nested.cc new file mode 100644 index 0000000..c207ba3 --- /dev/null +++ b/examples/dump_nested.cc @@ -0,0 +1,88 @@ +/* + * + * Example of dumping a map, containing values which are phmap maps or sets + * building this requires c++17 support + * + */ + +#include +#include + +template +class MyMap : public phmap::flat_hash_map> +{ +public: + using Set = phmap::flat_hash_set; + + void dump(const std::string &filename) + { + phmap::BinaryOutputArchive ar_out (filename.c_str()); + + ar_out.dump(this->size()); + for (auto& [k, v] : *this) + { + ar_out.dump(k); + v.dump(ar_out); + } + } + + void load(const std::string & filename) + { + phmap::BinaryInputArchive ar_in(filename.c_str()); + + size_t size; + ar_in.load(&size); + this->reserve(size); + + while (size--) + { + K k; + Set v; + + ar_in.load(&k); + v.load(ar_in); + + this->insert_or_assign(std::move(k), std::move(v)); + } + } + + void insert(K k, V v) + { + Set &set = (*this)[k]; + set.insert(v); + } + + friend std::ostream& operator<<(std::ostream& os, const MyMap& map) + { + for (const auto& [k, m] : map) + { + os << k << ": ["; + for (const auto& x : m) + os << x << ", "; + os << "]\n"; + } + return os; + } +}; + +int main() +{ + MyMap m; + m.insert(1, 5); + m.insert(1, 8); + m.insert(2, 3); + m.insert(1, 15); + m.insert(1, 27); + m.insert(2, 10); + m.insert(2, 13); + + std::cout << m << "\n"; + + m.dump("test_archive"); + m.clear(); + m.load("test_archive"); + + std::cout << m << "\n"; + + return 0; +}