Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
patricia_trie_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
39#include <print_rule.H>
40#include <tpl_patricia_trie.H>
41
42#include <algorithm>
43#include <cstdint>
44#include <iostream>
45#include <string>
46#include <vector>
47
48using namespace Aleph;
49
50namespace
51{
52template <typename UInt>
53std::vector<UInt> sorted_keys(const Array<UInt> & keys)
54{
55 std::vector<UInt> result;
56 result.reserve(keys.size());
57 for (const auto key : keys)
58 result.push_back(key);
59 std::sort(result.begin(), result.end());
60 return result;
61}
62
64{
65 std::cout << "[1] PatriciaSet: integer-key membership\n";
66 print_rule();
67
69 for (const auto id : {7U, 42U, 128U, 255U})
70 ids.insert(id);
71
72 std::cout << "contains 42: " << std::boolalpha << ids.contains(42)
73 << " (expect true)\n";
74 std::cout << "contains 99: " << ids.contains(99) << " (expect false)\n";
75
76 ids.erase(128);
77 std::cout << "contains 128 after erase: " << ids.contains(128)
78 << " (expect false)\n";
79
80 std::cout << "stored ids: ";
81 for (const auto id : sorted_keys(ids.keys()))
82 std::cout << id << " ";
83 std::cout << "\n\n";
84}
85
86void demo_map_lookup()
87{
88 std::cout << "[2] PatriciaMap: integer-key dictionary\n";
89 print_rule();
90
92 routes.insert(10, "default-lan");
93 routes.insert(42, "service-net");
94 routes.insert_or_assign(255, "broadcast");
95 routes.insert_or_assign(42, "service-net-v2");
96
97 if (const auto * value = routes.find(42); value != nullptr)
98 std::cout << "route 42: " << *value << "\n";
99
100 std::cout << "contains 255: " << routes.contains(255) << "\n";
101 std::cout << "contains 11: " << routes.contains(11) << "\n";
102
103 std::cout << "stored route ids: ";
104 for (const auto id : sorted_keys(routes.keys()))
105 std::cout << id << " ";
106 std::cout << "\n\n";
107}
108
110{
111 std::cout << "[3] Fixed-width bitwise keys\n";
112 print_rule();
113
115 bytes.insert(0);
116 bytes.insert(1);
117 bytes.insert(128);
118 bytes.insert(255);
119
120 std::cout << "PatriciaSet<uint8_t>::bit_width = "
122 std::cout << "byte keys: ";
123 for (const auto key : sorted_keys(bytes.keys()))
124 std::cout << static_cast<unsigned>(key) << " ";
125 std::cout << "\n\n";
126}
127} // namespace
128
129int main()
130{
131 std::cout << "\n=== Aleph::PatriciaSet / PatriciaMap ===\n\n";
132
136
137 std::cout << "Done.\n";
138 return 0;
139}
size_t size_t int32_t value
Definition ca-c-api.h:116
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:138
Compressed bitwise map for unsigned integral keys.
bool insert(const Key key, const Value &value)
Insert key with a copy of value, only if key is absent.
Compressed bitwise set for unsigned integral keys.
bool insert(const Key key)
Insert key if absent.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
STL namespace.
int keys[]
PATRICIA/crit-bit set and map for fixed-width unsigned integer keys.