From ca9f02fda378da225a0017022598d3e8dcd5f81c Mon Sep 17 00:00:00 2001 From: greg7mdp Date: Fri, 12 Aug 2022 20:24:24 -0400 Subject: [PATCH] Fix issue #44 - example to dump TriviallyCopyable types doesn't work any more --- examples/dump_nested.cc | 12 ++++++------ parallel_hashmap/phmap_dump.h | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) mode change 100644 => 100755 examples/dump_nested.cc diff --git a/examples/dump_nested.cc b/examples/dump_nested.cc old mode 100644 new mode 100755 index c207ba3..259c191 --- a/examples/dump_nested.cc +++ b/examples/dump_nested.cc @@ -18,11 +18,11 @@ public: { phmap::BinaryOutputArchive ar_out (filename.c_str()); - ar_out.dump(this->size()); + ar_out.saveBinary(this->size()); for (auto& [k, v] : *this) { - ar_out.dump(k); - v.dump(ar_out); + ar_out.saveBinary(k); + ar_out.saveBinary(v); } } @@ -31,7 +31,7 @@ public: phmap::BinaryInputArchive ar_in(filename.c_str()); size_t size; - ar_in.load(&size); + ar_in.loadBinary(&size); this->reserve(size); while (size--) @@ -39,8 +39,8 @@ public: K k; Set v; - ar_in.load(&k); - v.load(ar_in); + ar_in.loadBinary(&k); + ar_in.loadBinary(&v); this->insert_or_assign(std::move(k), std::move(v)); } diff --git a/parallel_hashmap/phmap_dump.h b/parallel_hashmap/phmap_dump.h index bf91c05..93eda8c 100644 --- a/parallel_hashmap/phmap_dump.h +++ b/parallel_hashmap/phmap_dump.h @@ -158,6 +158,19 @@ public: return true; } + template + typename std::enable_if::value, bool>::type + saveBinary(const V& v) { + ofs_.write(reinterpret_cast(&v), sizeof(V)); + return true; + } + + template + auto saveBinary(const Map& v) -> decltype(v.phmap_dump(*this), bool()) + { + return v.phmap_dump(*this); + } + private: std::ofstream ofs_; }; @@ -174,6 +187,19 @@ public: return true; } + template + typename std::enable_if::value, bool>::type + loadBinary(V* v) { + ifs_.read(reinterpret_cast(v), sizeof(V)); + return true; + } + + template + auto loadBinary(Map* v) -> decltype(v->phmap_load(*this), bool()) + { + return v->phmap_load(*this); + } + private: std::ifstream ifs_; };