6 # ifndef ALEPH_MULTIMAP_H
7 # define ALEPH_MULTIMAP_H
9 # include <ah_stdc++_utils.H>
10 # include <tpl_treapRk.H>
11 # include <tpl_nodePool.H>
74 template <
typename Key,
77 template <
class,
class>
class KTree =
Treap_Rk,
78 template <
class,
class>
class TTree =
Treap_Rk
89 Tdata() : num_reps(0) { }
91 Tdata(
const T & e) : elem(e), num_reps(0) { }
93 Tdata(
const Tdata & item) : elem(item.elem), num_reps(item.num_reps)
103 bool operator () (
const Tdata & op1,
const Tdata & op2)
const
105 return Tless () (op1.elem, op2.elem);
110 typedef TTree<Tdata, Cmpt> T_Tree;
112 typedef typename T_Tree::Node Tnode;
123 Kdata() : num_reps(0) { }
125 Kdata(
const Kdata & item) : key(item.key), num_reps(0)
127 t_tree.getRoot() =
copyRec(const_cast<Kdata&>(item).t_tree.getRoot());
128 num_reps = item.num_reps;
143 t_tree.getRoot() =
copyRec(const_cast<Kdata&>(item).t_tree.getRoot());
146 num_reps = item.num_reps;
154 bool operator () (
const Kdata & op1,
const Kdata & op2)
const
156 return Compare () (op1.key, op2.key);
160 typedef KTree<Kdata, Cmpk> K_Tree;
162 typedef typename K_Tree::Node Knode;
179 Kdata searching_kdata;
183 Kdata & key_kdata(
const Key & key)
185 searching_kdata.key = key;
186 return searching_kdata;
191 multimap() : kpool(100), tpool(100), num_elem(0) { }
205 typedef std::pair<Key, T> Pair;
215 typedef size_t size_type;
217 typedef Key key_type;
219 typedef T mapped_type;
223 const size_t &
size ()
const {
return num_elem; }
229 int sizek =
sizeof(Knode);
230 int sizet =
sizeof(Tnode);
232 double total_mem = pow(2, __WORDSIZE);
234 size_type ret_val = floor(total_mem / (sizek + sizet));
241 bool empty ()
const {
return k_tree.is_empty(); }
245 typedef typename K_Tree::Iterator K_Itor;
246 typedef typename T_Tree::Iterator T_Itor;
256 friend class multimap<Key, T, Compare, KTree, TTree>;
259 mutable K_Tree * k_tree_ptr;
261 mutable T_Tree * t_tree_ptr;
263 mutable int pos_in_k;
264 mutable int pos_in_t;
271 I(k_tree_ptr != NULL);
275 I(
KEY(k_it.get_curr()).t_tree.size() > 0);
276 underflow = overflow =
false;
277 pos_in_k = pos_in_t = 0;
278 t_tree_ptr = &
KEY(k_it.get_curr()).t_tree;
279 t_it = T_Itor(*t_tree_ptr);
286 : multimap_ptr(m), k_tree_ptr(&m->k_tree), k_it(*k_tree_ptr, kp),
287 t_tree_ptr(&
KEY(kp).t_tree), t_it(*t_tree_ptr, tp),
288 pos_in_k(kpos), pos_in_t(tpos), underflow(
false), overflow(
false)
290 I(not
KEY(kp).t_tree.is_empty());
291 I(
KEY(tp).num_reps > 0 and tpos <
KEY(tp).num_reps);
295 : multimap_ptr(m), k_tree_ptr(&m->k_tree), k_it(*k_tree_ptr, p),
296 t_tree_ptr(&
KEY(p).t_tree), t_it(*t_tree_ptr),
297 pos_in_t(0), underflow(
false), overflow(
false)
299 I(
KEY(p).num_reps > 0);
300 I(not
KEY(p).t_tree.is_empty());
321 : multimap_ptr(const_cast<
multimap*>(&mm)),
322 k_tree_ptr(&multimap_ptr->k_tree), k_it(*k_tree_ptr)
329 : multimap_ptr(it.multimap_ptr), k_tree_ptr(it.k_tree_ptr), k_it(it.k_it),
330 t_tree_ptr(it.t_tree_ptr), t_it(it.t_it),
331 pos_in_k(it.pos_in_k), pos_in_t(it.pos_in_t),
332 underflow(it.underflow), overflow(it.overflow)
339 : multimap_ptr(NULL), k_tree_ptr(NULL), t_tree_ptr(NULL),
340 pos_in_k(-1), pos_in_t(-1), underflow(true), overflow(true)
351 multimap_ptr = it.multimap_ptr;
352 k_tree_ptr = it.k_tree_ptr;
354 t_tree_ptr = it.t_tree_ptr;
356 pos_in_k = it.pos_in_k;
357 pos_in_t = it.pos_in_t;
358 underflow = it.underflow;
359 overflow = it.overflow;
366 bool has_curr()
const {
return k_it.has_curr(); }
368 Knode * get_curr() {
return k_it.get_curr(); }
370 Kdata & get_curr_kdata() {
return KEY(get_curr()); }
377 return value_type(get_curr_kdata().key,
KEY(t_it.get_curr()).elem);
393 ret_pair.first = get_curr_kdata().key;
394 ret_pair.second =
KEY(t_it.get_curr()).elem;
410 t_tree_ptr = &
KEY(get_curr()).t_tree;
411 t_it = T_Itor(*t_tree_ptr);
412 pos_in_k = pos_in_t = 0;
425 Kdata & kdata =
KEY(get_curr());
426 t_tree_ptr = &kdata.t_tree;
427 t_it = T_Itor(*t_tree_ptr);
429 pos_in_k = kdata.num_reps - 1;
430 pos_in_t =
KEY(t_it.get_curr()).num_reps - 1;
455 bool is_at_end()
const {
return not has_curr(); }
463 void put_in_overflow()
467 if (k_tree_ptr->is_empty())
473 void put_in_underflow()
491 t_tree_ptr = &
KEY(get_curr()).t_tree;
492 t_it = T_Itor(*t_tree_ptr);
496 void forward_tree_iterators()
518 I(t_tree_ptr == NULL);
519 throw std::overflow_error(
"Multimap::iterator is already "
523 I(t_it.has_curr() and t_tree_ptr != NULL);
525 Tdata & tdata =
KEY(t_it.get_curr());
526 if (++pos_in_t < tdata.num_reps)
529 forward_tree_iterators();
541 t_tree_ptr = &
KEY(get_curr()).t_tree;
542 t_it = T_Itor(*t_tree_ptr);
544 pos_in_t =
KEY(t_it.get_curr()).num_reps - 1;
547 void backward_tree_iterators()
552 pos_in_t =
KEY(t_it.get_curr()).num_reps - 1;
569 I(t_tree_ptr == NULL);
570 throw std::underflow_error(
"Multimap::iterator is "
571 "already in underflow");
574 I(t_it.has_curr() and t_tree_ptr != NULL);
579 backward_tree_iterators();
584 Kdata & kdata = get_curr_kdata();
585 Tnode * tp = t_it.get_curr();
586 Tdata & tdata =
KEY(tp);
588 --multimap_ptr->num_elem;
590 if (--tdata.num_reps == 0)
595 else if (pos_in_t == tdata.num_reps)
603 I(kdata.num_reps > 0);
607 if (kdata.num_reps == 0)
609 Knode * kp = k_it.del();
610 I(
KEY(kp).t_tree.is_empty());
616 if (not k_it.has_curr())
622 t_tree_ptr = &
KEY(get_curr()).t_tree;
623 t_it = T_Itor(*t_tree_ptr);
624 pos_in_k = pos_in_t = 0;
632 if (this->has_curr() and it.has_curr())
633 return (t_it.get_curr() == it.t_it.get_curr() and
634 pos_in_t == it.pos_in_t);
636 if (this->is_at_end() and it.is_at_end())
638 I(this->overflow and it.overflow);
648 return not (*
this == itor);
694 I(
KEY(t_it.get_curr()).num_reps > 0);
696 const size_t treps =
KEY(t_it.get_curr()).num_reps;
697 const int remain_in_t_node = treps - pos_in_t;
698 if (n < remain_in_t_node)
702 I(pos_in_t <
KEY(t_it.get_curr()).num_reps);
707 n -= remain_in_t_node;
714 I(pos_in_k ==
KEY(k_it.get_curr()).num_reps);
722 throw std::overflow_error(
"Multimap::iterator is "
723 "already in overflow");
726 I(
KEY(get_curr()).num_reps > 0);
728 const int remain_in_k_node =
KEY(get_curr()).num_reps;
729 if (n < remain_in_k_node)
731 t_tree_ptr = &
KEY(get_curr()).t_tree;
732 t_it = T_Itor(*t_tree_ptr);
733 pos_in_k = pos_in_t = 0;
737 n -= remain_in_k_node;
754 Knode * kp = kpool.
allocate(key_kdata(value.first));
755 Knode * kq = k_tree.search_or_insert(kp);
760 I(
KEY(kq).key == value.first);
762 T_Tree & t_tree =
KEY(kq).t_tree;
763 const Tdata tdata(value.second);
768 Tnode * tq = t_tree.search_or_insert(tp);
789 template <
class InputIterator>
790 void insert(InputIterator first,
const InputIterator & last)
792 while (first != last)
797 template <
class InputIterator>
798 multimap(InputIterator first,
const InputIterator & last)
801 this->
insert(first, last);
808 k_tree.getRoot() =
copyRec(const_cast<multimap&>(m).k_tree.getRoot());
809 num_elem = m.num_elem;
822 k_tree.getRoot() =
copyRec(const_cast<multimap&>(m).k_tree.getRoot());
823 num_elem = m.num_elem;
849 Knode * kp =
const_cast<iterator&
>(hint).get_curr();
850 Kdata & kdata =
KEY(kp);
853 if (Aleph::are_equals <Key, Compare> (kdata.key, value.first))
856 Tnode * tq = hint.t_it.get_curr();
857 const Tdata & tdata =
KEY(tq);
859 if (Aleph::no_equals <T, Tless> (tdata.elem, value.second))
862 tq = kdata.t_tree.search_or_insert(tp);
890 size_type
erase(
const key_type & key)
892 Knode * p = k_tree.remove(key_kdata(key));
896 size_type ret_val =
KEY(p).num_reps;
933 size_type
count(
const Key & key)
const
935 Kdata & kdata =
const_cast<multimap*
>(
this)->key_kdata(key);
936 Knode * p =
const_cast<multimap*
>(
this)->k_tree.search(kdata);
940 return KEY(p).num_reps;
947 Knode * p = k_tree.search(key_kdata(key));
963 if (k_tree.is_empty())
966 std::pair<int, Knode*> p = k_tree.find_position(key_kdata(key));
967 Knode * kp = p.second;
971 if (Aleph::are_equals <Key, Compare> (
KEY(kp).key, key))
974 if (p.first == k_tree.size())
980 if (Compare () (ret->first, key))
996 if (k_tree.is_empty())
999 std::pair<int, Knode*> p = k_tree.find_position(key_kdata(key));
1000 Knode * kp = p.second;
1004 if (Aleph::are_equals <Key, Compare> (
KEY(kp).key, key))
1007 if (p.first == k_tree.size())
1013 if (Compare () (ret->first, key))
1029 Knode * p = k_tree.search(key_kdata(key));
1033 return std::pair<iterator,iterator>(e, e);
1038 last +=
KEY(p).num_reps;
1040 return std::pair<iterator,iterator>(first, last);
1044 std::pair<const iterator,const iterator>
equal_range(
const Key & key)
const
1052 k_tree.swap(other.k_tree);
1053 std::swap(num_elem, other.num_elem);
1063 K_Itor kit1(const_cast<multimap*>(
this)->k_tree);
1064 K_Itor kit2(const_cast<multimap&>(rhs).k_tree);
1065 for (; kit1.has_curr() and kit2.has_curr(); kit1.next(), kit2.next())
1067 Kdata & kdata1 =
KEY(kit1.get_curr());
1068 Kdata & kdata2 =
KEY(kit2.get_curr());
1070 const size_t & n1 = kdata1.num_reps;
1071 const size_t & n2 = kdata2.num_reps;
1076 const Key & key1 = kdata1.key;
1077 const Key & key2 = kdata2.key;
1078 if (Compare () (key1, key2))
1080 else if (Compare () (key2, key1))
1084 if (kit1.has_curr() or kit2.has_curr())
1085 return kit2.has_curr();
1097 K_Itor kit1(const_cast<multimap*>(
this)->k_tree);
1098 K_Itor kit2(const_cast<multimap&>(rhs).k_tree);
1099 for (; kit1.has_curr() and kit2.has_curr(); kit1.next(), kit2.next())
1101 Kdata & kdata1 =
KEY(kit1.get_curr());
1102 Kdata & kdata2 =
KEY(kit2.get_curr());
1104 const size_t & n1 = kdata1.num_reps;
1105 const size_t & n2 = kdata2.num_reps;
1110 const Key & key1 = kdata1.key;
1111 const Key & key2 = kdata2.key;
1112 if (Compare () (key1, key2))
1114 else if (Compare () (key2, key1))
1118 if (kit1.has_curr() or kit2.has_curr())
1119 return kit2.has_curr();
1130 K_Itor kit1(const_cast<multimap*>(
this)->k_tree);
1131 K_Itor kit2(const_cast<multimap&>(rhs).k_tree);
1132 for (; kit1.has_curr() and kit2.has_curr(); kit1.next(), kit2.next())
1134 Kdata & kdata1 =
KEY(kit1.get_curr());
1135 Kdata & kdata2 =
KEY(kit2.get_curr());
1137 if (kdata1.num_reps != kdata2.num_reps)
1140 const Key & key1 = kdata1.key;
1141 const Key & key2 = kdata2.key;
1142 if (Compare () (key1, key2))
1144 else if (Compare () (key2, key1))
1148 if (kit1.has_curr() or kit2.has_curr())
1157 return not (*
this == rhs);
1177 return rhs <= *
this;
1203 Exception_Prototypes (std::domain_error)
1207 throw std::domain_error(
"key not found on constant multimap");
1220 # endif // ALEPH_MULTIMAP_H
iterator erase(const_iterator &position)
Elimina del multimapeo el elemento situado en position.
Definition: Multimap.H:879
iterator operator+=(size_type n)
Definition: Multimap.H:687
iterator insert(const value_type &value)
Definition: Multimap.H:752
iterator find(const Key &key)
Definition: Multimap.H:945
iterator erase(iterator first, const_iterator &last)
Definition: Multimap.H:917
std::pair< iterator, iterator > equal_range(const Key &key)
Definition: Multimap.H:1027
Definition: tpl_treapRk.H:902
bool operator<(const multimap &rhs) const
Definition: Multimap.H:1061
const size_t & size() const
Definition: Multimap.H:223
Definition: ahFunction.H:145
void clear()
Vacía el multimapeo. Todos los elementos son borrados.
Definition: Multimap.H:199
const iterator const_iterator
iterador constante
Definition: Multimap.H:744
Pair value_type
Tipo de valor de clave manejado (que es un par)
Definition: Multimap.H:208
iterator()
Constructor vacío; uso indefinido.
Definition: Multimap.H:338
bool operator>=(const multimap &rhs) const
Definition: Multimap.H:1175
iterator operator++()
Definition: Multimap.H:653
bool operator==(const multimap &rhs) const
Retorna true si this es exactamente igual a rhs.
Definition: Multimap.H:1125
multimap::reference reference
Tipo referencia a elemento dentro del multi-mapeo.
Definition: Multimap.H:316
size_type count(const Key &key) const
retorna la cantidad pares con clave igual a key
Definition: Multimap.H:933
iterator upper_bound(const Key &key)
Definition: Multimap.H:994
const T & operator[](const Key &key)
Definition: Multimap.H:1193
iterator lower_bound(const Key &key)
Retorna un iterador posicionado en el lugar donde sería insertado key.
Definition: Multimap.H:961
iterator(const multimap &mm)
Definition: Multimap.H:320
iterator insert(const_iterator &hint, const value_type &value)
Definition: Multimap.H:845
const_iterator lower_bound(const Key &key) const
Definition: Multimap.H:987
Definition: Multimap.H:254
void insert(InputIterator first, const InputIterator &last)
Definition: Multimap.H:790
multimap(InputIterator first, const InputIterator &last)
Construye un multimapeo con los pares del rango [first,last).
Definition: Multimap.H:798
multimap::value_type * pointer
Tipo apuntador a elemento dentro del multi-mapeo.
Definition: Multimap.H:313
bool operator>(const multimap &rhs) const
Definition: Multimap.H:1165
void deallocate(Node *p)
Definition: tpl_nodePool.H:76
bool operator!=(const multimap &rhs) const
Retorna true si this es distinto de rhs.
Definition: Multimap.H:1155
std::pair< const iterator, const iterator > equal_range(const Key &key) const
Definition: Multimap.H:1044
bool operator==(const iterator &it) const
Retorna true si los iteradores tienen el mismo estado.
Definition: Multimap.H:630
void destroyRec(Node *&root)
Definition: tpl_binNodeUtils.H:798
size_type max_size() const
Definition: Multimap.H:227
iterator end() const
retorna un iterador al último elemento del multimapeo.
Definition: Multimap.H:930
const_iterator upper_bound(const Key &key) const
Definition: Multimap.H:1020
iterator(const iterator &it)
Constructor copia (masivamente usado por el estándar)
Definition: Multimap.H:328
const value_type * operator->()
Definition: Multimap.H:391
Definition: Multimap.H:80
multimap & operator=(const multimap &m)
Definition: Multimap.H:814
value_type operator*()
Retorna una copia del par actual del iterador.
Definition: Multimap.H:375
bool operator<=(const multimap &rhs) const
Definition: Multimap.H:1095
Node * copyRec(Node *src_root)
Definition: tpl_binNodeUtils.H:821
iterator & operator=(const iterator &it)
Asignación.
Definition: Multimap.H:346
iterator begin() const
retorna un iterador al primer elemento del multimapeo.
Definition: Multimap.H:927
iterator operator--()
Definition: Multimap.H:670
size_type erase(const key_type &key)
Definition: Multimap.H:890
void swap(multimap &other)
Intercambia los valores de this con los de other.
Definition: Multimap.H:1050
multimap::value_type & reference
Referencia al par.
Definition: Multimap.H:211
#define KEY(p)
Definition: tpl_binNode.H:206
bool empty() const
Definition: Multimap.H:241
Node * allocate()
Definition: tpl_nodePool.H:30
const_iterator find(const Key &key) const
Definition: Multimap.H:955
bool operator!=(const iterator &itor) const
Retorna true si los iteradores tienen estados distintos.
Definition: Multimap.H:646
multimap::size_type difference_type
Definition: Multimap.H:310
multimap(const multimap &m)
Constructor copia.
Definition: Multimap.H:805
multimap::value_type value_type
El tipo de dato que contiene el multi-mapeo.
Definition: Multimap.H:306