128# include <tclap/CmdLine.h>
132using namespace Aleph;
167template <
typename Set>
177 auto start = chrono::high_resolution_clock::now();
180 auto end = chrono::high_resolution_clock::now();
181 result.
insert_ms = chrono::duration<double, milli>(end - start).count();
188 start = chrono::high_resolution_clock::now();
191 auto* p = set.search(x);
193 cerr <<
"ERROR: element " << x <<
" not found!" <<
endl;
195 end = chrono::high_resolution_clock::now();
196 result.
search_ms = chrono::duration<double, milli>(end - start).count();
199 start = chrono::high_resolution_clock::now();
202 end = chrono::high_resolution_clock::now();
203 result.
remove_ms = chrono::duration<double, milli>(end - start).count();
206 cerr <<
"ERROR: set not empty after removals!" <<
endl;
226 cout <<
"=== Basic Operations Demo ===" <<
endl <<
endl;
232 cout <<
"Inserting elements: 5, 3, 7, 1, 4, 6, 9" <<
endl;
233 for (
int x : {5, 3, 7, 1, 4, 6, 9})
237 cout <<
"Elements (in order): ";
243 << (
avl_set.search(4) ?
"found" :
"not found") <<
endl;
244 cout <<
"Searching for 8: "
245 << (
avl_set.search(8) ?
"found" :
"not found") <<
endl;
249 cout <<
"Contains 10: " << (
avl_set.contains(10) ?
"yes" :
"no") <<
endl;
258 cout <<
"Elements after removal: ";
271 cout <<
"=== Different Tree Types Demo ===" <<
endl <<
endl;
279 vector<int> data = {50, 25, 75, 10, 30, 60, 90, 5, 15, 27, 35};
281 cout <<
"Inserting same data into different tree types:" <<
endl;
283 for (
int x : data)
cout << x <<
" ";
294 cout <<
"Tree heights (lower is better balanced):" <<
endl;
301 cout <<
endl <<
"After searching for 5, 10, 15 (watch Splay change):" <<
endl;
305 cout <<
" Splay height after searches: "
307 cout <<
" (Splay moves accessed elements toward root)" <<
endl;
318 cout <<
"=== Ranked Operations Demo ===" <<
endl <<
endl;
320 cout <<
"Ranked trees maintain subtree sizes, enabling O(log n) operations:" <<
endl;
321 cout <<
" select(i) - get i-th smallest element (0-indexed)" <<
endl;
322 cout <<
" position(x) - get rank/position of element x" <<
endl <<
endl;
324 cout <<
"Available ranked tree types:" <<
endl;
325 cout <<
" - Avl_Tree_Rk : AVL with rank (strictly balanced, deterministic)" <<
endl;
326 cout <<
" - Treap_Rk : Treap with rank (randomized balance)" <<
endl <<
endl;
332 vector<int> data = {100, 50, 150, 25, 75, 125, 175, 10, 200};
340 cout <<
"Set contents (sorted): ";
345 cout <<
"Positional access - select(i) returns i-th element:" <<
endl;
346 cout <<
" Index AVL_Rk Treap_Rk" <<
endl;
347 cout <<
" ----- ------ --------" <<
endl;
350 cout <<
" " << i <<
" "
356 cout <<
endl <<
"Element ranks - position(x) returns index of x:" <<
endl;
357 cout <<
" Value AVL_Rk Treap_Rk" <<
endl;
358 cout <<
" ----- ------ --------" <<
endl;
359 for (
int x : {10, 50, 100, 150, 200})
362 <<
avl_rk.position(x) <<
" "
367 cout <<
endl <<
"Practical use - Finding median in O(log n):" <<
endl;
389 cout <<
"=== Functional Programming Features ===" <<
endl <<
endl;
392 for (
int i = 1; i <= 10; ++i)
395 cout <<
"Original set: ";
400 auto evens = set.
filter([](
int x) {
return x % 2 == 0; });
401 cout <<
"Filter (even): ";
407 cout <<
"Map (square): ";
417 cout <<
" All positive? " << (set.
all([](
int x) {
return x > 0; }) ?
"yes" :
"no") <<
endl;
418 cout <<
" Exists > 5? " << (set.
exists([](
int x) {
return x > 5; }) ?
"yes" :
"no") <<
endl;
419 cout <<
" All <= 10? " << (set.
all([](
int x) {
return x <= 10; }) ?
"yes" :
"no") <<
endl;
430 cout <<
"=== Performance Comparison ===" <<
endl;
431 cout <<
"Testing with " << n <<
" elements (seed: " << seed <<
")" <<
endl <<
endl;
435 cout <<
"Nothing to benchmark: n=0\n\n";
443 for (
size_t i = 0; i < n; ++i)
444 data.push_back(
rand());
447 sort(data.begin(), data.end());
448 data.erase(
unique(data.begin(), data.end()), data.end());
452 cout <<
"Nothing to benchmark: all generated keys collapsed to an empty set\n\n";
457 for (
size_t i = data.size(); i > 1; --i)
458 swap(data[i - 1], data[
rand() % i]);
465 cout <<
"Standard trees (no rank support):" <<
endl;
472 cout <<
endl <<
"Ranked trees (with select/position support):" <<
endl;
478 cout << left <<
setw(18) <<
"Tree Type"
479 << right <<
setw(12) <<
"Insert(ms)"
480 <<
setw(12) <<
"Search(ms)"
481 <<
setw(12) <<
"Remove(ms)"
482 <<
setw(10) <<
"Height"
490 <<
setw(12) << r.insert_ms
491 <<
setw(12) << r.search_ms
492 <<
setw(12) << r.remove_ms
493 <<
setw(10) << r.height
500 cout <<
" - Height: tree height (log2(" << data.
size() <<
") ~= "
502 cout <<
" - Avg Path: average path length from root (ideal ~= log2(n))" <<
endl;
503 cout <<
" - Splay tree optimizes for access patterns, not balance" <<
endl;
504 cout <<
" - _Rk variants have slight overhead for maintaining subtree sizes" <<
endl;
505 cout <<
" - Use _Rk trees when you need select(i) or position(x) operations" <<
endl;
518 "Demonstration of DynSetTree with different BST implementations.\n"
519 "Shows how Aleph-w allows swapping data structure implementations\n"
520 "through template parameters.",
523 TCLAP::ValueArg<size_t>
nArg(
"n",
"count",
524 "Number of elements for performance test",
525 false, 100000,
"size_t");
528 TCLAP::ValueArg<unsigned int>
seedArg(
"s",
"seed",
529 "Random seed (0 = use time)",
530 false, 0,
"unsigned int");
533 TCLAP::SwitchArg
allArg(
"a",
"all",
534 "Run all demonstrations (not just performance)",
539 "Show detailed tree statistics",
545 size_t n =
nArg.getValue();
546 unsigned int seed =
seedArg.getValue();
551 seed =
time(
nullptr);
553 cout <<
"DynSetTree - Multiple BST Implementations Demo" <<
endl;
554 cout <<
"==============================================" <<
endl <<
endl;
568 catch (TCLAP::ArgException& e)
570 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.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Key * search(const Key &key) const
Find an element in the set.
Node * get_root_node() const
size_t size() const noexcept
Count the number of elements of the list.
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.
void demonstrate_functional_features()
TimingResult benchmark_set(const string &name, const vector< int > &data, bool verbose)
void demonstrate_ranked_operations()
void demonstrate_tree_types()
void demonstrate_basic_operations()
void run_performance_comparison(size_t n, unsigned int seed, bool verbose)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log2_function > > log2(const __gmp_expr< T, U > &expr)
size_t internal_path_length(Node *p) noexcept
Compute the internal path length.
size_t computeHeightRec(Node *root) noexcept
Compute recursively the height of root
Main namespace for Aleph-w library functions.
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
bool verbose
Flag enabling verbose output.
DynList< T > maps(const C &c, Op op)
Classic map operation.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
size_t internal_path_length
Dynamic set implementations based on balanced binary search trees.