Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_treap.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
52# ifndef TPL_TREAP_H
53# define TPL_TREAP_H
54
55# include <type_traits>
56# include <ah-random.H>
57# include <ahFunction.H>
58# include <tpl_binNodeUtils.H>
59# include <treapNode.H>
60# include <tpl_binTreeOps.H>
61# include <ah-errors.H>
62# include <ah-concepts.H>
63
64using namespace Aleph;
65
66namespace Aleph
67{
154 template <template <typename> class NodeType, typename Key, class Compare>
155 requires StrictWeakOrder<Compare, Key>
157 {
158 public:
159 using Node = NodeType<Key>;
160
161 private:
166 Compare cmp;
167
168 // The seed of the generator for that of a constructor, as it was taken
169 // for GSL: through an unsigned int, modulo 2^32 - 1
170 static unsigned long generator_seed(const unsigned long seed) noexcept
171 {
172 return static_cast<unsigned int>(seed)
173 % static_cast<unsigned long>(Gsl_Compatible_Rng::max());
174 }
175
176 public:
186 void set_seed(const unsigned long seed)
187 {
189 }
190
198 void swap(Gen_Treap & tree) noexcept(std::is_nothrow_swappable_v<Compare>)
199 {
200 // The comparison first: if its swap throws, the nodes stay where
201 // they were
202 std::swap(cmp, tree.cmp);
203 std::swap(tree_root, tree.tree_root);
204 r.swap(tree.r);
205 }
206
208 Compare &key_comp() noexcept { return cmp; }
209
211 Compare &get_compare() noexcept { return key_comp(); }
212
223 Gen_Treap(const unsigned long seed, const Compare & __cmp = Compare())
225 r(Random_Source::Engine::Mersenne_Twister, generator_seed(seed)), cmp(__cmp)
226 {
228 }
229
245 noexcept(std::is_nothrow_default_constructible_v<Node> and
246 std::is_nothrow_default_constructible_v<Compare> and
247 std::is_nothrow_copy_constructible_v<Compare>)
248 : Gen_Treap(Compare())
249 {
250 // empty
251 }
252
262 Gen_Treap(const Compare & cmp)
263 noexcept(std::is_nothrow_default_constructible_v<Node> and
264 std::is_nothrow_copy_constructible_v<Compare>)
266 {
268 }
269
293 noexcept(bin_node_detail::nothrow_header_from_v<Node> and
294 bin_node_detail::nothrow_compare_taken_v<Compare>)
295 : head(bin_node_detail::header_from(tree.head)),
296 head_ptr(&head), tree_root(RLINK(head_ptr)), r(std::move(tree.r)),
297 cmp(bin_node_detail::compare_to_take(tree.cmp))
298 {
300 tree_root = tree.tree_root;
301 tree.tree_root = Node::NullPtr;
302 }
303
305 [[nodiscard]] Random_Source & random_source() noexcept { return r; }
306
309 [[nodiscard]] const Random_Source & random_source() const noexcept { return r; }
310
320 [[deprecated("it makes a Mersenne Twister in a treap that has none: "
321 "use random_source(); it goes in 8.0.0")]]
322 [[nodiscard]] Gsl_Compatible_Rng & rng() { return r.mersenne_twister(); }
323
324# if ALEPH_WITH_GSL
333 [[deprecated("the generator is no longer of GSL: use random_source(); it goes in 8.0.0")]]
334 gsl_rng * gsl_rng_object() { return r.gsl(); }
335# endif
336
338 Node *&getRoot() noexcept { return tree_root; }
339
350 Node * search(const Key & key) const noexcept(nothrow_compare_v<Compare, Key>)
351 {
352 Node *ret_val = searchInBinTree(tree_root, key, cmp);
353 return ret_val == Node::NullPtr ? nullptr : ret_val;
354 }
355
356 private:
357 // The insertions and the removal compare on the way down and link or
358 // rotate on the way back, so a comparison that throws leaves the tree
359 // as it was
360 Node * insert(Node *root, Node *p) noexcept(nothrow_compare_v<Compare, Key>)
361 {
362 if (root == Node::NullPtr)
363 return p;
364
365 const Key & pk = KEY(p);
366 const Key & rk = KEY(root);
367 Node *insertion_result = nullptr;
368 if (cmp(pk, rk))
369 {
370 insertion_result = insert(LLINK(root), p);
371 if (insertion_result == Node::NullPtr)
372 return Node::NullPtr;
373
374 LLINK(root) = insertion_result;
375 if (PRIO(insertion_result) < PRIO(root))
376 return rotate_to_right(root);
377 return root;
378 }
379 if (cmp(rk, pk))
380 {
381 insertion_result = insert(RLINK(root), p);
382 if (insertion_result == Node::NullPtr)
383 return Node::NullPtr;
384
385 RLINK(root) = insertion_result;
386 if (PRIO(insertion_result) < PRIO(root))
387 return rotate_to_left(root);
388 return root;
389 }
390
391 return Node::NullPtr;
392 }
393
395 noexcept(nothrow_compare_v<Compare, Key>)
396 {
397 if (root == Node::NullPtr)
398 return root = p;
399
400 const Key & pk = KEY(p);
401 const Key & rk = KEY(root);
402 if (cmp(pk, rk))
403 {
404 Node *ret = search_or_insert(LLINK(root), p);
405 if (ret == p) // p was inserted?
406 if (PRIO(LLINK(root)) < PRIO(root))
408
409 return ret;
410 }
411 if (cmp(rk, pk))
412 {
413 Node *ret = search_or_insert(RLINK(root), p);
414 if (ret == p) // p was inserted?
415 if (PRIO(RLINK(root)) < PRIO(root))
417
418 return ret;
419 }
420
421 return root;
422 }
423
424 Node * insert_dup(Node *root, Node *p) noexcept(nothrow_compare_v<Compare, Key>)
425 {
426 if (root == Node::NullPtr)
427 return p;
428
429 const Key & pk = KEY(p);
430 const Key & rk = KEY(root);
431 if (cmp(pk, rk))
432 {
433 if (Node *result = LLINK(root) = insert_dup(LLINK(root), p); PRIO(result) < PRIO(root))
434 return rotate_to_right(root);
435 return root;
436 }
437 if (Node *result = RLINK(root) = insert_dup(RLINK(root), p); PRIO(result) < PRIO(root))
438 return rotate_to_left(root);
439 return root;
440 }
441
442 public:
458 Node * insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
459 {
460 assert(p != Node::NullPtr);
461
462 PRIO(p) = r.get(); // Priority Random Selection
463 Node *result = insert(tree_root, p);
464 if (result == Node::NullPtr)
465 return nullptr;
466
467 tree_root = result;
468
469 return p;
470 }
471
490 Node * search_or_insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
491 {
492 assert(p != Node::NullPtr);
493
494 PRIO(p) = r.get(); // Priority Random Selection
495
496 return search_or_insert(tree_root, p);
497 }
498
512 Node * insert_dup(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
513 {
514 assert(p != Node::NullPtr);
515
516 PRIO(p) = r.get(); // Priority Random Selection
518
519 return p;
520 }
521
524 bool verify() const
525 {
527 }
528
539 Node * remove(const Key & key) noexcept(nothrow_compare_v<Compare, Key>)
540 {
541 Node **pp = &RLINK(head_ptr);
542 Node *p = tree_root;
543
544 while (p != Node::NullPtr) // descender buscando la clave
545 {
546 const Key & pk = KEY(p);
547 if (cmp(key, pk))
548 {
549 pp = &LLINK(p);
550 p = LLINK(p);
551 }
552 else if (cmp(pk, key))
553 {
554 pp = &RLINK(p);
555 p = RLINK(p);
556 }
557 else
558 break; //found!
559 }
560
561 if (p == Node::NullPtr)
562 return nullptr; // Key was not found
563
564 // rotate p until it becomes a leaf
565 while (not (LLINK(p) == Node::NullPtr and RLINK(p) == Node::NullPtr))
566 if (PRIO(LLINK(p)) < PRIO(RLINK(p)))
567 {
568 *pp = rotate_to_right(p);
569 pp = &RLINK(*pp);
570 }
571 else
572 {
573 *pp = rotate_to_left(p);
574 pp = &LLINK(*pp);
575 }
576
577 *pp = Node::NullPtr;
578
579 p->reset();
580
581 return p;
582 }
583
584 private:
585 static Node * join_exclusive(Node *t1, Node *t2) noexcept
586 {
587 if (t1 == Node::NullPtr)
588 return t2;
589
590 if (t2 == Node::NullPtr)
591 return t1;
592
593 if (PRIO(t1) < PRIO(t2))
594 {
595 RLINK(t1) = join_exclusive(RLINK(t1), t2);
596 return t1;
597 }
598 LLINK(t2) = join_exclusive(t1, LLINK(t2));
599 return t2;
600 }
601
602 Node * remove(Node *& root, const Key & key) noexcept(nothrow_compare_v<Compare, Key>)
603 {
604 if (root == Node::NullPtr)
605 return Node::NullPtr;
606
607 const Key & rk = KEY(root);
608 if (cmp(key, rk))
609 return remove(LLINK(root), key);
610 if (cmp(rk, key))
611 return remove(RLINK(root), key);
612
613 Node *ret = root; // key found!
615 ret->reset();
616
617 return ret;
618 }
619
620 void join_dup(Node *& t1, Node *t2) noexcept
621 {
622 if (t2 == Node::NullPtr)
623 return;
624
625 Node *l = LLINK(t2), *r = RLINK(t2);
626 t2->reset();
627 t1 = insert_dup(t1, t2);
628 join_dup(t1, l);
629 join_dup(t1, r);
630 }
631
632 void join(Node *& t1, Node *t2, Node *& dup) noexcept
633 {
634 if (t2 == Node::NullPtr)
635 return;
636
637 Node *l = LLINK(t2), *r = RLINK(t2);
638 t2->reset();
639 ins:
640 Node *ret = insert(t1, t2);
641 if (ret == Node::NullPtr)
642 {
643 dup = insert_dup(dup, remove(t1, KEY(t2)));
644 goto ins;
645 }
646
647 t1 = ret;
648 join(t1, l, dup);
649 join(t1, r, dup);
650 }
651
652 public:
670 void join(Gen_Treap & t, Gen_Treap & dup) noexcept
671 {
672 join(tree_root, t.tree_root, dup.tree_root);
673 t.tree_root = Node::NullPtr;
674 }
675
690 void join_dup(Gen_Treap & t) noexcept
691 {
692 join_dup(tree_root, t.tree_root);
693 t.tree_root = Node::NullPtr;
694 }
695
707 void join_exclusive(Gen_Treap & t) noexcept
708 {
709 tree_root = join_exclusive(tree_root, t.tree_root);
710 t.tree_root = Node::NullPtr;
711 }
712
727 bool split_key(const Key & key, Gen_Treap & t1, Gen_Treap & t2)
728 noexcept(nothrow_compare_v<Compare, Key>)
729 {
730 return split_key_rec(tree_root, key, t1.getRoot(), t2.getRoot(), cmp);
731 }
732
748 void split_key_dup(const Key & key, Gen_Treap & t1, Gen_Treap & t2)
749 noexcept(nothrow_compare_v<Compare, Key>)
750 {
751 split_key_dup_rec(tree_root, key, t1.getRoot(), t2.getRoot(), cmp);
752 }
753
760 struct Iterator : public BinNodeInfixIterator<Node>
761 {
763 Iterator() noexcept = default;
765 };
766 };
767
794 template <typename Key, class Compare = Aleph::less<Key>>
795 requires StrictWeakOrder<Compare, Key>
796 struct Treap : public Gen_Treap<TreapNode, Key, Compare>
797 {
799 using Base::Base;
800 };
801
802
829 template <typename Key, class Compare = Aleph::less<Key>>
831 struct Treap_Vtl : public Gen_Treap<TreapNodeVtl, Key, Compare>
832 {
834 using Base::Base;
835 };
836} // end namespace Aleph
837# endif // TPL_TREAP_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Standard functor implementations and comparison objects.
@ KEY
Definition btreepic.C:169
Inorder iterator on the nodes of a binary tree.
Treap - A randomized binary search tree using heap-ordered priorities.
Definition tpl_treap.H:157
void swap(Gen_Treap &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
Definition tpl_treap.H:198
Node * search_or_insert(Node *&root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_treap.H:394
Gen_Treap(const unsigned long seed, const Compare &__cmp=Compare())
Builds an empty treap whose priorities are those of GSL for seed: it draws them from a Mersenne Twist...
Definition tpl_treap.H:223
static unsigned long generator_seed(const unsigned long seed) noexcept
Definition tpl_treap.H:170
NodeType< Key > Node
Definition tpl_treap.H:159
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node in the tree.
Definition tpl_treap.H:512
Random_Source & random_source() noexcept
The source of random numbers, which gives the priorities.
Definition tpl_treap.H:305
Node *& getRoot() noexcept
Return the tree's root.
Definition tpl_treap.H:338
void join_dup(Node *&t1, Node *t2) noexcept
Definition tpl_treap.H:620
void join(Gen_Treap &t, Gen_Treap &dup) noexcept
Join this with t filtering the duplicated keys.
Definition tpl_treap.H:670
Node * insert_dup(Node *root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_treap.H:424
Random_Source r
Definition tpl_treap.H:165
Node *& tree_root
Definition tpl_treap.H:164
const Random_Source & random_source() const noexcept
The source of random numbers, to look at it: its engine, or whether it is equal to another one.
Definition tpl_treap.H:309
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search or insert a key.
Definition tpl_treap.H:490
bool verify() const
Return true if this is a consistent treap.
Definition tpl_treap.H:524
void set_seed(const unsigned long seed)
Seeds the Mersenne Twister of the treap, as gsl_rng_set() did: the priorities are from then on those ...
Definition tpl_treap.H:186
void split_key_dup(const Key &key, Gen_Treap &t1, Gen_Treap &t2) noexcept(nothrow_compare_v< Compare, Key >)
Split the tree according to a key that could be in the tree.
Definition tpl_treap.H:748
Compare & get_compare() noexcept
See key_comp().
Definition tpl_treap.H:211
Gen_Treap(const Compare &cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_copy_constructible_v< Compare >)
Builds an empty treap with the comparison criteria cmp, which draws its priorities from a SplitMix64 ...
Definition tpl_treap.H:262
Node head
The type of node.
Definition tpl_treap.H:162
Gen_Treap(Gen_Treap &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >)
Takes the nodes, the comparison criteria and the generator of tree, which keeps no nodes,...
Definition tpl_treap.H:292
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a key in a treap.
Definition tpl_treap.H:350
Node * remove(Node *&root, const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_treap.H:602
Gen_Treap() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_copy_constructible_v< Compare >)
Builds an empty treap with a comparison criteria built by default, which draws its priorities from a ...
Definition tpl_treap.H:244
static Node * join_exclusive(Node *t1, Node *t2) noexcept
Definition tpl_treap.H:585
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node in a treap.
Definition tpl_treap.H:458
void join(Node *&t1, Node *t2, Node *&dup) noexcept
Definition tpl_treap.H:632
Node * insert(Node *root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_treap.H:360
Gsl_Compatible_Rng & rng()
The Mersenne Twister of the treap, which a treap that was given no seed makes here,...
Definition tpl_treap.H:322
void join_dup(Gen_Treap &t) noexcept
Join this with t independently of the presence of duplicated keys.
Definition tpl_treap.H:690
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove a key from the tree.
Definition tpl_treap.H:539
void join_exclusive(Gen_Treap &t) noexcept
Join exclusive of this with t
Definition tpl_treap.H:707
bool split_key(const Key &key, Gen_Treap &t1, Gen_Treap &t2) noexcept(nothrow_compare_v< Compare, Key >)
Split the tree according to a key.
Definition tpl_treap.H:727
Compare & key_comp() noexcept
return the comparison criteria
Definition tpl_treap.H:208
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
The source of random numbers of a randomized structure, which draws from one of two generators.
Definition ah-random.H:883
@ Mersenne_Twister
A Gsl_Compatible_Rng, in dynamic memory.
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:1128
Gsl_Compatible_Rng & mersenne_twister()
The Mersenne Twister of the source, which is its engine from now on.
Definition ah-random.H:1087
void seed(const std::uint64_t s) noexcept
Draws from now on from the SplitMix64 of a seed.
Definition ah-random.H:1040
void swap(Random_Source &o) noexcept
Swaps the generators, with their states.
Definition ah-random.H:1021
Strict weak ordering constraint for BST comparators.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
Node * rotate_to_left(Node *p) noexcept
Rotate to the left the tree with root p
void split_key_dup_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Split a tree according to a key value.
bool split_key_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Split recursively according to a key.
Node * rotate_to_right(Node *p) noexcept
Rotate to the right the tree with root p
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
unsigned long & PRIO(Node *p) noexcept
Access the priority of a treap node.
Definition treapNode.H:120
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Search a key in a binary search tree using optimistic search.
Main namespace for Aleph-w library functions.
bool is_treap(Node *root) noexcept
Validate that a tree satisfies treap (heap) property.
Definition treapNode.H:144
and
Check uniqueness with explicit hash + equality functors.
const long Min_Priority
Minimum priority value.
Definition treapNode.H:78
STL namespace.
Iterator on nodes of the tree.
Definition tpl_treap.H:761
Iterator() noexcept=default
Default constructor creates an "end" iterator.
Treap (a special type of randomized binary search tree) using nodes with virtual destructors.
Definition tpl_treap.H:832
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:797
#define RLINK(i, n)
#define LLINK(i, n)
ValueArg< size_t > seed
Definition testHash.C:53
Utility functions for binary tree operations.
Binary tree operations (split, join, rotate).
Treap node definition with BST key and heap priority.
DynList< int > l