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;
139 ages.insert(
"Bob", 25);
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;
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;
248 cout << string(60,
'=') <<
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;
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;
322 cout <<
"Optional feature: " << value <<
endl;
330 cout <<
"\n" << string(60,
'=') <<
endl;
331 cout <<
"Functional Programming Features" <<
endl;
332 cout << string(60,
'=') <<
endl;
341 cout <<
"\nOriginal scores:" <<
endl;
343 cout <<
" " << p.first <<
": " << p.second <<
endl;
355 return p.second >= 60;
357 cout <<
"All passed (>= 60): " << (
all_passed ?
"yes" :
"no") <<
endl;
361 return p.second == 100;
366 cout <<
"\nHigh scorers (>= 90):" <<
endl;
367 auto high =
scores.filter([](
const auto& p) {
return p.second >= 90; });
368 high.for_each([](
auto& p) {
369 cout <<
" " << p.first <<
": " << p.second <<
endl;
374 return acc + p.second;
376 cout <<
"\nTotal score: " <<
total <<
endl;
385 cout <<
"\n" << string(60,
'=') <<
endl;
386 cout <<
"Performance Comparison (n = " << n <<
")" <<
endl;
387 cout << string(60,
'=') <<
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();
419 cout << setw(12) << name <<
": "
420 <<
"Insert " << setw(6) <<
insert_us <<
" us, "
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;
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) const noexcept
Collect all keys.
bool has(const Key &key) const noexcept
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
const size_t & size() const
Returns the cardinality of the set.
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.
and
Check uniqueness with explicit hash + equality functors.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Dynamic key-value map based on balanced binary search trees.