2 # ifndef TPL_DYNSETTREE_H
3 # define TPL_DYNSETTREE_H
5 # include <tpl_binNodeUtils.H>
6 # include <tpl_binNodeXt.H>
7 # include <tpl_binTree.H>
8 # include <tpl_treap.H>
9 # include <tpl_treapRk.H>
11 # include <tpl_rand_tree.H>
12 # include <tpl_rb_tree.H>
13 # include <tpl_splay_tree.H>
14 # include <tpl_nodePool.H>
16 using namespace Aleph;
31 template <
typename,
class>
class Tree =
Avl_Tree,
40 typedef typename Tree<Key, Compare>::Node
Node;
44 static const size_t dim = 13;
46 typedef Tree<Key, Compare> Tree_Type;
48 mutable Tree<Key, Compare> tree;
56 typedef Key Item_Type;
68 std::swap(num_nodes, dset.num_nodes);
73 : tree(std::move(cmp)), num_nodes(0), node_pool(dim)
79 : tree(cmp), num_nodes(0), node_pool(dim)
86 : tree(srcTree.tree.get_compare()), num_nodes(srcTree.num_nodes),
87 node_pool(srcTree.dim)
89 Node * srcRoot = srcTree.tree.getRoot();
92 tree.getRoot() =
copyRec(srcRoot);
103 list.for_each([
this] (
const Key & k) {
insert(k); });
126 if (
this == &srcTree)
129 Node *src_root = (
Node*) const_cast<DynSetTree&>(srcTree).tree.getRoot();
133 tree.getRoot() =
copyRec(src_root);
134 num_nodes = srcTree.num_nodes;
154 Key * __insert(
Node * p)
156 if (tree.search_or_insert(p) != p)
158 node_pool.deallocate(p);
164 return &p->get_key();
179 return __insert(node_pool.allocate(key));
184 return __insert(node_pool.allocate(std::move(key)));
187 Key * append(
const Key & key)
189 return __insert(node_pool.allocate(key));
192 Key * append(Key && key)
194 return __insert(node_pool.allocate(std::move(key)));
199 Key * __search_or_insert(
Node * p)
201 Node * q = tree.search_or_insert(p);
203 node_pool.deallocate(p);
207 return &q->get_key();
227 return __search_or_insert(node_pool.allocate(key));
232 return __search_or_insert(node_pool.allocate(std::move(key)));
237 Key * __insert_dup(
Node * q)
239 Node * p = tree.insert_dup(q);
241 return &p->get_key();
246 Key * insert_dup(
const Key & key)
248 return __insert_dup(node_pool.allocate(key));
251 Key * insert_dup(Key && key)
253 return __insert_dup(node_pool.allocate(std::move(key)));
257 Key *
put(
const Key & key)
262 Key *
put(Key && key)
264 return insert(std::move(key));
274 size_t remove(
const Key & key)
276 Node * p =
static_cast<Node*
>(tree.remove(key));
281 node_pool.deallocate(p);
292 bool has(
const Key & key)
const
297 bool contains(
const Key & key)
const
316 Key &
find(
const Key & key)
throw(std::exception, std::domain_error)
318 Node * node =
static_cast<Node*
>(tree.search(key));
321 throw std::domain_error(
"key not found");
323 return node->get_key();
343 return std::pair<int, Key*> (0, NULL);
345 std::pair<int, Node*> p = tree.find_position(key);
347 return std::pair<int, Key*> (p.first, &p.second->get_key());
365 throw(std::exception, std::domain_error)
367 Node * node =
static_cast<Node*
>(tree.search(key));
372 return &(node->get_key());
380 throw std::domain_error(
"set is empty");
382 return find_min(tree.getRoot())->get_key();
393 throw std::domain_error(
"set is empty");
395 return find_max(tree.getRoot())->get_key();
408 const size_t &
size()
const {
return num_nodes; }
420 Node * get_root_node()
const {
return tree.getRoot(); }
422 const Key & get_root()
const
425 throw std::domain_error(
"Tree is empty");
426 return KEY(tree.getRoot());
437 Node * root =
static_cast<Node*
>(tree.getRoot());
455 std::pair<long, Node*> p = tree.position(key);
466 return tree.select(i)->get_key();
469 const Key &
select(
const size_t & i)
const
471 return tree.select(i)->get_key();
474 Key & operator () (
const size_t & i)
479 Key & access (
const size_t & i)
491 template <
class Key_Op>
496 Node_Op(Key_Op & __key_op) : key_op(__key_op) { }
498 Node_Op(Key_Op && __key_op) : key_op(__key_op)
503 void operator () (
Node * root)
536 template <
class Key_Op>
539 Node * root = (
Node *) tree.getRoot();
541 Node_Op <Key_Op> node_op(const_cast<Key_Op&>(key_op));
547 template <
class Key_Op>
550 for_each_preorder<Key_Op>(key_op);
577 template <
class Key_Op>
580 Node * root = (
Node *) tree.getRoot();
582 Node_Op <Key_Op> node_op(const_cast<Key_Op&>(key_op));
588 template <
class Key_Op>
591 for_each_inorder<Key_Op>(key_op);
618 template <
class Key_Op>
621 Node * root = (
Node *) tree.getRoot();
623 Node_Op <Key_Op> node_op(const_cast<Key_Op&>(key_op));
629 template <
class Key_Op>
632 for_each_postorder<Key_Op>(key_op);
653 join(tree.getRoot(), t.tree.getRoot(), dup.tree.getRoot());
656 t.tree.getRoot() = Node::NullPtr;
658 dup.num_nodes =
COUNT(dup.tree.getRoot());
659 num_nodes =
COUNT(tree.getRoot());
684 tree.join_dup(t.tree);
686 t.tree.getRoot() = Node::NullPtr;
687 num_nodes =
COUNT(tree.getRoot());
708 if (not split_key_rec_xt<Node, Compare>(tree.getRoot(), key,
709 l.tree.getRoot(), r.tree.getRoot()))
712 tree.getRoot() = Node::NullPtr;
714 l.num_nodes =
COUNT(l.tree.getRoot());
715 r.num_nodes =
COUNT(r.tree.getRoot());
735 split_pos_rec(tree.getRoot(), pos, l.tree.getRoot(), r.tree.getRoot());
737 tree.getRoot() = Node::NullPtr;
739 l.num_nodes =
COUNT(l.tree.getRoot());
740 r.num_nodes =
COUNT(r.tree.getRoot());
757 split_key_dup_rec_xt<Node,Compare>(tree.getRoot(), key,
758 l.tree.getRoot(), r.tree.getRoot());
760 tree.getRoot() = Node::NullPtr;
762 l.num_nodes =
COUNT(l.tree.getRoot());
763 r.num_nodes =
COUNT(r.tree.getRoot());
772 mutable int curr_pos;
774 static const int Pos_Not_Current = -1;
775 static const int Pos_Empty_Container = -2;
776 static const int Pos_Not_Updated = -3;
780 bool is_container_empty()
const
782 return tree_ptr->
size() == 0;
785 bool pos_updated()
const
787 return curr_pos != Pos_Not_Updated;
790 bool curr_updated()
const
797 return pos_updated() and curr_updated();
800 void update_pos()
const
804 curr_pos = tree_ptr->
position(curr->get_key());
807 void update_curr()
const
809 I(curr_pos != Pos_Not_Updated);
811 if (curr_pos == Pos_Empty_Container or curr_pos == Pos_Not_Current or
812 curr_pos == tree_ptr->
size())
815 curr = Node::key_to_node(tree_ptr->
select(curr_pos));
821 Iterator() : tree_ptr(NULL), curr(NULL), curr_pos(Pos_Not_Current)
828 : tree_ptr(&const_cast<
DynSetTree&>(tree)), curr(NULL)
830 curr_pos = is_container_empty() ? Pos_Empty_Container : 0;
836 if (tree_ptr == NULL)
837 std::domain_error(
"There is not tree associated to iterator");
840 curr = Node::key_to_node(tree_ptr->
select(curr_pos));
846 if (tree_ptr == NULL)
847 std::domain_error(
"There is not tree associated to iterator");
850 curr = Node::key_to_node(
KEY(p));
856 : tree_ptr(itor.tree_ptr), curr(itor.curr), curr_pos(itor.curr_pos)
867 tree_ptr = itor.tree_ptr;
869 curr_pos = itor.curr_pos;
878 curr_pos = is_container_empty() ? Pos_Empty_Container : 0;
886 is_container_empty() ? Pos_Empty_Container : tree_ptr->
size() - 1;
897 std::pair<long, Key*> p = tree_ptr->
position(key);
923 if (not curr_updated())
946 throw (std::exception, std::underflow_error, std::overflow_error)
948 if (not pos_updated())
952 throw std::range_error (
"DynSetTree::Iterator has not current");
954 if (curr_pos >
COUNT (tree_ptr->getRoot() ) )
955 throw std::range_error (
"DynSetTree::iterator has not current");
964 if (not pos_updated())
967 return curr_pos >= 0 and curr_pos < tree_ptr->
size();
978 void prev() throw (std::exception, std::underflow_error)
981 throw std::underflow_error (
"DynSetTree::Iterator has not current");
988 void next() throw (std::exception, std::overflow_error)
991 throw std::underflow_error (
"DynSetTree::Iterator has not current");
1002 throw std::underflow_error (
"DynSetTree::Iterator has not current");
1004 if (not curr_updated())
1007 Key ret_val = curr->get_key();
1008 tree_ptr->
remove(ret_val);
1018 if (is_container_empty() and itor.is_container_empty())
1021 if (pos_updated() and itor.pos_updated())
1022 return curr_pos == itor.curr_pos;
1024 if (curr_updated() and itor.curr_updated())
1025 return curr == itor.curr;
1027 if (not pos_updated())
1030 return curr_pos == itor.curr_pos;
1035 return curr_pos == itor.curr_pos;
1041 return not (*
this == itor);
1059 template <
class Operation>
1062 return Aleph::traverse(tree.getRoot(), [&op] (
Node * p)
1064 return op(p->get_key());
1068 template <
class Operation>
1069 bool traverse(Operation && op = Operation())
1071 return traverse<Operation>(op);
1074 template <
class Operation>
1075 bool traverse(Operation & op)
const
1077 return Aleph::traverse(tree.getRoot(), [&op] (
Node * p)
1079 return op(p->get_key());
1083 template <
class Operation>
1084 bool traverse(Operation && op = Operation())
const
1086 return traverse<Operation>(op);
1089 Functional_Methods(Key);
1095 # define SETTREE_ITOR(Name, Key, Cmp) \
1096 class Iterator : public DynSetTree<Key, Name, Cmp>::Iterator \
1099 Iterator() : DynSetTree<Key, Name, Cmp>::Iterator() \
1102 Iterator(DynSetTree<Key, Name, Cmp> & tree) \
1103 : DynSetTree<Key, Name, Cmp>::Iterator(tree) \
1114 template <
typename Key,
class Compare = Aleph::less<Key>>
1125 template <
typename Key,
class Compare = Aleph::less<Key>>
1136 template <
typename Key,
class Compare = Aleph::less<Key>>
1147 template <
typename Key,
class Compare = Aleph::less<Key>>
1173 template <
typename Key,
class Compare = Aleph::less<Key>>
1183 template <
typename Key,
class Compare = Aleph::less<Key>>
1187 SETTREE_ITOR(
Treap_Rk, Key, Compare);
1198 template <
typename Key,
class Compare = Aleph::less<Key>>
void for_each_inorder(Key_Op &key_op)
Definition: tpl_dynSetTree.H:578
Definition: tpl_dynSetTree.H:766
size_t get_current_position() const
Definition: tpl_dynSetTree.H:945
Definition: tpl_treapRk.H:902
Definition: tpl_nodePool.H:19
Definition: tpl_binNodeUtils.H:407
Tree< Key, Compare >::Node Node
Definition: tpl_dynSetTree.H:40
void set_key(const Key &key)
Coloca el iterador en la clave key.
Definition: tpl_dynSetTree.H:844
DynSetTree & join(DynSetTree &t, DynSetTree &dup)
Definition: tpl_dynSetTree.H:650
size_t remove(const Key &key)
Definition: tpl_dynSetTree.H:274
const size_t & size() const
Retorna la cardinalidad del conjunto.
Definition: tpl_dynSetTree.H:408
Definition: ahFunction.H:145
bool operator!=(const Iterator &itor) const
Retorna true si *this no es igual a itor.
Definition: tpl_dynSetTree.H:1039
Iterator()
Constructor vacío; no tiene sentido si no se asigna un treap.
Definition: tpl_dynSetTree.H:821
DynSetTree(const DynSetTree &srcTree)
Instancia un conjunto dinámico copia de srcTree.
Definition: tpl_dynSetTree.H:85
void reset_to_node(Node *node)
Definition: tpl_dynSetTree.H:907
Definition: tpl_dynSetTree.H:1115
const Key & get_last()
Definition: tpl_dynSetTree.H:399
Node * find_max(Node *root)
Definition: tpl_binNodeUtils.H:1784
#define COUNT(p)
Definition: tpl_binNodeXt.H:34
void reset_last()
Reinicia el iterador al último nodo (mayor) del treap.
Definition: tpl_dynSetTree.H:882
virtual ~DynSetTree()
Destructor; todos los elementos son liberados.
Definition: tpl_dynSetTree.H:147
void split_pos(const size_t pos, DynSetTree &l, DynSetTree &r)
Definition: tpl_dynSetTree.H:733
Definition: tpl_dynSetTree.H:1126
void set_pos(size_t pos)
Coloca el iterador en la posición pos.
Definition: tpl_dynSetTree.H:834
void reset_first()
Reinicia el iterador al primer nodo (menor) del treap.
Definition: tpl_dynSetTree.H:875
Key * search(const Key &key) const
Definition: tpl_dynSetTree.H:364
Key & select(const size_t &i)
Definition: tpl_dynSetTree.H:464
std::pair< int, Key * > find_position(const Key &key) const
Definition: tpl_dynSetTree.H:340
size_t internal_path_length() const
Definition: tpl_dynSetTree.H:415
void reset_to_key(const Key &key)
Definition: tpl_dynSetTree.H:895
size_t internal_path_length(Node *p)
Definition: tpl_binNodeUtils.H:1259
Node * find_min(Node *root)
Definition: tpl_binNodeUtils.H:1764
void for_each_preorder(Key_Op &&key_op=Key_Op())
Definition: tpl_dynSetTree.H:548
void swap(DynSetTree &dset)
Definition: tpl_dynSetTree.H:65
Key & get_current() const
Retorna el nodo actual.
Definition: tpl_dynSetTree.H:921
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Definition: tpl_binNodeUtils.H:93
Definition: tpl_binNodeUtils.H:325
Iterator(const DynSetTree &tree)
Instancia un iterador a partir del menor nodo del treap __tree.
Definition: tpl_dynSetTree.H:827
const Key & min() const
Definition: tpl_dynSetTree.H:377
void for_each_in_preorder(void(*visitFct)(Node *, int, int))
Definition: tpl_dynSetTree.H:435
Definition: tpl_dynSetTree.H:1137
const Key & max() const
Definition: tpl_dynSetTree.H:390
bool split_key(const Key &key, DynSetTree &l, DynSetTree &r)
Definition: tpl_dynSetTree.H:706
void split_key_dup(const Key &key, DynSetTree &l, DynSetTree &r)
Definition: tpl_dynSetTree.H:755
Definition: tpl_dynSetTree.H:1148
Key & find(const Key &key)
Definition: tpl_dynSetTree.H:316
size_t computeHeightRec(Node *node)
Definition: tpl_binNodeUtils.H:778
DynSetTree & join_dup(DynSetTree &t)
Definition: tpl_dynSetTree.H:682
const Key & get_first()
Definition: tpl_dynSetTree.H:386
Key & get_curr() const
Retorna el nodo actual.
Definition: tpl_dynSetTree.H:930
bool operator==(const Iterator &itor) const
Retorna true si *this está sobre el mismo nodo que itor.
Definition: tpl_dynSetTree.H:1016
void destroyRec(Node *&root)
Definition: tpl_binNodeUtils.H:798
void for_each_inorder(Key_Op &&key_op=Key_Op())
Definition: tpl_dynSetTree.H:589
bool is_empty() const
Retorna true si el conjunto está vacío.
Definition: tpl_dynSetTree.H:411
Key del()
Definition: tpl_dynSetTree.H:999
bool has_curr() const
Definition: tpl_dynSetTree.H:972
void next()
Avanza el iterador una posición hacia delante.
Definition: tpl_dynSetTree.H:988
bool exist(const Key &key) const
Retorna true si key pertenece al conjunto dinámico.
Definition: tpl_dynSetTree.H:287
void for_each_preorder(Key_Op &key_op)
Definition: tpl_dynSetTree.H:537
Definition: tpl_dynSetTree.H:34
Definition: tpl_binNodeUtils.H:172
Iterator & operator=(const Iterator &itor)
Asigna al iterador this el iterador itor.
Definition: tpl_dynSetTree.H:862
void for_each_postorder(Key_Op &&key_op=Key_Op())
Definition: tpl_dynSetTree.H:630
Definition: tpl_dynSetTree.H:1184
Node * copyRec(Node *src_root)
Definition: tpl_binNodeUtils.H:821
void prev()
Avanza el iterador una posición hacia atrás.
Definition: tpl_dynSetTree.H:978
void split_pos_rec(Node *r, const size_t i, Node *&ts, Node *&tg)
Definition: tpl_binNodeXt.H:541
Definition: tpl_avl.H:573
void for_each_postorder(Key_Op &key_op)
Definition: tpl_dynSetTree.H:619
Definition: tpl_dynSetTree.H:1199
bool check_binary_search_tree(Node *p)
Definition: tpl_binNodeUtils.H:1647
DynSetTree(Compare &&cmp=Compare())
Instancia un conjunto dinámico vacío.
Definition: tpl_dynSetTree.H:72
long position(const Key &key) const
Definition: tpl_dynSetTree.H:453
size_t height() const
Calcula y retorna la altura del árbol binario de búsqueda.
Definition: tpl_dynSetTree.H:430
Key * search_or_insert(const Key &key)
Definition: tpl_dynSetTree.H:225
#define KEY(p)
Definition: tpl_binNode.H:206
Definition: tpl_dynSetTree.H:1174
bool traverse(Operation &op)
Definition: tpl_dynSetTree.H:1060
void empty()
Elimina todos los elementos del conjunto.
Definition: tpl_dynSetTree.H:112
Key * put(const Key &key)
Seudo sinonimo de insert; no retorna ningún valor.
Definition: tpl_dynSetTree.H:257
void reset_to_pos(size_t pos)
Coloca la posición actual del iterador en la posición pos.
Definition: tpl_dynSetTree.H:914
Definition: tpl_binTreeOps.H:25
Iterator(const Iterator &itor)
Instancia un iterador a partir del estado del iterador itor.
Definition: tpl_dynSetTree.H:855
DynSetTree & join(DynSetTree &t, DynSetTree &&dup=DynSetTree())
Definition: tpl_dynSetTree.H:665
bool has_current() const
Definition: tpl_dynSetTree.H:962
Key * insert(const Key &key)
Definition: tpl_dynSetTree.H:177
Definition: tpl_dynSetTree.H:1152