Implemented serialization machinery.

This commit is contained in:
Davi de Castro Reis 2011-05-16 11:26:18 -03:00
parent 5a4ba7516c
commit c630eb2a70
3 changed files with 15 additions and 5 deletions

View File

@ -44,10 +44,10 @@ MPHTable::~MPHTable() {
} }
void MPHTable::clear() { void MPHTable::clear() {
delete [] g_; if (!deserialized_) delete [] g_;
g_ = NULL; g_ = NULL;
g_size_ = 0; g_size_ = 0;
delete [] ranktable_; if (!deserialized_) delete [] ranktable_;
ranktable_ = NULL; ranktable_ = NULL;
ranktable_size_ = 0; ranktable_size_ = 0;
// TODO(davi) implement me // TODO(davi) implement me
@ -115,7 +115,7 @@ void MPHTable::Assigning(
vector<bool> marked_vertices(n_ + 1); vector<bool> marked_vertices(n_ + 1);
// Initialize vector of half nibbles with all bits set. // Initialize vector of half nibbles with all bits set.
g_size_ = static_cast<uint32_t>(ceil(n_/4.0)); g_size_ = static_cast<uint32_t>(ceil(n_/4.0));
delete [] g_; if (!deserialized_) delete [] g_;
g_ = NULL; g_ = NULL;
uint8_t* g = new uint8_t[g_size_]; uint8_t* g = new uint8_t[g_size_];
memset(g, std::numeric_limits<uint8_t>::max(), g_size_); memset(g, std::numeric_limits<uint8_t>::max(), g_size_);
@ -169,7 +169,7 @@ void MPHTable::Ranking() {
uint32_t size = k_ >> 2U; uint32_t size = k_ >> 2U;
ranktable_size_ = static_cast<uint32_t>( ranktable_size_ = static_cast<uint32_t>(
ceil(n_ / static_cast<double>(k_))); ceil(n_ / static_cast<double>(k_)));
delete [] ranktable_; if (!deserialized_) delete [] ranktable_;
ranktable_ = NULL; ranktable_ = NULL;
uint32_t* ranktable = new uint32_t[ranktable_size_]; uint32_t* ranktable = new uint32_t[ranktable_size_];
memset(ranktable, 0, ranktable_size_*sizeof(uint32_t)); memset(ranktable, 0, ranktable_size_*sizeof(uint32_t));
@ -228,6 +228,7 @@ bool MPHTable::deserialize(const char* serialized_memory) {
g_ = reinterpret_cast<const uint8_t*>(serialized_memory + sizeof(MPHTable)); g_ = reinterpret_cast<const uint8_t*>(serialized_memory + sizeof(MPHTable));
ranktable_ = reinterpret_cast<const uint32_t*>( ranktable_ = reinterpret_cast<const uint32_t*>(
serialized_memory + sizeof(MPHTable) + g_size_); serialized_memory + sizeof(MPHTable) + g_size_);
deserialized_ = true;
return true; return true;
} }

View File

@ -24,7 +24,8 @@ class MPHTable {
public: public:
MPHTable(double c = 1.23, uint8_t b = 7) : MPHTable(double c = 1.23, uint8_t b = 7) :
c_(c), b_(b), m_(0), n_(0), k_(0), r_(0), 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(); ~MPHTable();
template <class SeededHashFcn, class ForwardIterator> template <class SeededHashFcn, class ForwardIterator>
@ -76,6 +77,8 @@ class MPHTable {
// perfect hash function graph. // perfect hash function graph.
uint32_t hash_seed_[3]; uint32_t hash_seed_[3];
bool deserialized_;
static const uint8_t valuemask[]; static const uint8_t valuemask[];
static void set_2bit_value(uint8_t *d, uint32_t i, uint8_t v) { static void set_2bit_value(uint8_t *d, uint32_t i, uint8_t v) {
d[(i >> 2)] &= ((v << ((i & 3) << 1)) | valuemask[i & 3]); d[(i >> 2)] &= ((v << ((i & 3) << 1)) | valuemask[i & 3]);
@ -113,6 +116,7 @@ bool MPHTable::Reset(ForwardIterator begin, ForwardIterator end) {
Assigning(edges, queue); Assigning(edges, queue);
std::vector<TriGraph::Edge>().swap(edges); std::vector<TriGraph::Edge>().swap(edges);
Ranking(); Ranking();
deserialized_ = false;
return true; return true;
} }

View File

@ -33,5 +33,10 @@ int main(int argc, char** argv) {
cerr << endl; cerr << endl;
sort(ids.begin(), ids.end()); sort(ids.begin(), ids.end());
for (vector<int>::size_type i = 0; i < ids.size(); ++i) assert(ids[i] == static_cast<vector<int>::value_type>(i)); for (vector<int>::size_type i = 0; i < ids.size(); ++i) assert(ids[i] == static_cast<vector<int>::value_type>(i));
char* serialized = new char[mph_table.serialize_bytes_needed()];
mph_table.serialize(serialized);
SimpleMPHTable<string> other_mph_table;
other_mph_table.deserialize(serialized);
} }