Skip to content

Commit

Permalink
Fix issue #44 - example to dump TriviallyCopyable types doesn't work any
Browse files Browse the repository at this point in the history
more
  • Loading branch information
greg7mdp committed Aug 13, 2022
1 parent d62b847 commit ca9f02f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
12 changes: 6 additions & 6 deletions examples/dump_nested.cc 100644 → 100755
Expand Up @@ -18,11 +18,11 @@ class MyMap : public phmap::flat_hash_map<K, phmap::flat_hash_set<V>>
{
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);
}
}

Expand All @@ -31,16 +31,16 @@ class MyMap : public phmap::flat_hash_map<K, phmap::flat_hash_set<V>>
phmap::BinaryInputArchive ar_in(filename.c_str());

size_t size;
ar_in.load(&size);
ar_in.loadBinary(&size);
this->reserve(size);

while (size--)
{
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));
}
Expand Down
26 changes: 26 additions & 0 deletions parallel_hashmap/phmap_dump.h
Expand Up @@ -158,6 +158,19 @@ class BinaryOutputArchive {
return true;
}

template<typename V>
typename std::enable_if<type_traits_internal::IsTriviallyCopyable<V>::value, bool>::type
saveBinary(const V& v) {
ofs_.write(reinterpret_cast<const char *>(&v), sizeof(V));
return true;
}

template<typename Map>
auto saveBinary(const Map& v) -> decltype(v.phmap_dump(*this), bool())
{
return v.phmap_dump(*this);
}

private:
std::ofstream ofs_;
};
Expand All @@ -174,6 +187,19 @@ class BinaryInputArchive {
return true;
}

template<typename V>
typename std::enable_if<type_traits_internal::IsTriviallyCopyable<V>::value, bool>::type
loadBinary(V* v) {
ifs_.read(reinterpret_cast<char *>(v), sizeof(V));
return true;
}

template<typename Map>
auto loadBinary(Map* v) -> decltype(v->phmap_load(*this), bool())
{
return v->phmap_load(*this);
}

private:
std::ifstream ifs_;
};
Expand Down

0 comments on commit ca9f02f

Please sign in to comment.