Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
hashDry.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
33# ifndef HASHDRY_H
34# define HASHDRY_H
35
57# include <limits>
58# include <type_traits>
59# include <utility>
60
61# include <aleph.H>
62# include <primes.H>
63# include <tpl_dynArray.H>
64# include <ah-errors.H>
65# include <ah-iterator.H>
66
68{
81 template <class Bucket>
82 [[nodiscard]] inline size_t checked_table_len(const size_t len)
83 {
84 ah_overflow_error_if(len > std::numeric_limits<size_t>::max() / sizeof(Bucket))
85 << "hash table: the bytes of " << len << " buckets do not fit in a size_t";
86 return len;
87 }
88
101 template <class Bucket>
102 [[nodiscard]] inline size_t prime_table_len(const size_t len)
103 {
104 return checked_table_len<Bucket>(Primes::next_prime(checked_table_len<Bucket>(len)));
105 }
106} // namespace hash_dry_detail
107
170template <class HashTbl, typename Key>
172{
176 HashTbl * me() { return static_cast<HashTbl*>(this); }
177
181 const HashTbl * const_me() const { return static_cast<const HashTbl*>(this); }
182
186 static constexpr bool nothrow_key_swap =
187 noexcept(std::swap(std::declval<Key &>(), std::declval<Key &>()));
188
202 template <class Bucket>
203 void store_copy(Bucket * bucket, const Key & key)
204 {
205 if constexpr (std::is_nothrow_copy_assignable_v<Key>)
206 bucket->key = key;
207 else
208 try
209 {
210 bucket->key = key;
211 }
212 catch (...)
213 {
214 me()->undo_allocate_bucket(bucket, key);
215 throw;
216 }
217 }
218
232 template <class Bucket>
233 void store_move(Bucket * bucket, Key & key)
234 {
235 if constexpr (nothrow_key_swap)
236 std::swap(bucket->key, key);
237 else
238 store_copy(bucket, key);
239 }
240
241protected:
242
255 void rebuild(const size_t new_len)
256 {
257 auto * new_table =
258 new typename HashTbl::Bucket[hash_dry_detail::checked_table_len<typename HashTbl::Bucket>(new_len)];
259 typename HashTbl::Bucket * old_table = me()->table;
260 const size_t old_len = me()->len;
261 const size_t old_N = me()->N;
262 const size_t old_deleted = me()->deleted_buckets;
263
264 me()->table = new_table;
265 me()->len = new_len;
266 me()->N = 0;
267 me()->deleted_buckets = 0;
268
269 try
271 for (size_t i = 0, c = 0; i < old_len and c < old_N; ++i)
272 if (old_table[i].status == HashTbl::BUSY)
273 {
274 Key & key = old_table[i].key;
275 auto bucket = me()->allocate_bucket(key);
276 if constexpr (nothrow_key_swap)
277 std::swap(bucket->key, key);
278 else
279 bucket->key = key;
280 ++c;
281 }
282 }
283 catch (...)
284 {
285 me()->table = old_table;
286 me()->len = old_len;
287 me()->N = old_N;
288 me()->deleted_buckets = old_deleted;
289 delete [] new_table;
290 throw;
291 }
292
293 assert(old_N == me()->N);
294
295 delete [] old_table;
296 }
297
309 [[nodiscard]] size_t rebuild_capacity() const
310 {
311 const HashTbl * t = const_me();
312 if (2.0f*t->N/t->len >= t->upper_alpha)
313 return Primes::next_prime(2 * t->len);
314 return t->len;
315 }
316
327 [[nodiscard]] size_t capacity_after_insertion() const
328 {
329 const HashTbl * t = const_me();
330 if (not t->with_resize
331 or 1.0f*(t->N + t->deleted_buckets)/t->len < t->upper_alpha)
332 return 0;
333 return rebuild_capacity();
334 }
335
346 void shrink_after_removal() noexcept
347 {
348 if (not me()->with_resize or me()->current_alpha() >= me()->lower_alpha)
349 return;
350
351 try
352 {
353 // resize() would refuse a size without room for the keys, and the
354 // key is already gone; a size that leaves them over the upper
355 // threshold would make the next insertion grow the table again.
356 const size_t new_len = Primes::next_prime(me()->len/2 + 1);
357 if (me()->N < new_len and 1.0f*me()->N/new_len < me()->upper_alpha)
358 resize(new_len);
359 }
360 catch (...)
361 {
362 // resize() leaves the table as it was
363 }
364 }
365
366public:
367
375 [[nodiscard]] constexpr float get_lower_alpha() const noexcept { return const_me()->lower_alpha; }
376
384 [[nodiscard]] constexpr float get_upper_alpha() const noexcept { return const_me()->upper_alpha; }
385
389 [[nodiscard]] constexpr bool get_with_resize() const noexcept { return const_me()->with_resize; }
390
400 void set_with_resize(const bool with_resize) noexcept { me()->with_resize = with_resize; }
401
413 void copy_from_table(const HashTbl & other)
414 {
415 assert(me()->N == 0 and me()->len >= other.N);
416 for (long i = 0, c = 0; c < other.N; ++i)
417 {
418 auto & bucket = other.table[i];
419 if (bucket.status != HashTbl::BUSY)
420 continue;
421
422 (void)me()->insert(bucket.key);
423 ++c;
424 }
425 assert(me()->N == other.N);
426 }
427
437 {
438 for (long i = 0; i < me()->len; ++i)
439 me()->table[i].reset();
440 me()->N = 0;
441 me()->deleted_buckets = 0;
442 }
443
467 {
468 if (me()->len == 0) [[unlikely]]
469 {
470 empty();
471 return true;
472 }
473
474 // Keep at least one EMPTY bucket as a sentinel.
475 if (me()->N + me()->deleted_buckets < me()->len - 1) [[likely]]
476 return true;
477
478 if (not me()->with_resize)
479 return me()->N < me()->len - 1; // its keys must not move
480
481 if (me()->N < me()->len - 1) // the buckets of removed keys take the room
483 else
484 me()->resize(Primes::next_prime(2 * me()->len));
485 return true;
486 }
487
507 [[nodiscard]] Key * insert(const Key & key)
508 {
510 return nullptr;
511
512 auto bucket = me()->allocate_bucket(key);
513 if (bucket == nullptr)
514 return nullptr;
515
516 store_copy(bucket, key);
517
518 return me()->test_resize(bucket, key);
519 }
520
541 [[nodiscard]] Key * insert(Key && key)
542 {
544 return nullptr;
545
546 auto bucket = me()->allocate_bucket(key);
547 if (bucket == nullptr)
548 return nullptr;
549
550 store_move(bucket, key);
551
552 return me()->test_resize(bucket, bucket->key);
553 }
554
575 [[nodiscard]] Key * search_or_insert(const Key & key)
576 {
578 return me()->search(key); // full: only a key already there is found
579
580 auto r = me()->hard_allocate_bucket(key);
581 auto bucket = get<0>(r);
582 if (bucket == nullptr)
583 return nullptr;
584 if (get<1>(r))
585 return &bucket->key;
586
587 store_copy(bucket, key);
588
589 return me()->test_resize(bucket, bucket->key);
590 }
591
614 [[nodiscard]] Key * search_or_insert(Key && key)
615 {
617 return me()->search(key); // full: only a key already there is found
618
619 auto r = me()->hard_allocate_bucket(key);
620 auto bucket = get<0>(r);
621 if (bucket == nullptr)
622 return nullptr;
623 if (get<1>(r))
624 return &bucket->key;
625
626 store_move(bucket, key);
627
628 return me()->test_resize(bucket, bucket->key);
629 }
630
661 [[nodiscard]] std::pair<Key*, bool> contains_or_insert(const Key & key)
662 {
664 {
665 Key * key_ptr = me()->search(key); // full: only a key already there
666 return { key_ptr, key_ptr != nullptr };
667 }
668
669 auto r = me()->hard_allocate_bucket(key);
670 auto bucket = get<0>(r);
671 if (bucket == nullptr)
672 return { nullptr, false };
673 if (get<1>(r))
674 return { &bucket->key, true };
675
676 store_copy(bucket, key);
677 auto key_ptr = me()->test_resize(bucket, bucket->key);
678
679 return { key_ptr, false };
680 }
681
704 [[nodiscard]] std::pair<Key*, bool> contains_or_insert(Key && key)
705 {
707 {
708 Key * key_ptr = me()->search(key); // full: only a key already there
709 return { key_ptr, key_ptr != nullptr };
710 }
711
712 auto r = me()->hard_allocate_bucket(key);
713 auto bucket = get<0>(r);
714 if (bucket == nullptr)
715 return { nullptr, false };
716 if (get<1>(r))
717 return { &bucket->key, true };
718
719 store_move(bucket, key);
720 auto key_ptr = me()->test_resize(bucket, bucket->key);
721
722 return { key_ptr, false };
723 }
724
733 [[nodiscard]] Key * append(const Key & key)
734 {
735 return this->insert(key);
736 }
737
746 [[nodiscard]] Key * append(Key && key)
747 {
748 return this->insert(std::move(key));
749 }
750
758 [[nodiscard]] constexpr bool has(const Key & key) const noexcept
759 {
760 return const_me()->search(key) != nullptr;
761 }
762
769 [[nodiscard]] constexpr bool contains(const Key & key) const noexcept { return has(key); }
770
782 [[nodiscard]] Key & find(const Key & key)
783 {
784 auto key_ptr = me()->search(key);
785 ah_domain_error_if(key_ptr == nullptr)
786 << "Key not found in hash";
787
788 return *key_ptr;
789 }
790
800 [[nodiscard]] const Key & find(const Key & key) const
801 {
802 return const_cast<OhashCommon*>(this)->find(key);
803 }
804
813 const Key & operator [] (const Key & key) const
814 {
815 return find(key);
816 }
817
830 const Key & operator [] (const Key & key)
831 {
832 Key * key_ptr = search_or_insert(key);
833 ah_overflow_error_if(key_ptr == nullptr)
834 << "Hash table is full and does not resize itself";
835
836 return *key_ptr;
837 }
838
860 void remove_ptr(Key * key)
861 {
862 auto bucket = HashTbl::key_to_bucket(key);
863 ah_invalid_argument_if(not me()->is_valid_bucket(bucket))
864 << "remove_ptr(): key does not point into the table";
865 ah_domain_error_if(bucket->status != HashTbl::BUSY)
866 << "remove_ptr(): the key of the bucket was removed";
867 me()->deallocate_bucket(bucket);
869 }
870
888 size_t resize(size_t new_size)
889 {
890 if (new_size == 0 or new_size == me()->len)
891 return me()->len;
892
893 // new_size must leave at least one EMPTY bucket as sentinel
894 ah_overflow_error_if(me()->N >= new_size)
895 << "New size is not enough for current number of entries";
896
897 rebuild(new_size);
898
899 return me()->len;
900 }
901
915 void rehash()
916 {
917 if (me()->len == 0)
918 return; // the storage was taken by a move: nothing to rebuild
919
920 rebuild(me()->len);
921 }
922
933 void empty()
934 {
935 auto * new_table = new typename HashTbl::Bucket [Primes::DefaultPrime];
936 delete [] me()->table;
937 me()->table = new_table;
938 me()->N = 0;
939 me()->deleted_buckets = 0;
940 me()->len = Primes::DefaultPrime;
941 }
942
949 void clear() { empty(); }
950
954 [[nodiscard]] constexpr size_t size() const noexcept { return const_me()->N; }
955
959 [[nodiscard]] constexpr bool is_empty() const noexcept { return const_me()->N == 0; }
960
964 [[nodiscard]] constexpr size_t capacity() const noexcept { return const_me()->len; }
965
989 {
990 HashTbl * table_ptr = nullptr;
991 long curr_idx = 0;
992 long ordinal = 0;
993
997 [[nodiscard]] bool check() const noexcept
998 {
999 assert(table_ptr != nullptr);
1000 return curr_idx >= 0 and curr_idx < static_cast<long>(table_ptr->len) and
1001 ordinal >= -1 and ordinal <= static_cast<long>(table_ptr->size());
1002 }
1003
1008 {
1009 assert(check());
1010 if (++ordinal == table_ptr->size())
1011 return;
1012
1013 while (table_ptr->table[++curr_idx].status != HashTbl::BUSY);
1014 }
1015
1020 {
1021 assert(check());
1022 if (--ordinal == -1)
1023 return;
1024
1025 while (table_ptr->table[--curr_idx].status != HashTbl::BUSY);
1026 }
1027
1032 {
1034 << "Iterator next() overflow";
1036 }
1037
1042 {
1044 << "Iterator next() underflow";
1046 }
1047
1048 public:
1049
1055 void reset_first() noexcept
1056 {
1057 assert(table_ptr != nullptr);
1058 curr_idx = 0;
1059 ordinal = -1;
1060 if (table_ptr->is_empty())
1061 {
1062 curr_idx = table_ptr->len;
1063 ordinal = 0;
1064 return;
1065 }
1066
1067 while (table_ptr->table[curr_idx].status != HashTbl::BUSY)
1068 ++curr_idx;
1069
1070 ordinal = 0;
1071 assert(check());
1072 }
1073
1079 void reset_last() noexcept
1080 {
1081 assert(table_ptr != nullptr);
1082 if (table_ptr->is_empty())
1083 {
1084 curr_idx = -1;
1085 ordinal = -1;
1086 return;
1087 }
1088
1089 curr_idx = table_ptr->len - 1;
1090 while (table_ptr->table[curr_idx].status != HashTbl::BUSY)
1091 --curr_idx;
1092
1093 ordinal = table_ptr->size() - 1;
1094 assert(check());
1095 }
1096
1101 void end() noexcept
1102 {
1103 put_itor_at_the_end(*this);
1104 }
1105
1109 [[nodiscard]] constexpr long get_pos() const noexcept { return ordinal; }
1110
1115 Key & get_curr_ne() noexcept
1116 {
1117 assert(table_ptr->table[curr_idx].status == HashTbl::BUSY);
1118 return table_ptr->table[curr_idx].key;
1119 }
1120
1125 const Key & get_curr_ne() const noexcept
1126 {
1127 return const_cast<Iterator*>(this)->get_curr_ne();
1128 }
1129
1135 Key & get_curr()
1136 {
1138 << "O hash::Iterator next() overflow";
1139
1141 << "O hash::Iterator next() underflow";
1142
1143 assert(check());
1144
1145 return get_curr_ne();
1146 }
1147
1153 const Key & get_curr() const
1154 {
1155 return const_cast<Iterator*>(this)->get_curr();
1156 }
1157
1161 bool has_curr() const noexcept
1162 {
1163 assert(table_ptr != nullptr);
1164 if (table_ptr->is_empty())
1165 return false;
1166 return ordinal >= 0 and ordinal < table_ptr->size();
1167 }
1168
1172 [[nodiscard]] constexpr bool is_last() const noexcept { return ordinal == table_ptr->size() - 1; }
1173
1178
1183
1188
1193
1196 Iterator() noexcept = default;
1197
1201 Iterator(HashTbl & table) noexcept
1202 : table_ptr(&table), curr_idx(0), ordinal(-1)
1203 {
1204 reset_first();
1205 }
1206
1208 Iterator(const HashTbl &) = delete;
1209
1221 void del()
1222 {
1224 << "Overflow in del() of iterator";
1225
1226 table_ptr->deallocate_bucket(&table_ptr->table[curr_idx]);
1227 // the last entry has no next one: looking for it went past the end
1228 if (ordinal == static_cast<long>(table_ptr->size()))
1229 return;
1230
1231 while (table_ptr->table[++curr_idx].status != HashTbl::BUSY);
1232 }
1233 };
1234
1237
1247 [[nodiscard]] DynList<Key> keys() const
1248 {
1249 return const_me()->template maps<Key> ([] (const Key & key) { return key; });
1250 }
1251
1256 [[nodiscard]] auto items() const { return keys(); }
1257
1264 struct Stats
1265 {
1266 size_t num_busy = 0;
1267 size_t num_deleted = 0;
1268 size_t num_empty = 0;
1270 float avg = 0;
1271 float var = 0;
1272 size_t max_len = 0;
1273
1275 Stats() : max_len(std::numeric_limits<size_t>::min())
1276 {
1277 assert(lens.size() == 0);
1278 }
1279 };
1280
1288 void print_stats(const Stats & stats) const
1289 {
1290 std::cout << "M = " << this->capacity() << '\n'
1291 << "N = " << this->size() << '\n'
1292 << "busy slots = " << stats.num_busy << '\n'
1293 << "deleted slots = " << stats.num_deleted << '\n'
1294 << "empty slots = " << stats.num_empty << '\n'
1295 << "alpha = " << const_me()->current_alpha() << '\n'
1296 << "max length = " << stats.max_len << '\n';
1297 for (size_t i = 0; i < stats.lens.size(); ++i)
1298 std::cout << " " << i << " = " << stats.lens(i) << '\n';
1299 }
1300
1310 [[nodiscard]] constexpr float current_alpha() const noexcept
1311 {
1312 if (const_me()->len == 0) [[unlikely]]
1313 return 0;
1314 return 1.0f*const_me()->N/const_me()->len;
1315 }
1316};
1317
1318
1319namespace hash_dry_detail
1320{
1335 template <class Bucket>
1336 inline void free_bucket_not_taken(Bucket * bucket) noexcept
1337 {
1338 if (static_cast<Aleph::Dlink *>(bucket)->is_empty())
1339 delete bucket;
1340 }
1341
1356 template <class Cmp>
1357 inline void swap_compare(Cmp & a, Cmp & b) noexcept
1358 {
1359 if constexpr (std::is_move_constructible_v<Cmp>
1360 and std::is_move_assignable_v<Cmp>)
1361 std::swap(a, b);
1362 else
1363 {
1364 Cmp t(a);
1365 a = b;
1366 b = t;
1367 }
1368 }
1369} // namespace hash_dry_detail
1370
1390template <class HashTbl>
1392{
1396 HashTbl * me() { return static_cast<HashTbl*>(this); }
1397
1401 const HashTbl * const_me() const { return static_cast<const HashTbl*>(this); }
1402
1403public:
1404
1410 struct Stats
1411 {
1412 float avg;
1413 float var;
1415 };
1416
1417private:
1418
1426 static void update_stat_len(DynArray<size_t> & lens, size_t i)
1427 {
1428 lens.touch(i) += 1;
1429 }
1430
1438 size_t bucket_length(const size_t i) const
1439 {
1440 const auto & table = const_me()->table;
1441 Dlink * list;
1442 if constexpr (requires { table.test(i); })
1443 {
1444 list = table.test(i);
1445 if (list == nullptr)
1446 return 0;
1447 }
1448 else
1449 list = &table[i];
1450
1451 size_t count = 0;
1452 for (typename HashTbl::BucketItor it(*list); it.has_curr(); it.next())
1453 ++count;
1454 return count;
1455 }
1456
1457public:
1458
1470 Stats stats() const
1471 {
1472 DynArray<size_t> lens;
1473 for (size_t i = 0; i < const_me()->capacity(); ++i)
1475 lens.reserve(lens.size()); // no unwritten gaps: every length is readable
1476
1477 float avg = 0, sum = 0;
1478 for (size_t i = 0; i < lens.size(); ++i)
1479 {
1480 avg += lens(i)*i;
1481 sum += lens(i);
1482 }
1483 if (sum > 0) // a table without lists, as a moved-from one, has no chains
1484 avg /= sum;
1485
1486 float var = 0;
1487 for (size_t i = 0; i < lens.size(); ++i)
1488 {
1489 float s = i - avg;
1490 var += lens(i)*s*s;
1491 }
1492 if (sum > 0)
1493 var /= sum;
1494
1495 Stats stats;
1496 stats.avg = avg;
1497 stats.var = var;
1498 std::swap(lens, stats.lens);
1499
1500 return stats;
1501 }
1502
1511 void print_stats(const Stats & stats) const
1512 {
1513 std::cout << "M = " << const_me()->capacity() << '\n'
1514 << "N = " << const_me()->size() << '\n'
1515 << "busy slots = " << const_me()->busy_slots_counter << '\n'
1516 << "Average = " << stats.avg << '\n'
1517 << "Desv = " << sqrt(stats.var) << '\n'
1518 << "alpha = " << const_me()->current_alpha()
1519 << '\n';
1520 for (size_t i = 0; i < stats.lens.size(); ++i)
1521 std::cout << " " << i << " = " << stats.lens(i) << '\n';
1522 }
1523
1532 void set_upper_alpha(float _upper_alpha)
1533 {
1534 ah_domain_error_if(_upper_alpha <= me()->lower_alpha)
1535 << "upper_alpha lower than lower_alpha";
1536
1537 me()->upper_alpha = _upper_alpha;
1538 }
1539
1548 void set_lower_alpha(float _lower_alpha)
1549 {
1550 ah_domain_error_if(_lower_alpha >= me()->upper_alpha)
1551 << "lower_alpha greater than upper_alpha";
1552
1553 me()->lower_alpha = _lower_alpha;
1554 }
1555
1559 [[nodiscard]] constexpr float get_lower_alpha() const noexcept { return const_me()->lower_alpha; }
1560
1564 [[nodiscard]] constexpr float get_upper_alpha() const noexcept { return const_me()->upper_alpha; }
1565
1569 [[nodiscard]] constexpr bool get_with_resize() const noexcept { return const_me()->with_resize; }
1570
1580 void set_with_resize(const bool with_resize) noexcept { me()->with_resize = with_resize; }
1581};
1582
1583
1584# endif // HASHDRY_H
Exception handling system with formatted messages for Aleph-w.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:787
STL-compatible iterator adapters for Aleph containers.
Core header for the Aleph-w library.
void put_itor_at_the_end(Itor &it) noexcept
Definition aleph.H:54
Read-only view of an Aleph iterator: the iterator of a constant container.
T & touch(const size_t i)
Touch the entry i.
size_t size() const noexcept
Return the current dimension of array.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
CRTP mixin providing statistics and configuration for chained hash tables.
Definition hashDry.H:1392
size_t bucket_length(const size_t i) const
Returns the length of the collision chain of bucket i.
Definition hashDry.H:1438
constexpr float get_upper_alpha() const noexcept
Returns the upper load factor threshold.
Definition hashDry.H:1564
void set_upper_alpha(float _upper_alpha)
Sets the upper load factor threshold.
Definition hashDry.H:1532
HashTbl * me()
Returns a pointer to the derived class (CRTP pattern).
Definition hashDry.H:1396
void set_with_resize(const bool with_resize) noexcept
Turns the automatic resizing on or off.
Definition hashDry.H:1580
void set_lower_alpha(float _lower_alpha)
Sets the lower load factor threshold.
Definition hashDry.H:1548
Stats stats() const
Computes statistics about chain length distribution.
Definition hashDry.H:1470
constexpr bool get_with_resize() const noexcept
Returns whether the table resizes itself.
Definition hashDry.H:1569
static void update_stat_len(DynArray< size_t > &lens, size_t i)
Updates chain length histogram.
Definition hashDry.H:1426
void print_stats(const Stats &stats) const
Prints statistics to standard output.
Definition hashDry.H:1511
const HashTbl * const_me() const
Returns a const pointer to the derived class (CRTP pattern).
Definition hashDry.H:1401
constexpr float get_lower_alpha() const noexcept
Returns the lower load factor threshold.
Definition hashDry.H:1559
Bidirectional iterator for traversing hash table entries.
Definition hashDry.H:989
Key & get_curr()
Returns a reference to the current entry with bounds checking.
Definition hashDry.H:1135
Iterator() noexcept=default
Default constructor creates an "end" iterator.
bool check() const noexcept
Validates iterator state (debug only).
Definition hashDry.H:997
bool has_curr() const noexcept
Checks if the iterator is at a valid position.
Definition hashDry.H:1161
constexpr long get_pos() const noexcept
Returns the current logical position.
Definition hashDry.H:1109
const Key & get_curr() const
Returns a const reference to the current entry with bounds checking.
Definition hashDry.H:1153
Iterator(const HashTbl &)=delete
A constant table is iterated through Const_Iterator.
void del()
Deletes the current entry and advances to the next.
Definition hashDry.H:1221
void end() noexcept
Positions the iterator at the end (past-the-last element).
Definition hashDry.H:1101
HashTbl * table_ptr
Pointer to the hash table being iterated.
Definition hashDry.H:990
void prev()
Moves to the previous entry with bounds checking.
Definition hashDry.H:1192
void next()
Advances to the next entry with bounds checking.
Definition hashDry.H:1177
long curr_idx
Current bucket index.
Definition hashDry.H:991
const Key & get_curr_ne() const noexcept
Returns a const reference to the current entry without checking.
Definition hashDry.H:1125
void locate_next_available_entry()
Advances to next entry with bounds checking.
Definition hashDry.H:1031
void prev_ne()
Moves to the previous entry without bounds checking.
Definition hashDry.H:1187
void locate_prev_available_entry_ne()
Moves to previous entry without bounds checking.
Definition hashDry.H:1019
Key & get_curr_ne() noexcept
Returns a reference to the current entry without checking.
Definition hashDry.H:1115
void reset_first() noexcept
Positions the iterator at the first entry.
Definition hashDry.H:1055
void reset_last() noexcept
Positions the iterator at the last entry.
Definition hashDry.H:1079
void locate_prev_available_entry()
Moves to previous entry with bounds checking.
Definition hashDry.H:1041
long ordinal
Logical position (0-based count of visited entries).
Definition hashDry.H:992
void next_ne() noexcept
Advances to the next entry without bounds checking.
Definition hashDry.H:1182
void locate_next_available_entry_ne() noexcept
Advances to next entry without bounds checking.
Definition hashDry.H:1007
constexpr bool is_last() const noexcept
Checks if the iterator is at the last entry.
Definition hashDry.H:1172
CRTP mixin providing common operations for open addressing hash tables.
Definition hashDry.H:172
HashTbl * me()
Returns a pointer to the derived class (CRTP pattern).
Definition hashDry.H:176
size_t capacity_after_insertion() const
Capacity that an insertion leaves the table with, or 0 if it keeps it.
Definition hashDry.H:327
Key * append(const Key &key)
Alias for insert() (copy version).
Definition hashDry.H:733
void rebuild(const size_t new_len)
Places the keys of the table in new_len new buckets.
Definition hashDry.H:255
std::pair< Key *, bool > contains_or_insert(const Key &key)
Searches for a key, inserting if not found, with existence flag (copy version).
Definition hashDry.H:661
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
std::pair< Key *, bool > contains_or_insert(Key &&key)
Searches for a key, inserting if not found, with existence flag (move version).
Definition hashDry.H:704
Key * search_or_insert(const Key &key)
Searches for a key, inserting it if not found (copy version).
Definition hashDry.H:575
constexpr size_t capacity() const noexcept
Returns the current capacity of the table.
Definition hashDry.H:964
Key * search_or_insert(Key &&key)
Searches for a key, inserting it if not found (move version).
Definition hashDry.H:614
void clear()
Empties the container.
Definition hashDry.H:949
size_t resize(size_t new_size)
Resizes the hash table to a new capacity.
Definition hashDry.H:888
void store_copy(Bucket *bucket, const Key &key)
Stores a copy of a key in the bucket reserved for it.
Definition hashDry.H:203
static constexpr bool nothrow_key_swap
Whether two keys can be swapped without throwing.
Definition hashDry.H:186
void store_move(Bucket *bucket, Key &key)
Moves a key into the bucket reserved for it.
Definition hashDry.H:233
void clean_table()
Removes all entries from the table without deallocating storage.
Definition hashDry.H:436
constexpr float get_upper_alpha() const noexcept
Returns the upper load factor threshold for automatic growing.
Definition hashDry.H:384
const Key & find(const Key &key) const
Finds a key and returns a const reference to it.
Definition hashDry.H:800
void set_with_resize(const bool with_resize) noexcept
Turns the automatic resizing on or off.
Definition hashDry.H:400
void empty()
Clears all entries and resets to default capacity.
Definition hashDry.H:933
constexpr bool is_empty() const noexcept
Checks if the table is empty.
Definition hashDry.H:959
void copy_from_table(const HashTbl &other)
Copies all entries from another hash table.
Definition hashDry.H:413
void print_stats(const Stats &stats) const
Prints statistics to standard output.
Definition hashDry.H:1288
void remove_ptr(Key *key)
Removes an entry by pointer.
Definition hashDry.H:860
size_t rebuild_capacity() const
Capacity to rebuild a table that resizes itself with, when its buckets in use are too many.
Definition hashDry.H:309
DynList< Key > keys() const
Returns a list containing all keys in the table.
Definition hashDry.H:1247
Key * insert(Key &&key)
Inserts a key into the hash table (move version).
Definition hashDry.H:541
const Key & operator[](const Key &key) const
Subscript operator for const access.
Definition hashDry.H:813
constexpr float get_lower_alpha() const noexcept
Returns the lower load factor threshold for automatic shrinking.
Definition hashDry.H:375
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Definition hashDry.H:507
constexpr bool has(const Key &key) const noexcept
Checks if a key exists in the table.
Definition hashDry.H:758
constexpr bool contains(const Key &key) const noexcept
Alias for has().
Definition hashDry.H:769
Key & find(const Key &key)
Finds a key and returns a reference to it.
Definition hashDry.H:782
void rehash()
Rebuilds the hash table with the same capacity.
Definition hashDry.H:915
bool check_resize_before_insert()
Makes room for one more key, if the table can take it.
Definition hashDry.H:466
void shrink_after_removal() noexcept
Shrinks a table that resizes itself, after a removal took its load factor under the lower threshold.
Definition hashDry.H:346
Key * append(Key &&key)
Alias for insert() (move version).
Definition hashDry.H:746
constexpr bool get_with_resize() const noexcept
Returns whether the table resizes itself.
Definition hashDry.H:389
auto items() const
Alias for keys().
Definition hashDry.H:1256
constexpr float current_alpha() const noexcept
Returns the current load factor (alpha).
Definition hashDry.H:1310
const HashTbl * const_me() const
Returns a const pointer to the derived class (CRTP pattern).
Definition hashDry.H:181
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sqrt_function > > sqrt(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4136
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4189
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
void swap_compare(Cmp &a, Cmp &b) noexcept
Swaps the comparators of two hash tables.
Definition hashDry.H:1357
Table::Bucket Bucket
Definition lin-hash.cc:57
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Definition primes.C:381
unsigned long next_prime(unsigned long n)
Find a prime >= n in the database, for hash table sizing.
Definition primes.C:383
size_t prime_table_len(const size_t len)
The length of a table for a request of len buckets: the next prime.
Definition hashDry.H:102
size_t checked_table_len(const size_t len)
Checks that the bytes of a table of buckets fit in a size_t.
Definition hashDry.H:82
STL namespace.
Prime number utilities for hash tables and mathematical operations.
Statistics about chain length distribution.
Definition hashDry.H:1411
DynArray< size_t > lens
Distribution: lens[i] = number of chains with length i.
Definition hashDry.H:1414
float avg
Average chain length.
Definition hashDry.H:1412
float var
Variance of chain lengths.
Definition hashDry.H:1413
Statistics about hash table performance.
Definition hashDry.H:1265
size_t num_deleted
Number of buckets marked as deleted.
Definition hashDry.H:1267
size_t num_empty
Number of empty buckets.
Definition hashDry.H:1268
DynArray< size_t > lens
Distribution of probe sequence lengths.
Definition hashDry.H:1269
float avg
Average probe sequence length.
Definition hashDry.H:1270
Stats()
Default constructor initializing max_len to minimum.
Definition hashDry.H:1275
size_t num_busy
Number of occupied buckets.
Definition hashDry.H:1266
float var
Variance of probe sequence lengths.
Definition hashDry.H:1271
size_t max_len
Maximum probe sequence length.
Definition hashDry.H:1272
int keys[]
Gsl_Compatible_Rng r
Lazy and scalable dynamic array implementation.