Aleph-w 7.10.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
86 template <
87 typename Key, typename Data,
88 template <typename, class> class Tree = Avl_Tree,
89 class Compare = Aleph::less<Key>>
90 requires StrictWeakOrder<Compare, Key>
91 class DynMapTree :
92 public DynSetTree<std::pair<Key, Data>, Tree,
93 Dft_Pair_Cmp<Key, Data, Compare>>
94 {
95 using Pair = std::pair<Key, Data>;
96
97 using Base =
99
100 using Node = typename Base::Node;
101
102 // The splay trees move what a search finds to the root, which only
103 // their own search does: a search by the key alone would skip it
104 static constexpr bool splays =
105 requires (typename Base::Tree_Type & t, const Pair & p) { t.splay(p); };
106
107 // Whether a search by the key cannot throw: it takes the comparison of
108 // the tree, which a tree given by the user may copy, compares constant
109 // keys and takes the result as a condition and, in a splay tree, builds
110 // the pair that the tree searches for, a copy of the key and a Data
111 // built by default and moved
112 static constexpr bool nothrow_search =
113 noexcept(std::declval<typename Base::Tree_Type &>().get_compare()) and
114 nothrow_condition_v<const Compare &, const Key &, const Key &> and
115 (not splays or (std::is_nothrow_copy_constructible_v<Key> and
116 std::is_nothrow_default_constructible_v<Data> and
117 std::is_nothrow_move_constructible_v<Data>));
118
119 // The pair whose key is equivalent to key, or nullptr. It descends
120 // comparing key with the first member of each pair, so it builds no
121 // pair: no copy of the key and no Data, which may allocate or throw.
122 // Two comparisons per level stop at the pair found, as the search of
123 // the AVL tree does, whatever the key. One comparison per level, down
124 // to a leaf, as searchInBinTree() does, makes fewer, but Clang takes
125 // the child at an address computed from the comparison, without a
126 // branch: each level then waits for the comparison of the one above,
127 // and a long std::string, whose characters are out of the node, for
128 // their memory. The second comparison keeps the branch, which lets the
129 // processor go down while it compares. It brings the children of each
130 // node to the cache meanwhile. A splay tree searches for a pair built
131 // from key, so that it moves what it finds to the root.
132 Pair * search_pair(const Key & key) const noexcept(nothrow_search)
133 {
134 if constexpr (splays)
135 {
136 static_assert(std::is_default_constructible_v<Data>,
137 "DynMapTree::search() on a splay tree requires Data to be default-constructible");
138 return const_cast<Pair *>(Base::search(Pair(key, Data())));
139 }
140 else
141 {
142 const Compare & cmp = this->get_compare().cmp;
143 for (Node * p = this->get_root_node(); p != Node::NullPtr; )
144 {
145 __builtin_prefetch(LLINK(p));
146 __builtin_prefetch(RLINK(p));
147 const Key & pk = KEY(p).first; // constant, as in the pairs
148 if (cmp(key, pk))
149 p = LLINK(p);
150 else if (cmp(pk, key))
151 p = RLINK(p);
152 else
153 return &KEY(p);
154 }
155 return nullptr;
156 }
157 }
158
159 public:
160
161 using Base::Base;
162
164 {
165 keys.for_each([this] (auto & k) { this->insert(std::make_pair(k, Data())); });
166 }
167
168 DynMapTree() = default;
169
171
172 using Key_Type = Key;
173
175
176 using Value_Type = Data ;
177
178 // using Base::Base; // no more need. But I don't remember why I put it
179 using Base::insert;
180
181 static Data & get_data(Key & key) noexcept
182 {
183 return key_to_pair<Key, Data>(&key)->second;
184 }
185
186 static const Data & get_data(const Key & key) noexcept
187 {
188 return key_to_pair<Key, Data>(&key)->second;
189 }
190
191 static const Key & get_key(Data * data_ptr) noexcept
192 {
193 return data_to_pair<Key, Data>(data_ptr)->first;
194 }
195
196 static const Key & get_key(const Data * data_ptr) noexcept
197 {
198 return data_to_pair<Key, Data>(data_ptr)->first;
199 }
200
211 Pair * insert(const Key & key, const Data & data)
212 {
213 return this->Base::insert(Pair(key, data));
214 }
215
216 Pair * insert(const Key & key, Data && data = Data())
217 {
218 return this->Base::insert(Pair(key, std::forward<Data>(data)));
219 }
220
221 Pair * insert(Key && key, const Data & data)
222 {
223 return this->Base::insert(Pair(std::forward<Key>(key), data));
224 }
225
226 Pair * insert(Key && key, Data && data = Data())
227 {
228 return this->Base::insert(Pair(std::forward<Key>(key),
229 std::forward<Data>(data)));
230 }
231
232 Pair * append(const Key & key, const Data & data)
233 {
234 return this->Base::insert(Pair(key, data));
235 }
236
237 Pair * append(const Key & key, Data && data = Data())
238 {
239 return this->Base::insert(Pair(key, std::forward<Data>(data)));
240 }
241
242 Pair * append(Key && key, const Data & data)
243 {
244 return this->Base::insert(Pair(std::forward<Key>(key), data));
245 }
246
247 Pair * append(Key && key, Data && data = Data())
248 {
249 return this->Base::insert(Pair(std::forward<Key>(key),
250 std::forward<Data>(data)));
251 }
252
257 Pair * put(const Key & key, const Data & data)
258 {
259 return insert(key, data);
260 }
261
262 Pair * put(const Key & key, Data && data)
263 {
264 return insert(key, std::forward<Data>(data));
265 }
266
267 Pair * put(Key && key, const Data & data)
268 {
269 return insert(std::forward<Key>(key), data);
270 }
271
272 Pair * put(Key && key, Data && data)
273 {
274 return insert(std::forward<Key>(key), std::forward<Data>(data));
275 }
276
285 Data remove(const Key & key)
286 {
287 static_assert(std::is_default_constructible_v<Data>,
288 "DynMapTree::remove() requires Data to be default-constructible");
289 Pair p(key, Data());
290 return this->del(p).second;
291 }
292
293 Data remove(Key && key)
294 {
295 static_assert(std::is_default_constructible_v<Data>,
296 "DynMapTree::remove() requires Data to be default-constructible");
297 Pair p(std::forward<Key>(key), Data());
298 return this->del(p).second;
299 }
300
312 void remove_key(const Key & key)
313 {
314 static_assert(std::is_default_constructible_v<Data>,
315 "DynMapTree::remove_key() requires Data to be default-constructible");
316 const size_t n = this->size();
317 ah_domain_error_if(Base::remove(Pair(key, Data())) == n)
318 << "DynMapTree::remove_key: key is not in the map";
319 }
320
345 Pair * search(const Key & key) noexcept(nothrow_search)
346 {
347 return search_pair(key);
348 }
349
352 const Pair * search(const Key & key) const noexcept(nothrow_search)
353 {
354 return search_pair(key);
355 }
356
358 Pair * search(Key && key) noexcept(nothrow_search) { return search_pair(key); }
359
362 const Pair * search(Key && key) const noexcept(nothrow_search) { return search_pair(key); }
363
370 bool has(const Key & key) const noexcept(nothrow_search) { return search_pair(key) != nullptr; }
371
373 bool has(Key && key) const noexcept(nothrow_search) { return search_pair(key) != nullptr; }
374
376 bool contains(const Key & key) const noexcept(nothrow_search) { return has(key); }
377
379 bool contains(Key && key) const noexcept(nothrow_search) { return has(key); }
380
390 Data & find(const Key & key)
391 {
392 Pair * p = search_pair(key);
393 ah_domain_error_if(p == nullptr) << "key not found";
394 return p->second;
395 }
396
399 const Data & find(const Key & key) const
400 {
401 const Pair * p = search_pair(key);
402 ah_domain_error_if(p == nullptr) << "key not found";
403 return p->second;
404 }
405
406 Data & operator [] (const Key & key)
407 {
408 static_assert(std::is_default_constructible_v<Data>,
409 "DynMapTree::operator[] requires Data to be default-constructible");
410 return this->search_or_insert(Pair(key, Data()))->second;
411 }
412
413 const Data & operator [] (const Key & key) const
414 {
415 return this->find(key);
416 }
417
418 Data & operator [] (Key && key)
419 {
420 static_assert(std::is_default_constructible_v<Data>,
421 "DynMapTree::operator[] requires Data to be default-constructible");
422 return this->search_or_insert(Pair(std::move(key), Data()))->second;
423 }
424
425 const Data & operator [] (Key && key) const
426 {
427 return this->find(std::move(key));
428 }
429
430 using Iterator = typename Base::Iterator;
431
434
436 {
437 return this->template maps<Key>([] (const auto & p) -> const Key & { return p.first; });
438 }
439
445 {
446 return this->template maps<Data>([] (const auto & p) -> const Data & { return p.second; });
447 }
448
454 {
455 DynList<Data*> ret;
456 for (Iterator it(*this); it.has_curr(); it.next_ne())
457 ret.append(&it.get_curr().second);
458 return ret;
459 }
460
466 {
467 DynList<Pair*> ret;
468 for (Iterator it(*this); it.has_curr(); it.next_ne())
469 ret.append(&it.get_curr());
470 return ret;
471 }
472 };
473} // end namespace Aleph
474
475
476# include <tpl_binTree.H>
477# include <tpl_avl.H>
478# include <tpl_rb_tree.H>
479# include <tpl_rand_tree.H>
480# include <tpl_treap.H>
481# include <tpl_treapRk.H>
482# include <tpl_splay_tree.H>
483
484namespace Aleph {
489 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
495
500 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
510 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
516
521 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
527
532 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
538
547 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
553
558 template <typename Key, typename Type, class Compare = Aleph::less<Key> >
564
565 template <typename T, class Op, class C>
567 {
569 for (auto it = c.get_it(); it.has_curr(); it.next_ne())
570 {
571 const auto & curr = it.get_curr();
572 ret.insert(curr, op(curr));
573 }
574 return ret;
575 }
576
577} // end namespace Aleph
578
579# endif /* TPL_DYNMAPTREE_H */
580
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#define Derived_Build(Name)
Declare build() in a class derived from a container.
Definition ah-dry.H:2881
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
@ KEY
Definition btreepic.C:169
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
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)
static const Key & get_key(Data *data_ptr) noexcept
void remove_key(const Key &key)
Deletes the pair whose key is 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
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
const Data & find(const Key &key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
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)
Pair * search(Key &&key) noexcept(nothrow_search)
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynList< Pair * > items_ptr()
Collect pointers to all stored pairs.
Pair * append(const Key &key, Data &&data=Data())
Data remove(Key &&key)
static const Key & get_key(const Data *data_ptr) noexcept
DynList< Key > keys() const
typename Base::Node Node
Pair * search_pair(const Key &key) const noexcept(nothrow_search)
static constexpr bool splays
DynMapTree()=default
static constexpr bool nothrow_search
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Pair * put(Key &&key, const Data &data)
Data & operator[](const Key &key)
bool contains(const Key &key) const noexcept(nothrow_search)
Same as has().
std::pair< Key, Data > Pair
Pair * append(Key &&key, Data &&data=Data())
const Pair * search(Key &&key) const noexcept(nothrow_search)
This is an overloaded member function, provided for convenience. It differs from the above function o...
static Data & get_data(Key &key) noexcept
Pair * insert(const Key &key, Data &&data=Data())
DynList< Data > values() const
Collect all values.
bool has(Key &&key) const noexcept(nothrow_search)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool contains(Key &&key) const noexcept(nothrow_search)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Pair * insert(Key &&key, Data &&data=Data())
typename Base::Const_Iterator Const_Iterator
Read-only iterator: the iterator of a constant map.
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.
const Pair * search(const Key &key) const noexcept(nothrow_search)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool has(const Key &key) const noexcept(nothrow_search)
Return whether key is in the map.
Dynamic set backed by balanced binary search trees with automatic memory management.
typename Tree< Key, Compare >::Node Node
Type of binary node used by the binary search tree internal.
const size_t & size() const
Returns the cardinality of the set.
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.
size_t remove(const Key &key)
Removes a key from the dynamic set.
Key * search(const Key &key)
Find an element in the set.
Aleph::Const_Iterator< Iterator > Const_Iterator
Read-only iterator: the iterator of a constant set.
decltype(auto) get_compare() const noexcept(noexcept(tree.get_compare()))
The comparator that orders the keys, as the tree gives it.
Key * search_or_insert(const Key &key)
Look for the key in the binary search tree or inserts it if it is not found.
Node * get_root_node() const
and
Insert a new element into the container by constructing it from the given args.
Definition ah-dry.H:1273
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
Main namespace for Aleph-w library functions.
@ 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 a virtual destructor.
Definition tpl_avl.H:831
Default comparator for pair types in hash maps.
Definition ahDry.H:206
#define RLINK(i, n)
#define LLINK(i, n)
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.