Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynMapOhash.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
39#ifndef TPL_DYNMAPOHASH_H
40#define TPL_DYNMAPOHASH_H
41
42#include <cstddef>
43#include <functional>
44#include <type_traits>
45#include <utility>
46
47#include <tpl_olhash.H>
48#include <tpl_odhash.H>
49# include <ah-concepts.H>
50
51namespace Aleph
52{
53
114template <typename Key, typename Data,
116 template <typename, class> class HashTable = ODhashTable>
119 public HashTable<std::pair<Key, Data>, Dft_Pair_Cmp<Key, Data, Cmp>>
120{
122 using Pair = std::pair<Key, Data>;
123
126
127 using Base::Base;
128 using Base::insert;
129
131 using Hash_Fct = std::function<size_t(const Key &)>;
132
134 using Hash_Fct_Ptr = size_t (*) (const Key &);
135
137 using Key_Type = Key;
138
140 using Data_Type = Data;
141
143 using Value_Type = Data;
144
147
150
165 Hash_Fct_Ptr second_hash_fct = snd_hash_ptr_fct<Key>,
166 Cmp cmp = Cmp(),
167 float lower_alpha = hash_default_lower_alpha,
168 float upper_alpha = hash_default_upper_alpha,
169 bool with_resize = true)
170 : Base(len, std::bind(map_hash_fct<Key, Data, Hash_Fct>,
172 std::bind(map_hash_fct<Key, Data, Hash_Fct>,
173 second_hash_fct, std::placeholders::_1),
174 Dft_Pair_Cmp<Key, Data, Cmp>(cmp),
175 lower_alpha, upper_alpha, with_resize) {}
176
188 [[nodiscard]] static Pair * key_to_pair(Key * ptr) noexcept
189 {
190 return reinterpret_cast<Pair*>(ptr);
191 }
192
193 [[nodiscard]] static const Pair * key_to_pair(const Key * ptr) noexcept
194 {
195 return reinterpret_cast<const Pair*>(ptr);
196 }
197
208 [[nodiscard]] static Pair * data_to_pair(Data * ptr) noexcept
209 {
210 return reinterpret_cast<Pair*>(
211 reinterpret_cast<char*>(ptr) - offsetof(Pair, second));
212 }
213
214 [[nodiscard]] static const Pair * data_to_pair(const Data * ptr) noexcept
215 {
216 return reinterpret_cast<const Pair*>(
217 reinterpret_cast<const char*>(ptr) - offsetof(Pair, second));
218 }
219
230 [[nodiscard]] static Data & get_data(Key & key) noexcept
231 {
232 return key_to_pair(&key)->second;
233 }
234
235 [[nodiscard]] static const Data & get_data(const Key & key) noexcept
236 {
237 return key_to_pair(&key)->second;
238 }
239
250 [[nodiscard]] static const Key & get_key(Data * data_ptr) noexcept
251 {
252 return data_to_pair(data_ptr)->first;
253 }
254
255 [[nodiscard]] static const Key & get_key(const Data * data_ptr) noexcept
256 {
257 return data_to_pair(data_ptr)->first;
258 }
259
271 Pair * insert(const Key & key, const Data & data)
272 {
273 return this->Base::insert(Pair(key, data));
274 }
275
282 Pair * insert(const Key & key, Data && data)
283 {
284 return this->Base::insert(Pair(key, std::forward<Data>(data)));
285 }
286
293 Pair * insert(Key && key, Data && data)
294 {
295 return this->Base::insert(Pair(std::forward<Key>(key),
296 std::forward<Data>(data)));
297 }
298
305 Pair * insert(Key && key, const Data & data)
306 {
307 return this->Base::insert(Pair(std::forward<Key>(key), data));
308 }
309
315 [[nodiscard]] Pair * search(const Key & key) const noexcept
316 {
317 static_assert(std::is_default_constructible<Data>::value,
318 "MapOpenHash::search() requires Data to be default-constructible");
319 return this->Base::search(Pair(key, Data()));
320 }
321
327 [[nodiscard]] Pair * search(Key && key) const noexcept
328 {
329 static_assert(std::is_default_constructible<Data>::value,
330 "MapOpenHash::search() requires Data to be default-constructible");
331 return this->Base::search(Pair(std::move(key), Data()));
332 }
333
339 [[nodiscard]] bool has(const Key & key) const noexcept
340 {
341 return search(key) != nullptr;
342 }
343
349 [[nodiscard]] bool has(Key && key) const noexcept
350 {
351 return search(std::move(key)) != nullptr;
352 }
353
361 [[nodiscard]] bool contains(const Key & key) const noexcept
362 {
363 return has(key);
364 }
365
371 [[nodiscard]] bool contains(Key && key) const noexcept
372 {
373 return has(std::move(key));
374 }
375
383 [[nodiscard]] Data & find(const Key & key)
384 {
385 static_assert(std::is_default_constructible<Data>::value,
386 "MapOpenHash::find() requires Data to be default-constructible");
387 return Base::find(Pair(key, Data())).second;
388 }
389
397 [[nodiscard]] Data & find(Key && key)
398 {
399 static_assert(std::is_default_constructible<Data>::value,
400 "MapOpenHash::find() requires Data to be default-constructible");
401 return Base::find(Pair(std::move(key), Data())).second;
402 }
403
411 [[nodiscard]] const Data & find(const Key & key) const
412 {
413 static_assert(std::is_default_constructible<Data>::value,
414 "MapOpenHash::find() requires Data to be default-constructible");
415 return Base::find(Pair(key, Data())).second;
416 }
417
425 [[nodiscard]] const Data & find(Key && key) const
426 {
427 static_assert(std::is_default_constructible<Data>::value,
428 "MapOpenHash::find() requires Data to be default-constructible");
429 return Base::find(Pair(std::move(key), Data())).second;
430 }
431
443 [[nodiscard]] Data & operator [] (const Key & key)
444 {
445 static_assert(std::is_default_constructible<Data>::value,
446 "MapOpenHash::operator[] requires Data to be default-constructible");
447 return this->search_or_insert(Pair(key, Data()))->second;
448 }
449
457 [[nodiscard]] const Data & operator [] (const Key & key) const
458 {
459 return this->find(key);
460 }
461
467 [[nodiscard]] Data & operator [] (Key && key)
468 {
469 static_assert(std::is_default_constructible<Data>::value,
470 "MapOpenHash::operator[] requires Data to be default-constructible");
471 return this->search_or_insert(Pair(std::move(key), Data()))->second;
472 }
473
481 [[nodiscard]] const Data & operator [] (Key && key) const
482 {
483 return this->find(std::move(key));
484 }
485
494 void remove_by_data(Data & data)
495 {
496 Base::remove_ptr(data_to_pair(&data));
497 }
498
505 void remove(const Key & key)
506 {
507 static_assert(std::is_default_constructible<Data>::value,
508 "MapOpenHash::remove() requires Data to be default-constructible");
509 Base::remove(Pair(key, Data()));
510 }
511
518 void remove(Key && key)
519 {
520 static_assert(std::is_default_constructible<Data>::value,
521 "MapOpenHash::remove() requires Data to be default-constructible");
522 Base::remove(Pair(std::move(key), Data()));
523 }
524
526 using Iterator = typename Base::Iterator;
527
533 {
534 return this->template maps<Key>([] (auto p) { return p.first; });
535 }
536
542 {
543 return this->template maps<Data>([] (auto p) { return p.second; });
544 }
545
553 {
555 for (Iterator it(*this); it.has_curr(); it.next_ne())
556 ret.append(&it.get_curr().second);
557 return ret;
558 }
559
567 {
569 for (Iterator it(*this); it.has_curr(); it.next_ne())
570 ret.append(&it.get_curr());
571 return ret;
572 }
573};
574
575
591template <typename Key, typename Data, class Cmp = Aleph::equal_to<Key>>
597
598
614template <typename Key, typename Data, class Cmp = Aleph::equal_to<Key>>
616struct MapODhash : public MapOpenHash<Key, Data, Cmp, ODhashTable>
617{
618 using MapOpenHash<Key, Data, Cmp, ODhashTable>::MapOpenHash;
619};
620
621} // end namespace Aleph
622
623#endif // TPL_DYNMAPOHASH_H
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
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:182
Open addressing hash table with linear probing collision resolution.
Definition tpl_olhash.H:170
Equivalence relation constraint for equality comparators.
Definition ah-concepts.H:97
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4118
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
size_t map_hash_fct(Fct fct, const std::pair< Key, Data > &p) noexcept
Definition hash-fct.H:1110
const float hash_default_upper_alpha
Definition hash-dry.C:40
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.
const float hash_default_lower_alpha
Definition hash-dry.C:38
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Definition primes.C:381
STL namespace.
Default comparator for pair types in hash maps.
Definition ahDry.H:190
Open addressing hash map using double hashing.
Open addressing hash map using linear probing.
Open addressing hash map for key-value pairs.
void remove(Key &&key)
Remove an entry by key (move semantics).
MapOpenHash(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr first_hash_fct=dft_hash_ptr_fct< Key >, Hash_Fct_Ptr second_hash_fct=snd_hash_ptr_fct< Key >, Cmp cmp=Cmp(), float lower_alpha=hash_default_lower_alpha, float upper_alpha=hash_default_upper_alpha, bool with_resize=true)
Construct a map with specified parameters.
const Data & find(Key &&key) const
Find and return the value for a key (const, move).
std::pair< Key, Data > Pair
The key-value pair type stored in the map.
void remove(const Key &key)
Remove an entry by key.
DynList< Data > values() const
Get a list of all values in the map.
Key Key_Type
The type of keys in the map.
Data & operator[](const Key &key)
Access or insert a value by key.
const Data & find(const Key &key) const
Find and return the value for a key (const).
bool contains(const Key &key) const noexcept
Check if a key exists in the map.
Pair Item_Type
The item type stored in the map (key-value pair).
static const Key & get_key(const Data *data_ptr) noexcept
Pair * insert(Key &&key, Data &&data)
Insert a key-value pair (move both).
Data & find(const Key &key)
Find and return the value for a key.
static const Key & get_key(Data *data_ptr) noexcept
Get the key associated with a data pointer.
bool has(const Key &key) const noexcept
Check if a key exists in the map.
Data Data_Type
The type of mapped values.
static const Pair * data_to_pair(const Data *ptr) noexcept
Pair * insert(const Key &key, Data &&data)
Insert a key-value pair (move data).
bool contains(Key &&key) const noexcept
Check if a key exists in the map (move semantics).
typename Base::Iterator Iterator
Iterator type for traversing the map.
static const Pair * key_to_pair(const Key *ptr) noexcept
HashTable< std::pair< Key, Data >, Dft_Pair_Cmp< Key, Data, Cmp > > Base
The base hash table type.
bool has(Key &&key) const noexcept
Check if a key exists in the map (move semantics).
DynList< Key > keys() const
Get a list of all keys in the map.
static Pair * data_to_pair(Data *ptr) noexcept
Convert a data pointer to its containing pair pointer.
Data Value_Type
Alias for Data_Type (compatibility with other containers).
Pair * insert(Key &&key, const Data &data)
Insert a key-value pair (move key, copy data).
static const Data & get_data(const Key &key) noexcept
DynList< Data * > values_ptr()
Get a list of pointers to all values.
Pair * search(Key &&key) const noexcept
Search for a key in the map (move semantics).
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair (copy semantics).
size_t(*)(const Key &) Hash_Fct_Ptr
Function pointer type for hash functions.
static Data & get_data(Key &key) noexcept
Get the data associated with a key by reference.
std::function< size_t(const Key &)> Hash_Fct
Function type for hash functions.
void remove_by_data(Data &data)
Remove an entry by its data pointer.
static Pair * key_to_pair(Key *ptr) noexcept
Convert a key pointer to its containing pair pointer.
DynList< Pair * > items_ptr()
Get a list of pointers to all pairs.
Pair * search(const Key &key) const noexcept
Search for a key in the map.
Data & find(Key &&key)
Find and return the value for a key (move semantics).
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.