Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynSetHash.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_DYNSETHASH_H
46# define TPL_DYNSETHASH_H
47
48# include <algorithm>
49# include <typeinfo>
50# include <type_traits>
51# include <utility>
52# include <ahDry.H>
53# include <ahIterator.H>
54# include <primes.H>
55# include <htlist.H>
56# include <tpl_dynArray.H>
57# include <tpl_dynMapOhash.H>
58# include <tpl_dynLhash.H>
59# include <tpl_linHash.H>
60# include <ah-concepts.H>
61
62
63namespace Aleph
64{
76 template <typename Key,
77 template <typename, class> class HashTable = LhashTable,
79 requires EqualityComparator<Cmp, Key>
81 : public HashTable<Key, Cmp>,
82 public GenericTraverse<DynHashTable<Key, HashTable, Cmp>>,
83 public LocateFunctions<DynHashTable<Key, HashTable, Cmp>, Key>,
84 public FunctionalMethods<DynHashTable<Key, HashTable, Cmp>, Key>,
85 public GenericKeys<DynHashTable<Key, HashTable, Cmp>, Key>,
86 public EqualToMethod<DynHashTable<Key, HashTable, Cmp>>,
87 public StlAlephIterator<DynHashTable<Key, HashTable, Cmp>>
88 {
89 protected:
90 using Base = HashTable<Key, Cmp>;
91
92 using Bucket = typename HashTable<Key, Cmp>::Bucket;
93
94 public:
96 using Hash_Fct = typename Base::Hash_Fct;
97
98 using Hash_Fct_Ptr = typename Base::Hash_Fct_Ptr;
99
100 using Key_Type = Key;
101
102 using Item_Type = Key;
103
116 Hash_Fct_Ptr hash_fct = Aleph::dft_hash_ptr_fct<Key>,
117 Cmp cmp = Cmp(),
118 float lower_alpha = hash_default_lower_alpha,
119 float upper_alpha = hash_default_upper_alpha)
120 : Base(len, hash_fct, cmp, lower_alpha, upper_alpha, true, true)
121 {
122 // empty
123 }
124
125 DynHashTable(size_t len, Hash_Fct hash_fct, Cmp cmp,
126 float lower_alpha, float upper_alpha)
127 : Base(len, hash_fct, cmp, lower_alpha, upper_alpha, true, true)
128 {
129 // empty
130 }
131
132 private:
133 void copy(const DynHashTable & other)
134 {
135 for (typename Base::Iterator it(other); it.has_curr(); it.next_ne())
136 {
137 auto *bucket = static_cast<Bucket *>(it.get_curr());
138 insert(bucket->get_key());
139 }
140 }
141
142 public:
163 : Base(other.len == 0 ? Primes::DefaultPrime : other.len, other.hash_fct,
164 const_cast<DynHashTable &>(other).get_compare(),
165 other.lower_alpha, other.upper_alpha, true,
166 other.get_with_resize())
167 {
168 copy(other);
169 }
170
184 DynHashTable(DynHashTable && other) noexcept
185 requires std::is_nothrow_move_constructible_v<Base>
186 : Base(std::move(static_cast<Base &>(other)))
187 {
188 // empty
189 }
190
207 DynHashTable(DynHashTable && other) noexcept
208 requires (not std::is_nothrow_move_constructible_v<Base>)
209 : Base(other.len, other.hash_fct, other.get_compare(),
210 other.lower_alpha, other.upper_alpha, true, true)
211 {
212 this->swap(other);
213 }
214
216
218 {
219 this->empty();
220 }
221
227 void clear() { this->empty(); }
228
230
236 bool contains(const Key & key) const noexcept { return Base::contains(key); }
237
243 bool is_empty() const noexcept { return this->Base::is_empty(); }
244
256 {
257 if (this == &other)
258 return *this;
259
260 DynHashTable tmp(other);
261 this->swap(tmp);
262
263 return *this;
264 }
265
267 {
268 if (this == &other)
269 return *this;
270
271 this->empty();
272 this->swap(other);
273 return *this;
274 }
275
276 protected:
277 // The table takes the bucket, or it is freed: when the key is in the
278 // table already, and when the insertion throws before the bucket is
279 // in a list
280 Key * insert_bucket(Bucket *bucket)
281 {
282 Bucket *ret_val = nullptr;
283 try
284 {
285 ret_val = static_cast<Bucket *>(this->Base::insert(bucket));
286 }
287 catch (...)
288 {
290 throw;
291 }
292
293 if (ret_val == nullptr) // is the key in the table?
294 { // yes! ==> free bucket
295 delete bucket;
296 return nullptr;
297 }
298
299 return &ret_val->get_key();
300 }
301
302 // As insert_bucket(), for a search that inserts the key if it is not
303 // in the table
304 std::pair<Key *, bool> search_or_insert_bucket(Bucket *bucket)
305 {
306 Bucket *ret_val = nullptr;
307 try
308 {
309 ret_val = static_cast<Bucket *>(this->Base::search_or_insert(bucket));
310 }
311 catch (...)
312 {
314 throw;
315 }
316
317 if (ret_val != bucket) // is the key in the table?
318 { // yes! ==> free bucket
319 delete bucket;
320 return {&ret_val->get_key(), true};
321 }
322
323 return {&ret_val->get_key(), false};
324 }
325
326 public:
329 Key * insert(const Key & key)
330 {
331 return insert_bucket(new Bucket(key));
332 }
333
334 Key * insert(Key && key)
335 {
336 return insert_bucket(new Bucket(std::forward<Key>(key)));
337 }
338
339 Key * search_or_insert(const Key & key)
340 {
341 return get<0>(search_or_insert_bucket(new Bucket(key)));
342 }
343
344 Key * search_or_insert(Key && key)
345 {
346 return get<0>(search_or_insert_bucket(new Bucket(std::forward<Key>(key))));
347 }
348
349 // Returns true if key is already in the table. Otherwise, inserts key and
350 // returns false.
351 std::pair<Key *, bool> contains_or_insert(const Key & key)
352 {
353 return search_or_insert_bucket(new Bucket(key));
354 }
355
356 std::pair<Key *, bool> contains_or_insert(Key && key)
357 {
358 return search_or_insert_bucket(new Bucket(std::forward<Key>(key)));
359 }
360
361 Key * add(const Key & key)
362 {
363 return insert_bucket(new Bucket(key));
364 }
365
366 Key * add(Key && key)
367 {
368 return insert_bucket(new Bucket(std::forward<Key>(key)));
369 }
370
371 Key * append(const Key & key)
372 {
373 return insert_bucket(new Bucket(key));
374 }
375
376 Key * append(Key && key)
377 {
378 return insert_bucket(new Bucket(std::forward<Key>(key)));
379 }
380
388 Key * search(const Key & key) noexcept
389 {
390 auto *bucket = static_cast<Bucket *>(this->Base::search(key));
391 return bucket != nullptr ? &bucket->get_key() : nullptr;
392 }
393
396 const Key * search(const Key & key) const noexcept
397 {
398 auto *bucket = static_cast<Bucket *>(this->Base::search(key));
399 return bucket != nullptr ? &bucket->get_key() : nullptr;
400 }
401
407 bool has(const Key & key) const noexcept
408 {
409 return this->Base::search(key) != nullptr;
410 }
411
412 const Key &find(const Key & key) const
413 {
414 auto *bucket = static_cast<Bucket *>(this->Base::search(key));
415 ah_domain_error_if(bucket == nullptr) << "Key not found in hash";
416
417 return bucket->get_key();
418 }
419
420 Key &find(const Key & key)
421 {
422 auto *bucket = static_cast<Bucket *>(this->Base::search(key));
423
424 ah_domain_error_if(bucket == nullptr) << "Key not found in hash";
425
426 return bucket->get_key();
427 }
428
429 protected:
430 static Bucket * key_to_bucket(Key *key)
431 {
432 return static_cast<Bucket *>(Dnode<Key>::data_to_node(*key));
433 }
434
435 public:
450 void remove(Key *key)
451 {
452 Bucket *bucket = key_to_bucket(key);
453 this->Base::remove(bucket);
454 delete bucket;
455 }
456
457 Key remove(const Key & key)
458 {
459 auto *bucket = static_cast<Bucket *>(this->Base::search(key));
460 ah_domain_error_if(bucket == nullptr) << "Key not found in hash table";
461
462 this->Base::remove(bucket);
463 auto ret_val = bucket->get_key();
464 delete bucket;
465 return ret_val;
466 }
467
468 class Iterator : public Base::Iterator
469 {
470 public:
471 using Item_Type = Key;
472
474
476 Iterator() noexcept = default;
477
479 Iterator(DynHashTable & table) : Base::Iterator(table) {}
480
482 Iterator(const DynHashTable &) = delete;
483
484 Key &get_curr_ne() noexcept
485 {
486 return this->Base::Iterator::get_curr_ne()->get_key();
487 }
488
489 const Key &get_curr_ne() const noexcept
490 {
491 return const_cast<Iterator *>(this)->get_curr_ne();
492 }
493
494 const Key &get_curr() const
495 {
496 return const_cast<Iterator *>(this)->get_curr();
497 }
498
499 Key &get_curr()
500 {
501 return this->Base::Iterator::get_curr()->get_key();
502 }
503
504 void del() { delete this->Base::Iterator::del(); }
505 };
506
509
510 const Key &get_first() const
511 {
512 return this->get_it().get_curr();
513 }
514
516 {
517 return this->get_it().get_curr();
518 }
519
520 const Key &get_last() const
521 {
522 auto it = this->get_it();
523 it.reset_last();
524 return it.get_curr();
525 }
526
527 Key &get_last()
528 {
529 auto it = this->get_it();
530 it.reset_last();
531 return it.get_curr();
532 }
533 };
534
539 template <typename Key, class Cmp = Aleph::equal_to<Key>>
540 requires EqualityComparator<Cmp, Key>
541 struct DynSetLhash : public DynHashTable<Key, LhashTable, Cmp>
542 {
544 using Base::Base;
546 };
547
552 template <typename Key, class Cmp = Aleph::equal_to<Key>>
554 struct DynSetLinHash : public DynHashTable<Key, LinearHashTable, Cmp>
555 {
557 using Base::Base;
559 };
560
565 template <typename Key, class Cmp = Aleph::equal_to<Key>>
567
572 template <typename Key, typename Data,
573 template <class, class> class HashTable = LhashTable,
577 : public DynHashTable<std::pair<Key, Data>,
578 HashTable, Dft_Pair_Cmp<Key, Data, Cmp>>
579 {
580 using Pair = std::pair<Key, Data>;
581
582 using Base =
584
585 using Bucket = typename Base::Bucket;
586
587 public:
588 using Hash_Fct = std::function<size_t(const Key &)>;
589 using Hash_Fct_Ptr = size_t (*)(const Key &);
590
591 private:
592 // The hash function of the keys, which the table applies to the first
593 // member of each pair: search() finds a pair by it, without building
594 // one. It is no_key_hash() after set_hash_fct() takes a function of
595 // pairs, which gives no hash of a key alone
597
598 // The hash of the keys while the table has a function of pairs. It
599 // sends the search by the key to the list 0, which finds a pair only
600 // if it is there; a miss then searches for a pair built from the key,
601 // as the table places them. The search by the key, which is what the
602 // maps do, so checks nothing while it finds pairs
603 static size_t no_key_hash(const Key &) noexcept { return 0; }
604
605 // Whether a search by a key that comes as K (const Key &, or Key for
606 // the searches that take an rvalue) cannot throw. The search by the
607 // hash of the keys builds nothing; after set_hash_fct() takes a
608 // function of pairs, the search builds one from the key and a Data
609 // built by default. A Data with no default constructor leaves only the
610 // first search: set_hash_fct() then takes no function of pairs. The
611 // members of the pair are checked apart, as libstdc++ 11 does not
612 // declare the constructors of std::pair noexcept
613 template <typename K>
614 static constexpr bool nothrow_search =
615 not std::is_default_constructible_v<Data> or
616 (std::is_nothrow_constructible_v<Key, K> and
617 std::is_nothrow_default_constructible_v<Data> and
618 std::is_nothrow_move_constructible_v<Data>);
619
620 // Whether the hash table can search by Key alone, without building a
621 // Pair (and so without requiring a default-constructible Data). It is a
622 // function, not a static data member: a function body is a
623 // complete-class context, where DynMapHashTable is complete, while the
624 // initializer of a static data member is not. MSVC evaluates such an
625 // initializer while the class is still incomplete, so the member access
626 // below failed there and the search fell back to Pair(key, Data()).
627 static constexpr bool has_search_with_custom_hash() noexcept
628 {
629 return requires(const DynMapHashTable *self, Hash_Fct_Ptr hf, const Key & k)
630 {
631 self->search_with_custom_hash(hf, k);
632 };
633 }
634
635 public:
636 static Data &get_data(const Key & key)
637 {
638 return key_to_pair<Key, Data>(&const_cast<Key &>(key))->second;
639 }
640
641 static const Key &get_key(Data *data_ptr)
642 {
643 return data_to_pair<Key, Data>(data_ptr)->first;
644 }
645
646 using Value_Type = Data;
647
648 // using Base::Base; // no more need. But I don't remember why I put it
649 using Base::insert; // in this way, insert with a pair is exported
650
652 using Iterator = typename Base::Iterator;
653
655 Hash_Fct_Ptr hash_fct = dft_hash_ptr_fct<Key>,
656 Cmp cmp = Cmp(),
657 float lower_alpha = hash_default_lower_alpha,
658 float upper_alpha = hash_default_upper_alpha)
659 : Base(len, hash_fct_detail::map_hash_of<Key, Data>(hash_fct),
660 Dft_Pair_Cmp<Key, Data, Cmp>(cmp), lower_alpha, upper_alpha),
661 original_hash_fct(hash_fct) {}
662
665
668 DynMapHashTable(DynMapHashTable &&) noexcept = default;
669
671 DynMapHashTable &operator =(const DynMapHashTable &) = default;
672
681 DynMapHashTable &operator =(DynMapHashTable && other) noexcept
682 {
683 Base::operator =(std::move(other));
684 std::swap(original_hash_fct, other.original_hash_fct);
685 return *this;
686 }
687
695 void swap(DynMapHashTable & other) noexcept
696 {
697 Base::swap(other);
698 std::swap(original_hash_fct, other.original_hash_fct);
699 }
700
715 {
716 Base::set_hash_fct(hash_fct_detail::map_hash_of<Key, Data>(fct));
717 original_hash_fct = fct;
718 }
719
735 void set_hash_fct(typename Base::Hash_Fct fct)
736 requires std::is_default_constructible_v<Data>
737 {
738 Base::set_hash_fct(std::move(fct));
740 }
741
744 requires std::is_default_constructible_v<Data>
745 {
746 Base::set_hash_fct(fct);
748 }
749
753 Pair * insert(const Key & key, const Data & data)
754 {
755 return this->insert_bucket(new typename Base::Bucket(Pair(key, data)));
756 }
757
758 Pair * insert(const Key & key, Data && data)
759 {
760 return this->insert_bucket
761 (new typename Base::Bucket(Pair(key, std::forward<Data>(data))));
762 }
763
764 Pair * insert(Key && key, Data && data)
765 {
766 return this->insert_bucket
767 (new typename Base::Bucket(Pair(std::forward<Key>(key), std::forward<Data>(data))));
768 }
769
770 Pair * insert(Key && key, const Data & data)
771 {
772 return this->insert_bucket
773 (new typename Base::Bucket(Pair(std::forward<Key>(key), data)));
774 }
775
776 private:
777
778 // The search for a pair built from key, as the table places them:
779 // after set_hash_fct() takes a function of pairs, which gives no hash
780 // of a key alone, or over a table that cannot search by the key alone.
781 // Out of line and cold, so that the search by the key, which builds
782 // nothing, stays as short as it was
783 template <typename K>
784 [[gnu::noinline, gnu::cold]]
785 const Pair * search_by_pair(K && key) const noexcept(nothrow_search<K>)
786 {
787 return Base::search(Pair(std::forward<K>(key), Data()));
788 }
789
790 public:
791
807 // Always inline, as the four searches: Clang leaves the search over a
808 // linear table out of line once it checks a miss, and then pays a
809 // call on every search
810 [[gnu::always_inline]]
811 Pair * search(const Key & key) noexcept(nothrow_search<const Key &>)
812 {
813 if constexpr (has_search_with_custom_hash())
814 {
815 auto *bucket = static_cast<Bucket *>(
816 this->search_with_custom_hash(original_hash_fct, key));
817 if (bucket != nullptr)
818 return &bucket->get_key();
819
820 if constexpr (std::is_default_constructible_v<Data>)
821 if (original_hash_fct == no_key_hash) [[unlikely]]
822 return const_cast<Pair *>(search_by_pair(key));
823 return nullptr;
824 }
825 else
826 return const_cast<Pair *>(search_by_pair(key));
827 }
828
830 [[gnu::always_inline]]
831 const Pair * search(const Key & key) const noexcept(nothrow_search<const Key &>)
832 {
833 if constexpr (has_search_with_custom_hash())
834 {
835 auto *bucket = static_cast<Bucket *>(
836 this->search_with_custom_hash(original_hash_fct, key));
837 if (bucket != nullptr)
838 return &bucket->get_key();
839
840 if constexpr (std::is_default_constructible_v<Data>)
841 if (original_hash_fct == no_key_hash) [[unlikely]]
842 return search_by_pair(key);
843 return nullptr;
844 }
845 else
846 return search_by_pair(key);
847 }
848
852 [[gnu::always_inline]]
853 Pair * search(Key && key) noexcept(nothrow_search<Key>)
854 {
855 if constexpr (has_search_with_custom_hash())
856 {
857 auto *bucket = static_cast<Bucket *>(
858 this->search_with_custom_hash(original_hash_fct, key));
859 if (bucket != nullptr)
860 return &bucket->get_key();
861
862 if constexpr (std::is_default_constructible_v<Data>)
863 if (original_hash_fct == no_key_hash) [[unlikely]]
864 return const_cast<Pair *>(search_by_pair(std::move(key)));
865 return nullptr;
866 }
867 else
868 return const_cast<Pair *>(search_by_pair(std::move(key)));
869 }
870
872 [[gnu::always_inline]]
873 const Pair * search(Key && key) const noexcept(nothrow_search<Key>)
874 {
875 if constexpr (has_search_with_custom_hash())
876 {
877 auto *bucket = static_cast<Bucket *>(
878 this->search_with_custom_hash(original_hash_fct, key));
879 if (bucket != nullptr)
880 return &bucket->get_key();
881
882 if constexpr (std::is_default_constructible_v<Data>)
883 if (original_hash_fct == no_key_hash) [[unlikely]]
884 return search_by_pair(std::move(key));
885 return nullptr;
886 }
887 else
888 return search_by_pair(std::move(key));
889 }
890
897 bool has(const Key & key) const noexcept(nothrow_search<const Key &>)
898 {
899 return search(key) != nullptr;
900 }
901
903 bool has(Key && key) const noexcept(nothrow_search<Key>)
904 {
905 return search(std::forward<Key>(key)) != nullptr;
906 }
907
914 bool contains(const Key & key) const noexcept(nothrow_search<const Key &>)
915 {
916 return has(key);
917 }
918
920 bool contains(Key && key) const noexcept(nothrow_search<Key>)
921 {
922 return has(std::forward<Key>(key));
923 }
924
933 Data &find(const Key & key)
934 {
935 auto *pair = search(key);
936 ah_domain_error_if(pair == nullptr) << "Key not found in hash table";
937 return pair->second;
938 }
939
940 const Data &find(const Key & key) const
941 {
942 auto *pair = search(key);
943 ah_domain_error_if(pair == nullptr) << "Key not found in hash table";
944 return pair->second;
945 }
946
953 Data &operator [](const Key & key)
954 {
955 return this->search_or_insert(Pair(key, Data()))->second;
956 }
957
958 const Data &operator [](const Key & key) const
959 {
960 return this->find(key);
961 }
962
963 Data &operator [](Key && key)
964 {
965 return this->search_or_insert(Pair(std::forward<Key>(key), Data()))->second;
966 }
967
968 const Data &operator [](Key && key) const
969 {
970 return this->find(std::forward<Key>(key));
971 }
972
975 void remove_by_data(Data & data)
976 {
977 Base::remove(data_to_pair<Key, Data>(&data));
978 }
979
992 Data remove(const Key & key)
993 {
994 auto *pair = search(key);
995 ah_domain_error_if(pair == nullptr) << "Key not found in hash table";
996
997 auto ret_val = std::move(pair->second);
998 auto *bucket = Base::key_to_bucket(pair);
999 this->Base::Base::remove(bucket);
1000 delete bucket;
1001 return ret_val;
1002 }
1003
1004 Data remove(Key && key)
1005 {
1006 return remove(key); // Forward to const& version
1007 }
1008
1010 {
1011 return this->template maps<Key>([](const auto & p) -> const Key & { return p.first; });
1012 }
1013
1021 {
1022 return this->template maps<Data>([](const auto & p) -> const Data & { return p.second; });
1023 }
1024
1026 {
1027 DynList<Data *> ret;
1028 for (Iterator it(*this); it.has_curr(); it.next_ne())
1029 ret.append(&it.get_curr().second);
1030 return ret;
1031 }
1032
1034 {
1035 DynList<Pair *> ret;
1036 for (Iterator it(*this); it.has_curr(); it.next_ne())
1037 ret.append(&it.get_curr());
1038 return ret;
1039 }
1040 };
1041
1046 template <typename Key, typename Data,
1047 class Cmp = Aleph::equal_to<Key>>
1049
1054 template <typename Key, typename Data,
1055 class Cmp = Aleph::equal_to<Key>>
1057
1058
1059 // Implementation coming from ahFunctional.H
1060
1061 template <typename T, template <typename> class Container>
1062 inline
1064 {
1065 DynSetLhash<T> table(c1);
1066 c2.for_each([&table](const T & item)
1067 {
1068 table.insert(item);
1069 });
1070 return table.keys();
1071 }
1072
1073 template <typename T, template <typename> class Container = DynList>
1074 inline
1076 {
1077 DynSetLhash<T> set1 = c1;
1078 DynSetLhash<T> set2 = c2;
1079 return set1.filter([&set2](const T & i) { return set2.contains(i); });
1080 }
1081
1082 template <typename T, template <typename> class Container>
1083 inline
1085 {
1086 return DynSetLhash<T>(c).keys();
1087 }
1088
1089 template <typename T, template <typename> class Container>
1090 inline
1092 {
1093 DynList<T> ret;
1094 DynSetLhash<T> table;
1095
1096 c.for_each([&table, &ret](const T & i)
1097 {
1098 auto *ptr = table.insert(i);
1099 if (ptr == nullptr)
1100 ret.append(i);
1101 });
1102
1103 return ret;
1104 }
1105
1106 template <typename T, template <typename> class Container>
1107 inline
1109 {
1111 DynSetLhash<T> table;
1112
1113 size_t i = 0;
1114 c.for_each([&table, &ret, &i](const T & item)
1115 {
1116 auto *ptr = table.insert(item);
1117 if (ptr == nullptr)
1118 ret.append(std::pair<T, size_t>(item, i));
1119 ++i;
1120 });
1121
1122 return ret;
1123 }
1124} // end namespace Aleph
1125
1126# endif // TPL_DYNSETHASH_H
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
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
Iterator traits and STL-compatible iterator wrappers.
Read-only view of an Aleph iterator: the iterator of a constant container.
static Dnode * data_to_node(T &data) noexcept
Given an reference to the data in the node, returns a pointer to the Dnode object that contains it.
Definition tpl_dnode.H:282
Iterator(const DynHashTable &)=delete
A constant table is iterated through Const_Iterator.
const Key & get_curr_ne() const noexcept
Iterator() noexcept=default
Default constructor creates an "end" iterator.
Self-adjusting dynamic hash table.
Key * add(const Key &key)
Key * search_or_insert(const Key &key)
Key * insert_bucket(Bucket *bucket)
Key * append(const Key &key)
std::pair< Key *, bool > search_or_insert_bucket(Bucket *bucket)
bool contains(const Key &key) const noexcept
Return true if the key exists in the table.
typename HashTable< Key, Cmp >::Bucket Bucket
DynHashTable(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr hash_fct=Aleph::dft_hash_ptr_fct< Key >, Cmp cmp=Cmp(), float lower_alpha=hash_default_lower_alpha, float upper_alpha=hash_default_upper_alpha)
Creates a dynamic linear hash table.
void copy(const DynHashTable &other)
DynHashTable(const DynHashTable &other)
Copy constructor.
HashTable< Key, Cmp > Base
void remove(Key *key)
Removes a key from the hash table using its pointer.
const Key & get_last() const
DynHashTable & operator=(const DynHashTable &other)
Copy assignment.
Key * insert(const Key &key)
Inserts key into the hash set.
static Bucket * key_to_bucket(Key *key)
std::pair< Key *, bool > contains_or_insert(Key &&key)
Key * insert(Key &&key)
const Key & find(const Key &key) const
Key remove(const Key &key)
typename Base::Hash_Fct Hash_Fct
Hash function type.
DynHashTable(size_t len, Hash_Fct hash_fct, Cmp cmp, float lower_alpha, float upper_alpha)
void clear()
Empties the container.
typename Base::Hash_Fct_Ptr Hash_Fct_Ptr
Key * add(Key &&key)
std::pair< Key *, bool > contains_or_insert(const Key &key)
Key * search(const Key &key) noexcept
Searches for a key in the hash table.
bool has(const Key &key) const noexcept
Checks if a key exists in the hash table.
bool is_empty() const noexcept
Checks if the container is empty.
const Key * search(const Key &key) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Key * append(Key &&key)
const Key & get_first() const
DynHashTable(DynHashTable &&other) noexcept
Move constructor over a table that a move of its own takes, as the one of chained lists (DynSetLhash)...
Key & find(const Key &key)
Key * search_or_insert(Key &&key)
DynHashTable(DynHashTable &&other) noexcept
Move constructor over a table that no move of its own takes, as the linear one (DynSetLinHash): build...
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
DynList< Data > values() const
Returns the values of the map.
const Pair * search_by_pair(K &&key) const noexcept(nothrow_search< K >)
Data & operator[](const Key &key)
Subscript operator for map access/insertion.
bool has(const Key &key) const noexcept(nothrow_search< const Key & >)
Checks if a key exists in the map.
void remove_by_data(Data &data)
Removes from the table the key whose pointer must be the result of a prior insertion or search.
Hash_Fct_Ptr original_hash_fct
size_t(*)(const Key &) Hash_Fct_Ptr
Pair * insert(Key &&key, Data &&data)
static const Key & get_key(Data *data_ptr)
DynList< Data * > values_ptr()
const Data & find(const Key &key) const
Pair * insert(Key &&key, const Data &data)
std::pair< Key, Data > Pair
Pair * insert(const Key &key, const Data &data)
Inserts into the hash map the pair (key, record) indexed by key.
static constexpr bool has_search_with_custom_hash() noexcept
DynMapHashTable(DynMapHashTable &&) noexcept=default
Move constructor: takes the pairs of the other map, which is left empty and keeps its configuration.
DynMapHashTable(const DynMapHashTable &)=default
Copy constructor: the same pairs and the same configuration.
Data remove(const Key &key)
Removes a key-value pair from the map and returns the value.
static size_t no_key_hash(const Key &) noexcept
typename Base::Bucket Bucket
void set_hash_fct(typename Base::Hash_Fct fct)
Sets a hash function of the pairs, as the table takes it.
void swap(DynMapHashTable &other) noexcept
Swaps this map with other in constant time.
typename Base::Iterator Iterator
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Searches for the pair whose key is key.
static constexpr bool nothrow_search
DynList< Pair * > items_ptr()
std::function< size_t(const Key &)> Hash_Fct
Pair * search(Key &&key) noexcept(nothrow_search< Key >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool contains(Key &&key) const noexcept(nothrow_search< Key >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynList< Key > keys() const
Data & find(const Key &key)
Finds and returns a reference to the value associated with key.
static Data & get_data(const Key &key)
bool contains(const Key &key) const noexcept(nothrow_search< const Key & >)
Checks if a key exists in the map; an alias for has().
void set_hash_fct(typename Base::Hash_Fct_Ptr fct)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool has(Key &&key) const noexcept(nothrow_search< Key >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
const Pair * search(Key &&key) const noexcept(nothrow_search< Key >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynMapHashTable(size_t len=Primes::DefaultPrime, Hash_Fct_Ptr hash_fct=dft_hash_ptr_fct< Key >, Cmp cmp=Cmp(), float lower_alpha=hash_default_lower_alpha, float upper_alpha=hash_default_upper_alpha)
const Pair * search(const Key &key) const noexcept(nothrow_search< const Key & >)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void set_hash_fct(Hash_Fct_Ptr fct)
Sets the hash function of the keys.
Pair * insert(const Key &key, Data &&data)
Equality test for sets.
Definition ah-dry.H:3026
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
and
Insert a new element into the container by constructing it from the given args.
Definition ah-dry.H:1273
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
Definition ah-dry.H:2356
Common sequential searching methods on containers.
Definition ah-dry.H:555
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
Equivalence relation constraint for equality comparators.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
void free_bucket_not_taken(Bucket *bucket) noexcept
Frees a bucket that a hash table of lists was given and did not take.
Definition hashDry.H:1336
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Itor unique(Itor __first, Itor __last, BinaryPredicate __binary_pred=BinaryPredicate())
Remove consecutive duplicates in place.
Definition ahAlgo.H:1058
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
DynList< T > intercept(const Container< T > &c1, const Container< T > &c2)
Return intersection of two containers as a DynList.
DynList< std::pair< T, size_t > > repeated_with_index(const Container< T > &c)
Return repeated elements paired with their occurrence count.
DynList< T > repeated(const Container< T > &c)
Return elements that appear more than once in the container.
const float hash_default_upper_alpha
Definition hash-dry.C:40
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
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
Prime number utilities for hash tables and mathematical operations.
Default comparator for pair types in hash maps.
Definition ahDry.H:206
Hash-based dynamic set (defined in tpl_dynSetHash.H).
DynHashTable< Key, LhashTable, Cmp > Base
DynHashTable< Key, LinearHashTable, Cmp > Base
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
Definition tpl_lhash.H:1008
Generic list of items stored in a container.
Definition ah-dry.H:2896
Aleph::DynList< T > keys() const
See items().
Definition ah-dry.H:2916
Generic traversal of the container through its iterator.
Definition ah-dry.H:363
static int * k
Lazy and scalable dynamic array implementation.
Dynamic hash table mapping keys to records with separate chaining.
Dynamic map with open hashing.
Linear hashing with chaining.