Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
radix_tree_example.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
40#include <print_rule.H>
41#include <tpl_radix_tree.H>
42
43#include <algorithm>
44#include <iostream>
45#include <string>
46#include <vector>
47
48using namespace Aleph;
49
50namespace
51{
53{
54 std::cout << "[1] Insert/find/erase walkthrough\n";
55 print_rule();
56
57 RadixTree<int> word_count;
58 word_count.insert("apple", 3);
59 word_count.insert("app", 1);
60 word_count.insert("application", 7);
61
62 std::cout << "apple: " << *word_count.find("apple") << " (expect 3)\n";
63 std::cout << "contains 'appl': " << std::boolalpha
64 << word_count.contains("appl") << " (expect false)\n";
65
66 word_count.insert_or_assign("app", 2);
67 std::cout << "app after insert_or_assign: " << *word_count.find("app")
68 << " (expect 2)\n";
69
70 word_count.erase("application");
71 std::cout << "contains 'application' after erase: "
72 << word_count.contains("application") << " (expect false)\n";
73 std::cout << "still contains 'app': " << word_count.contains("app")
74 << " (expect true)\n\n";
75}
76
78{
79 std::cout << "[2] Prefix autocomplete (keys_with_prefix)\n";
80 print_rule();
81
83 for (const auto & word :
84 {"cat", "car", "cart", "carbon", "care", "dog", "door"})
86
87 auto suggestions = dictionary.keys_with_prefix("car");
88 std::vector<std::string> sorted_suggestions;
89 for (const auto & w : suggestions)
90 sorted_suggestions.push_back(w);
91 std::sort(sorted_suggestions.begin(), sorted_suggestions.end());
92
93 std::cout << "Words starting with \"car\": ";
94 for (const auto & w : sorted_suggestions)
95 std::cout << w << " ";
96 std::cout << "\n\n";
97}
98
100{
101 std::cout << "[3] Longest-prefix match (e.g. hierarchical route lookup)\n";
102 print_rule();
103
104 // A toy routing-like table: more specific routes shadow general ones.
106 routes.insert("/api", "generic-api-handler");
107 routes.insert("/api/users", "users-handler");
108 routes.insert("/api/users/admin", "admin-handler");
109
110 for (const auto & path :
111 {"/api/users/admin/settings", "/api/users/42", "/api/health"})
112 {
113 auto matched = routes.longest_prefix(path);
114 if (matched)
115 std::cout << path << " -> " << *routes.find(*matched) << " (matched \""
116 << *matched << "\")\n";
117 else
118 std::cout << path << " -> no route matched\n";
119 }
120 std::cout << "\n";
121}
122} // namespace
123
124int main()
125{
126 std::cout << "\n=== Aleph::RadixTree: compressed prefix tree ===\n\n";
127
131
132 std::cout << "Done.\n";
133 return 0;
134}
long double w
Definition btreepic.C:153
Compressed prefix tree mapping std::basic_string<Char> keys to values of type T.
const T * find(const Key &key) const noexcept
Look up key.
bool insert(const Key &key, const T &value)
Insert key with a copy of value, only if key is absent.
void insert_or_assign(const Key &key, T value)
Insert key with value, or overwrite the existing value if key is already present.
bool contains(const Key &key) const noexcept
Check whether key is present.
bool erase(const Key &key)
Remove key if present, merging any resulting single-child, valueless node back into a compressed edge...
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 main()
Compressed prefix tree (Aleph::RadixTree) mapping string keys to values.