4 # include <gsl/gsl_rng.h>
5 # include <ahFunction.H>
6 # include <tpl_binTreeOps.H>
7 # include <ran_array.h>
8 # include <treapNode.H>
10 using namespace Aleph;
16 unsigned long priority;
28 : priority(Max_Priority), count(0)
33 unsigned long & getPriority() {
return priority; }
35 unsigned long & getCount() {
return count; }
39 priority = Max_Priority;
86 template <
template <
class>
class NodeType,
class Key,
class Compare>
92 typedef NodeType<Key>
Node;
102 void init(
unsigned int seed)
104 PRIO(head_ptr) = Min_Priority;
105 r = gsl_rng_alloc(gsl_rng_mt19937);
108 throw std::bad_alloc();
110 gsl_rng_set(r, seed % gsl_rng_max(r));
123 : head_ptr(&head), tree_root(
RLINK(head_ptr)), r(NULL), cmp(__cmp)
129 : head_ptr(&head), tree_root(
RLINK(head_ptr)), r(NULL), cmp(__cmp)
157 std::swap(tree_root, tree.tree_root);
158 std::swap(cmp, tree.cmp);
159 std::swap(r, tree.r);
168 Node* ret_val = searchInBinTree<Node, Compare>(tree_root, key, cmp);
170 return ret_val == Node::NullPtr ? NULL : ret_val;
178 if (root == Node::NullPtr)
184 if (cmp(
KEY(p),
KEY(root)))
186 if (insert(
LLINK(root), p))
189 if (PRIO(
LLINK(root)) < PRIO(root) )
195 else if (cmp(
KEY(root),
KEY(p)))
197 if (insert(
RLINK(root), p))
200 if (PRIO(
RLINK(root)) < PRIO(root) )
217 if (root == Node::NullPtr)
220 if (cmp(
KEY(p),
KEY(root)))
222 Node * ret = search_or_insert(
LLINK(root), p);
227 if (PRIO(
LLINK(root)) < PRIO(root))
230 I(PRIO(root) <= PRIO(
LLINK(root)) and
231 PRIO(root) <= PRIO(
LLINK(root)));
236 else if (cmp(
KEY(root),
KEY(p)))
238 Node * ret = search_or_insert(
RLINK(root), p);
243 if (PRIO(
RLINK(root)) < PRIO(root))
246 I(PRIO(root) <= PRIO(
LLINK(root)) and
247 PRIO(root) <= PRIO(
LLINK(root)));
253 I(PRIO(root) <= PRIO(
LLINK(root)) and PRIO(root) <= PRIO(
LLINK(root)));
261 if (root == Node::NullPtr)
264 if (cmp(
KEY(p),
KEY(root)))
266 insert_dup(
LLINK(root), p);
268 if (PRIO(
LLINK(root)) < PRIO(root))
273 insert_dup(
RLINK(root), p);
275 if (PRIO(
RLINK(root)) < PRIO(root))
292 I (p != Node::NullPtr);
294 PRIO(p) = gsl_rng_get(r);
296 return insert(tree_root, p) ? p : NULL;
310 I(p != Node::NullPtr);
312 PRIO(p) = gsl_rng_get(r);
314 return insert_dup(tree_root, p);
331 I(p != Node::NullPtr);
333 PRIO(p) = gsl_rng_get(r);
335 return search_or_insert(tree_root, p);
338 bool verify() {
return is_treap(tree_root); }
344 if (t1 == Node::NullPtr)
347 if (t2 == Node::NullPtr)
350 if (PRIO(t1) < PRIO(t2) )
366 Node *
remove(
Node *& root,
const Key & key)
368 if (root == Node::NullPtr)
369 return Node::NullPtr;
373 if (cmp(key,
KEY(root) ))
374 ret_val =
remove(
LLINK(root), key);
375 else if (cmp(
KEY(root), key))
376 ret_val =
remove(
RLINK(root), key);
380 root = join_treaps(
LLINK(root),
RLINK(root) );
385 if (ret_val == Node::NullPtr)
386 return Node::NullPtr;
406 Node * ret_val =
remove(tree_root, key);
408 if (ret_val == Node::NullPtr)
431 Node *
remove(
const size_t & beg,
const size_t & end)
433 if (beg > end or end >
COUNT(tree_root))
434 throw std::range_error(
"remove of TreapRk out of range");
436 Node * before_beg, * after_end;
438 Node * ret_val = tree_root;
444 tree_root = join_treaps(before_beg, after_end);
467 return COUNT(tree_root);
473 return tree_root == Node::NullPtr;
490 std::pair<int, Node*> ret_val;
495 IG(
KEY(ret_val.second) == key, ret_val.first >= 0);
532 std::pair<int, Node*> r(-2, NULL);
560 t.tree_root->reset();
561 Node * ret = insert(t.tree_root);
563 dup.insert(t.tree_root);
571 tree_root = join_treaps(tree_root, t.tree_root);
588 mutable int curr_pos;
590 static const int Pos_Not_Current = -1;
591 static const int Pos_Empty_Container = -2;
592 static const int Pos_Not_Updated = -3;
596 bool is_container_empty()
const
601 bool pos_updated()
const
603 return curr_pos != Pos_Not_Updated;
606 bool curr_updated()
const
613 return pos_updated() and curr_updated();
616 void update_pos()
const
624 void update_curr()
const
626 I(curr_pos != Pos_Not_Updated);
628 if (curr_pos == Pos_Empty_Container or curr_pos == Pos_Not_Current or
638 Iterator() : tree_ptr(NULL), curr(NULL), curr_pos(Pos_Not_Current)
645 : tree_ptr(&__tree), curr(NULL)
647 curr_pos = is_container_empty() ? Pos_Empty_Container : 0;
653 : tree_ptr(&__tree), curr(__curr), curr_pos(Pos_Not_Updated)
660 : tree_ptr(&__tree), curr(NULL), curr_pos(pos)
667 : tree_ptr(itor.tree_ptr), curr(itor.curr), curr_pos(itor.curr_pos)
678 tree_ptr = itor.tree_ptr;
680 curr_pos = itor.curr_pos;
689 curr_pos = is_container_empty() ? Pos_Empty_Container : 0;
696 curr_pos = (is_container_empty() ? Pos_Empty_Container :
734 if (not curr_updated())
757 throw(std::exception, std::underflow_error, std::overflow_error)
759 if (not pos_updated())
763 throw std::range_error(
"TreapRk iterator has not current");
766 throw std::range_error(
"TreapRk iterator has not current");
775 if (not pos_updated())
778 return curr_pos >= 0 and curr_pos <
COUNT(tree_ptr->
getRoot());
789 void prev() throw(std::exception, std::underflow_error)
792 throw std::underflow_error(
"TreapRk iterator has not current");
799 void next() throw(std::exception, std::overflow_error)
802 throw std::underflow_error(
"TreapRk iterator has not current");
813 throw std::underflow_error(
"TreapRk iterator has not current");
815 if (not curr_updated())
818 Node * ret_val = tree_ptr->remove(
KEY(curr) );
828 if (is_container_empty() and itor.is_container_empty())
831 if (pos_updated() and itor.pos_updated())
832 return curr_pos == itor.curr_pos;
834 if (curr_updated() and itor.curr_updated())
835 return curr == itor.curr;
837 if (not pos_updated())
840 return curr_pos == itor.curr_pos;
845 return curr_pos == itor.curr_pos;
851 return not (*
this == itor);
859 bool verify(
const Iterator & it)
const
861 return tree_ptr->
getRoot() == it.tree_ptr->getRoot();
901 template <
class Key,
class Compare = Aleph::less<Key> >
908 Treap_Rk(
unsigned int seed, Compare && cmp = Compare())
909 :
Gen_Treap_Rk<Treap_Rk_Node, Key, Compare>(seed, std::move(cmp))
914 Treap_Rk(Compare && cmp = Compare())
915 :
Gen_Treap_Rk<Treap_Rk_Node, Key, Compare>(time(NULL), std::move(cmp))
920 Treap_Rk(
unsigned int seed, Compare & cmp)
927 :
Gen_Treap_Rk<Treap_Rk_Node, Key, Compare>(time(NULL), cmp)
968 template <
class Key,
class Compare = Aleph::less<Key> >
976 :
Gen_Treap_Rk<Treap_Rk_NodeVtl, Key, Compare>(seed, std::move(cmp))
982 :
Gen_Treap_Rk<Treap_Rk_NodeVtl, Key, Compare>(time(NULL), std::move(cmp))
988 :
Gen_Treap_Rk<Treap_Rk_NodeVtl, Key, Compare>(seed, cmp)
994 :
Gen_Treap_Rk<Treap_Rk_NodeVtl, Key, Compare>(time(NULL), cmp)
1002 # endif // TPL_TREAPRK_H
void reset_last()
Reinicia el iterador al último nodo (mayor) del treap.
Definition: tpl_treapRk.H:693
Treap_Rk(unsigned int seed, Compare &&cmp=Compare())
Definition: tpl_treapRk.H:908
void reset_to_node(Node *node)
Definition: tpl_treapRk.H:718
bool operator==(const Iterator &itor) const
Retorna true si *this está sobre el mismo nodo que itor.
Definition: tpl_treapRk.H:826
#define LLINK(p)
Definition: tpl_binNode.H:196
Gen_Treap_Rk(unsigned int seed, Compare &__cmp)
Constructor; inicializa generador de números aleatorios.
Definition: tpl_treapRk.H:122
Node * get_current() const
Retorna el nodo actual.
Definition: tpl_treapRk.H:732
Definition: tpl_treapRk.H:902
std::pair< int, Node * > find_position(const Key &key)
Definition: tpl_treapRk.H:530
#define RLINK(p)
Definition: tpl_binNode.H:201
Iterator & operator=(const Iterator &itor)
Asigna al iterador this el iterador itor.
Definition: tpl_treapRk.H:673
std::pair< int, Node * > position(const Key &key)
Definition: tpl_treapRk.H:488
void next()
Avanza el iterador una posición hacia delante.
Definition: tpl_treapRk.H:799
Definition: tpl_treapRk.H:969
Definition: tpl_treapRk.H:87
Node *& getRoot()
Retorna la raíz del treap extendido.
Definition: tpl_treapRk.H:163
void reset_to_pos(size_t pos)
Coloca la posición actual del iterador en la posición pos.
Definition: tpl_treapRk.H:725
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p)
Definition: tpl_binNodeXt.H:135
Gen_Treap_Rk(const Gen_Treap_Rk &)
Definition: tpl_treapRk.H:145
bool operator!=(const Iterator &itor) const
Retorna true si *this no es igual a itor.
Definition: tpl_treapRk.H:849
bool is_empty() const
Retorna true si el treap está vacío.
Definition: tpl_treapRk.H:471
Iterator(Gen_Treap_Rk &__tree, const size_t &pos)
Instancia un iterador a partir de la posición pos.
Definition: tpl_treapRk.H:659
Node * del()
Definition: tpl_treapRk.H:810
#define COUNT(p)
Definition: tpl_binNodeXt.H:34
Node * insert_dup(Node *p)
Definition: tpl_treapRk.H:308
bool has_curr() const
Definition: tpl_treapRk.H:783
size_t size() const
Retorna la cantidad de nodos que contiene el treap.
Definition: tpl_treapRk.H:465
void split_key_dup_rec_xt(Node *root, const typename Node::key_type &key, Node *&l, Node *&r)
Definition: tpl_binNodeXt.H:427
Node * rotate_to_right_xt(Node *p)
Definition: tpl_binNodeXt.H:757
Definition: tpl_binTreeOps.H:632
Definition: tpl_treapRk.H:582
NodeType< Key > Node
El tipo de nodo interno.
Definition: tpl_treapRk.H:92
Treap_Rk_Vtl(unsigned int seed, Compare &&cmp=Compare())
Definition: tpl_treapRk.H:975
Node * insert(Node *p)
Definition: tpl_treapRk.H:290
Compare & get_compare()
Definition: tpl_treapRk.H:119
Node * select(Node *r, const size_t &pos)
Definition: tpl_binNodeXt.H:89
Node * get_curr() const
Retorna el nodo actual.
Definition: tpl_treapRk.H:741
void swap(Gen_Treap_Rk &tree)
Definition: tpl_treapRk.H:155
Iterator(Gen_Treap_Rk &__tree)
Instancia un iterador a partir del menor nodo del treap __tree.
Definition: tpl_treapRk.H:644
Compare & key_comp()
Retorna una referencia al criterio de comparación.
Definition: tpl_treapRk.H:116
size_t get_current_position() const
Definition: tpl_treapRk.H:756
void reset_first()
Reinicia el iterador al primer nodo (menor) del treap.
Definition: tpl_treapRk.H:686
#define DECLARE_BINNODE_SENTINEL(Name, height, Control_Data)
Definition: tpl_binNode.H:170
void split_pos_rec(Node *r, const size_t i, Node *&ts, Node *&tg)
Definition: tpl_binNodeXt.H:541
Node * select(const size_t &i)
Definition: tpl_treapRk.H:459
Node * rotate_to_left_xt(Node *p)
Definition: tpl_binNodeXt.H:776
Iterator(const Iterator &itor)
Instancia un iterador a partir del estado del iterador itor.
Definition: tpl_treapRk.H:666
bool split_key_rec_xt(Node *root, const typename Node::key_type &key, Node *&l, Node *&r)
Definition: tpl_binNodeXt.H:373
Node * search(const Key &key)
Busca la clave key en el treap extendido this.
Definition: tpl_treapRk.H:166
#define KEY(p)
Definition: tpl_binNode.H:206
Iterator(Gen_Treap_Rk &__tree, Node *__curr)
Definition: tpl_treapRk.H:652
void prev()
Avanza el iterador una posición hacia atrás.
Definition: tpl_treapRk.H:789
Iterator()
Constructor vacío; no tiene sentido si no se asigna un treap.
Definition: tpl_treapRk.H:638
Definition: tpl_treapRk.H:14
void reset_to_key(const Key &key)
Definition: tpl_treapRk.H:706
Node * search_or_insert(Node *p)
Definition: tpl_treapRk.H:329
bool has_current() const
Definition: tpl_treapRk.H:773