diff --git a/examples/dump_load.cc b/examples/dump_load.cc index 7f461db..020ce52 100644 --- a/examples/dump_load.cc +++ b/examples/dump_load.cc @@ -40,7 +40,7 @@ void dump_load_parallel_flat_hash_map() { mp2.load(ar_in); } - for (const auto& n : mp2) + for (const auto& n : mp2) std::cout << "key: " << n.first << ", value: " << n.second << "\n"; } diff --git a/parallel_hashmap/phmap_dump.h b/parallel_hashmap/phmap_dump.h index bd36ac9..1c49057 100644 --- a/parallel_hashmap/phmap_dump.h +++ b/parallel_hashmap/phmap_dump.h @@ -19,8 +19,6 @@ // limitations under the License. // --------------------------------------------------------------------------- -#include -#include #include #include #include @@ -44,14 +42,15 @@ struct IsTriviallyCopyable> { namespace container_internal { -//// raw_hash_set +// ------------------------------------------------------------------------ +// dump/load for raw_hash_set +// ------------------------------------------------------------------------ template template bool raw_hash_set::dump(OutputArchive& ar) { static_assert(type_traits_internal::IsTriviallyCopyable::value, - "value_type should be dumpable"); + "value_type should be trivially copyable"); - typename OutputArchive::Guard guard(&ar); if (!ar.dump(size_)) { std::cerr << "Failed to dump size_" << std::endl; return false; @@ -81,9 +80,8 @@ template template bool raw_hash_set::load(InputArchive& ar) { static_assert(type_traits_internal::IsTriviallyCopyable::value, - "value_type should be dumpable"); - - typename InputArchive::Guard guard(&ar); + "value_type should be trivially copyable"); + raw_hash_set().swap(*this); // clear any existing content if (!ar.load(&size_)) { std::cerr << "Failed to load size_" << std::endl; return false; @@ -111,7 +109,9 @@ bool raw_hash_set::load(InputArchive& ar) { return true; } -////// parallel_hash_set +// ------------------------------------------------------------------------ +// dump/load for parallel_hash_set +// ------------------------------------------------------------------------ template class RefSet, class Mtx_, @@ -119,9 +119,8 @@ template bool parallel_hash_set::dump(OutputArchive& ar) { static_assert(type_traits_internal::IsTriviallyCopyable::value, - "value_type should be dumpable"); + "value_type should be trivially copyable"); - typename OutputArchive::Guard guard(&ar); if (! ar.dump(subcnt())) { std::cerr << "Failed to dump meta!" << std::endl; return false; @@ -144,9 +143,8 @@ template bool parallel_hash_set::load(InputArchive& ar) { static_assert(type_traits_internal::IsTriviallyCopyable::value, - "value_type should be dumpable"); + "value_type should be trivially copyable"); - typename InputArchive::Guard guard(&ar); size_t submap_count = 0; if (!ar.load(&submap_count)) { std::cerr << "Failed to load submap count!" << std::endl; @@ -168,119 +166,60 @@ bool parallel_hash_set::load(InputArch } return true; } -} // namesapce container_internal +} // namespace container_internal -// ArchiveOutput & ArchiveInput - -#define CHECK_FILE(f) { \ - if (!f.is_open()) { \ - std::cerr << "File is not open!" << std::endl; \ - return false; \ - } \ -} - -template -class ArchiveGuard { -public: - ArchiveGuard(Archive* ar): ar_(ar) { - if (ar_->guard_ == NULL) { - ar_->guard_ = this; - } - }; - ~ArchiveGuard() { - if (ar_ && ar_->guard_ == this) { - ar_->finish(); - } - } -private: - Archive* ar_; -}; +// ------------------------------------------------------------------------ +// BinaryArchive +// File is closed when archive object is destroyed +// ------------------------------------------------------------------------ +// ------------------------------------------------------------------------ +// ------------------------------------------------------------------------ class BinaryOutputArchive { public: - using Guard = ArchiveGuard; - - BinaryOutputArchive(const std::string& file_path): offset_(0), guard_(NULL) { - ofs_.open(file_path.c_str(), std::ios_base::binary); + BinaryOutputArchive(const char *file_path) { + ofs_.open(file_path, std::ios_base::binary); } - virtual ~BinaryOutputArchive() { - finish(); - } - - bool dump(char* p, size_t sz) { - CHECK_FILE(ofs_); + bool dump(const char *p, size_t sz) { ofs_.write(p, sz); - offset_ += sz; return true; } template typename std::enable_if::value, bool>::type dump(const V& v) { - CHECK_FILE(ofs_); - ofs_.write(reinterpret_cast(const_cast(&v)), sizeof(V)); - offset_ += sizeof(V); + ofs_.write(reinterpret_cast(&v), sizeof(V)); return true; } - void finish() { - if (ofs_.is_open()) { - ofs_.close(); - offset_ = 0; - } - } - private: - friend class ArchiveGuard; std::ofstream ofs_; - size_t offset_; - Guard* guard_; }; class BinaryInputArchive { public: - using Guard = ArchiveGuard; - - BinaryInputArchive(const std::string& file_path): offset_(0), guard_(NULL) { - ifs_.open(file_path.c_str(), std::ios_base::binary); - } - - virtual ~BinaryInputArchive() { - finish(); + BinaryInputArchive(const char * file_path) { + ifs_.open(file_path, std::ios_base::binary); } bool load(char* p, size_t sz) { - CHECK_FILE(ifs_); ifs_.read(p, sz); - offset_ += sz; return true; } template typename std::enable_if::value, bool>::type load(V* v) { - CHECK_FILE(ifs_); - ifs_.read(reinterpret_cast(v), sizeof(V)); - offset_ += sizeof(V); + ifs_.read(reinterpret_cast(v), sizeof(V)); return true; } - void finish() { - if (ifs_.is_open()) { - ifs_.close(); - offset_ = 0; - } - } - private: - friend class ArchiveGuard; std::ifstream ifs_; - size_t offset_; - Guard* guard_; }; } // namespace phmap