27 # ifndef TPL_TREAPRK_H 28 # define TPL_TREAPRK_H 30 # include <gsl/gsl_rng.h> 31 # include <ahFunction.H> 32 # include <tpl_binTreeOps.H> 33 # include <ran_array.h> 34 # include <treapNode.H> 36 using namespace Aleph;
40 class TreapRkNode_Data
42 unsigned long priority;
48 TreapRkNode_Data() noexcept : priority(Max_Priority), count(1)
53 TreapRkNode_Data(SentinelCtor) noexcept : priority(Max_Priority), count(0)
58 unsigned long & getPriority() noexcept {
return priority; }
60 unsigned long & getCount() noexcept {
return count; }
62 void reset() noexcept { count = 1; }
106 template <
template <
class>
class NodeType,
class Key,
class Compare>
111 using Node = NodeType<Key>;
121 void init(
unsigned int seed)
123 PRIO(head_ptr) = Min_Priority;
124 r = gsl_rng_alloc(gsl_rng_mt19937);
127 throw std::bad_alloc();
129 gsl_rng_set(r, seed % gsl_rng_max(r));
135 void set_seed(
unsigned long seed) noexcept { gsl_rng_set(r, seed); }
146 : head_ptr(&head), tree_root(
RLINK(head_ptr)), r(nullptr), cmp(__cmp)
165 std::swap(tree_root, tree.tree_root);
166 std::swap(cmp, tree.cmp);
167 std::swap(r, tree.r);
171 Node *&
getRoot() noexcept {
return tree_root; }
173 Node * getRoot()
const noexcept {
return tree_root; }
181 Node *
search(
const Key & key)
const noexcept
184 return ret_val == Node::NullPtr ? nullptr : ret_val;
189 bool insert(Node *& root, Node * p) noexcept
191 if (root == Node::NullPtr)
197 if (cmp(
KEY(p),
KEY(root)))
199 if (insert(
LLINK(root), p))
208 else if (cmp(
KEY(root),
KEY(p)))
210 if (insert(
RLINK(root), p))
225 Node * search_or_insert(Node *& root, Node * p) noexcept
227 if (root == Node::NullPtr)
230 if (cmp(
KEY(p),
KEY(root)))
232 Node * ret = search_or_insert(
LLINK(root), p);
245 else if (cmp(
KEY(root),
KEY(p)))
247 Node * ret = search_or_insert(
RLINK(root), p);
267 Node * insert_dup(Node *& root, Node * p) noexcept
269 if (root == Node::NullPtr)
272 if (cmp(
KEY(p),
KEY(root)))
274 insert_dup(
LLINK(root), p);
281 insert_dup(
RLINK(root), p);
303 assert(p != Node::NullPtr);
305 PRIO(p) = gsl_rng_get(r);
307 return insert(tree_root, p) ? p :
nullptr;
319 assert(p != Node::NullPtr);
321 PRIO(p) = gsl_rng_get(r);
323 return insert_dup(tree_root, p);
341 assert(p != Node::NullPtr);
343 PRIO(p) = gsl_rng_get(r);
345 return search_or_insert(tree_root, p);
349 bool verify()
const {
return is_treap(tree_root); }
355 if (t1 == Node::NullPtr)
358 if (t2 == Node::NullPtr)
375 Node *
remove(Node *& root,
const Key & key) noexcept
377 if (root == Node::NullPtr)
378 return Node::NullPtr;
381 if (cmp(key,
KEY(root) ))
382 ret_val =
remove(
LLINK(root), key);
383 else if (cmp(
KEY(root), key))
384 ret_val =
remove(
RLINK(root), key);
393 if (ret_val == Node::NullPtr)
394 return Node::NullPtr;
409 Node *
remove(
const Key & key) noexcept
411 Node * ret_val =
remove(tree_root, key);
412 if (ret_val == Node::NullPtr)
428 Node *
remove(
const size_t beg,
const size_t end)
430 if (beg > end or end >
COUNT(tree_root))
431 throw std::range_error(
"remove of TreapRk out of range");
433 Node * before_beg, * after_end, * aux;
435 Node * ret_val = tree_root;
447 static Node * remove_pos(Node *& root,
const size_t pos) noexcept
451 Node * ret_val = root;
458 return remove_pos(
LLINK(root), pos);
474 if (pos >=
COUNT(tree_root))
475 throw std::out_of_range(
"infix position out of range");
477 return remove_pos(tree_root, pos);
493 size_t size() const noexcept {
return COUNT(tree_root); }
496 bool is_empty() const noexcept {
return tree_root == Node::NullPtr; }
507 std::pair<int, Node*>
position(
const Key & key)
const noexcept
509 std::pair<int, Node*> ret_val;
545 std::pair<int, Node*> r(-2,
nullptr);
595 void join_dup(Node *& t1, Node * t2) noexcept
597 if (t2 == Node::NullPtr)
602 t1 = insert_dup(t1, t2);
607 void join(Node *& t1, Node * t2, Node *& dup) noexcept
609 if (t2 == Node::NullPtr)
615 Node * ret = insert(t1, t2);
616 if (ret == Node::NullPtr)
618 dup = insert_dup(dup,
remove(t1,
KEY(t2)));
643 join(tree_root, t.tree_root, dup.tree_root);
644 t.tree_root = Node::NullPtr;
657 join_dup(tree_root, t.tree_root);
658 t.tree_root = Node::NullPtr;
675 t.tree_root = Node::NullPtr;
690 mutable int curr_pos;
692 static const int Pos_Not_Current = -1;
693 static const int Pos_Empty_Container = -2;
694 static const int Pos_Not_Updated = -3;
698 bool is_container_empty()
const noexcept
703 bool pos_updated()
const noexcept
705 return curr_pos != Pos_Not_Updated;
708 bool curr_updated()
const noexcept
710 return curr !=
nullptr;
713 bool is_updated() noexcept
715 return pos_updated() and curr_updated();
718 void update_pos()
const noexcept
720 assert(curr !=
nullptr);
726 void update_curr()
const noexcept
728 assert(curr_pos != Pos_Not_Updated);
730 if (curr_pos == Pos_Empty_Container or curr_pos == Pos_Not_Current or
740 : tree_ptr(
nullptr), curr(
nullptr), curr_pos(Pos_Not_Current)
747 : tree_ptr(&const_cast<Gen_Treap_Rk&>(__tree)), curr(
nullptr)
749 curr_pos = is_container_empty() ? Pos_Empty_Container : 0;
754 : tree_ptr(&const_cast<Gen_Treap_Rk&>(__tree)),
755 curr(__curr), curr_pos(Pos_Not_Updated)
762 : tree_ptr(&const_cast<Gen_Treap_Rk&>(__tree)),
763 curr(
nullptr), curr_pos(pos)
769 : tree_ptr(itor.tree_ptr), curr(itor.curr), curr_pos(itor.curr_pos)
779 tree_ptr = itor.tree_ptr;
781 curr_pos = itor.curr_pos;
790 curr_pos = is_container_empty() ? Pos_Empty_Container : 0;
797 curr_pos = (is_container_empty() ? Pos_Empty_Container :
804 put_itor_at_the_end(*
this);
841 if (not curr_updated())
850 return get_curr_ne();
863 if (not pos_updated())
867 throw std::range_error(
"TreapRk iterator has not current");
870 throw std::range_error(
"TreapRk iterator has not current");
876 size_t get_pos()
const {
return get_current_position(); }
881 if (not pos_updated())
884 return curr_pos >= 0 and curr_pos <
COUNT(tree_ptr->
getRoot());
892 throw std::underflow_error(
"TreapRk iterator has not current");
898 void next_ne() noexcept
909 throw std::underflow_error(
"TreapRk iterator has not current");
917 throw std::underflow_error(
"TreapRk iterator has not current");
919 if (not curr_updated())
922 Node * ret_val = tree_ptr->remove(
KEY(curr) );
930 bool operator == (
const Iterator & itor)
const noexcept
932 if (is_container_empty() and itor.is_container_empty())
935 if (pos_updated() and itor.pos_updated())
936 return curr_pos == itor.curr_pos;
938 if (curr_updated() and itor.curr_updated())
939 return curr == itor.curr;
941 if (not pos_updated())
944 return curr_pos == itor.curr_pos;
949 return curr_pos == itor.curr_pos;
955 return not (*
this == itor);
963 bool verify(
const Iterator & it)
const noexcept
1009 template <
typename Key,
class Compare = Aleph::less<Key>>
1055 template <
typename Key,
class Compare = Aleph::less<Key>>
1065 # endif // TPL_TREAPRK_H void split_pos(size_t pos, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2)
Definition: tpl_treapRk.H:588
void reset_to_key(const Key &key) noexcept
Definition: tpl_treapRk.H:814
void reset_to_pos(size_t pos) noexcept
Put the current to the position pos.
Definition: tpl_treapRk.H:832
Node * rotate_to_left_xt(Node *p) noexcept
Definition: tpl_binNodeXt.H:935
void next()
Definition: tpl_treapRk.H:906
Node * select(const size_t i) const
Definition: tpl_treapRk.H:487
Definition: tpl_treapRk.H:107
void split_key_dup(const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept
Definition: tpl_treapRk.H:577
bool has_curr() const noexcept
Return true if iterator has current node.
Definition: tpl_treapRk.H:879
Iterator(const Gen_Treap_Rk &__tree) noexcept
Initialize an iterator on __tree
Definition: tpl_treapRk.H:746
auto & COUNT(Node *p) noexcept
Definition: tpl_binNodeXt.H:66
void reset_to_node(Node *node) noexcept
Definition: tpl_treapRk.H:825
long find_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept
Definition: tpl_binNodeXt.H:290
std::pair< int, Node * > position(const Key &key) const noexcept
Definition: tpl_treapRk.H:507
void split_key_dup_rec_xt(Node *&root, const typename Node::key_type &key, Node *&l, Node *&r, Compare &cmp) noexcept
Definition: tpl_binNodeXt.H:572
Node * del()
Remove the current node and move the iterator one position forward.
Definition: tpl_treapRk.H:914
size_t get_current_position() const
Definition: tpl_treapRk.H:861
Node * remove_pos(const size_t pos)
Definition: tpl_treapRk.H:472
Node * search(const Key &key) const noexcept
Definition: tpl_treapRk.H:181
Node * insert(Node *p) noexcept
Definition: tpl_treapRk.H:301
Node * insert_dup(Node *p) noexcept
Definition: tpl_treapRk.H:317
bool verify() const
Return true if the treap is consistent.
Definition: tpl_treapRk.H:349
void join(Gen_Treap_Rk &t, Gen_Treap_Rk &dup) noexcept
Definition: tpl_treapRk.H:641
Node * rotate_to_right_xt(Node *p) noexcept
Definition: tpl_binNodeXt.H:914
Compare & key_comp() noexcept
return the comparison criteria
Definition: tpl_treapRk.H:138
std::pair< int, Node * > find_position(const Key &key) const noexcept
Definition: tpl_treapRk.H:543
Definition: tpl_binTreeOps.H:560
Definition: tpl_treapRk.H:684
Node * searchInBinTree(Node *root, const typename Node::key_type &key, Compare cmp=Compare()) noexcept
Definition: tpl_binNodeUtils.H:1323
Node::Key_Type & KEY(Node *p) noexcept
Definition: tpl_binNode.H:318
void split_pos_rec(Node *&r, const size_t i, Node *&ts, Node *&tg)
Definition: tpl_binNodeXt.H:710
Node * select(Node *r, const size_t pos)
Definition: tpl_binNodeXt.H:150
void join_dup(Gen_Treap_Rk &t) noexcept
Definition: tpl_treapRk.H:655
Iterator(const Gen_Treap_Rk &__tree, Node *__curr) noexcept
Initialize an iterator startin from node __curr
Definition: tpl_treapRk.H:753
void swap(Gen_Treap_Rk &tree) noexcept
Swap in constant time all the nodes of this with tree
Definition: tpl_treapRk.H:163
size_t get_pos() const
Definition: tpl_treapRk.H:876
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept
Definition: tpl_binNodeXt.H:210
Node *& LLINK(Node *p) noexcept
Definition: tpl_binNode.H:298
Node * get_curr_ne() const noexcept
Return the current node.
Definition: tpl_treapRk.H:839
void reset_last() noexcept
Reset the iterator to the last position.
Definition: tpl_treapRk.H:794
bool is_empty() const noexcept
Return true if tree is empty.
Definition: tpl_treapRk.H:496
Definition: tpl_treapRk.H:1010
Node * join(Node *t1, Node *t2, Node *&dup, Compare cmp=Compare()) noexcept
Definition: tpl_binNodeUtils.H:1855
Compare & get_compare() noexcept
Definition: tpl_treapRk.H:141
#define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data)
Definition: tpl_binNode.H:272
unsigned long & PRIO(Node *p) noexcept
Definition: treapNode.H:63
size_t size() const noexcept
Return the number of nodes contained in the tree.
Definition: tpl_treapRk.H:493
Node *& getRoot() noexcept
Return the tree's root.
Definition: tpl_treapRk.H:171
bool split_key_rec_xt(Node *&root, const typename Node::key_type &key, Node *&l, Node *&r, Compare &cmp) noexcept
Definition: tpl_binNodeXt.H:512
Node * join_exclusive(Node *&ts, Node *&tg) noexcept
Definition: tpl_binNodeUtils.H:1708
void join_exclusive(Gen_Treap_Rk &t) noexcept
Definition: tpl_treapRk.H:672
Definition: tpl_treapRk.H:1056
Gen_Treap_Rk(unsigned long seed, Compare __cmp=Compare())
Definition: tpl_treapRk.H:145
Node * get_curr() const noexcept
Definition: tpl_treapRk.H:848
Node *& RLINK(Node *p) noexcept
Definition: tpl_binNode.H:307
void reset_first() noexcept
Reset the iterator to the first position.
Definition: tpl_treapRk.H:787
void prev()
Definition: tpl_treapRk.H:889
void end() noexcept
Put the iterator in the end state.
Definition: tpl_treapRk.H:802
Iterator(const Gen_Treap_Rk &__tree, const size_t pos) noexcept
Initialize an iterator starting from the iorder position pos
Definition: tpl_treapRk.H:761
Node * search_or_insert(Node *p) noexcept
Definition: tpl_treapRk.H:339
bool split_key(const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept
Definition: tpl_treapRk.H:561
void set_seed(unsigned long seed) noexcept
Set the random number generator seed.
Definition: tpl_treapRk.H:135