Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_splay_treeRk.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
48# ifndef TPL_SPLAY_TREE_RK_H
49# define TPL_SPLAY_TREE_RK_H
50
51# include <type_traits>
52# include <utility>
53# include <tpl_binNodeXt.H>
54# include <ah-errors.H>
55# include <ah-concepts.H>
56
57using namespace Aleph;
58
59
118template <template <class> class NodeType, typename Key, class Compare>
119 requires StrictWeakOrder<Compare, Key>
121{
122public:
123 typedef NodeType<Key> Node;
124
125private:
127 Compare cmp;
128 static constexpr signed char CmpLess = -1;
129 static constexpr signed char CmpEqual = 0;
130 static constexpr signed char CmpGreater = 1;
131
132 signed char splay_impl(const Key & key) noexcept
133 {
134 assert(root != Node::NullPtr); // splay requires non-empty tree
135 Node header(sentinelCtor);
136 Node * head_ptr = &header;
137 Node * l = head_ptr;
138 Node * r = head_ptr;
139 Node * t = root;
140
141 size_t l_size = 0;
142 size_t r_size = 0;
143 signed char cmp_result = CmpEqual;
144
145 while (true)
146 {
147 if (const Key & tk = KEY(t); cmp(key, tk)) // key < tk: go left
148 {
149 cmp_result = CmpLess;
150 if (LLINK(t) == Node::NullPtr) [[unlikely]]
151 break;
152
153 if (cmp(key, KEY(LLINK(t)))) // zig-zig case
154 {
155 t = rotate_to_right_xt(t);
156 if (LLINK(t) == Node::NullPtr) [[unlikely]]
157 break;
158 }
159
160 LLINK(r) = t;
161 r = t;
162 t = LLINK(t);
163 r_size += 1 + COUNT(RLINK(r));
164 }
165 else if (cmp(tk, key)) // tk < key: go right
166 {
167 cmp_result = CmpGreater;
168 if (RLINK(t) == Node::NullPtr) [[unlikely]]
169 break;
170
171 if (cmp(KEY(RLINK(t)), key)) // zig-zig case
172 {
173 t = rotate_to_left_xt(t);
174 if (RLINK(t) == Node::NullPtr) [[unlikely]]
175 break;
176 }
177
178 RLINK(l) = t;
179 l = t;
180 t = RLINK(t);
181 l_size += 1 + COUNT(LLINK(l));
182 }
183 else [[unlikely]] // key == tk: found
184 {
185 cmp_result = CmpEqual;
186 break;
187 }
188 }
189
190 l_size += COUNT(LLINK(t));
191 r_size += COUNT(RLINK(t));
192 COUNT(t) = l_size + r_size + 1;
193
194 /* reassembling of subtrees in one with t as root */
195 RLINK(l) = LLINK(t);
196 LLINK(r) = RLINK(t);
197
198 LLINK(t) = RLINK(head_ptr);
199 RLINK(t) = LLINK(head_ptr);
200
201 for (Node * y = LLINK(t); y != Node::NullPtr; y = RLINK(y))
202 {
203 COUNT(y) = l_size;
204 l_size -= 1 + COUNT(LLINK(y));
205 }
206
207 for (Node * y = RLINK(t); y != Node::NullPtr; y = LLINK(y))
208 {
209 COUNT(y) = r_size;
210 r_size -= 1 + COUNT(RLINK(y));
211 }
212
213 root = t;
214
215 return cmp_result;
216 }
217
218public:
220 typedef Key key_type;
221
223 Compare &key_comp() { return cmp; }
224
226 Compare &get_compare() { return key_comp(); }
227
240 void splay(const Key & key) noexcept
241 {
242 (void) splay_impl(key);
243 }
244
258 GenTdSplayTreeRk() noexcept(std::is_nothrow_default_constructible_v<Compare>)
259 : root(Node::NullPtr), cmp()
260 {
261 // Empty
262 }
263
271 GenTdSplayTreeRk(Compare __cmp)
272 noexcept(std::is_nothrow_constructible_v<Compare, Compare &>)
273 : root(Node::NullPtr), cmp(__cmp)
274 {
275 // Empty
276 }
277
287
290
306 noexcept(bin_node_detail::nothrow_compare_taken_v<Compare>)
307 : root(Node::NullPtr), cmp(bin_node_detail::compare_to_take(tree.cmp))
308 {
309 root = tree.root;
310 tree.root = Node::NullPtr;
311 }
312
319 void swap(GenTdSplayTreeRk & tree) noexcept(std::is_nothrow_swappable_v<Compare>)
320 {
321 // the swap that the specification evaluates: that of Compare found by
322 // its arguments, else std::swap(); first, since it may throw
323 using std::swap;
324 swap(cmp, tree.cmp);
325 std::swap(root, tree.root);
326 }
327
340 GenTdSplayTreeRk & operator = (GenTdSplayTreeRk && tree) noexcept(noexcept(swap(tree)))
341 {
342 swap(tree);
343 return *this;
344 }
345
348 { /* empty */
349 }
350
351private:
353 {
354 COUNT(p) = COUNT(root) + 1;
355 const Key & pk = KEY(p);
356 const Key & rk = KEY(root);
357 if (cmp(pk, rk))
358 {
359 COUNT(root) -= COUNT(LLINK(root));
360 LLINK(p) = LLINK(root);
361 RLINK(p) = root;
362 LLINK(root) = Node::NullPtr;
363 }
364 else
365 {
366 COUNT(root) -= COUNT(RLINK(root));
367 RLINK(p) = RLINK(root);
368 LLINK(p) = root;
369 RLINK(root) = Node::NullPtr;
370 }
371
372 return root = p; // inserted node become root
373 }
374
375public:
386 {
387 assert(p != Node::NullPtr);
388 assert(COUNT(p) == 1);
389 assert(LLINK(p) == Node::NullPtr and RLINK(p) == Node::NullPtr);
390
391 /* test insertion in empty tree */
392 if (root == Node::NullPtr)
393 return root = p;
394
395 const Key & key = KEY(p);
396
397 const signed char cmp_result = splay_impl(key);
398 if (cmp_result == CmpEqual)
399 return nullptr; // item is already in tree
400
401 return __insert(p);
402 }
403
414 {
415 assert(p != Node::NullPtr);
416 assert(LLINK(p) == Node::NullPtr and RLINK(p) == Node::NullPtr);
417
418 /* test insertion in empty tree */
419 if (root == Node::NullPtr)
420 return root = p;
421
422 splay_impl(KEY(p));
423
424 return __insert(p);
425 }
426
436 Node * search(const Key & key)
437 {
438 if (root == Node::NullPtr)
439 return nullptr;
440
441 const signed char cmp_result = splay_impl(key);
442
443 return cmp_result == CmpEqual ? root : nullptr;
444 }
445
457 {
458 if (root == Node::NullPtr)
459 return root = p;
460
461 const Key & key = KEY(p);
462 const signed char cmp_result = splay_impl(key);
463 if (cmp_result == CmpEqual)
464 return root;
465
466 return __insert(p);
467 }
468
469private:
480 Node * splay_max(Node * r) noexcept
481 {
482 if (r == Node::NullPtr || RLINK(r) == Node::NullPtr)
483 return r;
484
485 Node header(sentinelCtor);
486 Node * head_ptr = &header;
487 Node * l = head_ptr;
488 Node * t = r;
489
490 size_t l_size = 0;
491
492 // Always go right to find maximum
493 while (RLINK(t) != Node::NullPtr)
494 {
495 // Zig-zig: if grandchild exists on the right, rotate twice
496 if (RLINK(RLINK(t)) != Node::NullPtr)
497 {
498 t = rotate_to_left_xt(t);
499 if (RLINK(t) == Node::NullPtr)
500 break;
501 }
502
503 // Link left: attach t to l's right
504 RLINK(l) = t;
505 l = t;
506 t = RLINK(t);
507 l_size += 1 + COUNT(LLINK(l));
508 }
509
510 // Reassemble
511 l_size += COUNT(LLINK(t));
512 COUNT(t) = l_size + 1;
513
514 RLINK(l) = LLINK(t);
515 LLINK(t) = RLINK(head_ptr);
516
517 // Recompute counts along the left spine
518 for (Node * y = LLINK(t); y != Node::NullPtr; y = RLINK(y))
519 {
520 COUNT(y) = l_size;
521 l_size -= 1 + COUNT(LLINK(y));
522 }
523
524 return t;
525 }
526
527public:
539 Node * remove(const Key & key)
540 {
541 if (root == Node::NullPtr)
542 return nullptr;
543
544 const signed char cmp_result = splay_impl(key);
545 if (cmp_result != CmpEqual)
546 return nullptr; /* key not found */
547
548 Node *ret_val = root; /* store node to delete */
549
550 if (LLINK(root) == Node::NullPtr)
551 root = RLINK(root);
552 else if (RLINK(root) == Node::NullPtr)
553 root = LLINK(root);
554 else
555 {
556 Node *right_subtree = RLINK(root);
557
558 // Splay the maximum of left subtree to become root.
559 // We use splay_max() instead of splay(key) because splay(key) can
560 // bring a duplicate that is not the actual maximum, corrupting counts.
561 // splay_max() uses proper zig-zig rotations to maintain O(log n) amortized.
563
564 // The new root has no right child (it was the maximum)
565 // Connect the right subtree
566 RLINK(root) = right_subtree;
567 COUNT(root) = COUNT(LLINK(root)) + 1 + COUNT(right_subtree);
568 }
569
570 ret_val->reset();
571
572 return ret_val;
573 }
574
577 {
578 return root;
579 }
580
581 bool verify() const { return check_rank_tree(root) and check_bst(root, cmp); }
582
584 size_t size() const
585 {
586 return COUNT(root);
587 }
588
590 bool is_empty() const
591 {
592 return root == Node::NullPtr;
593 }
594
609 std::pair<long, Node *> position(const Key & key)
610 {
611 if (root == Node::NullPtr)
612 return {-1, nullptr};
613
614 const signed char cmp_result = splay_impl(key);
615 if (cmp_result == CmpEqual)
616 return {static_cast<long>(COUNT(LLINK(root))), root};
617
618 return {-1, nullptr};
619 }
620
651 std::pair<long, Node *> find_position(const Key & key)
652 {
653 if (root == Node::NullPtr)
654 return {-1, nullptr};
655
656 const signed char cmp_result = splay_impl(key);
657 if (cmp_result == CmpEqual)
658 return {static_cast<long>(COUNT(LLINK(root))), root};
659
660 // key not found - determine position it would have
661 if (cmp_result == CmpLess)
662 return {static_cast<long>(COUNT(LLINK(root))) - 1, root}; // less than root
663 return {static_cast<long>(COUNT(LLINK(root))) + 1, root};
664 // greater than root
665 }
666
677 Node * select(const size_t & i)
678 {
679 return Aleph::select(root, i);
680 }
681
687 {
689 Node *curr = Node::NullPtr;
690 size_t pos = 0;
691
692 public:
693 Iterator() = default;
694
695 Iterator(GenTdSplayTreeRk & tree) noexcept
696 : tree_ptr(&tree), curr(Node::NullPtr), pos(0)
697 {
698 if (tree_ptr->root != Node::NullPtr)
700 }
701
702 bool has_curr() const noexcept { return curr != Node::NullPtr; }
703
704 Node * get_curr() const
705 {
706 ah_overflow_error_if(curr == Node::NullPtr)
707 << "Iterator is at end";
708 return curr;
709 }
710
711 Node * get_curr_ne() const noexcept { return curr; }
712
713 void next()
714 {
715 ah_overflow_error_if(curr == Node::NullPtr)
716 << "Iterator is at end";
718 ++pos;
719 }
720
721 void next_ne() noexcept
722 {
724 ++pos;
725 }
726
727 size_t get_pos() const noexcept { return pos; }
728
729 void reset_first() noexcept
730 {
731 pos = 0;
732 if (tree_ptr != nullptr && tree_ptr->root != Node::NullPtr)
734 else
735 curr = Node::NullPtr;
736 }
737
738 private:
739 static Node * find_min(Node *r) noexcept
740 {
741 while (LLINK(r) != Node::NullPtr)
742 r = LLINK(r);
743 return r;
744 }
745
746 static Node * inorder_successor(Node *root, const Key & key) noexcept
747 {
748 Node *successor = Node::NullPtr;
749 Node *curr = root;
750
751 while (curr != Node::NullPtr)
752 {
753 if (key < KEY(curr))
754 {
755 successor = curr;
756 curr = LLINK(curr);
757 }
758 else
759 curr = RLINK(curr);
760 }
761
762 return successor;
763 }
764 };
765};
766
767
768template <typename Key, class Compare = Aleph::less<Key>>
769 requires StrictWeakOrder<Compare, Key>
770struct Splay_Tree_Rk : public GenTdSplayTreeRk<BinNodeXt, Key, Compare>
771{
773 using Base::Base;
774};
775
776
777template <typename Key, class Compare = Aleph::less<Key>>
778 requires StrictWeakOrder<Compare, Key>
779struct Splay_Tree_Rk_Vtl : public GenTdSplayTreeRk<BinNodeXtVtl, Key, Compare>
780{
782 using Base::Base;
783};
784
785
786#endif /* TPL_SPLAY_TREE_RK_H */
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
@ sentinelCtor
Definition ahDefs.H:83
@ KEY
Definition btreepic.C:169
Inorder iterator over the extended splay tree.
size_t get_pos() const noexcept
Node * get_curr_ne() const noexcept
static Node * find_min(Node *r) noexcept
Iterator(GenTdSplayTreeRk &tree) noexcept
bool has_curr() const noexcept
static Node * inorder_successor(Node *root, const Key &key) noexcept
Top-down splay tree with rank support.
GenTdSplayTreeRk(GenTdSplayTreeRk &&tree) noexcept(bin_node_detail::nothrow_compare_taken_v< Compare >)
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
std::pair< long, Node * > position(const Key &key)
Returns the inorder (sorted) position of key.
Node * splay_max(Node *r) noexcept
Splay the maximum element to the root.
void splay(const Key &key) noexcept
Search key within the tree and splay that node; if key is not found, the last node of the search is s...
Compare & get_compare()
See key_comp().
static constexpr signed char CmpGreater
std::pair< long, Node * > find_position(const Key &key)
Returns the inorder (sorted) position of key.
GenTdSplayTreeRk() noexcept(std::is_nothrow_default_constructible_v< Compare >)
Builds an empty tree with a comparison criteria built by default.
void swap(GenTdSplayTreeRk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Exchange in O(1) the nodes and the comparison criteria of this tree and another.
Node * insert(Node *p)
Inserts a node in a top down splay tree.
GenTdSplayTreeRk(const GenTdSplayTreeRk &)=delete
Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
NodeType< Key > Node
Node * remove(const Key &key)
Remove a key from a top-down splay tree.
Node * search_or_insert(Node *p)
Searches the key of p and, if it is not in the tree, inserts p.
Compare & key_comp()
Returns a reference to the comparison functor.
bool is_empty() const
Returns true if the tree is empty.
signed char splay_impl(const Key &key) noexcept
Node * search(const Key &key)
Searches a key in a top down splay tree.
GenTdSplayTreeRk & operator=(const GenTdSplayTreeRk &)=delete
Not copyable, for the same reason as the copy constructor.
GenTdSplayTreeRk(Compare __cmp) noexcept(std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with the comparison criteria __cmp.
Node * __insert(Node *p)
static constexpr signed char CmpEqual
Key key_type
The key type stored in the node.
Node * select(const size_t &i)
Returns the node whose inorder position in the extended tree is i.
Node * insert_dup(Node *p)
Inserts a node in a top down splay tree, even if its key is already in the tree.
virtual ~GenTdSplayTreeRk()
Destructor.
static constexpr signed char CmpLess
size_t size() const
Returns the number of nodes stored in the tree.
Node *& getRoot()
Get the top-down splay tree's root.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
Node * rotate_to_left_xt(Node *p) noexcept
Rotate to left the extended binary tree with root p.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
Node * rotate_to_right_xt(Node *p) noexcept
Rotate to right the extended bianry tree with root p
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
#define RLINK(i, n)
#define LLINK(i, n)
Gsl_Compatible_Rng r
Extended binary node with subtree count.
DynList< int > l