8 # include <ah_stdc++_utils.H>
9 # include <tpl_treapRk.H>
10 # include <tpl_nodePool.H>
38 template <
class,
class>
class Tree =
Treap_Rk>
43 Tree<T, Compare> tree;
45 typedef typename Tree<T, Compare>::Node Node;
73 friend class set<T, Compare, Tree>;
75 typedef typename Tree<T, Compare>::Iterator Iterator;
77 Tree<T, Compare> * tree;
82 iterator (Tree<T, Compare> & _tree, Node * node)
83 : tree (&_tree), itor (_tree, node), underflow (
false), overflow (
false)
90 if (tree->size () == 0)
91 underflow = overflow =
true;
93 underflow = overflow =
false;
96 iterator (Tree<T, Compare> & _tree) : tree (&_tree), itor (_tree)
132 if (not itor.has_current() )
146 if (not itor.has_current() )
169 iterator () : tree (NULL), underflow (true), overflow (true)
177 return KEY (itor.get_current () );
183 return &
KEY (itor.get_current () );
222 itor.reset_to_pos(itor.get_current_position () + n);
230 itor.reset_to_pos(itor.get_current_position () - n);
237 return itor == _itor.itor;
243 return not (itor == _itor.itor);
246 bool verify (
const set & _set)
const
248 return itor.verify ( (Tree<T, Compare>*) &_set.tree);
251 bool verify (
const iterator & it)
const
253 return itor.verify (it.itor);
258 set () : node_pool(100) { }
263 void * ptr = c.tree.getRoot();
264 tree.getRoot () =
copyRec ((Node*) ptr);
280 return COUNT (tree.getRoot () ) == 0;
293 typename Tree<T, Compare>::Iterator it1 (tree), it2 (c.tree);
295 while (it1.has_current () and it2.has_current () )
297 if (Aleph::no_equals<T, Compare> (
KEY(it1.get_curr()),
298 KEY(it2.get_curr())))
312 return not (*
this == c);
322 typename Tree<T, Compare>::Iterator itor1 (tree), itor2 (c.tree);
324 while (itor1.has_current () and itor2.has_current () )
326 if (Compare () (
KEY(itor1.get_curr()),
KEY(itor2.get_curr())))
328 else if (Compare () (
KEY(itor2.get_curr()),
KEY(itor1.get_curr())))
335 if (itor1.has_current () )
338 return itor2.has_current ();
345 return not (*
this == c or *
this < c);
352 return not (*
this > c );
359 return not (*
this < c);
375 if (tree.search (value) == NULL)
396 Node * node = tree.search (value);
401 iterator itor (tree);
402 itor.itor.reset_to_node (node);
417 return iterator (tree, p);
430 iterator upper(tree, p);
446 tree.getRoot () =
copyRec (c.tree.getRoot () );
455 std::swap (tree.getRoot (), c.tree.getRoot () );
461 return iterator (tree);
467 iterator last (tree);
488 std::pair<iterator, bool>
insert (
const T & value)
490 Node * p = node_pool.allocate(value);
492 iterator itor (tree);
494 if (tree.search_or_insert(p) != p)
496 node_pool.deallocate(p);
497 itor.itor.reset_to_key (value);
499 return std::pair<iterator, bool> (itor,
false);
502 itor.itor.reset_to_node(p);
504 return std::pair<iterator, bool> (itor,
true);
523 template <
typename Itor>
526 Aleph::verify_iterators (beg, end);
550 iterator
insert (
const iterator & ,
const T & value)
552 Node * p = node_pool.allocate(value);
554 iterator _itor(tree);
556 if (tree.search_or_insert(p) != p)
558 node_pool.deallocate(p);
559 _itor.itor.reset_to_key(value);
562 _itor.itor.reset_to_node(p);
584 template <
typename Itor>
587 Aleph::verify_iterators (beg, end);
606 Node * p = tree.remove (value);
611 node_pool.deallocate(p);
631 Aleph::verify_container_and_iterator (*
this, pos);
633 node_pool.deallocate(pos.itor.del());
649 iterator
erase (
const iterator & beg,
const iterator &
end)
651 Aleph::verify_container_and_iterator (*
this, beg);
652 Aleph::verify_iterators (beg, end);
654 iterator ret_val =
end;
656 const size_t pos_beg = beg.itor.get_current_position ();
657 const size_t pos_end = end.itor.get_current_position ();
659 Node * removed_tree = tree.remove(pos_beg, pos_end - 1);
686 template <
typename T>
690 Aleph::verify_iterators(it1, it2);
692 return it2.itor.get_current_position() - it1.itor.get_current_position();
set(const set &c)
Instancia una copia del conjunto c.
Definition: Set.H:261
iterator erase(const iterator &beg, const iterator &end)
Definition: Set.H:649
iterator insert(const iterator &, const T &value)
Definition: Set.H:550
iterator operator--()
Definition: Set.H:204
iterator upper_bound(const T &value)
Definition: Set.H:423
Definition: tpl_treapRk.H:902
bool operator==(const iterator &_itor) const
Retorna true si los iterador tienen el mismo estado.
Definition: Set.H:235
Definition: tpl_nodePool.H:19
bool operator!=(const set &c) const
Definition: Set.H:310
set< T >::size_type difference_type
Definition: Set.H:157
iterator find(const T &value)
Definition: Set.H:394
set< T >::reference reference
Tipo referencia a elemento dentro del conjunto.
Definition: Set.H:163
void clear()
Borra todos los elementos del conjunto.
Definition: Set.H:667
Definition: ahFunction.H:145
iterator()
Constructor vacío; no tiene validez si no se asocia un conjunto.
Definition: Set.H:169
set & operator=(set &c)
Asigna todo el contenido de c a this.
Definition: Set.H:439
bool operator>=(const set &c) const
Definition: Set.H:357
bool operator<=(const set &c) const
Definition: Set.H:350
Node * search_rank_parent(Node *root, const typename Node::key_type &key, Compare &&cmp=Compare())
Definition: tpl_binNodeUtils.H:1921
size_t size_type
Tipo numérico para los tamaños (natural).
Definition: Set.H:64
set::value_type & reference
Tipo genérico de referencia a elemento del conjunto.
Definition: Set.H:58
#define COUNT(p)
Definition: tpl_binNodeXt.H:34
iterator begin()
Retorna un iterador posicionado al primer elemento del conjunto.
Definition: Set.H:459
void swap(set &c)
Definition: Set.H:453
Definition: ahIterator.H:8
iterator lower_bound(const T &value)
Definition: Set.H:410
size_type erase(const T &value)
Definition: Set.H:604
bool empty()
Retorna true si el contenedor esta vacío.
Definition: Set.H:278
const T & operator*()
Proporciona una referencia al elemento actual.
Definition: Set.H:175
bool operator>(const set &c) const
Definition: Set.H:343
std::pair< iterator, bool > insert(const T &value)
Definition: Set.H:488
T value_type
El tipo dato de los elementos del conjunto.
Definition: Set.H:52
iterator operator-=(const size_type &n)
Definition: Set.H:228
void destroyRec(Node *&root)
Definition: tpl_binNodeUtils.H:798
bool operator==(const set &c) const
Definition: Set.H:285
const set< T >::reference const_reference
Tipo referencia a elemento dentro del conjunto.
Definition: Set.H:166
bool operator!=(const iterator &_itor) const
Retorna true si los iterador tienen estados distintos.
Definition: Set.H:241
set()
Constructor vacío.
Definition: Set.H:258
iterator operator+=(const size_type &n)
Definition: Set.H:220
set(Itor beg, const Itor &end)
Definition: Set.H:524
size_type count(const T &value)
Definition: Set.H:373
Node * copyRec(Node *src_root)
Definition: tpl_binNodeUtils.H:821
set::value_type * pointer
Tipo genérico de puntero a elemento del conjunto.
Definition: Set.H:55
void erase(iterator pos)
Definition: Set.H:629
set< T >::value_type value_type
El tipo de dato que contiene el conjunto.
Definition: Set.H:153
void insert(Itor beg, const Itor &end)
Definition: Set.H:585
iterator operator++()
Definition: Set.H:188
const set::value_type & const_reference
Tipo genérico de referencia constante a elemento del conjunto.
Definition: Set.H:61
size_type size()
Retorna la cantidad de elementos que contiene el conjunto.
Definition: Set.H:275
#define KEY(p)
Definition: tpl_binNode.H:206
const T * operator->()
"Dereferencia" un puntero al elemento actual.
Definition: Set.H:181
iterator end()
Retorna un iterador posicionado al último elemento del conjunto.
Definition: Set.H:465
bool operator<(const set &c) const
Definition: Set.H:317
set< T >::value_type * pointer
Tipo apuntador a elemento dentro del conjunto.
Definition: Set.H:160
T key_type
El tipo dato de los elementos del conjunto.
Definition: Set.H:67
~set()
Definition: Set.H:269