Aleph-w  1.9
General library for algorithms and data structures
tpl_dynMapTree.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef TPL_DYNMAPTREE_H
28 # define TPL_DYNMAPTREE_H
29 
30 # include <tpl_dynSetTree.H>
31 
32 using namespace Aleph;
33 
34 namespace Aleph {
35 
58 template <
59  typename Key, typename Data,
60  template <typename, class> class Tree = Avl_Tree,
61  class Compare = Aleph::less<Key>>
62 class DynMapTree :
63  public DynSetTree<std::pair<Key, Data>, Tree,
64  Dft_Pair_Cmp<Key, Data, Compare>>
65 {
66  using Pair = std::pair<Key, Data>;
67 
68  using Base =
69  DynSetTree<std::pair<Key, Data>, Tree, Dft_Pair_Cmp<Key, Data, Compare>>;
70 
71 public:
72 
73  using Base::Base;
74 
75  DynMapTree(const DynList<Key> & keys)
76  {
77  keys.for_each([this] (auto & k) { this->insert(make_pair(k, Data())); });
78  }
79 
80  DynMapTree() {}
81 
82  using Key_Type = Key;
83 
84  using Item_Type = Key;
85 
86  using Value_Type = Data ;
87 
88  // using Base::Base; // no more need. But I don't remember why I put it
89  using Base::insert;
90 
91  static Data & get_data(const Key & key) noexcept
92  {
93  return key_to_pair<Key, Data>(&const_cast<Key&>(key))->second;
94  }
95 
96  static const Key & get_key(Data * data_ptr) noexcept
97  {
98  return data_to_pair<Key, Data>(data_ptr)->first;
99  }
100 
112  Pair * insert(const Key & key, const Data & data)
113  {
114  return this->Base::insert(Pair(key, data));
115  }
116 
117  Pair * insert(const Key & key, Data && data = Data())
118  {
119  return this->Base::insert(Pair(key, std::forward<Data>(data)));
120  }
121 
122  Pair * insert(Key && key, const Data & data)
123  {
124  return this->Base::insert(Pair(std::forward<Key>(key), data));
125  }
126 
127  Pair * insert(Key && key, Data && data = Data())
128  {
129  return this->Base::insert(Pair(std::forward<Key>(key),
130  std::forward<Data>(data)));
131  }
132 
133  Pair * append(const Key & key, const Data & data)
134  {
135  return this->Base::insert(Pair(key, data));
136  }
137 
138  Pair * append(const Key & key, Data && data = Data())
139  {
140  return this->Base::insert(Pair(key, std::forward<Data>(data)));
141  }
142 
143  Pair * append(Key && key, const Data & data)
144  {
145  return this->Base::insert(Pair(std::forward<Key>(key), data));
146  }
147 
148  Pair * append(Key && key, Data && data = Data())
149  {
150  return this->Base::insert(Pair(std::forward<Key>(key),
151  std::forward<Data>(data)));
152  }
153 
165  Pair * put(const Key & key, const Data & data)
166  {
167  return insert(key, data);
168  }
169 
170  Pair * put(const Key & key, Data && data)
171  {
172  return insert(key, std::forward<Data>(data));
173  }
174 
175  Pair * put(Key && key, const Data & data)
176  {
177  return insert(std::forward<Key>(key), data);
178  }
179 
180  Pair * put(Key && key, Data && data)
181  {
182  return insert(std::forward<Key>(key), std::forward<Data>(data));
183  }
184 
193  Data remove(const Key & key)
194  {
195  Pair p(key, Data());
196  return this->del(p).second;
197  }
198 
199  Data remove(Key && key)
200  {
201  return this->del(forward<Key>(key), Data()).second;
202  }
203 
214  Pair * search(const Key & key) const noexcept
215  {
216  return this->Base::search(Pair(key, Data()));
217  }
218 
219  Pair * search(Key && key) const noexcept
220  {
221  return this->Base::search(Pair(move(key), Data()));
222  }
223 
224  bool has(const Key & key) const noexcept { return search(key) != nullptr; }
225 
226  bool has(Key && key) const noexcept { return search(move(key)) != nullptr; }
227 
228  bool contains(const Key & key) const noexcept { return has(key); }
229 
230  bool contains(Key && key) const noexcept { return has(move(key)); }
231 
242  Data & find(const Key & key)
243  {
244  return this->Base::find(Pair(key, Data())).second;
245  }
246 
247  const Data & find(const Key & key) const
248  {
249  return this->Base::find(Pair(key, Data())).second;
250  }
251 
252  Data & operator [] (const Key & key)
253  {
254  return this->search_or_insert(Pair(key, Data()))->second;
255  }
256 
257  const Data & operator [] (const Key & key) const
258  {
259  return this->find(key);
260  }
261 
262  Data & operator [] (Key && key)
263  {
264  return this->search_or_insert(Pair(move(key), Data()))->second;
265  }
266 
267  const Data & operator [] (Key && key) const
268  {
269  return this->find(move(key));
270  }
271 
272  using Iterator = typename Base::Iterator;
273 
274  DynList<Key> keys() const
275  {
276  return this->template maps<Key>([] (auto p) { return p.first; });
277  }
278 
279  DynList<Data> values() const
280  {
281  return this->template maps<Data>([] (auto p) { return p.second; });
282  }
283 
284  DynList<Data*> values_ptr()
285  {
286  DynList<Data*> ret;
287  for (Iterator it(*this); it.has_curr(); it.next_ne())
288  ret.append(&it.get_curr_ne().second);
289  return ret;
290  }
291 
292  DynList<Pair*> items_ptr()
293  {
294  DynList<Pair*> ret;
295  for (Iterator it(*this); it.has_curr(); it.next_ne())
296  ret.append(&it.get_curr_ne());
297  return ret;
298  }
299 };
300 } // end namespace Aleph
301 
302 
303 # include <tpl_binTree.H>
304 # include <tpl_avl.H>
305 # include <tpl_rb_tree.H>
306 # include <tpl_rand_tree.H>
307 # include <tpl_treap.H>
308 # include <tpl_treapRk.H>
309 # include <tpl_splay_tree.H>
310 
311 namespace Aleph {
318  template <typename Key, typename Type, class Compare = Aleph::less<Key> >
320 
327  template <typename Key, typename Type, class Compare = Aleph::less<Key> >
335  template <typename Key, typename Type, class Compare = Aleph::less<Key> >
337  { /* empty */ };
338 
346  template <typename Key, typename Type, class Compare = Aleph::less<Key> >
348  { /* empty */ };
349 
356  template <typename Key, typename Type, class Compare = Aleph::less<Key> >
358  { /* empty */ };
359 
367  template <typename Key, typename Type, class Compare = Aleph::less<Key> >
369  { /* empty */ };
370 
377  template <typename Key, typename Type, class Compare = Aleph::less<Key> >
379  { /* empty */ };
380 
381  template <typename T, class Op, class C>
382 DynMapTree<typename C::Item_Type, T> map_unify(const C & c, Op op)
383 {
385  for (auto it = c.get_it(); it.has_curr(); it.next_ne())
386  {
387  const auto & curr = it.get_curr_ne();
388  ret.insert(curr, op(curr));
389  }
390  return ret;
391 }
392 
393 } // end namespace Aleph
394 
395 # endif /* TPL_DYNMAPTREE_H */
396 
Pair * insert(const Key &key, const Data &data)
Definition: tpl_dynMapTree.H:112
Definition: tpl_dynMapTree.H:328
void for_each(Operation &operation) noexcept(noexcept(operation))
Definition: htlist.H:589
Definition: tpl_dynMapTree.H:62
Pair * put(const Key &key, const Data &data)
Definition: tpl_dynMapTree.H:165
Definition: tpl_dynMapTree.H:368
Definition: tpl_dynMapTree.H:347
Definition: tpl_dynMapTree.H:319
Definition: ah-comb.H:35
Definition: tpl_dynMapTree.H:336
Pair * search(const Key &key) const noexcept
Definition: tpl_dynMapTree.H:214
Definition: tpl_dynMapTree.H:378
Definition: tpl_dynMapTree.H:357
Definition: tpl_dynSetTree.H:61
std::pair< Key, Data > & find(const std::pair< Key, Data > &key) const
Definition: tpl_dynSetTree.H:414
Definition: ahDry.H:41
std::pair< Key, Data > * search_or_insert(const std::pair< Key, Data > &key)
Definition: tpl_dynSetTree.H:254
T & append(const T &item)
Definition: htlist.H:1471
std::pair< Key, Data > del(const std::pair< Key, Data > &key)
Definition: tpl_dynSetTree.H:349
std::pair< Key, Data > * insert(const std::pair< Key, Data > &key)
Definition: tpl_dynSetTree.H:195
std::pair< Key, Data > * search(const std::pair< Key, Data > &key) const
Definition: tpl_dynSetTree.H:462
Data & find(const Key &key)
Definition: tpl_dynMapTree.H:242

Leandro Rabindranath León