52void demo_basic_usage()
54 std::cout <<
"[1] Insert/find/erase walkthrough\n";
58 word_count.
insert(
"apple", 3);
59 word_count.
insert(
"app", 1);
60 word_count.
insert(
"application", 7);
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";
67 std::cout <<
"app after insert_or_assign: " << *word_count.
find(
"app")
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";
77void demo_autocomplete()
79 std::cout <<
"[2] Prefix autocomplete (keys_with_prefix)\n";
83 for (
const auto & word :
84 {
"cat",
"car",
"cart",
"carbon",
"care",
"dog",
"door"})
85 dictionary.
insert(word, 0);
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());
93 std::cout <<
"Words starting with \"car\": ";
94 for (
const auto &
w : sorted_suggestions)
95 std::cout <<
w <<
" ";
99void demo_longest_prefix_match()
101 std::cout <<
"[3] Longest-prefix match (e.g. hierarchical route lookup)\n";
106 routes.
insert(
"/api",
"generic-api-handler");
107 routes.
insert(
"/api/users",
"users-handler");
108 routes.
insert(
"/api/users/admin",
"admin-handler");
110 for (
const auto & path :
111 {
"/api/users/admin/settings",
"/api/users/42",
"/api/health"})
115 std::cout << path <<
" -> " << *routes.
find(*matched) <<
" (matched \""
116 << *matched <<
"\")\n";
118 std::cout << path <<
" -> no route matched\n";
126 std::cout <<
"\n=== Aleph::RadixTree: compressed prefix tree ===\n\n";
130 demo_longest_prefix_match();
132 std::cout <<
"Done.\n";
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.
std::optional< Key > longest_prefix(const Key &key) const
Find the longest stored key that is a prefix of key.
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.
Array< Key > keys_with_prefix(const Key &prefix) const
Return every stored key that starts with prefix.
bool erase(const Key &key)
Remove key if present, merging any resulting single-child, valueless node back into a compressed edge...
Main namespace for Aleph-w library functions.
void print_rule()
Prints a horizontal rule for example output separation.
Compressed prefix tree (Aleph::RadixTree) mapping string keys to values.