Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynMapTree.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
45# ifndef TPL_DYNMAPTREE_H
46# define TPL_DYNMAPTREE_H
47
48# include <type_traits>
49# include <utility>
50
51# include <tpl_dynSetTree.H>
52
53using namespace Aleph;
54
55namespace Aleph {
56
81 template <
82 typename Key, typename Data,
83 template <typename, class> class Tree = Avl_Tree,
84 class Compare = Aleph::less<Key>>
85 class DynMapTree :
86 public DynSetTree<std::pair<Key, Data>, Tree,
87 Dft_Pair_Cmp<Key, Data, Compare>>
88 {
89 using Pair = std::pair<Key, Data>;
90
91 using Base =
93
94 public:
95
96 using Base::Base;
97
99 {
100 keys.for_each([this] (auto & k) { this->insert(std::make_pair(k, Data())); });
101 }
102
103 DynMapTree() = default;
104
105 using Key_Type = Key;
106
108
109 using Value_Type = Data ;
110
111 // using Base::Base; // no more need. But I don't remember why I put it
112 using Base::insert;
113
114 static Data & get_data(Key & key) noexcept
115 {
116 return key_to_pair<Key, Data>(&key)->second;
117 }
118
119 static const Data & get_data(const Key & key) noexcept
120 {
121 return key_to_pair<Key, Data>(&key)->second;
122 }
123
124 static const Key & get_key(Data * data_ptr) noexcept
125 {
126 return data_to_pair<Key, Data>(data_ptr)->first;
127 }
128
129 static const Key & get_key(const Data * data_ptr) noexcept
130 {
131 return data_to_pair<Key, Data>(data_ptr)->first;
132 }
133
144 Pair * insert(const Key & key, const Data & data)
145 {
146 return this->Base::insert(Pair(key, data));
147 }
148
149 Pair * insert(const Key & key, Data && data = Data())
150 {
151 return this->Base::insert(Pair(key, std::forward<Data>(data)));
152 }
153
154 Pair * insert(Key && key, const Data & data)
155 {
156 return this->Base::insert(Pair(std::forward<Key>(key), data));
157 }
158
159 Pair * insert(Key && key, Data && data = Data())
160 {
161 return this->Base::insert(Pair(std::forward<Key>(key),
162 std::forward<Data>(data)));
163 }
164
165 Pair * append(const Key & key, const Data & data)
166 {
167 return this->Base::insert(Pair(key, data));
168 }
169
170 Pair * append(const Key & key, Data && data = Data())
171 {
172 return this->Base::insert(Pair(key, std::forward<Data>(data)));
173 }
174
175 Pair * append(Key && key, const Data & data)
176 {
177 return this->Base::insert(Pair(std::forward<Key>(key), data));
178 }
179
180 Pair * append(Key && key, Data && data = Data())
181 {
182 return this->Base::insert(Pair(std::forward<Key>(key),
183 std::forward<Data>(data)));
184 }
185
190 Pair * put(const Key & key, const Data & data)
191 {
192 return insert(key, data);
193 }
194
195 Pair * put(const Key & key, Data && data)
196 {
197 return insert(key, std::forward<Data>(data));
198 }
199
200 Pair * put(Key && key, const Data & data)
201 {
202 return insert(std::forward<Key>(key), data);
203 }
204
205 Pair * put(Key && key, Data && data)
206 {
207 return insert(std::forward<Key>(key), std::forward<Data>(data));
208 }
209
218 Data remove(const Key & key)
219 {
220 static_assert(std::is_default_constructible_v<Data>,
221 "DynMapTree::remove() requires Data to be default-constructible");
222 Pair p(key, Data());
223 return this->del(p).second;
224 }
225
226 Data remove(Key && key)
227 {
228 static_assert(std::is_default_constructible_v<Data>,
229 "DynMapTree::remove() requires Data to be default-constructible");
230 Pair p(std::forward<Key>(key), Data());
231 return this->del(p).second;
232 }
233
234 void remove_key(const Key & key)
235 {
236 static_assert(std::is_default_constructible_v<Data>,
237 "DynMapTree::remove_key() requires Data to be default-constructible");
238 (void) this->del(Pair(key, Data()));
239 }
240
251 Pair * search(const Key & key) const noexcept
252 {
253 static_assert(std::is_default_constructible_v<Data>,
254 "DynMapTree::search() requires Data to be default-constructible");
255 return this->Base::search(Pair(key, Data()));
256 }
257
258 Pair * search(Key && key) const noexcept
259 {
260 static_assert(std::is_default_constructible_v<Data>,
261 "DynMapTree::search() requires Data to be default-constructible");
262 return this->Base::search(Pair(std::move(key), Data()));
263 }
264
265 bool has(const Key & key) const noexcept { return search(key) != nullptr; }
266
267 bool has(Key && key) const noexcept { return search(std::move(key)) != nullptr; }
268
269 bool contains(const Key & key) const noexcept { return has(key); }
270
271 bool contains(Key && key) const noexcept { return has(std::move(key)); }
272
279 Data & find(const Key & key)
280 {
281 static_assert(std::is_default_constructible_v<Data>,
282 "DynMapTree::find() requires Data to be default-constructible");
283 return this->Base::find(Pair(key, Data())).second;
284 }
285
286 const Data & find(const Key & key) const
287 {
288 static_assert(std::is_default_constructible_v<Data>,
289 "DynMapTree::find() requires Data to be default-constructible");
290 return this->Base::find(Pair(key, Data())).second;
291 }
292
293 Data & operator [] (const Key & key)
294 {
295 static_assert(std::is_default_constructible_v<Data>,
296 "DynMapTree::operator[] requires Data to be default-constructible");
297 return this->search_or_insert(Pair(key, Data()))->second;
298 }
299
300 const Data & operator [] (const Key & key) const
301 {
302 return this->find(key);
303 }
304
305 Data & operator [] (Key && key)
306 {
307 static_assert(std::is_default_constructible_v<Data>,
308 "DynMapTree::operator[] requires Data to be default-constructible");
309 return this->search_or_insert(Pair(std::move(key), Data()))->second;
310 }
311
312 const Data & operator [] (Key && key) const
313 {
314 return this->find(std::move(key));
315 }
316
317 using Iterator = typename Base::Iterator;
318
320 {
321 return this->template maps<Key>([] (auto p) { return p.first; });
322 }
323
329 {
330 return this->template maps<Data>([] (auto p) { return p.second; });
331 }
332
338 {
340 for (Iterator it(*this); it.has_curr(); it.next_ne())
341 ret.append(&it.get_curr().second);
342 return ret;
343 }
344
350 {
352 for (Iterator it(*this); it.has_curr(); it.next_ne())
353 ret.append(&it.get_curr());
354 return ret;
355 }
356 };
357} // end namespace Aleph
358
359
360# include <tpl_binTree.H>
361# include <tpl_avl.H>
362# include <tpl_rb_tree.H>
363# include <tpl_rand_tree.H>
364# include <tpl_treap.H>
365# include <tpl_treapRk.H>
366# include <tpl_splay_tree.H>
367
368namespace Aleph {
373 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
375
380 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
386 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
388 { /* empty */ };
389
394 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
397
402 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
404 { /* empty */ };
405
414 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
416 { /* empty */ };
417
422 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
425
426 template <typename T, class Op, class C>
428 {
430 for (auto it = c.get_it(); it.has_curr(); it.next_ne())
431 {
432 const auto & curr = it.get_curr();
433 ret.insert(curr, op(curr));
434 }
435 return ret;
436 }
437
438} // end namespace Aleph
439
440# endif /* TPL_DYNMAPTREE_H */
441
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
Dynamic map implemented with an AVL tree.
Dynamic map implemented with a classic binary search tree.
Dynamic map implemented with a randomized BST.
Dynamic map implemented with a red-black tree.
Dynamic map implemented with a splay tree.
Dynamic map implemented with a ranked treap.
Dynamic map implemented with a treap.
Generic key-value map implemented on top of a binary search tree.
Pair * put(Key &&key, Data &&data)
bool has(Key &&key) const noexcept
bool contains(Key &&key) const noexcept
static const Key & get_key(Data *data_ptr) noexcept
void remove_key(const Key &key)
Pair * insert(Key &&key, const Data &data)
typename Base::Iterator Iterator
DynMapTree(const DynList< Key > &keys)
Data remove(const Key &key)
Deletes the pair key,data
const Data & find(const Key &key) const
Pair * search(Key &&key) const noexcept
Pair * append(Key &&key, const Data &data)
Pair * put(const Key &key, const Data &data)
Alias for insert().
Pair * put(const Key &key, Data &&data)
Pair * append(const Key &key, const Data &data)
DynList< Pair * > items_ptr()
Collect pointers to all stored pairs.
Pair * append(const Key &key, Data &&data=Data())
Pair * search(const Key &key) const noexcept
Collect all keys.
Data remove(Key &&key)
bool has(const Key &key) const noexcept
static const Key & get_key(const Data *data_ptr) noexcept
DynList< Key > keys() const
DynMapTree()=default
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Pair * put(Key &&key, const Data &data)
bool contains(const Key &key) const noexcept
Data & operator[](const Key &key)
std::pair< Key, Data > Pair
Pair * append(Key &&key, Data &&data=Data())
static Data & get_data(Key &key) noexcept
Pair * insert(const Key &key, Data &&data=Data())
DynList< Data > values() const
Collect all values.
Pair * insert(Key &&key, Data &&data=Data())
DynList< Data * > values_ptr()
Collect pointers to all values.
static const Data & get_data(const Key &key) noexcept
Data & find(const Key &key)
Find the value associated with key.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key del(const Key &key)
Deletes key and returns a full copy of stored key.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Key & find(const Key &key) const
Returns a modifiable reference to an element within the set.
Key * search(const Key &key) const
Find an element in the set.
Key * search_or_insert(const Key &key)
Look for the key in the binary search tree or inserts it if it is not found.
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Definition ah-dry.H:904
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
@ Tree
Basic arc (in spanning tree).
DynMapTree< typename C::Item_Type, T > map_unify(const C &c, Op op)
DynList< T > maps(const C &c, Op op)
Classic map operation.
AVL binary search tree with nodes without virtual destructor.
Definition tpl_avl.H:704
Default comparator for pair types in hash maps.
Definition ahDry.H:190
AVL tree implementation (height-balanced BST).
Generic unbalanced binary search tree.
Dynamic set implementations based on balanced binary search trees.
Randomized binary search tree.
Red-Black tree implementation (bottom-up balancing).
Top-down splay tree implementation (without rank support).
Treap with rank (order statistics).
Treap: randomized BST combining tree and heap properties.