45# ifndef TPL_DYNMAPTREE_H
46# define TPL_DYNMAPTREE_H
48# include <type_traits>
87 typename Key,
typename Data,
90 requires StrictWeakOrder<Compare, Key>
93 Dft_Pair_Cmp<Key, Data, Compare>>
95 using Pair = std::pair<Key, Data>;
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>));
136 static_assert(std::is_default_constructible_v<Data>,
137 "DynMapTree::search() on a splay tree requires Data to be default-constructible");
145 __builtin_prefetch(
LLINK(p));
146 __builtin_prefetch(
RLINK(p));
147 const Key & pk =
KEY(p).first;
150 else if (
cmp(pk, key))
165 keys.for_each([
this] (
auto &
k) { this->
insert(std::make_pair(
k, Data())); });
183 return key_to_pair<Key, Data>(&key)->second;
186 static const Data &
get_data(
const Key & key)
noexcept
188 return key_to_pair<Key, Data>(&key)->second;
191 static const Key &
get_key(Data * data_ptr)
noexcept
193 return data_to_pair<Key, Data>(data_ptr)->first;
196 static const Key &
get_key(
const Data * data_ptr)
noexcept
198 return data_to_pair<Key, Data>(data_ptr)->first;
229 std::forward<Data>(data)));
250 std::forward<Data>(data)));
257 Pair *
put(
const Key & key,
const Data & data)
264 return insert(key, std::forward<Data>(data));
269 return insert(std::forward<Key>(key), data);
274 return insert(std::forward<Key>(key), std::forward<Data>(data));
287 static_assert(std::is_default_constructible_v<Data>,
288 "DynMapTree::remove() requires Data to be default-constructible");
290 return this->
del(p).second;
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;
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();
318 <<
"DynMapTree::remove_key: key is not in the map";
399 const Data &
find(
const Key & key)
const
408 static_assert(std::is_default_constructible_v<Data>,
409 "DynMapTree::operator[] requires Data to be default-constructible");
415 return this->
find(key);
420 static_assert(std::is_default_constructible_v<Data>,
421 "DynMapTree::operator[] requires Data to be default-constructible");
427 return this->
find(std::move(key));
437 return this->
template maps<Key>([] (
const auto & p) ->
const Key & {
return p.first; });
446 return this->
template maps<Data>([] (
const auto & p) ->
const Data & {
return p.second; });
456 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
457 ret.
append(&it.get_curr().second);
468 for (
Iterator it(*
this); it.has_curr(); it.next_ne())
469 ret.
append(&it.get_curr());
489 template <
typename Key,
typename Type,
class Compare = Aleph::less<Key> >
500 template <
typename Key,
typename Type,
class Compare = Aleph::less<Key> >
510 template <
typename Key,
typename Type,
class Compare = Aleph::less<Key> >
521 template <
typename Key,
typename Type,
class Compare = Aleph::less<Key> >
532 template <
typename Key,
typename Type,
class Compare = Aleph::less<Key> >
547 template <
typename Key,
typename Type,
class Compare = Aleph::less<Key> >
558 template <
typename Key,
typename Type,
class Compare = Aleph::less<Key> >
565 template <
typename T,
class Op,
class C>
569 for (
auto it = c.get_it(); it.has_curr(); it.next_ne())
571 const auto & curr = it.get_curr();
572 ret.
insert(curr, op(curr));
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
#define Derived_Build(Name)
Declare build() in a class derived from a container.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
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())
static const Key & get_key(const Data *data_ptr) noexcept
DynList< Key > keys() const
Pair * search_pair(const Key &key) const noexcept(nothrow_search)
static constexpr bool splays
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.
QuadTree - Hierarchical spatial index for 2D points.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
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.
Default comparator for pair types in hash maps.
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.