From c630eb2a70b7aaa66dedcfec92f1bba694df206a Mon Sep 17 00:00:00 2001 From: Davi de Castro Reis Date: Mon, 16 May 2011 11:26:18 -0300 Subject: [PATCH] Implemented serialization machinery. --- cxxmph/mph_table.cc | 9 +++++---- cxxmph/mph_table.h | 6 +++++- cxxmph/mph_table_test.cc | 5 +++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cxxmph/mph_table.cc b/cxxmph/mph_table.cc index 3c74c2a..fae8e98 100644 --- a/cxxmph/mph_table.cc +++ b/cxxmph/mph_table.cc @@ -44,10 +44,10 @@ MPHTable::~MPHTable() { } void MPHTable::clear() { - delete [] g_; + if (!deserialized_) delete [] g_; g_ = NULL; g_size_ = 0; - delete [] ranktable_; + if (!deserialized_) delete [] ranktable_; ranktable_ = NULL; ranktable_size_ = 0; // TODO(davi) implement me @@ -115,7 +115,7 @@ void MPHTable::Assigning( vector marked_vertices(n_ + 1); // Initialize vector of half nibbles with all bits set. g_size_ = static_cast(ceil(n_/4.0)); - delete [] g_; + if (!deserialized_) delete [] g_; g_ = NULL; uint8_t* g = new uint8_t[g_size_]; memset(g, std::numeric_limits::max(), g_size_); @@ -169,7 +169,7 @@ void MPHTable::Ranking() { uint32_t size = k_ >> 2U; ranktable_size_ = static_cast( ceil(n_ / static_cast(k_))); - delete [] ranktable_; + if (!deserialized_) delete [] ranktable_; ranktable_ = NULL; uint32_t* ranktable = new uint32_t[ranktable_size_]; memset(ranktable, 0, ranktable_size_*sizeof(uint32_t)); @@ -228,6 +228,7 @@ bool MPHTable::deserialize(const char* serialized_memory) { g_ = reinterpret_cast(serialized_memory + sizeof(MPHTable)); ranktable_ = reinterpret_cast( serialized_memory + sizeof(MPHTable) + g_size_); + deserialized_ = true; return true; } diff --git a/cxxmph/mph_table.h b/cxxmph/mph_table.h index 1b713e5..51d26ea 100644 --- a/cxxmph/mph_table.h +++ b/cxxmph/mph_table.h @@ -24,7 +24,8 @@ class MPHTable { public: MPHTable(double c = 1.23, uint8_t b = 7) : c_(c), b_(b), m_(0), n_(0), k_(0), r_(0), - g_(NULL), g_size_(0), ranktable_(NULL), ranktable_size_(0) { } + g_(NULL), g_size_(0), ranktable_(NULL), ranktable_size_(0), + deserialized_(false) { } ~MPHTable(); template @@ -76,6 +77,8 @@ class MPHTable { // perfect hash function graph. uint32_t hash_seed_[3]; + bool deserialized_; + static const uint8_t valuemask[]; static void set_2bit_value(uint8_t *d, uint32_t i, uint8_t v) { d[(i >> 2)] &= ((v << ((i & 3) << 1)) | valuemask[i & 3]); @@ -113,6 +116,7 @@ bool MPHTable::Reset(ForwardIterator begin, ForwardIterator end) { Assigning(edges, queue); std::vector().swap(edges); Ranking(); + deserialized_ = false; return true; } diff --git a/cxxmph/mph_table_test.cc b/cxxmph/mph_table_test.cc index d12f901..c9e91a8 100644 --- a/cxxmph/mph_table_test.cc +++ b/cxxmph/mph_table_test.cc @@ -33,5 +33,10 @@ int main(int argc, char** argv) { cerr << endl; sort(ids.begin(), ids.end()); for (vector::size_type i = 0; i < ids.size(); ++i) assert(ids[i] == static_cast::value_type>(i)); + + char* serialized = new char[mph_table.serialize_bytes_needed()]; + mph_table.serialize(serialized); + SimpleMPHTable other_mph_table; + other_mph_table.deserialize(serialized); }