118#include <tclap/CmdLine.h>
121using namespace Aleph;
128 cout <<
"\n" << string(60,
'=') <<
endl;
129 cout <<
"DynMapTree: Basic Operations" <<
endl;
135 cout <<
"\n--- Insertion ---" <<
endl;
140 ages[
"Charlie"] = 35;
144 cout <<
"Inserted 5 entries" <<
endl;
154 auto* result =
ages.search(
"Charlie");
155 if (result !=
nullptr)
156 cout <<
"Charlie found: " << result->second <<
endl;
158 result =
ages.search(
"Unknown");
159 cout <<
"Unknown found: " << (result ?
"yes" :
"no") <<
endl;
161 cout <<
"\n--- Modification ---" <<
endl;
164 cout <<
"Updated Alice's age to " <<
ages[
"Alice"] <<
endl;
166 cout <<
"\n--- Iteration (sorted by key) ---" <<
endl;
168 for (
auto it =
ages.
get_it(); it.has_curr(); it.next())
170 auto&
pair = it.get_curr();
174 cout <<
"\n--- Removal ---" <<
endl;
179 cout <<
"\n--- Contains check ---" <<
endl;
180 cout <<
"Has Alice: " << (
ages.has(
"Alice") ?
"yes" :
"no") <<
endl;
181 cout <<
"Has Bob: " << (
ages.has(
"Bob") ?
"yes" :
"no") <<
endl;
189 cout <<
"\n" << string(60,
'=') <<
endl;
190 cout <<
"Different Tree Backend Implementations" <<
endl;
198 cout <<
"\n1. Avl_Tree (strictly balanced):" <<
endl;
199 cout <<
" Height guarantee: <= 1.44 * log2(n)" <<
endl;
207 cout <<
"\n2. Rb_Tree (red-black):" <<
endl;
208 cout <<
" Height guarantee: <= 2 * log2(n)" <<
endl;
216 cout <<
"\n3. Splay_Tree (self-adjusting):" <<
endl;
217 cout <<
" Amortized O(log n), good for locality" <<
endl;
225 cout <<
"\n4. Treap (tree + heap):" <<
endl;
226 cout <<
" Expected O(log n), randomized" <<
endl;
234 cout <<
"\n5. Rand_Tree (randomized):" <<
endl;
235 cout <<
" Expected O(log n), randomized" <<
endl;
238 cout <<
"\nAll backends provide the same interface!" <<
endl;
246 cout <<
"\n" << string(60,
'=') <<
endl;
247 cout <<
"Practical Example: Word Frequency Counter" <<
endl;
254 "the",
"quick",
"brown",
"fox",
"jumps",
"over",
"the",
"lazy",
"dog",
255 "the",
"fox",
"is",
"quick",
"and",
"the",
"dog",
"is",
"lazy",
256 "quick",
"foxes",
"are",
"brown",
"lazy",
"dogs",
"are",
"cute"
259 cout <<
"\nProcessing " << words.
size() <<
" words..." <<
endl;
261 for (
const auto&
word : words)
269 cout <<
"\nWord frequencies (alphabetically sorted):" <<
endl;
270 for (
auto it =
freq.
get_it(); it.has_curr(); it.next())
272 auto& p = it.get_curr();
273 cout <<
" " <<
setw(8) << p.first <<
": " << p.second <<
endl;
284 cout <<
"\n" << string(60,
'=') <<
endl;
285 cout <<
"Practical Example: Configuration Store" <<
endl;
292 config[
"app.name"] =
"MyApplication";
293 config[
"app.version"] =
"1.0.0";
294 config[
"database.host"] =
"localhost";
295 config[
"database.port"] =
"5432";
296 config[
"database.name"] =
"mydb";
297 config[
"logging.level"] =
"INFO";
298 config[
"logging.file"] =
"/var/log/app.log";
299 config[
"cache.enabled"] =
"true";
300 config[
"cache.ttl"] =
"3600";
302 cout <<
"\nAll configuration (sorted by key):" <<
endl;
303 for (
auto it = config.
get_it(); it.has_curr(); it.next())
305 auto& p = it.get_curr();
306 cout <<
" " << p.first <<
" = " << p.second <<
endl;
310 cout <<
"\n--- Accessing Specific Values ---" <<
endl;
311 cout <<
"App name: " << config[
"app.name"] <<
endl;
312 cout <<
"DB host: " << config[
"database.host"] <<
endl;
322 cout <<
"Optional feature: " << value <<
endl;
330 cout <<
"\n" << string(60,
'=') <<
endl;
331 cout <<
"Functional Programming Features" <<
endl;
341 cout <<
"\nOriginal scores:" <<
endl;
343 cout <<
" " << p.first <<
": " << p.second <<
endl;
355 return p.second >= 60;
361 return p.second == 100;
366 cout <<
"\nHigh scorers (>= 90):" <<
endl;
369 cout <<
" " << p.first <<
": " << p.second <<
endl;
374 return acc + p.second;
385 cout <<
"\n" << string(60,
'=') <<
endl;
386 cout <<
"Performance Comparison (n = " << n <<
")" <<
endl;
394 for (
int i = 0; i < n; ++i)
397 auto benchmark = [&](
auto& map,
const string& name) {
398 auto start = chrono::high_resolution_clock::now();
404 auto mid = chrono::high_resolution_clock::now();
407 volatile int dummy = 0;
410 auto* p = map.search(
k);
411 if (p)
dummy = p->second;
414 auto end = chrono::high_resolution_clock::now();
416 auto insert_us = chrono::duration_cast<chrono::microseconds>(
mid - start).count();
417 auto lookup_us = chrono::duration_cast<chrono::microseconds>(end -
mid).count();
451 cout <<
"\n--- Analysis ---" <<
endl;
452 cout <<
"All backends are O(log n) average case" <<
endl;
453 cout <<
"AVL: Most balanced, slightly slower updates" <<
endl;
454 cout <<
"Red-Black: Good balance, faster updates" <<
endl;
455 cout <<
"Splay: Best for repeated access patterns" <<
endl;
456 cout <<
"Treap/Rand: Good average, simple implementation" <<
endl;
463 TCLAP::CmdLine
cmd(
"DynMapTree Example",
' ',
"1.0");
465 TCLAP::ValueArg<int>
nArg(
"n",
"count",
466 "Number of elements for performance test",
false, 10000,
"int");
467 TCLAP::SwitchArg
basicArg(
"b",
"basic",
468 "Show basic operations",
false);
470 "Show different tree backends",
false);
471 TCLAP::SwitchArg
freqArg(
"w",
"words",
472 "Show word frequency example",
false);
473 TCLAP::SwitchArg
configArg(
"c",
"config",
474 "Show configuration store example",
false);
475 TCLAP::SwitchArg
funcArg(
"f",
"functional",
476 "Show functional programming features",
false);
477 TCLAP::SwitchArg
perfArg(
"p",
"performance",
478 "Run performance comparison",
false);
479 TCLAP::SwitchArg
allArg(
"a",
"all",
480 "Run all demos",
false);
493 int n =
nArg.getValue();
506 cout <<
"=== DynMapTree: Key-Value Mappings ===" <<
endl;
526 cout <<
"\n=== Summary ===" <<
endl;
527 cout <<
"DynMapTree provides ordered key-value storage" <<
endl;
528 cout <<
"Choose backend based on access patterns:" <<
endl;
529 cout <<
" - AVL for predictable performance" <<
endl;
530 cout <<
" - Splay for locality-heavy workloads" <<
endl;
531 cout <<
" - Treap for simplicity with good average case" <<
endl;
535 catch (TCLAP::ArgException& e)
537 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() <<
endl;
T & insert(const T &item)
Insert a new item by copy.
T remove()
Remove the first item of the list.
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) const noexcept
Collect all keys.
size_t size() const noexcept
Count the number of elements of the list.
__T foldl(const __T &init, Op &op) const
Fold the elements of the container to a specific result.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criteria.
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criteria.
bool all(Operation &operation) const
Check if all the elements of container satisfy a condition.
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
void demo_config_store()
Practical example: Configuration store.
void demo_basic_operations()
Demonstrate basic map operations.
void demo_tree_backends()
Demonstrate different tree backends.
void demo_functional()
Functional programming with maps.
void demo_word_frequency()
Practical example: Word frequency counter.
Main namespace for Aleph-w library functions.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Dynamic key-value map based on balanced binary search trees.