File: nested_table.cc

package info (click to toggle)
libcuckoo 0.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,036 kB
  • sloc: cpp: 16,538; ansic: 72; python: 34; sh: 15; makefile: 9
file content (43 lines) | stat: -rw-r--r-- 1,367 bytes parent folder | download | duplicates (2)
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;
      }
    }
  }
}