118#include <tclap/CmdLine.h>
121using namespace Aleph;
128 cout <<
"\n" << string(60,
'=') << endl;
129 cout <<
"DynMapTree: Basic Operations" << endl;
130 cout << string(60,
'=') << endl;
135 cout <<
"\n--- Insertion ---" << endl;
140 ages[
"Charlie"] = 35;
142 ages.
insert(make_pair(
"Eve", 42));
144 cout <<
"Inserted 5 entries" << endl;
145 cout <<
"Size: " << ages.
size() << endl;
147 cout <<
"\n--- Access ---" << endl;
150 cout <<
"Alice's age: " << ages[
"Alice"] << endl;
151 cout <<
"Bob's age: " << ages.
find(
"Bob") << 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();
171 cout <<
" " <<
pair.first <<
" -> " <<
pair.second << endl;
174 cout <<
"\n--- Removal ---" << endl;
177 cout <<
"Removed Bob, new size: " << ages.
size() << 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;
191 cout << string(60,
'=') << endl;
197 avl_map[3] =
"three";
198 cout <<
"\n1. Avl_Tree (strictly balanced):" << endl;
199 cout <<
" Height guarantee: <= 1.44 * log2(n)" << endl;
200 cout <<
" Size: " << avl_map.
size() << endl;
207 cout <<
"\n2. Rb_Tree (red-black):" << endl;
208 cout <<
" Height guarantee: <= 2 * log2(n)" << endl;
209 cout <<
" Size: " << rb_map.
size() << endl;
213 splay_map[1] =
"one";
214 splay_map[2] =
"two";
215 splay_map[3] =
"three";
216 cout <<
"\n3. Splay_Tree (self-adjusting):" << endl;
217 cout <<
" Amortized O(log n), good for locality" << endl;
218 cout <<
" Size: " << splay_map.
size() << endl;
222 treap_map[1] =
"one";
223 treap_map[2] =
"two";
224 treap_map[3] =
"three";
225 cout <<
"\n4. Treap (tree + heap):" << endl;
226 cout <<
" Expected O(log n), randomized" << endl;
227 cout <<
" Size: " << treap_map.
size() << endl;
233 rand_map[3] =
"three";
234 cout <<
"\n5. Rand_Tree (randomized):" << endl;
235 cout <<
" Expected O(log n), randomized" << endl;
236 cout <<
" Size: " << rand_map.
size() << endl;
238 cout <<
"\nAll backends provide the same interface!" << endl;
246 cout <<
"\n" << string(60,
'=') << endl;
247 cout <<
"Practical Example: Word Frequency Counter" << endl;
248 cout << string(60,
'=') << endl;
253 vector<string> words = {
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;
276 cout <<
"\nUnique words: " << freq.
size() << endl;
284 cout <<
"\n" << string(60,
'=') << endl;
285 cout <<
"Practical Example: Configuration Store" << endl;
286 cout << string(60,
'=') << 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;
315 auto* log_file = config.
search(
"logging.file");
317 cout <<
"Log file: " << log_file->second << endl;
320 auto* optional = config.
search(
"optional.feature");
321 string value = optional ? optional->second :
"default_value";
322 cout <<
"Optional feature: " <<
value << endl;
330 cout <<
"\n" << string(60,
'=') << endl;
331 cout <<
"Functional Programming Features" << endl;
332 cout << string(60,
'=') << endl;
335 scores[
"Alice"] = 85;
337 scores[
"Charlie"] = 78;
338 scores[
"Diana"] = 95;
341 cout <<
"\nOriginal scores:" << endl;
343 cout <<
" " << p.first <<
": " << p.second << endl;
347 size_t high_scorers = 0;
348 scores.
for_each([&high_scorers](
const auto& p) {
349 if (p.second >= 90) ++high_scorers;
351 cout <<
"\nStudents with score >= 90: " << high_scorers << endl;
354 bool all_passed = scores.
all([](
const auto& p) {
355 return p.second >= 60;
357 cout <<
"All passed (>= 60): " << (all_passed ?
"yes" :
"no") << endl;
360 bool any_perfect = scores.
exists([](
const auto& p) {
361 return p.second == 100;
363 cout <<
"Any perfect score: " << (any_perfect ?
"yes" :
"no") << endl;
366 cout <<
"\nHigh scorers (>= 90):" << endl;
367 auto high = scores.
filter([](
const auto& p) {
return p.second >= 90; });
369 cout <<
" " << p.first <<
": " << p.second << endl;
373 int total = scores.
foldl<
int>(0, [](
int acc,
const auto& p) {
374 return acc + p.second;
376 cout <<
"\nTotal score: " << total << endl;
377 cout <<
"Average: " << (total / (double)scores.
size()) << endl;
385 cout <<
"\n" << string(60,
'=') << endl;
386 cout <<
"Performance Comparison (n = " << n <<
")" << endl;
387 cout << string(60,
'=') << endl;
391 uniform_int_distribution<> dis(1, n * 10);
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();
419 cout << setw(12) << name <<
": "
420 <<
"Insert " << setw(6) << insert_us <<
" us, "
421 <<
"Lookup " << setw(6) << lookup_us <<
" us" << endl;
428 benchmark(map,
"Avl_Tree");
433 benchmark(map,
"Rb_Tree");
438 benchmark(map,
"Splay_Tree");
443 benchmark(map,
"Treap");
448 benchmark(map,
"Rand_Tree");
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;
459int main(
int argc,
char* argv[])
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);
469 TCLAP::SwitchArg backendsArg(
"t",
"trees",
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);
484 cmd.add(backendsArg);
491 cmd.parse(argc, argv);
493 int n = nArg.getValue();
494 bool runBasic = basicArg.getValue();
495 bool runBackends = backendsArg.getValue();
496 bool runFreq = freqArg.getValue();
497 bool runConfig = configArg.getValue();
498 bool runFunc = funcArg.getValue();
499 bool runPerf = perfArg.getValue();
500 bool runAll = allArg.getValue();
502 if (not runBasic
and not runBackends
and not runFreq
and
503 not runConfig
and not runFunc
and not runPerf)
506 cout <<
"=== DynMapTree: Key-Value Mappings ===" << endl;
508 if (runAll or runBasic)
511 if (runAll or runBackends)
514 if (runAll or runFreq)
517 if (runAll or runConfig)
520 if (runAll or runFunc)
523 if (runAll or runPerf)
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;
size_t size_t int32_t value
Generic key-value map implemented on top of a binary search tree.
Data remove(const Key &key)
Deletes the pair key,data
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
bool has(const Key &key) const noexcept(nothrow_search)
Return whether key is in the map.
const size_t & size() const
Returns the cardinality of the set.
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criterion.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
Acc foldl(const Acc &init, Op &op) const
Fold the elements of the container to a specific result.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
auto get_it()
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.
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Dynamic key-value map based on balanced binary search trees.