#if !defined(phmap_dump_h_guard_) #define phmap_dump_h_guard_ // --------------------------------------------------------------------------- // Copyright (c) 2019, Gregory Popovitch - greg7mdp@gmail.com // // providing dump/load/mmap_load // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // --------------------------------------------------------------------------- #include #include #include #include "phmap.h" namespace phmap { namespace type_traits_internal { #if defined(__GLIBCXX__) && __GLIBCXX__ < 20150801 template struct IsTriviallyCopyable : public std::integral_constant {}; #else template struct IsTriviallyCopyable : public std::is_trivially_copyable {}; #endif template struct IsTriviallyCopyable> { static constexpr bool value = IsTriviallyCopyable::value && IsTriviallyCopyable::value; }; } namespace priv { #if !defined(PHMAP_NON_DETERMINISTIC) && !defined(PHMAP_DISABLE_DUMP) // ------------------------------------------------------------------------ // dump/load for raw_hash_set // ------------------------------------------------------------------------ template template bool raw_hash_set::phmap_dump(OutputArchive& ar) const { static_assert(type_traits_internal::IsTriviallyCopyable::value, "value_type should be trivially copyable"); ar.saveBinary(&size_, sizeof(size_t)); if (size_ == 0) return true; ar.saveBinary(&capacity_, sizeof(size_t)); ar.saveBinary(ctrl_, sizeof(ctrl_t) * (capacity_ + Group::kWidth + 1)); ar.saveBinary(slots_, sizeof(slot_type) * capacity_); return true; } template template bool raw_hash_set::phmap_load(InputArchive& ar) { static_assert(type_traits_internal::IsTriviallyCopyable::value, "value_type should be trivially copyable"); raw_hash_set().swap(*this); // clear any existing content ar.loadBinary(&size_, sizeof(size_t)); if (size_ == 0) return true; ar.loadBinary(&capacity_, sizeof(size_t)); // allocate memory for ctrl_ and slots_ initialize_slots(capacity_); ar.loadBinary(ctrl_, sizeof(ctrl_t) * (capacity_ + Group::kWidth + 1)); ar.loadBinary(slots_, sizeof(slot_type) * capacity_); return true; } // ------------------------------------------------------------------------ // dump/load for parallel_hash_set // ------------------------------------------------------------------------ template class RefSet, class Mtx_, class Policy, class Hash, class Eq, class Alloc> template bool parallel_hash_set::phmap_dump(OutputArchive& ar) const { static_assert(type_traits_internal::IsTriviallyCopyable::value, "value_type should be trivially copyable"); size_t submap_count = subcnt(); ar.saveBinary(&submap_count, sizeof(size_t)); for (size_t i = 0; i < sets_.size(); ++i) { auto& inner = sets_[i]; typename Lockable::UniqueLock m(const_cast(inner)); if (!inner.set_.phmap_dump(ar)) { std::cerr << "Failed to dump submap " << i << std::endl; return false; } } return true; } template class RefSet, class Mtx_, class Policy, class Hash, class Eq, class Alloc> template bool parallel_hash_set::phmap_load(InputArchive& ar) { static_assert(type_traits_internal::IsTriviallyCopyable::value, "value_type should be trivially copyable"); size_t submap_count = 0; ar.loadBinary(&submap_count, sizeof(size_t)); if (submap_count != subcnt()) { std::cerr << "submap count(" << submap_count << ") != N(" << N << ")" << std::endl; return false; } for (size_t i = 0; i < submap_count; ++i) { auto& inner = sets_[i]; typename Lockable::UniqueLock m(const_cast(inner)); if (!inner.set_.phmap_load(ar)) { std::cerr << "Failed to load submap " << i << std::endl; return false; } } return true; } #endif // !defined(PHMAP_NON_DETERMINISTIC) && !defined(PHMAP_DISABLE_DUMP) } // namespace priv // ------------------------------------------------------------------------ // BinaryArchive // File is closed when archive object is destroyed // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ class BinaryOutputArchive { public: BinaryOutputArchive(const char *file_path) { ofs_.open(file_path, std::ofstream::out | std::ofstream::trunc | std::ofstream::binary); } bool saveBinary(const void *p, size_t sz) { ofs_.write(reinterpret_cast(p), sz); return true; } private: std::ofstream ofs_; }; class BinaryInputArchive { public: BinaryInputArchive(const char * file_path) { ifs_.open(file_path, std::ofstream::in | std::ofstream::binary); } bool loadBinary(void* p, size_t sz) { ifs_.read(reinterpret_cast(p), sz); return true; } private: std::ifstream ifs_; }; } // namespace phmap #ifdef CEREAL_SIZE_TYPE template using PhmapTrivCopyable = typename phmap::type_traits_internal::IsTriviallyCopyable; namespace cereal { // Overload Cereal serialization code for phmap::flat_hash_map // ----------------------------------------------------------- template void save(typename std::enable_if::value && PhmapTrivCopyable::value, typename cereal::BinaryOutputArchive>::type &ar, phmap::flat_hash_map const &hmap) { hmap.phmap_dump(ar); } template void load(typename std::enable_if::value && PhmapTrivCopyable::value, typename cereal::BinaryInputArchive>::type &ar, phmap::flat_hash_map &hmap) { hmap.phmap_load(ar); } // Overload Cereal serialization code for phmap::parallel_flat_hash_map // -------------------------------------------------------------------- template void save(typename std::enable_if::value && PhmapTrivCopyable::value, typename cereal::BinaryOutputArchive>::type &ar, phmap::parallel_flat_hash_map const &hmap) { hmap.phmap_dump(ar); } template void load(typename std::enable_if::value && PhmapTrivCopyable::value, typename cereal::BinaryInputArchive>::type &ar, phmap::parallel_flat_hash_map &hmap) { hmap.phmap_load(ar); } // Overload Cereal serialization code for phmap::flat_hash_set // ----------------------------------------------------------- template void save(typename std::enable_if::value, typename cereal::BinaryOutputArchive>::type &ar, phmap::flat_hash_set const &hset) { hset.phmap_dump(ar); } template void load(typename std::enable_if::value, typename cereal::BinaryInputArchive>::type &ar, phmap::flat_hash_set &hset) { hset.phmap_load(ar); } // Overload Cereal serialization code for phmap::parallel_flat_hash_set // -------------------------------------------------------------------- template void save(typename std::enable_if::value, typename cereal::BinaryOutputArchive>::type &ar, phmap::parallel_flat_hash_set const &hset) { hset.phmap_dump(ar); } template void load(typename std::enable_if::value, typename cereal::BinaryInputArchive>::type &ar, phmap::parallel_flat_hash_set &hset) { hset.phmap_load(ar); } } #endif #endif // phmap_dump_h_guard_