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# include <ah-concepts.H>
53
54using namespace Aleph;
55
56namespace Aleph {
57
82 template <
83 typename Key, typename Data,
84 template <typename, class> class Tree = Avl_Tree,
85 class Compare = Aleph::less<Key>>
87 class DynMapTree :
88 public DynSetTree<std::pair<Key, Data>, Tree,
89 Dft_Pair_Cmp<Key, Data, Compare>>
90 {
91 using Pair = std::pair<Key, Data>;
92
93 using Base =
95
96 public:
97
98 using Base::Base;
99
101 {
102 keys.for_each([this] (auto & k) { this->insert(std::make_pair(k, Data())); });
103 }
104
105 DynMapTree() = default;
106
107 using Key_Type = Key;
108
110
111 using Value_Type = Data ;
112
113 // using Base::Base; // no more need. But I don't remember why I put it
114 using Base::insert;
115
116 static Data & get_data(Key & key) noexcept
117 {
118 return key_to_pair<Key, Data>(&key)->second;
119 }
120
121 static const Data & get_data(const Key & key) noexcept
122 {
123 return key_to_pair<Key, Data>(&key)->second;
124 }
125
126 static const Key & get_key(Data * data_ptr) noexcept
127 {
128 return data_to_pair<Key, Data>(data_ptr)->first;
129 }
130
131 static const Key & get_key(const Data * data_ptr) noexcept
132 {
133 return data_to_pair<Key, Data>(data_ptr)->first;
134 }
135
146 Pair * insert(const Key & key, const Data & data)
147 {
148 return this->Base::insert(Pair(key, data));
149 }
150
151 Pair * insert(const Key & key, Data && data = Data())
152 {
153 return this->Base::insert(Pair(key, std::forward<Data>(data)));
154 }
155
156 Pair * insert(Key && key, const Data & data)
157 {
158 return this->Base::insert(Pair(std::forward<Key>(key), data));
159 }
160
161 Pair * insert(Key && key, Data && data = Data())
162 {
163 return this->Base::insert(Pair(std::forward<Key>(key),
164 std::forward<Data>(data)));
165 }
166
167 Pair * append(const Key & key, const Data & data)
168 {
169 return this->Base::insert(Pair(key, data));
170 }
171
172 Pair * append(const Key & key, Data && data = Data())
173 {
174 return this->Base::insert(Pair(key, std::forward<Data>(data)));
175 }
176
177 Pair * append(Key && key, const Data & data)
178 {
179 return this->Base::insert(Pair(std::forward<Key>(key), data));
180 }
181
182 Pair * append(Key && key, Data && data = Data())
183 {
184 return this->Base::insert(Pair(std::forward<Key>(key),
185 std::forward<Data>(data)));
186 }
187
192 Pair * put(const Key & key, const Data & data)
193 {
194 return insert(key, data);
195 }
196
197 Pair * put(const Key & key, Data && data)
198 {
199 return insert(key, std::forward<Data>(data));
200 }
201
202 Pair * put(Key && key, const Data & data)
203 {
204 return insert(std::forward<Key>(key), data);
205 }
206
207 Pair * put(Key && key, Data && data)
208 {
209 return insert(std::forward<Key>(key), std::forward<Data>(data));
210 }
211
220 Data remove(const Key & key)
221 {
222 static_assert(std::is_default_constructible_v<Data>,
223 "DynMapTree::remove() requires Data to be default-constructible");
224 Pair p(key, Data());
225 return this->del(p).second;
226 }
227
228 Data remove(Key && key)
229 {
230 static_assert(std::is_default_constructible_v<Data>,
231 "DynMapTree::remove() requires Data to be default-constructible");
232 Pair p(std::forward<Key>(key), Data());
233 return this->del(p).second;
234 }
235
236 void remove_key(const Key & key)
237 {
238 static_assert(std::is_default_constructible_v<Data>,
239 "DynMapTree::remove_key() requires Data to be default-constructible");
240 (void) this->del(Pair(key, Data()));
241 }
242
253 Pair * search(const Key & key) const noexcept
254 {
255 static_assert(std::is_default_constructible_v<Data>,
256 "DynMapTree::search() requires Data to be default-constructible");
257 return this->Base::search(Pair(key, Data()));
258 }
259
260 Pair * search(Key && key) const noexcept
261 {
262 static_assert(std::is_default_constructible_v<Data>,
263 "DynMapTree::search() requires Data to be default-constructible");
264 return this->Base::search(Pair(std::move(key), Data()));
265 }
266
267 bool has(const Key & key) const noexcept { return search(key) != nullptr; }
268
269 bool has(Key && key) const noexcept { return search(std::move(key)) != nullptr; }
270
271 bool contains(const Key & key) const noexcept { return has(key); }
272
273 bool contains(Key && key) const noexcept { return has(std::move(key)); }
274
281 Data & find(const Key & key)
282 {
283 static_assert(std::is_default_constructible_v<Data>,
284 "DynMapTree::find() requires Data to be default-constructible");
285 return this->Base::find(Pair(key, Data())).second;
286 }
287
288 const Data & find(const Key & key) const
289 {
290 static_assert(std::is_default_constructible_v<Data>,
291 "DynMapTree::find() requires Data to be default-constructible");
292 return this->Base::find(Pair(key, Data())).second;
293 }
294
295 Data & operator [] (const Key & key)
296 {
297 static_assert(std::is_default_constructible_v<Data>,
298 "DynMapTree::operator[] requires Data to be default-constructible");
299 return this->search_or_insert(Pair(key, Data()))->second;
300 }
301
302 const Data & operator [] (const Key & key) const
303 {
304 return this->find(key);
305 }
306
307 Data & operator [] (Key && key)
308 {
309 static_assert(std::is_default_constructible_v<Data>,
310 "DynMapTree::operator[] requires Data to be default-constructible");
311 return this->search_or_insert(Pair(std::move(key), Data()))->second;
312 }
313
314 const Data & operator [] (Key && key) const
315 {
316 return this->find(std::move(key));
317 }
318
319 using Iterator = typename Base::Iterator;
320
322 {
323 return this->template maps<Key>([] (auto p) { return p.first; });
324 }
325
331 {
332 return this->template maps<Data>([] (auto p) { return p.second; });
333 }
334
340 {
342 for (Iterator it(*this); it.has_curr(); it.next_ne())
343 ret.append(&it.get_curr().second);
344 return ret;
345 }
346
352 {
354 for (Iterator it(*this); it.has_curr(); it.next_ne())
355 ret.append(&it.get_curr());
356 return ret;
357 }
358 };
359} // end namespace Aleph
360
361
362# include <tpl_binTree.H>
363# include <tpl_avl.H>
364# include <tpl_rb_tree.H>
365# include <tpl_rand_tree.H>
366# include <tpl_treap.H>
367# include <tpl_treapRk.H>
368# include <tpl_splay_tree.H>
369
370namespace Aleph {
375 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
377
382 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
388 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
390 { /* empty */ };
391
396 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
399
404 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
406 { /* empty */ };
407
416 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
418 { /* empty */ };
419
424 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
427
428 template <typename T, class Op, class C>
430 {
432 for (auto it = c.get_it(); it.has_curr(); it.next_ne())
433 {
434 const auto & curr = it.get_curr();
435 ret.insert(curr, op(curr));
436 }
437 return ret;
438 }
439
440} // end namespace Aleph
441
442# endif /* TPL_DYNMAPTREE_H */
443
C++20 concepts for constraining comparison functors.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
T & append(const T &item)
Definition htlist.H:1271
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.
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
@ Tree
Basic arc (in spanning tree).
DynMapTree< typename C::Item_Type, T > map_unify(const C &c, Op op)
AVL binary search tree with nodes without virtual destructor.
Definition tpl_avl.H:737
Default comparator for pair types in hash maps.
Definition ahDry.H:190
static int * k
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.