1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
/* We demonstrate how to nest hash tables within one another, to store
* unstructured data, kind of like JSON. There's still the limitation that it's
* statically typed. */
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <libcuckoo/cuckoohash_map.hh>
typedef libcuckoo::cuckoohash_map<std::string, std::string> InnerTable;
typedef libcuckoo::cuckoohash_map<std::string, std::unique_ptr<InnerTable>> OuterTable;
int main() {
OuterTable tbl;
tbl.insert("bob", std::unique_ptr<InnerTable>(new InnerTable));
tbl.update_fn("bob", [](std::unique_ptr<InnerTable> &innerTbl) {
innerTbl->insert("nickname", "jimmy");
innerTbl->insert("pet", "dog");
innerTbl->insert("food", "bagels");
});
tbl.insert("jack", std::unique_ptr<InnerTable>(new InnerTable));
tbl.update_fn("jack", [](std::unique_ptr<InnerTable> &innerTbl) {
innerTbl->insert("friend", "bob");
innerTbl->insert("activity", "sleeping");
innerTbl->insert("language", "javascript");
});
{
auto lt = tbl.lock_table();
for (const auto &item : lt) {
std::cout << "Properties for " << item.first << std::endl;
auto innerLt = item.second->lock_table();
for (auto innerItem : innerLt) {
std::cout << "\t" << innerItem.first << " = " << innerItem.second
<< std::endl;
}
}
}
}
|