DAWG is highlevel C++ wrapper of dawgdic library. It allows you to store template data in very compact and effective Directed acyclic word graph data structure.
This is header-only library, so installation is not required. Just put it into your include path or project. Tested on:
- g++ 4.8
- clang++ 3.5.0
BuildFactory<double> builder;
builder.insert("HELLKITIE",23.1);
builder.insert("HELLO",0.5);
builder.insert("HELM",23.1);
shared_ptr<Dictionary<double>> dict = builder.build();
for(Dictionary<double>::ConstIterator it = dict->begin("HELL");it!=dict->end();++it){
cout << "[" <<it->key <<","<<it->value <<"]\n";
}
Output
[HELLKITIE,23.1]
[HELLO,0.5]
Example of ISerializable implementation:
struct FootballTeam : public ISerializable {
vector<size_t> players;
FootballTeam(initializer_list<size_t> nums): players(nums) {}
FootballTeam() {} //necessarily
virtual bool serialize(ostream& os) const {
size_t ts = players.size();
if(!os.write((const char *)(&ts),sizeof(size_t))) return false;
if(!os.write((const char *)(&players[0]), sizeof(size_t)*ts)) return false;
return true;
}
virtual bool deserialize(istream& is) {
size_t ts;
if (!is.read((char *)(&ts), sizeof(size_t))) return false;
players.resize(ts);
if (!is.read((char *)(&players[0]),sizeof(size_t)*ts)) return false;
return true;
}
};
Serialization:
BuildFactory<FootballTeam> builder;
builder.insert("Bucaneers",{2,12,7,5,3});
builder.insert("Cardinals",{6,43,23,1});
shared_ptr<Dictionary<FootballTeam>> dict = builder.build();
ofstream ofs("bindict",ofstream::binary);
dict->serialize(ofs);
ofs.close();
dict->clear();
ifstream ifs("bindict",ifstream::binary);
dict->deserialize(ifs);
FootballTeam bucaneers = dict->getValue("Bucaneers");
for(int i = 0;i<bucaneers.players.size();++i){
cout<<"Player number: "<<bucaneers.players[i]<< "\n";
}
ifs.close();
Output:
Player number:2
Player number:12
Player number:7
Player number:5
Player number:3