128# include <tclap/CmdLine.h>
134using namespace Aleph;
169template <
typename Set>
179 auto start = chrono::high_resolution_clock::now();
182 auto end = chrono::high_resolution_clock::now();
183 result.
insert_ms = chrono::duration<double, milli>(end - start).count();
190 start = chrono::high_resolution_clock::now();
193 auto* p = set.search(x);
195 cerr <<
"ERROR: element " << x <<
" not found!" <<
endl;
197 end = chrono::high_resolution_clock::now();
198 result.
search_ms = chrono::duration<double, milli>(end - start).count();
201 start = chrono::high_resolution_clock::now();
204 end = chrono::high_resolution_clock::now();
205 result.
remove_ms = chrono::duration<double, milli>(end - start).count();
208 cerr <<
"ERROR: set not empty after removals!" <<
endl;
212 cout <<
" " << name <<
":" <<
endl;
215 cout <<
" Avg path length: " << fixed << setprecision(2)
228 cout <<
"=== Basic Operations Demo ===" <<
endl <<
endl;
234 cout <<
"Inserting elements: 5, 3, 7, 1, 4, 6, 9" <<
endl;
235 for (
int x : {5, 3, 7, 1, 4, 6, 9})
239 cout <<
"Elements (in order): ";
240 avl_set.for_each([](
int x) { cout << x <<
" "; });
244 cout <<
endl <<
"Searching for 4: "
245 << (
avl_set.search(4) ?
"found" :
"not found") <<
endl;
246 cout <<
"Searching for 8: "
247 << (
avl_set.search(8) ?
"found" :
"not found") <<
endl;
250 cout <<
endl <<
"Contains 7: " << (
avl_set.contains(7) ?
"yes" :
"no") <<
endl;
251 cout <<
"Contains 10: " << (
avl_set.contains(10) ?
"yes" :
"no") <<
endl;
258 cout <<
endl <<
"Removing 3..." <<
endl;
260 cout <<
"Elements after removal: ";
261 avl_set.for_each([](
int x) { cout << x <<
" "; });
273 cout <<
"=== Different Tree Types Demo ===" <<
endl <<
endl;
281 vector<int> data = {50, 25, 75, 10, 30, 60, 90, 5, 15, 27, 35};
283 cout <<
"Inserting same data into different tree types:" <<
endl;
285 for (
int x : data) cout << x <<
" ";
296 cout <<
"Tree heights (lower is better balanced):" <<
endl;
303 cout <<
endl <<
"After searching for 5, 10, 15 (watch Splay change):" <<
endl;
307 cout <<
" Splay height after searches: "
309 cout <<
" (Splay moves accessed elements toward root)" <<
endl;
320 cout <<
"=== Ranked Operations Demo ===" <<
endl <<
endl;
322 cout <<
"Ranked trees maintain subtree sizes, enabling O(log n) operations:" <<
endl;
323 cout <<
" select(i) - get i-th smallest element (0-indexed)" <<
endl;
324 cout <<
" position(x) - get rank/position of element x" <<
endl <<
endl;
326 cout <<
"Available ranked tree types:" <<
endl;
327 cout <<
" - Avl_Tree_Rk : AVL with rank (strictly balanced, deterministic)" <<
endl;
328 cout <<
" - Treap_Rk : Treap with rank (randomized balance)" <<
endl <<
endl;
334 vector<int> data = {100, 50, 150, 25, 75, 125, 175, 10, 200};
342 cout <<
"Set contents (sorted): ";
343 avl_rk.for_each([](
int x) { cout << x <<
" "; });
347 cout <<
"Positional access - select(i) returns i-th element:" <<
endl;
348 cout <<
" Index AVL_Rk Treap_Rk" <<
endl;
349 cout <<
" ----- ------ --------" <<
endl;
350 for (
size_t i = 0; i <
avl_rk.size(); ++i)
352 cout <<
" " << i <<
" "
353 << setw(4) <<
avl_rk.select(i) <<
" "
358 cout <<
endl <<
"Element ranks - position(x) returns index of x:" <<
endl;
359 cout <<
" Value AVL_Rk Treap_Rk" <<
endl;
360 cout <<
" ----- ------ --------" <<
endl;
361 for (
int x : {10, 50, 100, 150, 200})
363 cout <<
" " << setw(3) << x <<
" "
364 <<
avl_rk.position(x) <<
" "
369 cout <<
endl <<
"Practical use - Finding median in O(log n):" <<
endl;
371 cout <<
" Median (middle element): " <<
avl_rk.select(
mid) <<
endl;
380 cout <<
endl <<
"Count elements < 100: " <<
avl_rk.position(100) <<
endl;
391 cout <<
"=== Functional Programming Features ===" <<
endl <<
endl;
394 for (
int i = 1; i <= 10; ++i)
397 cout <<
"Original set: ";
398 set.
for_each([](
int x) { cout << x <<
" "; });
402 auto evens = set.
filter([](
int x) {
return x % 2 == 0; });
403 cout <<
"Filter (even): ";
409 cout <<
"Map (square): ";
415 cout <<
"Fold (sum): " <<
sum <<
endl;
418 cout <<
endl <<
"Predicates:" <<
endl;
419 cout <<
" All positive? " << (set.
all([](
int x) {
return x > 0; }) ?
"yes" :
"no") <<
endl;
420 cout <<
" Exists > 5? " << (set.
exists([](
int x) {
return x > 5; }) ?
"yes" :
"no") <<
endl;
421 cout <<
" All <= 10? " << (set.
all([](
int x) {
return x <= 10; }) ?
"yes" :
"no") <<
endl;
432 cout <<
"=== Performance Comparison ===" <<
endl;
433 cout <<
"Testing with " << n <<
" elements (seed: " <<
seed <<
")" <<
endl <<
endl;
437 cout <<
"Nothing to benchmark: n=0\n\n";
445 for (
size_t i = 0; i < n; ++i)
446 data.push_back(
rand());
449 sort(data.begin(), data.end());
450 data.erase(
unique(data.begin(), data.end()), data.end());
454 cout <<
"Nothing to benchmark: all generated keys collapsed to an empty set\n\n";
459 for (
size_t i = data.size(); i > 1; --i)
460 swap(data[i - 1], data[
rand() % i]);
462 cout <<
"Actual unique elements: " << data.size() <<
endl <<
endl;
467 cout <<
"Standard trees (no rank support):" <<
endl;
474 cout <<
endl <<
"Ranked trees (with select/position support):" <<
endl;
480 cout << left << setw(18) <<
"Tree Type"
481 << right << setw(12) <<
"Insert(ms)"
482 << setw(12) <<
"Search(ms)"
483 << setw(12) <<
"Remove(ms)"
484 << setw(10) <<
"Height"
485 << setw(15) <<
"Avg Path" <<
endl;
486 cout << string(79,
'-') <<
endl;
490 cout << left << setw(18) <<
r.name
491 << right << fixed << setprecision(2)
492 << setw(12) <<
r.insert_ms
493 << setw(12) <<
r.search_ms
494 << setw(12) <<
r.remove_ms
495 << setw(10) <<
r.height
496 << setw(15) << setprecision(2)
497 << (
double)
r.internal_path_length / data.size() <<
endl;
501 cout <<
"Notes:" <<
endl;
502 cout <<
" - Height: tree height (log2(" << data.size() <<
") ~= "
503 << fixed << setprecision(1) <<
log2(data.size()) <<
")" <<
endl;
504 cout <<
" - Avg Path: average path length from root (ideal ~= log2(n))" <<
endl;
505 cout <<
" - Splay tree optimizes for access patterns, not balance" <<
endl;
506 cout <<
" - _Rk variants have slight overhead for maintaining subtree sizes" <<
endl;
507 cout <<
" - Use _Rk trees when you need select(i) or position(x) operations" <<
endl;
520 "Demonstration of DynSetTree with different BST implementations.\n"
521 "Shows how Aleph-w allows swapping data structure implementations\n"
522 "through template parameters.",
525 TCLAP::ValueArg<size_t>
nArg(
"n",
"count",
526 "Number of elements for performance test",
527 false, 100000,
"size_t");
530 TCLAP::ValueArg<unsigned int>
seedArg(
"s",
"seed",
531 "Random seed (0 = use time)",
532 false, 0,
"unsigned int");
535 TCLAP::SwitchArg
allArg(
"a",
"all",
536 "Run all demonstrations (not just performance)",
541 "Show detailed tree statistics",
547 size_t n =
nArg.getValue();
555 cout <<
"DynSetTree - Multiple BST Implementations Demo" <<
endl;
556 cout <<
"==============================================" <<
endl <<
endl;
568 cout <<
"Done." <<
endl;
570 catch (TCLAP::ArgException& e)
572 cerr <<
"Error: " << e.error() <<
" for arg " << e.argId() <<
endl;
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
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criterion.
bool all(Operation &operation) const
Check if all the elements of the 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.
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.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
bool verbose
Flag enabling verbose output.
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.