9 # include <ah_stdc++_utils.H>
10 # include <tpl_dnode.H>
11 # include <tpl_treapRk.H>
12 # include <tpl_nodePool.H>
40 template <
class,
class>
class Tree =
Treap_Rk
47 mutable size_t num_reps;
49 Node_Data() : num_reps(0) { }
51 Node_Data(
const T & k) : key(k), num_reps(1) { }
56 bool operator () (
const Node_Data & op1,
const Node_Data & op2)
const
58 return Compare () (op1.key, op2.key);
81 typedef Tree<Node_Data, Cmp_Data> Tree_Type;
83 typedef typename Tree_Type::Node Node;
85 typedef typename Tree_Type::Iterator Tree_Iterator;
87 mutable Tree_Type tree;
89 mutable size_t num_elem;
93 static T & get_key(Node * p) {
return KEY(p).key; }
95 static size_t & get_num_reps(Node * p) {
return KEY(p).num_reps; }
107 friend class multiset<T, Compare, Tree>;
109 typedef typename Tree_Type::Iterator Iterator;
113 mutable Iterator tree_it;
115 mutable int pos_in_node;
121 : multiset_ptr(mset_ptr), tree_it(mset_ptr->tree, curr_tree_node),
122 pos_in_node(pos), overflow (
false), underflow (
false)
129 if (tree_it.has_curr ())
131 underflow = overflow =
false;
135 underflow = overflow =
true;
139 : multiset_ptr(const_cast<multiset*>(&ms)), tree_it(ms.tree)
144 Node * get_curr_node() {
return tree_it.get_curr(); }
146 bool has_curr()
const
148 return tree_it.has_curr();
151 Node_Data & get_data() {
return KEY(get_curr_node()); }
153 T & get_key() {
return multiset::get_key(get_curr_node()); }
155 size_t & get_num_reps() {
return multiset::get_num_reps(get_curr_node()); }
176 : multiset_ptr(mset_ptr), tree_it(multiset_ptr->tree)
182 : multiset_ptr(it.multiset_ptr), tree_it(it.tree_it),
183 pos_in_node(it.pos_in_node),
184 overflow(it.overflow), underflow(it.underflow)
192 underflow = overflow =
true;
212 tree_it.reset_first ();
213 if (tree_it.has_curr ())
227 tree_it.reset_last ();
228 if (tree_it.has_curr ())
231 pos_in_node = get_num_reps() - 1;
242 tree_it.reset_last();
243 if (tree_it.has_curr())
262 bool is_at_end()
const
264 return not tree_it.has_curr();
281 if (++pos_in_node == get_num_reps())
285 if (tree_it.has_curr ())
291 I(*
this == compute_end());
304 if (pos_in_node-- == 0)
308 if (tree_it.has_curr ())
309 pos_in_node = get_num_reps();
317 Node * tree_node = get_curr_node();
318 size_t & num_reps = multiset::get_num_reps(tree_node);
322 --multiset_ptr->num_elem;
327 multiset_ptr->pool.deallocate(tree_node);
329 if (tree_it.has_curr ())
377 for (
int i = 0; i < n; ++i)
387 for (
int i = 0; i < n; ++i)
396 if (this->has_curr() and it.has_curr())
397 return pos_in_node == it.pos_in_node;
399 if (this->is_at_end() and it.is_at_end())
401 I(this->overflow and it.overflow);
411 return not (*
this == itor);
414 bool verify (
const multiset & _multiset)
const
416 return tree_it.verify ( (Tree_Type*)& (_multiset.tree));
419 bool verify (
const iterator & it)
const
421 return tree_it.verify (it.tree_it);
441 tree.getRoot () =
copyRec(c.tree.getRoot());
467 template <
typename Itor>
491 num_elem = c.num_elem;
502 return COUNT(tree.getRoot ()) == 0;
515 Tree_Iterator itor1 (tree), itor2 (c.tree);
517 while (itor1.has_curr() and itor2.has_curr())
519 Node * p1 = itor1.get_curr();
520 Node * p2 = itor2.get_curr();
522 if (no_equals <Node_Data, Cmp_Data> (
KEY(p1),
KEY(p2)))
524 else if (get_num_reps(p1) != get_num_reps(p2))
531 return not itor1.has_curr() and not itor2.has_curr();
538 return not (*
this == c);
550 while (itor1.has_curr() and itor2.has_curr())
552 if (Cmp_Data () (itor1.get_data(), itor2.get_data()))
554 else if (Cmp_Data () (itor2.get_data(), itor1.get_data()))
561 if (itor1.has_curr())
564 return itor2.has_curr();
571 return not (*
this == c or *
this < c);
578 return not (*
this > c );
585 return not (*
this < c);
597 Node * p = tree.search (Node_Data(value));
602 return get_num_reps(p);
624 Node * node = tree.search(Node_Data(value));
629 return iterator(const_cast<multiset*>(
this), node);
650 throw std::underflow_error (
"multiset is empty");
652 Node * tree_node = tree.search(Node_Data(value));
654 if (tree_node == NULL)
678 throw std::underflow_error(
"multiset is empty");
680 Node * tree_node = tree.search(Node_Data(value));
682 if (tree_node == NULL)
686 it.list_it.reset_last ();
695 std::swap (tree.getRoot (), c.tree.getRoot ());
696 std::swap (num_elem, c.num_elem);
710 return iterator(*this).compute_end();
724 Node * p = pool.allocate(Node_Data(value));
725 Node * ptr = tree.search_or_insert(p);
732 return iterator(
this, ptr, get_num_reps(ptr)++);
762 Aleph::verify_container_and_iterator (
this, pos);
764 if (pos != pos.compute_end())
766 Node * p = pos.get_curr_node();
768 if (are_equals <Node_Data, Cmp_Data> (
KEY(p), Node_Data(value)))
797 template <
typename Itor>
800 Aleph::verify_iterators (beg, end);
816 Node * tree_node = tree.remove(Node_Data(value));
818 if (tree_node == NULL)
821 const size_t ret_val = get_num_reps(tree_node);
823 pool.deallocate(tree_node);
845 Aleph::verify_container_and_iterator (*
this, pos);
847 Node * tree_node = pos.get_curr_node();
849 size_t & num_reps = get_num_reps(tree_node);
856 tree.remove(Node_Data(
KEY(tree_node)));
857 pool.deallocate(tree_node);
864 iterator delete_range (iterator beg,
const iterator &
end)
889 Aleph::verify_container_and_iterator (*
this, beg);
890 Aleph::verify_iterators (beg, end);
892 return delete_range(beg, end);
898 destroyRec (static_cast<Node *> (tree.getRoot ()));
904 template <
typename T>
924 # endif // AH_MULTISET_H
Definition: Multiset.H:103
multiset< T >::value_type * pointer
Tipo apuntador a elemento dentro del multi-conjunto.
Definition: Multiset.H:167
iterator find(const T &value) const
Definition: Multiset.H:622
Definition: tpl_treapRk.H:902
Definition: tpl_nodePool.H:19
size_type size() const
Retorna la cantidad de elementos que contiene el multi-conjunto.
Definition: Multiset.H:497
multiset::iterator const_iterator
Tipo iterador constante.
Definition: Multiset.H:426
size_type count(const T &value) const
Definition: Multiset.H:595
Definition: ahFunction.H:145
size_t size_type
Tipo numérico para los tamaños (natural).
Definition: Multiset.H:74
iterator begin() const
Definition: Multiset.H:701
iterator operator+=(size_type n)
Definition: Multiset.H:375
multiset()
Constructor vacío de un multi-conjunto.
Definition: Multiset.H:435
iterator insert(iterator pos, const T &value)
Definition: Multiset.H:760
multiset::value_type & reference
Tipo genérico de referencia a elemento del conjunto.
Definition: Multiset.H:68
void swap(multiset &c)
Definition: Multiset.H:693
#define COUNT(p)
Definition: tpl_binNodeXt.H:34
void erase(iterator pos)
Definition: Multiset.H:843
multiset(const multiset &c)
Instancia una copia del multi-conjunto c.
Definition: Multiset.H:447
multiset & operator=(const multiset &c)
Asigna todo el contenido de c a this.
Definition: Multiset.H:482
multiset< T >::value_type value_type
El tipo de dato que contiene el multi-conjunto.
Definition: Multiset.H:160
bool operator==(const multiset &c) const
Definition: Multiset.H:507
Definition: ahIterator.H:8
T key_type
El tipo dato de los elementos del conjunto.
Definition: Multiset.H:77
~multiset()
Definition: Multiset.H:476
const T & operator*()
Proporciona una referencia al elemento actual.
Definition: Multiset.H:197
const T * operator->()
"Dereferencia" un puntero al elemento actual.
Definition: Multiset.H:203
iterator upper_bound(const T &value) const
Definition: Multiset.H:675
bool operator!=(const multiset &c) const
Definition: Multiset.H:536
Definition: Multiset.H:42
iterator erase(iterator beg, const iterator &end)
Definition: Multiset.H:887
bool operator==(const iterator &it) const
Retorna true si los iteradores tienen el mismo estado.
Definition: Multiset.H:394
void insert(Itor beg, const Itor &end)
Definition: Multiset.H:798
void destroyRec(Node *&root)
Definition: tpl_binNodeUtils.H:798
bool operator>=(const multiset &c) const
Definition: Multiset.H:583
bool operator<(const multiset &c) const
Definition: Multiset.H:543
T value_type
El tipo dato de los elementos del conjunto.
Definition: Multiset.H:65
iterator end() const
Definition: Multiset.H:708
bool empty() const
Retorna true si el contenedor esta vacío.
Definition: Multiset.H:500
Node * copyRec(Node *src_root)
Definition: tpl_binNodeUtils.H:821
multiset::iterator const_reverse_iterator
Tipo iterador invertido constante.
Definition: Multiset.H:432
iterator operator++()
Definition: Multiset.H:343
void clear()
Borra todos los elementos del multi-conjunto.
Definition: Multiset.H:896
iterator lower_bound(const T &value) const
Definition: Multiset.H:647
iterator operator-=(size_type n)
Definition: Multiset.H:385
iterator insert(const T &value)
Definition: Multiset.H:722
bool operator>(const multiset &c) const
Definition: Multiset.H:569
multiset< T >::size_type difference_type
Definition: Multiset.H:164
#define KEY(p)
Definition: tpl_binNode.H:206
bool operator!=(const iterator &itor) const
Retorna true si los iteradores tienen estados distintos.
Definition: Multiset.H:409
multiset::iterator reverse_iterator
Tipo iterador invertido.
Definition: Multiset.H:429
bool operator<=(const multiset &c) const
Definition: Multiset.H:576
iterator()
Constructor vacío; no tiene validez si no se asocia un conjunto.
Definition: Multiset.H:190
multiset(Itor beg, const Itor &end)
Definition: Multiset.H:468
multiset< T >::reference reference
Tipo referencia a elemento dentro del multi-conjunto.
Definition: Multiset.H:170
const multiset::value_type & const_reference
Tipo genérico de referencia constante a elemento del conjunto.
Definition: Multiset.H:71
size_type erase(const T &value)
Definition: Multiset.H:814
const multiset< T >::reference const_reference
Tipo referencia a elemento dentro del multi-conjunto.
Definition: Multiset.H:173
iterator operator--()
Definition: Multiset.H:359