9 # include <ahFunction.H>
10 # include <ah_stdc++_utils.H>
11 # include <tpl_treapRk.H>
12 # include <tpl_nodePool.H>
54 template <
class,
class>
class Tree =
Treap_Rk
62 operator () (
const std::pair<Key, Elem> & __x,
63 const std::pair<Key, Elem> & __y)
const
65 return Compare () (__x.first, __y.first);
72 typedef std::pair<Key, Elem>
Pair;
84 typedef size_t size_type;
94 typedef Tree<Pair, Cmp> Tree_Type;
96 typedef typename Tree_Type::Node Node;
100 mutable Tree_Type tree;
102 Node * search_in_tree (
const Key & k)
104 return tree.search (
Pair(k, Elem()));
117 friend class map<Key, Elem>;
119 typedef typename Tree_Type::Iterator Iterator;
127 if (itor.has_current () )
128 underflow = overflow =
false;
130 underflow = overflow =
true;
164 if (not itor.has_current () )
178 if (not itor.has_current () )
182 iterator (Tree_Type & _tree, Node * node)
183 : itor (_tree, node), underflow (
false), overflow (
false)
205 iterator (Tree_Type & tree) : itor (tree)
213 return KEY(itor.get_current());
219 return &
KEY(itor.get_current());
258 itor.reset_to_pos (itor.get_current_position () + n);
266 itor.reset_to_pos (itor.get_current_position () - n);
273 return itor == _itor.itor;
279 return not (itor == _itor.itor);
282 bool verify (
const map & _map)
const
284 return itor.verify ( (Tree_Type*) &_map.tree);
287 bool verify (
const iterator & it)
const
289 return itor.verify (it.itor);
294 map () : node_pool(100) { }
299 tree.getRoot () =
copyRec (c.tree.getRoot () );
318 tree.getRoot () =
copyRec (c.tree.getRoot ());
324 size_t size ()
const {
return COUNT (tree.getRoot () ); }
339 typename Tree_Type::Iterator itor1 (tree), itor2 (c.tree);
341 while (itor1.has_current () and itor2.has_current () )
343 if (
KEY (itor1.get_current () ) !=
KEY (itor2.get_current () ) )
350 I (not itor1.has_current () and not itor2.has_current () );
359 return not *
this == c;
366 typename Tree_Type::Iterator itor1 (tree), itor2 (c.tree);
368 while (itor1.has_current () and itor2.has_current () )
370 if (
KEY (itor1.get_current () ) <
KEY (itor2.get_current () ))
372 else if (
KEY (itor2.get_current () ) <
KEY (itor1.get_current () ))
380 if (not itor1.has_current ())
390 return not (*
this == c or *
this < c);
397 return not (*
this > c );
404 return not (*
this < c);
420 if (search_in_tree (key) != NULL)
440 Node * p = search_in_tree (key);
453 search_rank_parent <Node, Cmp> (tree.getRoot(),
Pair(key, Elem()));
464 search_rank_parent <Node, Cmp> (tree.getRoot(),
Pair(key, Elem()));
468 if (
KEY (p).first == key)
513 Node * p = node_pool.allocate(key);
515 if (tree.search_or_insert(p) != p)
517 node_pool.deallocate(p);
518 return std::pair<iterator, bool> (
iterator (tree),
false);
521 return std::pair<iterator, bool> (
iterator(tree, p),
true);
539 template <
typename Itor>
586 template <
typename Itor>
606 Node * p = tree.remove (
Pair(key, Elem()));
611 node_pool.deallocate(p);
631 erase (
KEY (pos.itor.get_current ()).first);
649 Aleph::verify_iterators (beg, end);
650 Aleph::verify_container_and_iterator (*
this, beg);
654 const size_t pos_beg = beg.itor.get_current_position ();
655 const size_t pos_end = end.itor.get_current_position ();
657 Node * removed_tree = tree.remove (pos_beg, pos_end - 1);
678 Proxy (
map * m_ptr,
const Key & _key) : map_ptr (m_ptr), key (_key)
680 node = map_ptr->search_in_tree(key);
687 node = map_ptr->node_pool.allocate(std::make_pair(key, elem));
688 map_ptr->tree.insert(node);
691 KEY(node).second = elem;
701 if (proxy.node == NULL)
702 throw std::domain_error(
"key not found");;
704 if (map_ptr == proxy.map_ptr and key == proxy.key)
709 node = map_ptr->node_pool.allocate(
KEY(proxy.node));
710 map_ptr->tree.insert(node);
713 KEY(node).second =
KEY(proxy.node).second;
718 operator Elem & ()
const
721 throw std::domain_error (
"key not found");;
723 return KEY (node).second;
729 const Proxy operator [] (
const Key & key)
const
730 Exception_Prototypes (std::domain_error)
732 return Proxy (
this, key);
735 Proxy operator [] (
const Key & key)
736 Exception_Prototypes (std::domain_error)
738 return Proxy (
this, key);
747 # endif // ALEPH_MAP_H
bool empty() const
Retorna true si el contenedor esta vacío.
Definition: Map.H:327
map & operator=(const map &c)
Asigna todo el contenido de c a this.
Definition: Map.H:311
iterator find(const Key &key)
Definition: Map.H:438
iterator begin()
Retorna un iterador posicionado al primer elemento del mapeo.
Definition: Map.H:482
bool operator>(const map &c)
Definition: Map.H:388
iterator erase(const iterator &beg, const iterator &end)
Definition: Map.H:647
Definition: tpl_treapRk.H:902
void swap(map &c)
Definition: Map.H:476
Definition: tpl_nodePool.H:19
size_t size() const
Retorna la cantidad de elementos que contiene el mapeo.
Definition: Map.H:324
iterator insert(iterator, const Pair &key)
Definition: Map.H:564
iterator()
Constructor vacío; no tiene validez si no se asocia un mapeo.
Definition: Map.H:203
Definition: ahFunction.H:145
map::value_type value_type
El tipo de elemento que maneja el iterador.
Definition: Map.H:191
const Pair * operator->()
"Dereferencia" un puntero al elemento actual.
Definition: Map.H:217
Key key_type
El tipo de clave de indización.
Definition: Map.H:87
#define COUNT(p)
Definition: tpl_binNodeXt.H:34
const map::value_type & const_reference
Tipo generico referencia constante.
Definition: Map.H:82
void erase(iterator pos)
Definition: Map.H:629
map()
Constructor vacío.
Definition: Map.H:294
map(const map &c)
Instancia una copia del mapeo c.
Definition: Map.H:297
map::size_type difference_type
El tipo numérico para manejar aritmética.
Definition: Map.H:194
iterator operator-=(const size_type &n)
Definition: Map.H:264
bool operator==(const iterator &_itor) const
Retorna true si los iterador tienen el mismo estado.
Definition: Map.H:271
iterator operator+=(const size_type &n)
Definition: Map.H:256
bool operator>=(const map &c)
Definition: Map.H:402
const Pair & operator*()
Proporciona una referencia al elemento actual.
Definition: Map.H:211
map::value_type * pointer
Tipo de dato puntero a elemento actual.
Definition: Map.H:197
bool operator==(const map &c) const
Definition: Map.H:334
size_type count(const Key &key)
Definition: Map.H:418
bool operator<=(const map &c)
Definition: Map.H:395
bool operator!=(const iterator &_itor) const
Retorna true si los iterador tienen estados distintos.
Definition: Map.H:277
map(Itor beg, const Itor &end)
Definition: Map.H:540
size_type erase(const Key &key)
Definition: Map.H:604
std::pair< Key, Elem > Pair
Tipo par almacenado en mapeo.
Definition: Map.H:72
void destroyRec(Node *&root)
Definition: tpl_binNodeUtils.H:798
iterator operator--()
Definition: Map.H:240
map::value_type & reference
Definition: Map.H:79
void clear()
Borra todos los elementos del mapeo.
Definition: Map.H:665
Pair value_type
Tipo a exportar como tipo del contenedor.
Definition: Map.H:75
map::reference reference
Tipo de dato referencia a elemento actual.
Definition: Map.H:200
Node * copyRec(Node *src_root)
Definition: tpl_binNodeUtils.H:821
void insert(Itor beg, const Itor &end)
Definition: Map.H:587
std::pair< iterator, bool > insert(const Pair &key)
Definition: Map.H:511
bool operator<(const map &c) const
Definition: Map.H:364
iterator upper_bound(const Key &key)
Definition: Map.H:461
~map()
Definition: Map.H:305
#define KEY(p)
Definition: tpl_binNode.H:206
iterator lower_bound(const Key &key)
Definition: Map.H:450
iterator operator++()
Definition: Map.H:224
Elem mapped_type
El tipo de rango del mapeo.
Definition: Map.H:90
iterator end()
Retorna un iterador posicionado al último elemento del mapeo.
Definition: Map.H:488
bool operator!=(const map &c) const
Definition: Map.H:357