Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief example how easy it is to put keys in different data structures
5 : *
6 : * - * They might even have complete other properties than KeySet, e.g. in
7 : * this case:
8 : * - duplicates are allowed
9 : * - after sorting order is given by metadata "order"
10 : *
11 : * It is easy to create any other data structure, with any properties
12 : *
13 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
14 : */
15 :
16 : #include <keyset.hpp>
17 :
18 : #include <algorithm>
19 : #include <iostream>
20 : #include <vector>
21 :
22 0 : bool keyOrder (kdb::Key k1, kdb::Key k2)
23 : {
24 0 : int o1 = k1.getMeta<int> ("order");
25 0 : int o2 = k2.getMeta<int> ("order");
26 0 : return o1 < o2;
27 : }
28 :
29 0 : int main ()
30 : {
31 0 : using namespace kdb;
32 :
33 0 : std::vector<Key> vc;
34 0 : vc.push_back (Key ("user:/key3/1", KEY_META, "order", "2", KEY_END));
35 0 : vc.push_back (Key ("user:/begin", KEY_META, "order", "1", KEY_END));
36 0 : vc.push_back (Key ("user:/key3/4", KEY_META, "order", "3", KEY_END));
37 0 : vc.push_back (Key ("user:/key3/dup", KEY_META, "order", "4", KEY_END));
38 0 : vc.push_back (Key ("user:/key3/dup", KEY_END));
39 0 : vc.push_back (Key ("user:/unordered", KEY_END));
40 0 : vc.push_back (Key ("user:/end", KEY_META, "order", "5", KEY_END));
41 :
42 0 : std::sort (vc.begin (), vc.end (), keyOrder);
43 :
44 0 : KeySet ks (20, KS_END);
45 0 : std::cout << "Our Vector with special ordering:" << std::endl;
46 0 : for (auto k : vc)
47 : {
48 :
49 0 : std::cout << k.getName () << std::endl;
50 0 : ks.append (k);
51 : }
52 : // now we have a keyset (of course again with KeySet ordering and
53 : // duplicates removed.
54 0 : std::cout << "\nNow KeySet:" << std::endl;
55 0 : for (auto && ks_i : ks)
56 : {
57 0 : Key k (ks_i);
58 0 : std::cout << k.getName () << std::endl;
59 : }
60 0 : }
|