37# include <gtest/gtest.h>
56# include <type_traits>
61using namespace testing;
66constexpr size_t N = 1000;
68template <
class HashTbl>
81template <
class HashTbl>
96 auto it =
tbl.get_it();
97 ASSERT_FALSE(it.has_curr());
98 ASSERT_THROW(it.get_curr(), std::overflow_error);
99 ASSERT_THROW(it.next(), std::overflow_error);
104 TypeParam
tbl = this->
tbl;
106 ASSERT_EQ(
tbl.size(),
N);
108 auto it =
tbl.get_it();
109 EXPECT_NO_THROW(it.get_curr());
110 EXPECT_NO_THROW(it.next());
111 EXPECT_NO_THROW(it.reset_first());
113 for (; it.has_curr(); it.next_ne())
115 EXPECT_FALSE(it.has_curr());
116 auto ll =
l.
maps<
size_t>([](
auto &p)
117 {
return p.first; });
118 EXPECT_EQ(
sort(ll), this->items);
121 EXPECT_NO_THROW(it.reset_last());
122 for (; it.has_curr(); it.prev_ne())
124 ll =
l.
maps<
size_t>([](
auto &p)
125 {
return p.first; });
126 EXPECT_EQ(
sort(ll), this->items);
135 Types<MapODhash<size_t, string>,
150 Aleph::dft_hash_fct<int>,
151 Aleph::dft_hash_fct<int>,
157 EXPECT_TRUE(
tbl.is_empty());
158 ASSERT_NE(
tbl.insert(42),
nullptr);
159 ASSERT_NE(
tbl.insert(7),
nullptr);
160 EXPECT_NE(
tbl.search(42),
nullptr);
162 auto stats =
tbl.stats();
163 EXPECT_EQ(stats.num_busy,
static_cast<size_t>(2));
170 ASSERT_NE(ptr,
nullptr);
172 auto *bucket =
decltype(
tbl)::key_to_bucket(ptr);
173 ASSERT_NE(bucket,
nullptr);
174 EXPECT_EQ(bucket->key, 5);
176 EXPECT_NO_THROW(
tbl.remove(5));
183 EXPECT_THROW(
tbl.remove(2), std::domain_error);
190 auto *p1 =
tbl.insert(10);
191 ASSERT_NE(p1,
nullptr);
192 auto *p2 =
tbl.insert(10);
193 EXPECT_EQ(p2,
nullptr);
194 EXPECT_EQ(
tbl.size(),
static_cast<size_t>(1));
201 auto *p =
tbl.insert(5);
202 ASSERT_NE(p,
nullptr);
204 EXPECT_NO_THROW(
tbl.remove(5));
205 EXPECT_EQ(
tbl.size(),
static_cast<size_t>(0));
206 EXPECT_THROW(
tbl.remove(5), std::domain_error);
213 for (
int i = 0; i < 4; ++i)
214 ASSERT_NE(
tbl.insert(i),
nullptr);
215 EXPECT_EQ(
tbl.insert(4),
nullptr);
216 EXPECT_EQ(
tbl.size(),
static_cast<size_t>(4));
217 EXPECT_EQ(
tbl.search(42),
nullptr);
222 auto constant_hash = [] (
const int &)
noexcept ->
size_t {
return 0u; };
226 auto *p1 =
tbl.insert(1);
227 auto *p2 =
tbl.insert(2);
228 auto *p3 =
tbl.insert(3);
229 ASSERT_NE(p1,
nullptr);
230 ASSERT_NE(p2,
nullptr);
231 ASSERT_NE(p3,
nullptr);
235 auto stats =
tbl.stats();
236 EXPECT_EQ(stats.num_busy,
static_cast<size_t>(2));
237 EXPECT_EQ(stats.num_deleted,
static_cast<size_t>(1));
238 EXPECT_EQ(stats.num_busy + stats.num_deleted + stats.num_empty,
239 static_cast<size_t>(
tbl.capacity()));
240 EXPECT_GE(stats.max_len,
static_cast<size_t>(1));
246 Aleph::dft_hash_fct<int>,
251 for (
int i = 0; i < 10; ++i)
252 ASSERT_NE(
tbl.insert(i),
nullptr);
254 for (
int i = 0; i < 10; ++i)
255 EXPECT_NE(
tbl.search(i),
nullptr);
256 EXPECT_EQ(
tbl.size(),
static_cast<size_t>(10));
261 auto const_hash = [](int) {
return static_cast<size_t>(0); };
264 for (
int i = 0; i < 4; ++i)
265 ASSERT_NE(
tbl.insert(i),
nullptr);
266 EXPECT_EQ(
tbl.insert(4),
nullptr);
268 for (
int i = 0; i < 4; ++i)
269 EXPECT_NE(
tbl.search(i),
nullptr);
271 auto stats =
tbl.stats();
272 EXPECT_EQ(stats.num_busy,
static_cast<size_t>(4));
273 EXPECT_GE(stats.max_len,
static_cast<size_t>(4));
279 Aleph::dft_hash_fct<int>,
284 for (
int i = 0; i < 6; ++i)
285 ASSERT_NE(
tbl.insert(i),
nullptr);
292 for (
int i = 10; i < 16; ++i)
293 ASSERT_NE(
tbl.insert(i),
nullptr);
296 for (
int i : {0, 2, 4, 5, 10, 11, 12, 13, 14, 15})
297 EXPECT_NE(
tbl.search(i),
nullptr);
303 auto *p1 =
tbl.search_or_insert(1);
304 ASSERT_NE(p1,
nullptr);
305 auto *p2 =
tbl.search_or_insert(1);
307 EXPECT_EQ(
tbl.size(),
static_cast<size_t>(1));
320 EXPECT_NE(b.
search(1),
nullptr);
321 EXPECT_NE(b.
search(2),
nullptr);
325 EXPECT_EQ(c.
size(),
static_cast<size_t>(2));
326 EXPECT_NE(c.
search(1),
nullptr);
329 EXPECT_NE(b.
search(5),
nullptr);
336 EXPECT_NE(a.
search(99),
nullptr);
337 EXPECT_EQ(d.
search(99),
nullptr);
343 EXPECT_NE(e.
search(1),
nullptr);
344 EXPECT_NE(e.
search(2),
nullptr);
345 EXPECT_EQ(e.
size(),
static_cast<size_t>(2));
351 EXPECT_NE(f.
search(1),
nullptr);
352 EXPECT_NE(f.
search(2),
nullptr);
359 auto stats_empty = tbl_empty.
stats();
360 EXPECT_EQ(stats_empty.num_busy,
static_cast<size_t>(0));
361 EXPECT_EQ(stats_empty.num_deleted,
static_cast<size_t>(0));
364 auto const_hash = [](int) {
return static_cast<size_t>(0); };
367 for (
int i = 0; i < 5; ++i)
368 (
void) tbl_full.
insert(i);
369 auto stats_full = tbl_full.
stats();
370 EXPECT_EQ(stats_full.num_busy,
static_cast<size_t>(4));
371 EXPECT_EQ(stats_full.num_empty,
static_cast<size_t>(1));
372 EXPECT_EQ(stats_full.num_deleted,
static_cast<size_t>(0));
373 EXPECT_FALSE(std::isnan(stats_full.avg));
374 EXPECT_FALSE(std::isnan(stats_full.var));
383 EXPECT_EQ(
tbl.size(),
static_cast<size_t>(0));
384 EXPECT_EQ(
tbl.search(1),
nullptr);
391 ASSERT_NE(
tbl.insert(1),
nullptr);
392 ASSERT_NE(
tbl.insert(2),
nullptr);
394 EXPECT_EQ(
tbl.insert(3),
nullptr);
396 EXPECT_EQ(
tbl.insert(2),
nullptr);
397 EXPECT_EQ(
tbl.size(),
static_cast<size_t>(2));
404 Aleph::dft_hash_fct<int>,
409 const auto cap0 =
tbl.capacity();
410 for (
int i = 0; i < 10; ++i)
412 EXPECT_GE(
tbl.capacity(), cap0);
413 for (
int i = 0; i < 10; ++i)
414 EXPECT_NE(
tbl.search(i),
nullptr);
424 EXPECT_EQ(
tbl.size(),
static_cast<size_t>(1));
426 EXPECT_NE(
tbl.search(2),
nullptr);
427 EXPECT_NE(
tbl.search(3),
nullptr);
428 EXPECT_EQ(
tbl.size(),
static_cast<size_t>(2));
435 Aleph::dft_hash_fct<int>,
436 Aleph::snd_hash_fct<int>,
441 for (
int i = 0; i < 4; ++i)
442 ASSERT_NE(
tbl.insert(i),
nullptr);
443 EXPECT_EQ(
tbl.insert(4),
nullptr);
446 EXPECT_THROW(
tbl.remove(99), std::domain_error);
452 auto *p1 =
tbl.insert(10);
453 ASSERT_NE(p1,
nullptr);
454 auto *p2 =
tbl.insert(10);
455 EXPECT_EQ(p2,
nullptr);
457 auto *p3 =
tbl.search_or_insert(10);
459 EXPECT_EQ(
tbl.size(),
static_cast<size_t>(1));
470 EXPECT_NE(b.
search(1),
nullptr);
473 EXPECT_EQ(c.
size(),
static_cast<size_t>(2));
478 EXPECT_NE(c.
search(99),
nullptr);
479 EXPECT_EQ(d.
search(99),
nullptr);
484 EXPECT_NE(e.
search(99),
nullptr);
489 EXPECT_NE(f.
search(99),
nullptr);
502 auto stats =
tbl.stats();
503 EXPECT_EQ(stats.num_busy,
static_cast<size_t>(2));
504 EXPECT_GE(stats.num_deleted,
static_cast<size_t>(0));
510 auto h2 = [](
int k) {
return static_cast<size_t>(
k * 13); };
512 EXPECT_TRUE(
tbl.insert(1));
513 EXPECT_NE(
tbl.search(1),
nullptr);
519 for (
int i = 0; i < 5; ++i)
521 auto stats =
tbl.stats();
522 EXPECT_FALSE(std::isnan(stats.avg));
523 EXPECT_FALSE(std::isnan(stats.var));
529 auto const_h2 = +[](int) {
return static_cast<size_t>(0); };
530 tbl.set_second_hash_fct(const_h2);
535 auto stats =
tbl.stats();
536 EXPECT_EQ(stats.num_busy,
static_cast<size_t>(3));
537 EXPECT_GE(stats.max_len,
static_cast<size_t>(1));
543 Aleph::dft_hash_fct<int>,
544 Aleph::snd_hash_fct<int>,
549 const auto cap0 =
tbl.capacity();
550 for (
int i = 0; i < 10; ++i)
552 EXPECT_GE(
tbl.capacity(), cap0);
553 for (
int i = 0; i < 10; ++i)
554 EXPECT_NE(
tbl.search(i),
nullptr);
563 EXPECT_NE(
tbl.search(10),
nullptr);
569 std::set<int> inserted;
570 for (
int i = 0; i < 7; ++i)
573 inserted.insert(i * 2);
575 auto it =
tbl.get_it();
576 std::set<int> iterated;
577 for (; it.has_curr(); it.next_ne())
578 iterated.insert(it.get_curr());
579 EXPECT_EQ(iterated, inserted);
588 auto const_hash = +[](
const int &) {
return static_cast<size_t>(0); };
595 ASSERT_NE(
tbl.insert(b),
nullptr);
596 EXPECT_EQ(
tbl.size(), 1u);
597 EXPECT_EQ(
tbl.get_num_busy_slots(), 1u);
599 auto * removed =
tbl.remove(b);
600 ASSERT_NE(removed,
nullptr);
603 EXPECT_EQ(
tbl.size(), 0u);
604 EXPECT_EQ(
tbl.get_num_busy_slots(), 0u);
605 EXPECT_DOUBLE_EQ(
tbl.current_alpha(), 0.0);
611 0.9f, 10.0f,
true,
true);
614 auto * b1 =
new Bucket(1);
615 auto * b2 =
new Bucket(2);
616 auto * b3 =
new Bucket(3);
618 ASSERT_NE(
tbl.insert(b1),
nullptr);
619 ASSERT_NE(
tbl.insert(b2),
nullptr);
620 ASSERT_NE(
tbl.insert(b3),
nullptr);
622 const auto old_cap =
tbl.capacity();
623 auto * removed =
tbl.remove(b1);
624 ASSERT_NE(removed,
nullptr);
627 EXPECT_LT(
tbl.capacity(), old_cap);
628 EXPECT_EQ(
tbl.size(), 2u);
630 delete tbl.remove(b2);
631 delete tbl.remove(b3);
636 auto const_hash = +[](
const int &) {
return static_cast<size_t>(1); };
641 for (
int k : {1, 2, 3})
642 ASSERT_NE(
tbl.insert(
new Bucket(
k)),
nullptr);
646 auto * removed = it.
del();
650 EXPECT_EQ(
tbl.size(), 0u);
651 EXPECT_EQ(
tbl.get_num_busy_slots(), 0u);
659 auto * b1 =
new Bucket(42);
660 ASSERT_NE(
tbl.insert(b1),
nullptr);
662 auto * b2 =
new Bucket(42);
663 auto * found =
tbl.search_or_insert(b2);
664 EXPECT_EQ(found, b1);
667 delete tbl.remove(b1);
674 auto * b1 =
new Bucket(1);
675 auto * b2 =
new Bucket(2);
681 EXPECT_EQ(dst.
size(), 2u);
682 EXPECT_NE(dst.
search(1),
nullptr);
683 EXPECT_NE(dst.
search(2),
nullptr);
694 auto * b1 =
new Bucket(7);
695 ASSERT_NE(
tbl.insert(b1),
nullptr);
696 EXPECT_EQ(
tbl.size(), 1u);
698 auto * b2 =
new Bucket(7);
699 EXPECT_EQ(
tbl.insert(b2),
nullptr);
702 auto * found =
tbl.search(7);
703 EXPECT_EQ(found, b1);
705 delete tbl.remove(b1);
710 auto const_hash = +[](
const int &) {
return static_cast<size_t>(3); };
715 auto * b1 =
new Bucket(1);
716 auto * b2 =
new Bucket(2);
717 auto * b3 =
new Bucket(3);
722 auto * first =
tbl.search(1);
723 ASSERT_NE(first,
nullptr);
724 EXPECT_EQ(
tbl.search_next(first),
nullptr);
726 delete tbl.remove(b1);
727 delete tbl.remove(b2);
728 delete tbl.remove(b3);
740 ASSERT_TRUE(it.has_curr());
742 EXPECT_THROW(it.get_curr(), std::overflow_error);
743 EXPECT_THROW(it.next(), std::overflow_error);
746 ASSERT_TRUE(it.has_curr());
748 EXPECT_THROW(it.get_curr(), std::underflow_error);
749 EXPECT_THROW(it.prev(), std::underflow_error);
762 for (
int k : {1, 2, 3})
767 EXPECT_EQ(
tbl.size(), 0u);
768 EXPECT_EQ(
tbl.get_num_busy_slots(), 0u);
769 EXPECT_DOUBLE_EQ(
tbl.current_alpha(), 0.0);
775 auto const_hash = +[](
const int &) {
return static_cast<size_t>(0); };
782 EXPECT_EQ(
tbl.size(), 1u);
793 auto * b1 =
new Bucket(1);
794 auto * b2 =
new Bucket(2);
795 auto * b3 =
new Bucket(3);
796 auto * bx =
new Bucket(99);
807 EXPECT_EQ(a.
size(), 1u);
808 EXPECT_EQ(b.
size(), 3u);
811 EXPECT_NE(a.
search(99),
nullptr);
812 EXPECT_NE(b.
search(1),
nullptr);
824 for (
int k : {1, 2, 102})
826 const size_t busy_before =
tbl.get_num_busy_slots();
828 tbl.set_hash_fct(+[](
const int &
k) {
return static_cast<size_t>(
k * 2); });
831 EXPECT_LE(
tbl.get_num_busy_slots(), 3u);
832 EXPECT_NE(
tbl.search(1),
nullptr);
833 EXPECT_NE(
tbl.search(2),
nullptr);
834 EXPECT_NE(
tbl.search(102),
nullptr);
835 EXPECT_NE(
tbl.get_num_busy_slots(), busy_before);
837 delete tbl.remove(
tbl.search(1));
838 delete tbl.remove(
tbl.search(2));
839 delete tbl.remove(
tbl.search(102));
848 const auto cap =
tbl.capacity();
849 for (
int k = 0;
k < 50; ++
k)
852 EXPECT_EQ(
tbl.capacity(), cap);
864 EXPECT_FALSE(
tbl.get_with_resize());
865 for (
int k = 0;
k < 50; ++
k)
866 ASSERT_NE(
tbl.insert(
new Bucket(
k)),
nullptr) <<
"key " <<
k;
867 EXPECT_EQ(
tbl.capacity(), 11u);
870 tbl.set_with_resize(
true);
871 EXPECT_TRUE(
tbl.get_with_resize());
872 ASSERT_NE(
tbl.insert(
new Bucket(50)),
nullptr);
873 const size_t grown =
tbl.capacity();
874 EXPECT_GT(grown, 11u);
877 tbl.set_with_resize(
false);
878 for (
int k = 0;
k < 45; ++
k)
879 delete tbl.remove(
tbl.search(
k));
880 EXPECT_EQ(
tbl.capacity(), grown);
883 tbl.set_with_resize(
true);
884 delete tbl.remove(
tbl.search(45));
885 EXPECT_LT(
tbl.capacity(), grown);
886 for (
int k = 46;
k <= 50; ++
k)
887 EXPECT_NE(
tbl.search(
k),
nullptr) <<
"key " <<
k;
897 for (
int k = 0;
k < 20; ++
k)
899 original.set_with_resize(
false);
902 EXPECT_FALSE(
copy.get_with_resize());
903 EXPECT_EQ(
copy.size(), 20u);
907 lin_original.set_with_resize(
false);
909 EXPECT_FALSE(lin_copy.get_with_resize());
917 bool operator()(
int a,
int b)
const {
return (a % 10) == (b % 10); }
920 auto const_hash = +[](
const int &) {
return static_cast<size_t>(0); };
925 auto * b1 =
new Bucket(10);
926 auto * b2 =
new Bucket(20);
927 ASSERT_NE(
tbl.insert(b1),
nullptr);
928 EXPECT_EQ(
tbl.insert(b2),
nullptr);
929 EXPECT_EQ(
tbl.size(), 1u);
931 delete tbl.remove(b1);
941 EXPECT_GT(
tbl.current_alpha(), 0.0);
946static_assert(!std::is_copy_constructible_v<LhashTable<int>>,
947 "LhashTable should not be copy constructible");
948static_assert(!std::is_copy_assignable_v<LhashTable<int>>,
949 "LhashTable should not be copy assignable");
957 size_t first_hash_a(
const long &
k)
noexcept
959 return static_cast<size_t>(
k) * 2654435761u;
962 size_t first_hash_b(
const long &
k)
noexcept
964 return static_cast<size_t>(
k) * 40503u + 17;
967 size_t second_hash_a(
const long &
k)
noexcept
969 return static_cast<size_t>(
k) + 7;
972 size_t second_hash_b(
const long &
k)
noexcept
974 return static_cast<size_t>(
k) * 3 + 11;
982 bool operator()(
const long & x,
const long &
y)
const noexcept
991 size_t first_hash = 0;
992 size_t second_hash = 0;
994 float lower_alpha = 0;
995 float upper_alpha = 0;
997 bool operator==(
const Table_Config &)
const =
default;
1000 const Table_Config & c)
1002 return out <<
"{hash " << c.first_hash <<
", second hash "
1003 << c.second_hash <<
", comparator " << c.cmp_tag
1004 <<
", thresholds " << c.lower_alpha <<
'/' << c.upper_alpha
1009 constexpr long Config_Probe_Key = 12345;
1013 const Table_Config config_a = {first_hash_a(Config_Probe_Key),
1014 second_hash_a(Config_Probe_Key), 1,
1016 const Table_Config config_b = {first_hash_b(Config_Probe_Key),
1017 second_hash_b(Config_Probe_Key), 2,
1027 template <
class Table>
struct Config_Of;
1029 template <>
struct Config_Of<Linear_Probing>
1031 static Linear_Probing make(
const bool a,
const size_t len)
1033 return a ? Linear_Probing(len, first_hash_a, Tagged_Equal{1},
1035 : Linear_Probing(len, first_hash_b, Tagged_Equal{2},
1036 0.25f, 0.90f, true);
1039 static size_t second_hash(
const Linear_Probing &,
const bool a)
1041 return a ? config_a.second_hash : config_b.second_hash;
1045 template <>
struct Config_Of<Double_Hashing>
1047 static Double_Hashing make(
const bool a,
const size_t len)
1049 return a ? Double_Hashing(len, first_hash_a, second_hash_a,
1050 Tagged_Equal{1}, 0.10f, 0.40f,
true)
1051 : Double_Hashing(len, first_hash_b, second_hash_b,
1052 Tagged_Equal{2}, 0.25f, 0.90f, true);
1055 static size_t second_hash(
const Double_Hashing & table,
bool)
1057 return table.get_second_hash_fct()(Config_Probe_Key);
1061 template <
class Table>
struct Config_Of_Chained
1063 static Table make(
const bool a,
const size_t len)
1065 return a ?
Table(len, first_hash_a, Tagged_Equal{1}, 0.10f, 0.40f)
1066 :
Table(len, first_hash_b, Tagged_Equal{2}, 0.25f, 0.90f);
1069 static size_t second_hash(
const Table &,
const bool a)
1071 return a ? config_a.second_hash : config_b.second_hash;
1075 template <>
struct Config_Of<Chained> : Config_Of_Chained<Chained> {};
1078 struct Config_Of<Linear_Hashing> : Config_Of_Chained<Linear_Hashing> {};
1082 template <
class Table>
1083 Table_Config config_of(
const Table & table,
const bool like_a)
1086 Config_Of<Table>::second_hash(table, like_a),
1091 template <
class Table>
1092 void insert_keys(
Table & table,
const long from,
const long to)
1094 for (
long k = from;
k < to; ++
k)
1095 ASSERT_NE(table.
insert(
k),
nullptr) <<
"key " <<
k;
1098 template <
class Table>
1099 ::testing::AssertionResult holds(
const Table & table,
long from,
long to)
1101 if (table.
size() !=
static_cast<size_t>(to - from))
1102 return ::testing::AssertionFailure()
1103 <<
"size is " << table.
size() <<
", expected " << to - from;
1104 for (
long k = from;
k < to; ++
k)
1106 return ::testing::AssertionFailure() <<
"key " <<
k <<
" not found";
1107 return ::testing::AssertionSuccess();
1110 template <
class Table>
1111 struct HashTableConfiguration :
public ::testing::Test {};
1113 using Configured_Tables = ::testing::Types<Linear_Probing, Double_Hashing,
1114 Chained, Linear_Hashing>;
1117 TYPED_TEST(HashTableConfiguration, ConstructionKeepsWhatItWasGiven)
1119 const auto a = Config_Of<TypeParam>::make(
true, 101);
1120 const auto b = Config_Of<TypeParam>::make(
false, 101);
1122 EXPECT_EQ(config_of(a,
true), config_a);
1123 EXPECT_EQ(config_of(b,
false), config_b);
1126 TYPED_TEST(HashTableConfiguration, CopyConstructionCopiesTheConfiguration)
1128 auto source = Config_Of<TypeParam>::make(
true, 101);
1129 insert_keys(source, 0, 40);
1131 const TypeParam
copy(source);
1133 EXPECT_EQ(config_of(
copy,
true), config_a);
1134 EXPECT_TRUE(holds(
copy, 0, 40));
1135 EXPECT_EQ(config_of(source,
true), config_a);
1136 EXPECT_TRUE(holds(source, 0, 40));
1142 TYPED_TEST(HashTableConfiguration, CopyAssignmentToALargerTable)
1144 auto source = Config_Of<TypeParam>::make(
true, 101);
1145 insert_keys(source, 0, 40);
1146 auto target = Config_Of<TypeParam>::make(
false, 1009);
1147 insert_keys(target, 500, 520);
1151 EXPECT_EQ(config_of(target,
true), config_a);
1152 EXPECT_TRUE(holds(target, 0, 40));
1153 EXPECT_EQ(config_of(source,
true), config_a);
1154 EXPECT_TRUE(holds(source, 0, 40));
1156 insert_keys(target, 40, 400);
1157 EXPECT_TRUE(holds(target, 0, 400));
1160 TYPED_TEST(HashTableConfiguration, CopyAssignmentToASmallerTable)
1162 auto source = Config_Of<TypeParam>::make(
true, 1009);
1163 insert_keys(source, 0, 400);
1164 auto target = Config_Of<TypeParam>::make(
false, 11);
1165 insert_keys(target, 500, 505);
1169 EXPECT_EQ(config_of(target,
true), config_a);
1170 EXPECT_TRUE(holds(target, 0, 400));
1171 EXPECT_TRUE(holds(source, 0, 400));
1174 TYPED_TEST(HashTableConfiguration, SelfCopyAssignmentChangesNothing)
1176 auto table = Config_Of<TypeParam>::make(
true, 101);
1177 insert_keys(table, 0, 40);
1178 const auto & same = table;
1182 EXPECT_EQ(config_of(table,
true), config_a);
1183 EXPECT_TRUE(holds(table, 0, 40));
1186 TYPED_TEST(HashTableConfiguration, MoveConstructionCarriesTheConfiguration)
1188 auto source = Config_Of<TypeParam>::make(
true, 101);
1189 insert_keys(source, 0, 40);
1191 TypeParam target(std::move(source));
1193 EXPECT_EQ(config_of(target,
true), config_a);
1194 EXPECT_TRUE(holds(target, 0, 40));
1197 EXPECT_TRUE(source.is_empty());
1198 EXPECT_EQ(source.size(), 0u);
1199 EXPECT_FALSE(source.contains(7));
1200 EXPECT_EQ(config_of(source,
true), config_a);
1201 insert_keys(source, 100, 300);
1202 EXPECT_TRUE(holds(source, 100, 300));
1203 EXPECT_TRUE(holds(target, 0, 40));
1206 TYPED_TEST(HashTableConfiguration, MoveAssignmentCarriesTheConfiguration)
1208 auto source = Config_Of<TypeParam>::make(
true, 101);
1209 insert_keys(source, 0, 40);
1210 auto target = Config_Of<TypeParam>::make(
false, 1009);
1211 insert_keys(target, 500, 520);
1213 target = std::move(source);
1215 EXPECT_EQ(config_of(target,
true), config_a);
1216 EXPECT_TRUE(holds(target, 0, 40));
1218 insert_keys(target, 40, 400);
1219 EXPECT_TRUE(holds(target, 0, 400));
1222 TYPED_TEST(HashTableConfiguration, SwapExchangesTheConfiguration)
1224 auto a = Config_Of<TypeParam>::make(
true, 101);
1225 insert_keys(a, 0, 40);
1226 auto b = Config_Of<TypeParam>::make(
false, 1009);
1227 insert_keys(b, 500, 520);
1231 EXPECT_EQ(config_of(a,
false), config_b);
1232 EXPECT_TRUE(holds(a, 500, 520));
1233 EXPECT_EQ(config_of(b,
true), config_a);
1234 EXPECT_TRUE(holds(b, 0, 40));
1236 insert_keys(a, 520, 900);
1237 insert_keys(b, 40, 400);
1238 EXPECT_TRUE(holds(a, 500, 900));
1239 EXPECT_TRUE(holds(b, 0, 400));
1242 TYPED_TEST(HashTableConfiguration, SwapWithAnEmptyTable)
1244 auto a = Config_Of<TypeParam>::make(
true, 101);
1245 insert_keys(a, 0, 40);
1246 auto b = Config_Of<TypeParam>::make(
false, 11);
1250 EXPECT_EQ(config_of(a,
false), config_b);
1251 EXPECT_TRUE(a.is_empty());
1252 EXPECT_EQ(config_of(b,
true), config_a);
1253 EXPECT_TRUE(holds(b, 0, 40));
1260 template <
class Table>
bool grows_when_filled(
Table & table)
1262 const size_t capacity = table.
capacity();
1265 (
void) table.
insert(1000000 +
k);
1266 return table.
capacity() != capacity;
1269 template <
class Table>
1270 struct OpenTableResizingPolicy :
public ::testing::Test
1275 static constexpr long Num_Keys = 6;
1277 static Table fixed_size(
const size_t len,
const long num_keys = 0)
1279 auto make = [len] ()
1281 if constexpr (std::is_same_v<Table, Linear_Probing>)
1282 return Table(len, first_hash_a, Tagged_Equal{1}, 0.10f, 0.40f,
1285 return Table(len, first_hash_a, second_hash_a, Tagged_Equal{1},
1286 0.10f, 0.40f,
false);
1288 Table table = make();
1293 static Table resizable(
const size_t len,
const long num_keys = 0)
1295 Table table = Config_Of<Table>::make(
false, len);
1301 using Open_Tables = ::testing::Types<Linear_Probing, Double_Hashing>;
1304 TYPED_TEST(OpenTableResizingPolicy, IsWhatTheConstructorWasGiven)
1306 auto fixed = this->fixed_size(11, this->Num_Keys);
1307 auto resizable = this->resizable(11, this->Num_Keys);
1309 EXPECT_FALSE(grows_when_filled(fixed));
1310 EXPECT_TRUE(grows_when_filled(resizable));
1313 TYPED_TEST(OpenTableResizingPolicy, IsCopied)
1315 const auto fixed = this->fixed_size(11, this->Num_Keys);
1316 const auto resizable = this->resizable(11, this->Num_Keys);
1318 TypeParam fixed_copy(fixed);
1319 TypeParam resizable_copy(resizable);
1320 EXPECT_FALSE(grows_when_filled(fixed_copy));
1321 EXPECT_TRUE(grows_when_filled(resizable_copy));
1325 for (
const size_t len : {size_t(5), size_t(1009)})
1327 auto to_fixed = this->resizable(len);
1329 EXPECT_FALSE(grows_when_filled(to_fixed)) <<
"capacity " << len;
1331 auto to_resizable = this->fixed_size(len);
1332 to_resizable = resizable;
1333 EXPECT_TRUE(grows_when_filled(to_resizable)) <<
"capacity " << len;
1339 auto fixed = this->fixed_size(11, this->Num_Keys);
1340 auto resizable = this->resizable(11, this->Num_Keys);
1342 TypeParam from_fixed(std::move(fixed));
1343 TypeParam from_resizable(std::move(resizable));
1345 auto to_fixed = this->resizable(101);
1346 to_fixed = std::move(from_fixed);
1347 EXPECT_FALSE(grows_when_filled(to_fixed));
1349 auto to_resizable = this->fixed_size(101);
1350 to_resizable = std::move(from_resizable);
1351 EXPECT_TRUE(grows_when_filled(to_resizable));
1354 TYPED_TEST(OpenTableResizingPolicy, IsSwapped)
1356 auto a = this->fixed_size(11, this->Num_Keys);
1357 auto b = this->resizable(11, this->Num_Keys);
1361 EXPECT_TRUE(grows_when_filled(a));
1362 EXPECT_FALSE(grows_when_filled(b));
1369 template <
class Table>
1370 struct OpenTableAfterMove :
public ::testing::Test
1374 static Table emptied_by_a_move(
const bool resizes =
true)
1376 auto make = [resizes] ()
1378 if constexpr (std::is_same_v<Table, OLhashTable<long>>)
1380 0.10f, 0.40f, resizes);
1382 return Table(101, first_hash_a, second_hash_a,
1385 Table source = make();
1386 insert_keys(source, 0, 40);
1387 Table target(std::move(source));
1388 EXPECT_TRUE(holds(target, 0, 40));
1393 using Plain_Open_Tables =
1398 TYPED_TEST(OpenTableAfterMove, TheTargetHasTheVeryBucketsOfTheSource)
1400 static_assert(std::is_nothrow_move_constructible_v<TypeParam>);
1403 insert_keys(source, 0, 1000);
1404 const size_t capacity = source.capacity();
1405 std::vector<const long *> where;
1406 for (
long k = 0;
k < 1000; ++
k)
1407 where.push_back(source.search(
k));
1409 TypeParam target(std::move(source));
1411 EXPECT_EQ(target.capacity(), capacity);
1412 ASSERT_TRUE(holds(target, 0, 1000));
1413 for (
long k = 0;
k < 1000; ++
k)
1414 ASSERT_EQ(std::as_const(target).
search(
k), where[
k]) <<
"key " <<
k;
1416 EXPECT_EQ(source.size(), 0u);
1417 EXPECT_EQ(source.capacity(), 0u);
1420 TYPED_TEST(OpenTableAfterMove, TheSourceAnswersAsAnEmptyTable)
1422 TypeParam source = this->emptied_by_a_move();
1424 EXPECT_EQ(source.size(), 0u);
1425 EXPECT_EQ(source.capacity(), 0u);
1426 EXPECT_TRUE(source.is_empty());
1427 EXPECT_EQ(source.current_alpha(), 0.0f);
1429 EXPECT_EQ(source.search(7),
nullptr);
1430 EXPECT_EQ(std::as_const(source).
search(7),
nullptr);
1431 EXPECT_FALSE(source.has(7));
1432 EXPECT_FALSE(source.contains(7));
1433 EXPECT_THROW((
void) source.find(7), std::domain_error);
1434 EXPECT_THROW(source.remove(7), std::domain_error);
1436 EXPECT_TRUE(source.keys().is_empty());
1438 source.for_each([&visited] (
const long &) { ++visited; });
1439 for ([[maybe_unused]]
const long & key : source)
1441 EXPECT_EQ(visited, 0u);
1443 typename TypeParam::Iterator it(source);
1444 EXPECT_FALSE(it.has_curr());
1445 EXPECT_THROW(it.del(), std::overflow_error);
1447 EXPECT_FALSE(it.has_curr());
1449 const auto stats = source.stats();
1450 EXPECT_EQ(stats.num_busy, 0u);
1451 EXPECT_EQ(stats.num_empty, 0u);
1452 EXPECT_EQ(stats.num_deleted, 0u);
1453 EXPECT_EQ(stats.max_len, 0u);
1454 EXPECT_EQ(stats.avg, 0.0f);
1455 EXPECT_EQ(stats.var, 0.0f);
1458 EXPECT_EQ(source.capacity(), 0u);
1459 EXPECT_EQ(source.resize(0), 0u);
1460 EXPECT_EQ(source.capacity(), 0u);
1463 TYPED_TEST(OpenTableAfterMove, TheSourceGetsBucketsWithItsFirstInsertion)
1466 TypeParam source = this->emptied_by_a_move();
1467 ASSERT_NE(source.insert(7),
nullptr);
1469 EXPECT_TRUE(holds(source, 7, 8));
1470 insert_keys(source, 8, 2000);
1471 EXPECT_TRUE(holds(source, 7, 2000));
1473 EXPECT_TRUE(holds(source, 8, 2000));
1476 TypeParam source = this->emptied_by_a_move();
1478 ASSERT_NE(source.insert(std::move(key)),
nullptr);
1479 EXPECT_TRUE(holds(source, 7, 8));
1482 TypeParam source = this->emptied_by_a_move();
1483 const long * stored = source.search_or_insert(7);
1484 ASSERT_NE(stored,
nullptr);
1485 EXPECT_EQ(source.search_or_insert(7), stored);
1486 EXPECT_TRUE(holds(source, 7, 8));
1489 TypeParam source = this->emptied_by_a_move();
1490 const auto first = source.contains_or_insert(7);
1491 ASSERT_NE(first.first,
nullptr);
1492 EXPECT_FALSE(first.second);
1493 EXPECT_TRUE(source.contains_or_insert(7).second);
1494 EXPECT_TRUE(holds(source, 7, 8));
1497 TypeParam source = this->emptied_by_a_move();
1498 EXPECT_EQ(source[7], 7);
1499 EXPECT_TRUE(holds(source, 7, 8));
1505 TYPED_TEST(OpenTableAfterMove, ATableThatDoesNotResizeGetsBucketsToo)
1507 TypeParam source = this->emptied_by_a_move(
false);
1509 ASSERT_NE(source.insert(7),
nullptr);
1511 insert_keys(source, 8, 50);
1512 EXPECT_TRUE(holds(source, 7, 50));
1513 EXPECT_FALSE(grows_when_filled(source));
1516 TYPED_TEST(OpenTableAfterMove, TheSourceCanBeResizedAndCleared)
1519 TypeParam source = this->emptied_by_a_move();
1520 EXPECT_EQ(source.resize(17), 17u);
1521 EXPECT_EQ(source.capacity(), 17u);
1522 EXPECT_TRUE(source.is_empty());
1523 insert_keys(source, 0, 10);
1524 EXPECT_TRUE(holds(source, 0, 10));
1527 TypeParam source = this->emptied_by_a_move();
1530 EXPECT_TRUE(source.is_empty());
1531 insert_keys(source, 0, 10);
1532 EXPECT_TRUE(holds(source, 0, 10));
1536 TYPED_TEST(OpenTableAfterMove, TheSourceCanBeCopiedMovedAndAssigned)
1539 const TypeParam source = this->emptied_by_a_move();
1540 TypeParam
copy(source);
1541 EXPECT_TRUE(
copy.is_empty());
1542 EXPECT_EQ(
copy.get_hash_fct()(Config_Probe_Key),
1543 first_hash_a(Config_Probe_Key));
1544 insert_keys(
copy, 0, 10);
1545 EXPECT_TRUE(holds(
copy, 0, 10));
1546 EXPECT_TRUE(source.is_empty());
1549 TypeParam source = this->emptied_by_a_move();
1550 TypeParam moved(std::move(source));
1551 EXPECT_TRUE(moved.is_empty());
1552 insert_keys(moved, 0, 10);
1553 insert_keys(source, 20, 30);
1554 EXPECT_TRUE(holds(moved, 0, 10));
1555 EXPECT_TRUE(holds(source, 20, 30));
1558 const TypeParam source = this->emptied_by_a_move();
1560 insert_keys(target, 100, 150);
1562 EXPECT_TRUE(target.is_empty());
1563 insert_keys(target, 0, 10);
1564 EXPECT_TRUE(holds(target, 0, 10));
1567 TypeParam source = this->emptied_by_a_move();
1569 insert_keys(other, 100, 150);
1571 EXPECT_TRUE(holds(source, 100, 150));
1572 EXPECT_TRUE(holds(other, 100, 150));
1575 TypeParam source = this->emptied_by_a_move();
1577 insert_keys(other, 100, 150);
1578 source = std::move(other);
1579 EXPECT_TRUE(holds(source, 100, 150));
1580 insert_keys(other, 0, 10);
1581 EXPECT_TRUE(holds(other, 0, 10));
1584 TypeParam source = this->emptied_by_a_move();
1586 insert_keys(other, 100, 150);
1588 EXPECT_TRUE(holds(source, 100, 150));
1589 EXPECT_TRUE(other.is_empty());
1590 insert_keys(other, 0, 10);
1591 EXPECT_TRUE(holds(other, 0, 10));
1603 static inline long copies_before_failure = -1;
1607 Fragile_Key() =
default;
1609 Fragile_Key(
const long v) :
value(v) {}
1611 Fragile_Key(
const Fragile_Key & other) :
value(other.
value)
1616 Fragile_Key & operator=(
const Fragile_Key & other)
1619 value = other.value;
1623 Fragile_Key(Fragile_Key &&) noexcept = default;
1625 Fragile_Key & operator=(Fragile_Key &&) noexcept = default;
1627 bool operator==(const Fragile_Key &) const = default;
1629 static
void may_throw()
1631 if (copies_before_failure == 0)
1632 throw std::runtime_error(
"no copy");
1633 if (copies_before_failure > 0)
1634 --copies_before_failure;
1638 size_t fragile_hash(
const Fragile_Key & key)
noexcept
1640 return first_hash_a(key.value);
1643 size_t fragile_second_hash(
const Fragile_Key & key)
noexcept
1645 return second_hash_a(key.value);
1649 struct Failing_Copies
1651 explicit Failing_Copies(
const long successful = 0)
1653 Fragile_Key::copies_before_failure = successful;
1656 ~Failing_Copies() { Fragile_Key::copies_before_failure = -1; }
1659 template <
class Table>
1660 struct HashTableCopyThatThrows :
public ::testing::Test
1662 static Table make(
const long from,
const long to)
1664 auto empty_table = [] ()
1666 if constexpr (std::is_same_v<Table, ODhashTable<Fragile_Key>>)
1667 return Table(101, fragile_hash, fragile_second_hash);
1669 return Table(101, fragile_hash);
1671 Table table = empty_table();
1672 for (
long k = from;
k < to; ++
k)
1673 EXPECT_NE(table.
insert(Fragile_Key(
k)),
nullptr) <<
"key " <<
k;
1677 static ::testing::AssertionResult holds_keys(
const Table & table,
1681 if (table.
size() !=
static_cast<size_t>(to - from))
1682 return ::testing::AssertionFailure()
1683 <<
"size is " << table.
size() <<
", expected " << to - from;
1684 for (
long k = from;
k < to; ++
k)
1686 return ::testing::AssertionFailure() <<
"key " <<
k <<
" not found";
1687 return ::testing::AssertionSuccess();
1691 using Fragile_Tables = ::testing::Types<OLhashTable<Fragile_Key>,
1699 TYPED_TEST(HashTableCopyThatThrows, AssignmentKeepsTheTarget)
1701 const TypeParam source = this->make(0, 30);
1702 TypeParam target = this->make(100, 110);
1705 Failing_Copies failing;
1706 EXPECT_THROW(target = source, std::runtime_error);
1709 EXPECT_TRUE(this->holds_keys(target, 100, 110));
1710 EXPECT_TRUE(this->holds_keys(source, 0, 30));
1713 EXPECT_TRUE(this->holds_keys(target, 0, 30));
1716 TYPED_TEST(HashTableCopyThatThrows, ConstructionReleasesWhatItBuilt)
1718 const TypeParam source = this->make(0, 30);
1721 Failing_Copies failing;
1722 EXPECT_THROW(TypeParam
copy(source), std::runtime_error);
1725 EXPECT_TRUE(this->holds_keys(source, 0, 30));
1732 TEST(OpenTableHashFunction, RehashPlacesTheKeysWithTheNewFunction)
1736 insert_keys(table, 0, 60);
1739 EXPECT_TRUE(holds(table, 0, 60));
1740 insert_keys(table, 60, 300);
1741 EXPECT_TRUE(holds(table, 0, 300));
1745 insert_keys(table, 0, 60);
1747 table.set_second_hash_fct(second_hash_b);
1749 EXPECT_TRUE(holds(table, 0, 60));
1750 for (
long k = 0;
k < 60;
k += 2)
1752 insert_keys(table, 60, 300);
1753 for (
long k = 1;
k < 60;
k += 2)
1754 EXPECT_TRUE(table.
contains(
k)) <<
"key " <<
k;
1755 EXPECT_EQ(table.
size(), 270u);
1766 TEST(OpenTableConstruction, TakesLambdasWithoutCaptures)
1768 auto plus_ten = [] (
const long &
k) {
return static_cast<size_t>(
k) + 10; };
1769 auto plus_twenty = [] (
const long &
k)
1771 return static_cast<size_t>(
k) + 20;
1776 EXPECT_EQ(one.get_hash_fct()(1), 11u);
1777 insert_keys(one, 0, 100);
1778 EXPECT_TRUE(holds(one, 0, 100));
1781 EXPECT_EQ(two.get_hash_fct()(1), 11u);
1785 EXPECT_EQ(with_a_function.get_hash_fct()(1), 11u);
1790 EXPECT_EQ(with_a_stored_one.get_hash_fct()(1), 21u);
1793 EXPECT_EQ(twice.get_hash_fct()(1), 11u);
1794 EXPECT_EQ(twice.get_second_hash_fct()(1), 21u);
1795 insert_keys(twice, 0, 100);
1796 EXPECT_TRUE(holds(twice, 0, 100));
1799 EXPECT_EQ(set.get_hash_fct()(1), 11u);
1803 TEST(OpenTableConstruction, OtherHashFunctionsAreTakenAsBefore)
1806 auto capturing = [bias] (
const long &
k)
1808 return static_cast<size_t>(
k + bias);
1810 auto plus_ten = [] (
const long &
k) {
return static_cast<size_t>(
k) + 10; };
1813 const size_t a = first_hash_a(1), b = first_hash_b(1);
1814 const size_t sa = second_hash_a(1);
1817 EXPECT_EQ(OL(11, first_hash_a,
eq, 0.1f, 0.5f,
true).get_hash_fct()(1), a);
1818 EXPECT_EQ(OL(11, &first_hash_b,
eq, 0.1f, 0.5f,
true).get_hash_fct()(1), b);
1819 EXPECT_EQ(OL(11, +plus_ten,
eq, 0.1f, 0.5f,
true).get_hash_fct()(1), 11u);
1820 EXPECT_EQ(OL(11, capturing,
eq, 0.1f, 0.5f,
true).get_hash_fct()(1), 31u);
1821 EXPECT_EQ(OL(11, stored,
eq, 0.1f, 0.5f,
true).get_hash_fct()(1), 31u);
1822 EXPECT_EQ(OL(11, plus_ten).get_hash_fct()(1), 11u);
1823 EXPECT_EQ(OL(11, plus_ten,
eq, 0.1f, 0.5f).get_hash_fct()(1), 11u);
1824 EXPECT_EQ(OL(11, first_hash_a, first_hash_b,
eq, 0.1f, 0.5f,
true)
1825 .get_hash_fct()(1), a);
1826 EXPECT_EQ(OL(11, stored, stored,
eq, 0.1f, 0.5f,
true).get_hash_fct()(1),
1828 EXPECT_EQ(OL(11, capturing, first_hash_b,
eq, 0.1f, 0.5f,
true)
1829 .get_hash_fct()(1), 31u);
1830 EXPECT_EQ(OL(11, first_hash_a, capturing,
eq, 0.1f, 0.5f,
true)
1831 .get_hash_fct()(1), a);
1832 EXPECT_EQ(OL(11, first_hash_b, plus_ten,
eq, 0.1f, 0.5f,
true)
1833 .get_hash_fct()(1), b);
1836 const OD plain(11, first_hash_a, second_hash_a,
eq, 0.1f, 0.5f,
true);
1837 EXPECT_EQ(plain.get_hash_fct()(1), a);
1838 EXPECT_EQ(plain.get_second_hash_fct()(1), sa);
1839 const OD lambda_first(11, plus_ten, second_hash_a,
eq, 0.1f, 0.5f,
true);
1840 EXPECT_EQ(lambda_first.get_hash_fct()(1), 11u);
1841 EXPECT_EQ(lambda_first.get_second_hash_fct()(1), sa);
1842 const OD lambda_second(11, first_hash_a, plus_ten,
eq, 0.1f, 0.5f,
true);
1843 EXPECT_EQ(lambda_second.get_hash_fct()(1), a);
1844 EXPECT_EQ(lambda_second.get_second_hash_fct()(1), 11u);
1845 const OD with_defaults(11, plus_ten, plus_ten);
1846 EXPECT_EQ(with_defaults.get_second_hash_fct()(1), 11u);
1855 struct Copy_Only_Key
1859 Copy_Only_Key() =
default;
1861 Copy_Only_Key(
const long v) :
value(v) {}
1863 Copy_Only_Key(
const Copy_Only_Key & other) :
value(other.
value)
1865 Fragile_Key::may_throw();
1868 Copy_Only_Key & operator=(
const Copy_Only_Key & other)
1870 Fragile_Key::may_throw();
1871 value = other.value;
1875 bool operator==(
const Copy_Only_Key &)
const =
default;
1878 static_assert(
noexcept(std::swap(std::declval<Fragile_Key &>(),
1879 std::declval<Fragile_Key &>())));
1880 static_assert(not
noexcept(std::swap(std::declval<Copy_Only_Key &>(),
1881 std::declval<Copy_Only_Key &>())));
1886 template <
class Key>
size_t crowded_hash(
const Key & key)
noexcept
1888 return static_cast<size_t>(key.value) % 7;
1891 template <
class Key>
size_t crowded_second_hash(
const Key & key)
noexcept
1893 return static_cast<size_t>(key.value) % 5;
1898 long value_of(
const long k)
noexcept {
return k; }
1900 template <
class Key>
long value_of(
const Key & key)
noexcept
1905 template <
class Key>
size_t chosen_hash(
const Key & key)
noexcept
1907 return static_cast<size_t>(value_of(key) / 1000);
1910 template <
class Key>
size_t chosen_second_hash(
const Key & key)
noexcept
1912 return static_cast<size_t>(value_of(key) % 1000);
1921 template <
class Table>
Table with_removed_keys(
const long removed)
1926 if constexpr (
requires (
Table & t) { t.get_second_hash_fct(); })
1927 return Table(101, chosen_hash<Key>, chosen_second_hash<Key>,
1933 for (
long b = 1; b <= removed; ++b)
1934 EXPECT_NE(table.
insert(Key(1000*b + 999)),
nullptr) <<
"bucket " << b;
1935 EXPECT_NE(table.
insert(Key(1000 + 2)),
nullptr);
1937 for (
long b = 1; b <= removed; ++b)
1938 table.
remove(Key(1000*b + 999));
1940 EXPECT_EQ(table.
size(), 1u);
1941 EXPECT_EQ(table.
stats().num_deleted,
static_cast<size_t>(removed));
1945 template <
class Table>
struct OpenTableInsertionThatThrows
1946 :
public ::testing::Test
1950 static Table make(
const size_t len,
const float upper_alpha,
1953 if constexpr (
requires (
Table & t) { t.get_second_hash_fct(); })
1954 return Table(len, crowded_hash<Key>, crowded_second_hash<Key>,
1958 0.05f, upper_alpha, resizes);
1962 static Table with_keys_and_removals(
const bool resizes)
1964 Table table = make(31, 0.95f, resizes);
1965 for (
long k = 0;
k < 20; ++
k)
1966 EXPECT_NE(table.
insert(Key(
k)),
nullptr) <<
"key " <<
k;
1967 for (
const long k : {1L, 4L, 7L, 10L, 13L, 16L})
1972 static std::vector<long> keys_of(
const Table & table)
1974 std::vector<long>
keys;
1975 table.for_each([&
keys] (
const Key & key) {
keys.push_back(key.value); });
1976 std::sort(
keys.begin(),
keys.end());
1984 static std::vector<std::pair<int, unsigned>> probes_of(
const Table & table)
1986 std::vector<std::pair<int, unsigned>> state;
1987 for (
size_t i = 0; i < table.
capacity(); ++i)
1988 if constexpr (
requires { table.
table[0].probe_counter; })
1989 state.emplace_back(table.
table[i].status,
1990 table.
table[i].probe_counter);
1992 state.emplace_back(table.
table[i].status, 0u);
1997 using Tables_With_Fragile_Keys =
2004 TYPED_TEST(OpenTableInsertionThatThrows, KeyCopyThatThrowsLeavesNoEntry)
2006 for (
const bool resizes : {
false,
true})
2008 TypeParam table = this->with_keys_and_removals(resizes);
2009 const auto keys = this->keys_of(table);
2010 const auto probes = this->probes_of(table);
2011 const size_t capacity = table.capacity();
2012 ASSERT_EQ(
keys.size(), 14u);
2014 for (
long k = 0;
k < 60; ++
k)
2015 for (
int operation = 0; operation < 4; ++operation)
2017 const Fragile_Key key(
k);
2018 const bool present = table.contains(key);
2021 Failing_Copies failing;
2026 case 0: (void) table.insert(key);
break;
2027 case 1: (void) table.search_or_insert(key);
break;
2028 case 2: (void) table.contains_or_insert(key);
break;
2029 default: (void) table[key];
break;
2032 catch (
const std::runtime_error &)
2038 ASSERT_EQ(threw, not present)
2039 <<
"key " <<
k <<
", operation " << operation;
2040 ASSERT_EQ(this->keys_of(table),
keys)
2041 <<
"key " <<
k <<
", operation " << operation;
2042 ASSERT_EQ(table.size(),
keys.size());
2043 ASSERT_EQ(table.capacity(), capacity);
2044 ASSERT_EQ(this->probes_of(table), probes)
2045 <<
"key " <<
k <<
", operation " << operation;
2049 for (
long k = 0;
k < 28; ++
k)
2051 const bool present = table.contains(Fragile_Key(
k));
2052 EXPECT_EQ(table.insert(Fragile_Key(
k)) !=
nullptr, not present)
2055 for (
long k = 0;
k < 28; ++
k)
2056 EXPECT_TRUE(table.contains(Fragile_Key(
k))) <<
"key " <<
k;
2057 for (
long k = 0;
k < 28; ++
k)
2058 table.remove(Fragile_Key(
k));
2059 EXPECT_TRUE(table.is_empty());
2066 TYPED_TEST(OpenTableInsertionThatThrows, GrowthThatFailsTakesTheKeyOutAgain)
2068 for (
int operation = 0; operation < 5; ++operation)
2070 TypeParam table = this->make(11, 0.5f,
true);
2071 for (
long k = 0;
k < 5; ++
k)
2072 ASSERT_NE(table.insert(Fragile_Key(
k)),
nullptr);
2073 ASSERT_EQ(table.capacity(), 11u);
2074 const auto keys = this->keys_of(table);
2075 const auto probes = this->probes_of(table);
2077 const Fragile_Key key(5);
2080 Failing_Copies failing(operation == 4 ? 0 : 1);
2084 EXPECT_THROW((
void) table.insert(key), std::runtime_error);
2087 EXPECT_THROW((
void) table.search_or_insert(key),
2088 std::runtime_error);
2091 EXPECT_THROW((
void) table.contains_or_insert(key),
2092 std::runtime_error);
2095 EXPECT_THROW((
void) table[key], std::runtime_error);
2098 EXPECT_THROW((
void) table.insert(Fragile_Key(5)),
2099 std::runtime_error);
2104 EXPECT_EQ(this->keys_of(table),
keys) <<
"operation " << operation;
2105 EXPECT_EQ(table.size(), 5u) <<
"operation " << operation;
2106 EXPECT_EQ(table.capacity(), 11u) <<
"operation " << operation;
2107 EXPECT_FALSE(table.contains(key)) <<
"operation " << operation;
2108 EXPECT_EQ(this->probes_of(table), probes)
2109 <<
"operation " << operation;
2111 ASSERT_NE(table.insert(key),
nullptr);
2112 EXPECT_GT(table.capacity(), 11u);
2113 for (
long k = 0;
k < 6; ++
k)
2114 EXPECT_TRUE(table.contains(Fragile_Key(
k))) <<
"key " <<
k;
2121 TYPED_TEST(OpenTableInsertionThatThrows, FailuresDoNotUseUpEmptyBuckets)
2123 TypeParam table = this->make(11, 0.35f,
true);
2124 for (
const long k : {1L, 3L, 5L})
2125 ASSERT_NE(table.insert(Fragile_Key(
k)),
nullptr);
2126 ASSERT_EQ(table.capacity(), 11u);
2127 const auto probes = this->probes_of(table);
2128 const size_t empty = table.stats().num_empty;
2131 for (
const long k : {0L, 2L, 4L})
2133 const Fragile_Key key(
k);
2135 Failing_Copies failing;
2136 EXPECT_THROW((
void) table.insert(key), std::runtime_error)
2140 Failing_Copies failing(1);
2141 EXPECT_THROW((
void) table.insert(key), std::runtime_error)
2146 EXPECT_EQ(table.size(), 3u);
2147 EXPECT_EQ(table.capacity(), 11u);
2148 EXPECT_EQ(table.stats().num_empty, empty);
2149 EXPECT_EQ(this->probes_of(table), probes);
2155 TYPED_TEST(OpenTableInsertionThatThrows, RebuildInPlaceThatFailsTakesTheKeyOutAgain)
2158 TypeParam table = with_removed_keys<TypeParam>(60);
2159 for (
long b = 62; b < 91; ++b)
2160 ASSERT_NE(table.insert(Fragile_Key(1000*b + 999)),
nullptr);
2161 ASSERT_EQ(table.stats().num_deleted, 60u);
2162 const auto keys = this->keys_of(table);
2163 const auto probes = this->probes_of(table);
2166 const Fragile_Key key(1000*91 + 999);
2169 Failing_Copies failing(1);
2170 EXPECT_THROW((
void) table.insert(key), std::runtime_error);
2172 EXPECT_EQ(this->keys_of(table),
keys);
2173 EXPECT_EQ(this->probes_of(table), probes);
2174 EXPECT_EQ(table.capacity(), 101u);
2175 EXPECT_FALSE(table.contains(key));
2177 ASSERT_NE(table.insert(key),
nullptr);
2178 EXPECT_EQ(table.capacity(), 101u);
2179 EXPECT_EQ(table.stats().num_deleted, 0u);
2180 EXPECT_EQ(table.size(), 31u);
2191 for (
const long k : {1L, 2L, 3L})
2192 ASSERT_NE(table.insert(Fragile_Key(
k)),
nullptr);
2193 table.remove(Fragile_Key(1));
2194 table.remove(Fragile_Key(2));
2195 ASSERT_EQ(table.stats().num_deleted, 2u);
2198 const Fragile_Key key(9);
2199 Failing_Copies failing;
2200 EXPECT_THROW((
void) table.insert(key), std::runtime_error);
2203 EXPECT_EQ(table.size(), 1u);
2204 EXPECT_TRUE(table.contains(Fragile_Key(3)));
2205 EXPECT_EQ(table.stats().num_deleted, 0u);
2206 EXPECT_EQ(table.stats().num_empty, 10u);
2211 TYPED_TEST(OpenTableInsertionThatThrows, FailuresDoNotDisturbTheTable)
2213 TypeParam table = this->make(11, 0.7f,
true);
2215 std::mt19937
rng(20261013);
2217 auto check = [&] (
const int step)
2219 ASSERT_EQ(table.size(),
oracle.size()) <<
"step " << step;
2220 for (
long k = 0;
k < 150; ++
k)
2221 ASSERT_EQ(table.contains(Fragile_Key(
k)),
oracle.count(
k) == 1)
2222 <<
"key " <<
k <<
", step " << step;
2225 for (
int step = 0; step < 20000; ++step)
2227 const long k =
static_cast<long>(
rng() % 150);
2228 const Fragile_Key key(
k);
2232 EXPECT_EQ(table.insert(key) !=
nullptr,
oracle.insert(
k).second);
2238 EXPECT_THROW(table.remove(key), std::domain_error);
2242 Failing_Copies failing;
2245 EXPECT_EQ(table.search_or_insert(key) !=
nullptr,
true);
2246 EXPECT_EQ(
oracle.count(
k), 1u);
2248 catch (
const std::runtime_error &)
2250 EXPECT_EQ(
oracle.count(
k), 0u);
2256 Failing_Copies failing(1);
2259 if (table.insert(key) !=
nullptr)
2262 catch (
const std::runtime_error &)
2264 EXPECT_EQ(
oracle.count(
k), 0u);
2269 if (step % 500 == 0)
2276 EXPECT_TRUE(table.is_empty());
2279 template <
class Table>
struct OpenTableWithCopyOnlyKeys
2280 :
public OpenTableInsertionThatThrows<Table> {};
2282 using Tables_With_Copy_Only_Keys =
2289 TYPED_TEST(OpenTableWithCopyOnlyKeys, ShrinkThatFailsKeepsTheCapacity)
2291 using Key =
typename TypeParam::Key_Type;
2292 TypeParam table = this->make(11, 0.95f,
true);
2293 for (
long k = 0;
k < 1000; ++
k)
2294 ASSERT_NE(table.insert(Key(
k)),
nullptr) <<
"key " <<
k;
2295 const size_t grown = table.capacity();
2298 Failing_Copies failing;
2299 for (
long k = 0;
k < 990; ++
k)
2301 ASSERT_NO_THROW(table.remove(Key(
k))) <<
"key " <<
k;
2303 ASSERT_NO_THROW(table.remove_ptr(table.search(Key(
k)))) <<
"key " <<
k;
2305 EXPECT_EQ(table.capacity(), grown);
2306 EXPECT_EQ(this->keys_of(table).
size(), 10u);
2307 for (
long k = 990;
k < 1000; ++
k)
2308 EXPECT_TRUE(table.contains(Key(
k))) <<
"key " <<
k;
2311 table.remove(Key(990));
2312 EXPECT_LT(table.capacity(), grown);
2313 for (
long k = 991;
k < 1000; ++
k)
2314 EXPECT_TRUE(table.contains(Key(
k))) <<
"key " <<
k;
2320 TYPED_TEST(OpenTableWithCopyOnlyKeys, MovedInKeyThatThrowsLeavesNoEntry)
2322 for (
const bool resizes : {
false,
true})
2324 TypeParam table = this->with_keys_and_removals(resizes);
2325 const auto keys = this->keys_of(table);
2326 const auto probes = this->probes_of(table);
2328 for (
long k = 0;
k < 60; ++
k)
2329 for (
int operation = 0; operation < 3; ++operation)
2331 Copy_Only_Key key(
k);
2332 const bool present = table.contains(key);
2335 Failing_Copies failing;
2340 case 0: (void) table.insert(std::move(key));
break;
2342 (void) table.search_or_insert(std::move(key));
2345 (void) table.contains_or_insert(std::move(key));
2349 catch (
const std::runtime_error &)
2354 ASSERT_EQ(threw, not present)
2355 <<
"key " <<
k <<
", operation " << operation;
2356 ASSERT_EQ(key.value,
k) <<
"the argument was not touched";
2357 ASSERT_EQ(this->keys_of(table),
keys)
2358 <<
"key " <<
k <<
", operation " << operation;
2359 ASSERT_EQ(this->probes_of(table), probes)
2360 <<
"key " <<
k <<
", operation " << operation;
2363 for (
long k = 20;
k < 28; ++
k)
2364 EXPECT_NE(table.insert(Copy_Only_Key(
k)),
nullptr) <<
"key " <<
k;
2365 for (
long k = 20;
k < 28; ++
k)
2366 EXPECT_TRUE(table.contains(Copy_Only_Key(
k))) <<
"key " <<
k;
2373 TYPED_TEST(OpenTableWithCopyOnlyKeys, GrowthThatFailsHalfwayLeavesTheTable)
2377 for (
size_t successful = 2; successful < 8; ++successful)
2379 TypeParam table = this->make(11, 0.5f,
true);
2380 for (
long k = 0;
k < 5; ++
k)
2381 ASSERT_NE(table.insert(Copy_Only_Key(
k)),
nullptr);
2382 ASSERT_EQ(table.capacity(), 11u);
2383 const auto keys = this->keys_of(table);
2384 const auto probes = this->probes_of(table);
2386 const Copy_Only_Key key(5);
2388 Failing_Copies failing(successful);
2389 EXPECT_THROW((
void) table.insert(key), std::runtime_error)
2390 << successful <<
" copies";
2393 EXPECT_EQ(this->keys_of(table),
keys) << successful <<
" copies";
2394 EXPECT_EQ(table.size(), 5u) << successful <<
" copies";
2395 EXPECT_EQ(table.capacity(), 11u) << successful <<
" copies";
2396 EXPECT_EQ(this->probes_of(table), probes) << successful <<
" copies";
2398 ASSERT_NE(table.insert(key),
nullptr);
2399 EXPECT_GT(table.capacity(), 11u);
2400 for (
long k = 0;
k < 6; ++
k)
2401 EXPECT_TRUE(table.contains(Copy_Only_Key(
k))) <<
"key " <<
k;
2406 TYPED_TEST(OpenTableWithCopyOnlyKeys, RebuildThatFailsLeavesTheTable)
2408 TypeParam table = this->with_keys_and_removals(
false);
2409 const auto keys = this->keys_of(table);
2410 const auto probes = this->probes_of(table);
2411 const size_t capacity = table.capacity();
2414 for (
size_t successful = 0; successful <
keys.size(); ++successful)
2417 Failing_Copies failing(successful);
2418 EXPECT_THROW(table.rehash(), std::runtime_error) << successful;
2420 ASSERT_EQ(this->keys_of(table),
keys) << successful;
2421 ASSERT_EQ(this->probes_of(table), probes) << successful;
2422 ASSERT_EQ(table.capacity(), capacity) << successful;
2425 Failing_Copies failing(successful);
2426 EXPECT_THROW((
void) table.resize(larger), std::runtime_error)
2429 ASSERT_EQ(this->keys_of(table),
keys) << successful;
2430 ASSERT_EQ(this->probes_of(table), probes) << successful;
2431 ASSERT_EQ(table.capacity(), capacity) << successful;
2435 EXPECT_EQ(this->keys_of(table),
keys);
2436 EXPECT_EQ(table.stats().num_deleted, 0u);
2437 EXPECT_EQ(table.resize(larger), larger);
2438 EXPECT_EQ(this->keys_of(table),
keys);
2445 template <
class Table>
struct OpenTableWithoutResizing
2446 :
public ::testing::Test
2448 static Table make(
const size_t len)
2450 if constexpr (
requires (
Table & t) { t.get_second_hash_fct(); })
2451 return Table(len, Aleph::dft_hash_ptr_fct<long>,
2464 for (
long k = 0;
k < 1000
and table.
insert(
k) !=
nullptr; ++
k)
2470 using Open_Tables_Of_Longs =
2477 TYPED_TEST(OpenTableWithoutResizing, KeepsOneBucketEmpty)
2479 TypeParam table = this->make(11);
2480 ASSERT_EQ(table.capacity(), 11u);
2482 EXPECT_EQ(this->
fill(table), 10u);
2483 EXPECT_EQ(table.size(), 10u);
2484 EXPECT_EQ(table.capacity(), 11u);
2485 EXPECT_EQ(table.stats().num_empty, 1u);
2487 const long absent = 500;
2488 EXPECT_EQ(table.insert(absent),
nullptr);
2489 EXPECT_EQ(table.insert(
long(absent)),
nullptr);
2490 EXPECT_FALSE(table.contains(absent));
2491 EXPECT_EQ(table.size(), 10u);
2495 EXPECT_NE(table.insert(absent),
nullptr);
2496 EXPECT_EQ(table.insert(3L),
nullptr);
2497 EXPECT_EQ(table.size(), 10u);
2498 EXPECT_EQ(table.stats().num_busy, 10u);
2503 TYPED_TEST(OpenTableWithoutResizing, FullTableFindsItsKeysAndRefusesNewOnes)
2505 TypeParam table = this->make(11);
2506 ASSERT_EQ(this->
fill(table), 10u);
2507 const long present = 3, absent = 500;
2508 long *
const stored = table.
search(present);
2509 ASSERT_NE(stored,
nullptr);
2511 EXPECT_EQ(table.search_or_insert(present), stored);
2512 EXPECT_EQ(table.search_or_insert(
long(present)), stored);
2513 EXPECT_EQ(table.contains_or_insert(present),
2514 std::make_pair(stored,
true));
2515 EXPECT_EQ(table.contains_or_insert(
long(present)),
2516 std::make_pair(stored,
true));
2517 EXPECT_EQ(&table[present], stored);
2519 EXPECT_EQ(table.search_or_insert(absent),
nullptr);
2520 EXPECT_EQ(table.search_or_insert(
long(absent)),
nullptr);
2521 EXPECT_EQ(table.contains_or_insert(absent),
2522 std::make_pair(
static_cast<long *
>(
nullptr),
false));
2523 EXPECT_EQ(table.contains_or_insert(
long(absent)),
2524 std::make_pair(
static_cast<long *
>(
nullptr),
false));
2525 EXPECT_THROW((
void) table[absent], std::overflow_error);
2527 EXPECT_EQ(table.size(), 10u);
2528 EXPECT_FALSE(table.contains(absent));
2531 template <
class Map>
struct OpenMapWithoutResizing :
public ::testing::Test
2538 TYPED_TEST(OpenMapWithoutResizing, SubscriptOnAFullMapThrows)
2540 TypeParam map(11, Aleph::dft_hash_ptr_fct<long>,
2543 for (
long k = 0;
k < 10; ++
k)
2544 ASSERT_NE(map.insert(
k,
static_cast<int>(
k)),
nullptr) <<
"key " <<
k;
2545 EXPECT_EQ(map.insert(10L, 10),
nullptr);
2548 const long present = 4;
2550 EXPECT_EQ(map.find(3L), 30);
2551 EXPECT_EQ(map.find(4L), 40);
2553 const long absent = 500;
2554 EXPECT_THROW((
void) map[absent], std::overflow_error);
2555 EXPECT_THROW((
void) map[501L], std::overflow_error);
2556 EXPECT_EQ(map.size(), 10u);
2557 EXPECT_FALSE(map.contains(absent));
2560 template <
class Map>
struct OpenMapShrinking :
public ::testing::Test
2567 TYPED_TEST(OpenMapShrinking, RemoveShrinksTheMap)
2569 TypeParam map(11, Aleph::dft_hash_ptr_fct<long>,
2572 for (
long k = 0;
k < 1000; ++
k)
2573 ASSERT_NE(map.insert(
k,
static_cast<int>(
k)),
nullptr) <<
"key " <<
k;
2574 const size_t grown = map.capacity();
2576 for (
long k = 0;
k < 990; ++
k)
2579 EXPECT_LT(map.capacity(), grown / 4);
2580 EXPECT_EQ(map.size(), 10u);
2581 for (
long k = 990;
k < 1000; ++
k)
2582 EXPECT_EQ(map.find(
k),
static_cast<int>(
k)) <<
"key " <<
k;
2589 template <
class Table>
struct OpenTableShrinking :
public ::testing::Test
2591 static Table make(
const size_t len,
const float lower,
const float upper)
2593 if constexpr (
requires (
Table & t) { t.get_second_hash_fct(); })
2594 return Table(len, Aleph::dft_hash_ptr_fct<long>,
2596 lower, upper,
true);
2599 lower, upper,
true);
2608 TYPED_TEST(OpenTableShrinking, KeysFitInTheSmallerTable)
2610 TypeParam table = this->make(101, 0.6f, 0.9f);
2611 for (
long k = 0;
k < 60; ++
k)
2612 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
2613 ASSERT_EQ(table.capacity(), 101u);
2615 for (
long k = 0;
k < 55; ++
k)
2617 ASSERT_NO_THROW(table.remove_ptr(table.search(
k))) <<
"key " <<
k;
2618 ASSERT_FALSE(table.contains(
k)) <<
"key " <<
k;
2619 ASSERT_EQ(table.size(),
static_cast<size_t>(59 -
k));
2620 ASSERT_LT(table.current_alpha(), 0.9f) <<
"key " <<
k;
2621 ASSERT_LT(table.size(), table.capacity()) <<
"key " <<
k;
2624 EXPECT_LT(table.capacity(), 101u);
2625 for (
long k = 55;
k < 60; ++
k)
2626 EXPECT_TRUE(table.contains(
k)) <<
"key " <<
k;
2631 TYPED_TEST(OpenTableShrinking, ShrinksWhenTheKeysLeaveOneBucketEmpty)
2633 TypeParam table = this->make(11, 0.6f, 0.9f);
2634 for (
long k = 0;
k < 7; ++
k)
2635 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
2636 ASSERT_EQ(table.capacity(), 11u);
2638 table.remove_ptr(table.search(0L));
2640 EXPECT_EQ(table.capacity(), 7u);
2641 EXPECT_EQ(table.size(), 6u);
2642 EXPECT_EQ(table.stats().num_empty, 1u);
2643 for (
long k = 1;
k < 7; ++
k)
2644 EXPECT_TRUE(table.contains(
k)) <<
"key " <<
k;
2648 TYPED_TEST(OpenTableShrinking, DefaultThresholdsStillShrink)
2652 for (
long k = 0;
k < 1000; ++
k)
2653 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
2654 const size_t grown = table.capacity();
2656 for (
long k = 0;
k < 990; ++
k)
2657 table.remove_ptr(table.search(
k));
2659 EXPECT_LT(table.capacity(), grown / 4);
2660 EXPECT_EQ(table.size(), 10u);
2661 for (
long k = 990;
k < 1000; ++
k)
2662 EXPECT_TRUE(table.contains(
k)) <<
"key " <<
k;
2667 TYPED_TEST(OpenTableShrinking, RemoveShrinksTooWhileTheTableResizes)
2671 for (
long k = 0;
k < 1000; ++
k)
2672 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
2673 const size_t grown = table.capacity();
2676 table.set_with_resize(
false);
2677 EXPECT_FALSE(table.get_with_resize());
2678 for (
long k = 0;
k < 900; ++
k)
2680 EXPECT_EQ(table.capacity(), grown);
2682 table.set_with_resize(
true);
2683 EXPECT_TRUE(table.get_with_resize());
2684 for (
long k = 900;
k < 990; ++
k)
2686 EXPECT_LT(table.capacity(), grown / 4);
2687 EXPECT_EQ(table.size(), 10u);
2688 for (
long k = 990;
k < 1000; ++
k)
2689 EXPECT_TRUE(table.contains(
k)) <<
"key " <<
k;
2694 TYPED_TEST(OpenTableShrinking, RemoveWaitsUntilTheKeysFit)
2696 TypeParam table = this->make(101, 0.6f, 0.9f);
2697 for (
long k = 0;
k < 60; ++
k)
2698 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
2699 ASSERT_EQ(table.capacity(), 101u);
2701 for (
long k = 0;
k < 55; ++
k)
2703 ASSERT_NO_THROW(table.remove(
k)) <<
"key " <<
k;
2704 ASSERT_EQ(table.size(),
static_cast<size_t>(59 -
k));
2705 ASSERT_LT(table.current_alpha(), 0.9f) <<
"key " <<
k;
2706 ASSERT_LT(table.size(), table.capacity()) <<
"key " <<
k;
2709 EXPECT_LT(table.capacity(), 101u);
2710 for (
long k = 55;
k < 60; ++
k)
2711 EXPECT_TRUE(table.contains(
k)) <<
"key " <<
k;
2716 TYPED_TEST(OpenTableShrinking, IteratorDelDoesNotShrink)
2720 for (
long k = 0;
k < 1000; ++
k)
2721 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
2722 const size_t grown = table.capacity();
2724 auto it = table.get_it();
2725 for (
size_t deleted = 0; deleted < 990; ++deleted)
2727 ASSERT_TRUE(it.has_curr());
2731 EXPECT_EQ(table.capacity(), grown);
2732 EXPECT_EQ(table.size(), 10u);
2737 TYPED_TEST(OpenTableShrinking, TurningResizingOnLetsAFullTableGrow)
2741 EXPECT_TRUE(table.get_with_resize());
2742 table.set_with_resize(
false);
2743 for (
long k = 0;
k < 10; ++
k)
2744 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
2745 EXPECT_EQ(table.insert(10L),
nullptr);
2747 table.set_with_resize(
true);
2748 ASSERT_NE(table.insert(10L),
nullptr);
2749 EXPECT_GT(table.capacity(), 11u);
2750 for (
long k = 0;
k <= 10; ++
k)
2751 EXPECT_TRUE(table.contains(
k)) <<
"key " <<
k;
2763 size_t colliding_hash(
const long &
k)
noexcept
2765 return static_cast<size_t>(
k);
2768 size_t colliding_second_hash(
const long &
k)
noexcept
2770 return static_cast<size_t>(
k / 7 +
k % 5);
2773 template <
class Table>
struct OpenTableRemovedKeys :
public ::testing::Test
2775 static Table make(
const size_t len,
const bool resizes)
2777 if constexpr (
requires (
Table & t) { t.get_second_hash_fct(); })
2778 return Table(len, colliding_hash, colliding_second_hash,
2782 0.05f, 0.9f, resizes);
2787 static bool under_threshold(
const Table & table)
2789 const auto stats = table.
stats();
2790 return 1.0f*(stats.num_busy + stats.num_deleted)/table.
capacity() < 0.9f;
2793 static ::testing::AssertionResult holds(
const Table & table,
2794 const std::set<long> &
keys)
2797 return ::testing::AssertionFailure()
2798 <<
"size is " << table.
size() <<
", expected " <<
keys.size();
2799 for (
const long k :
keys)
2801 return ::testing::AssertionFailure() <<
"key " <<
k <<
" not found";
2802 return ::testing::AssertionSuccess();
2810 TYPED_TEST(OpenTableRemovedKeys, ResizingTableStaysUnderItsThreshold)
2812 std::mt19937
rng(20261018);
2813 for (
int trial = 0; trial < 300; ++trial)
2815 TypeParam table = this->make(11,
true);
2816 std::set<long>
keys;
2817 for (
int step = 0; step < 300; ++step)
2819 const long k =
static_cast<long>(
rng() % 200);
2820 if (
keys.count(
k) == 0)
2822 ASSERT_NE(table.insert(
k),
nullptr);
2830 ASSERT_GT(table.stats().num_empty, 0u)
2831 <<
"trial " << trial <<
", step " << step;
2832 ASSERT_TRUE(this->under_threshold(table))
2833 <<
"trial " << trial <<
", step " << step;
2835 ASSERT_TRUE(this->holds(table,
keys)) <<
"trial " << trial;
2842 TYPED_TEST(OpenTableRemovedKeys, FixedTableNeverMovesItsKeys)
2844 std::mt19937
rng(20261018);
2845 for (
int trial = 0; trial < 300; ++trial)
2847 TypeParam table = this->make(11,
false);
2848 std::map<long, long *>
keys;
2849 for (
int step = 0; step < 300; ++step)
2851 const long k =
static_cast<long>(
rng() % 200);
2852 if (
keys.count(
k) == 0)
2854 long *
const stored = table.insert(
k);
2855 const bool full =
keys.size() == 10;
2856 ASSERT_EQ(stored ==
nullptr, full)
2857 <<
"trial " << trial <<
", step " << step;
2865 ASSERT_EQ(table.search(
k),
nullptr);
2867 ASSERT_EQ(table.capacity(), 11u);
2868 ASSERT_EQ(table.size(),
keys.size());
2869 for (
const auto & [key, where] :
keys)
2870 ASSERT_EQ(table.
search(key), where)
2871 <<
"key " << key <<
" moved, trial " << trial <<
", step " << step;
2879 TYPED_TEST(OpenTableRemovedKeys, RebuildsInPlaceWhileTheKeysAreFew)
2881 TypeParam table = with_removed_keys<TypeParam>(60);
2882 std::set<long>
keys = {1002};
2883 for (
long b = 62; b <= 91; ++b)
2885 ASSERT_EQ(table.stats().num_deleted, 60u) <<
"bucket " << b;
2886 ASSERT_NE(table.insert(1000*b + 999),
nullptr) <<
"bucket " << b;
2887 keys.insert(1000*b + 999);
2890 EXPECT_EQ(table.capacity(), 101u);
2891 EXPECT_EQ(table.stats().num_deleted, 0u);
2892 EXPECT_TRUE(this->holds(table,
keys));
2897 TYPED_TEST(OpenTableRemovedKeys, GrowsWhenTheKeysAreMany)
2899 TypeParam table = with_removed_keys<TypeParam>(40);
2900 std::set<long>
keys = {1002};
2901 for (
long b = 42; b <= 91; ++b)
2903 ASSERT_EQ(table.capacity(), 101u) <<
"bucket " << b;
2904 ASSERT_NE(table.insert(1000*b + 999),
nullptr) <<
"bucket " << b;
2905 keys.insert(1000*b + 999);
2908 EXPECT_GT(table.capacity(), 101u);
2909 EXPECT_EQ(table.stats().num_deleted, 0u);
2910 EXPECT_TRUE(this->holds(table,
keys));
2918 for (
const size_t num_keys : {size_t(30), size_t(60)})
2922 std::deque<long> order;
2923 size_t rebuilt_in_place = 0;
2924 for (
long k = 0;
k < 3000; ++
k)
2926 const size_t capacity = table.capacity();
2927 const size_t deleted = table.stats().num_deleted;
2928 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
2929 if (table.capacity() == capacity
and deleted > 1
2930 and table.stats().num_deleted == 0)
2935 table.remove(order.front());
2938 const auto stats = table.stats();
2939 ASSERT_LT(1.0f*(stats.num_busy + stats.num_deleted)/table.capacity(),
2940 0.9f) <<
"key " <<
k;
2944 for (
const long k : order)
2945 EXPECT_TRUE(table.
contains(
k)) <<
"key " <<
k;
2948 EXPECT_GT(rebuilt_in_place, 1u);
2949 EXPECT_EQ(table.capacity(), 101u);
2952 EXPECT_GT(table.capacity(), 101u);
2960 template <
class Table>
struct OpenTableIteratorDel :
public ::testing::Test
2967 TYPED_TEST(OpenTableIteratorDel, DeletingTheLastKeyLeavesTheIteratorAtTheEnd)
2970 TypeParam table = OpenTableRemovedKeys<TypeParam>::make(11,
false);
2971 ASSERT_NE(table.insert(2L),
nullptr);
2972 ASSERT_NE(table.insert(9L),
nullptr);
2974 auto it = table.get_it();
2975 ASSERT_EQ(it.get_curr(), 2L);
2977 ASSERT_EQ(it.get_curr(), 9L);
2980 EXPECT_FALSE(it.has_curr());
2981 EXPECT_EQ(table.size(), 1u);
2982 EXPECT_FALSE(table.contains(9L));
2984 ASSERT_TRUE(it.has_curr());
2985 EXPECT_EQ(it.get_curr(), 2L);
2992 template <
class Table>
struct OpenTableRandomOperations :
public ::testing::Test
2994 static Table make(
const size_t len,
const float lower,
const float upper,
2997 if constexpr (
requires (
Table & t) { t.get_second_hash_fct(); })
2998 return Table(len, colliding_hash, colliding_second_hash,
3002 lower, upper, resizes);
3017 TYPED_TEST(OpenTableRandomOperations, AgreesWithAnOracle)
3019 const std::pair<float, float> thresholds[] =
3020 {{0.05f, 0.9f}, {0.25f, 0.9f}, {0.6f, 0.9f}, {0.85f, 0.9f}, {0.01f, 0.99f}};
3021 const size_t lens[] = {2, 3, 5, 7, 11, 13};
3022 std::mt19937
rng(20261021);
3023 for (
int trial = 0; trial < 400; ++trial)
3025 const auto [lower, upper] = thresholds[
rng() % 5];
3026 TypeParam table = this->make(lens[
rng() % 6], lower, upper,
3029 std::map<long, const long *> places;
3030 for (
int step = 0; step < 300; ++step)
3032 const auto where = [&] { return ::testing::Message()
3033 <<
"trial " << trial <<
", step " << step; };
3034 const long k =
static_cast<long>(
rng() % 40);
3035 const unsigned op =
rng() % 10;
3036 const bool resizes = table.get_with_resize();
3037 const size_t capacity = table.capacity();
3038 const bool present =
oracle.count(
k) > 0;
3039 const bool full = table.size() + 1 == capacity;
3043 long * stored =
nullptr;
3046 stored = table.insert(
k);
3050 stored = table.insert(std::move(
copy));
3054 stored = table.search_or_insert(
k);
3055 found = present
and stored !=
nullptr;
3058 std::tie(stored, found) = table.contains_or_insert(
k);
3063 ASSERT_EQ(stored,
nullptr) << where();
3066 ASSERT_NE(stored,
nullptr) << where();
3067 ASSERT_TRUE(found) << where();
3070 else if (full
and not resizes)
3072 ASSERT_EQ(stored,
nullptr) << where();
3073 ASSERT_FALSE(found) << where();
3077 ASSERT_NE(stored,
nullptr) << where();
3078 ASSERT_FALSE(found) << where();
3079 ASSERT_EQ(*stored,
k) << where();
3085 const auto stats = table.stats();
3086 ASSERT_LT(1.0f*(stats.num_busy + stats.num_deleted)
3087 /table.capacity(), upper) << where();
3097 ASSERT_THROW(table.remove(
k), std::domain_error) << where();
3099 ASSERT_EQ(table.capacity(), capacity) << where();
3106 table.remove_ptr(table.search(
k));
3110 const size_t n =
oracle.size();
3112 const bool shrinks = resizes
and 1.0f*n/capacity < lower
3113 and n < smaller
and 1.0f*n/smaller < upper;
3114 ASSERT_EQ(table.capacity(), shrinks ? smaller : capacity)
3118 else if (op == 8
and rng() % 4 == 0)
3120 table.set_with_resize(not resizes);
3121 ASSERT_EQ(table.get_with_resize(), not resizes);
3125 for (
const long key :
oracle)
3126 places[key] = table.
search(key);
3130 ASSERT_EQ(table.size(),
oracle.size()) << where();
3131 ASSERT_LT(table.size(), table.capacity()) << where();
3132 for (
long key = 0; key < 40; ++key)
3133 ASSERT_EQ(table.contains(key),
oracle.count(key) > 0)
3134 <<
"key " << key <<
", " << where();
3135 if (not table.get_with_resize()
and not resizes)
3137 ASSERT_EQ(table.capacity(), capacity) << where();
3138 for (
const auto & [key, at] : places)
3139 ASSERT_EQ(table.
search(key), at)
3140 <<
"key " << key <<
" moved, " << where();
3144 std::set<long> listed;
3145 table.for_each([&listed] (
const long key) { listed.insert(key); });
3146 EXPECT_EQ(listed,
oracle) <<
"trial " << trial;
3154 template <
class Table>
struct OpenTableEdges :
public ::testing::Test
3156 static Table fixed(
const size_t len)
3158 return OpenTableRandomOperations<Table>::make(len, 0.05f, 0.9f,
false);
3171 0.05f, 0.9f,
false);
3172 for (
long k = 0;
k < 10; ++
k)
3173 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
3175 ASSERT_NE(table.insert(10L),
nullptr);
3176 for (
const long k : {1L, 2L, 3L, 4L, 5L, 6L})
3178 ASSERT_EQ(table.stats().num_empty, 0u);
3179 ASSERT_EQ(table.size(), 4u);
3181 table.set_with_resize(
true);
3182 ASSERT_NE(table.insert(1000L),
nullptr);
3183 EXPECT_EQ(table.capacity(), 11u);
3184 EXPECT_EQ(table.stats().num_deleted, 0u);
3185 EXPECT_GT(table.stats().num_empty, 0u);
3186 for (
const long k : {7L, 8L, 9L, 10L, 1000L})
3187 EXPECT_TRUE(table.contains(
k)) <<
"key " <<
k;
3196 0.05f, 0.9f,
false);
3197 for (
long k = 0;
k < 10; ++
k)
3198 ASSERT_NE(table.insert(
k),
nullptr) <<
"key " <<
k;
3200 ASSERT_NE(table.insert(10L),
nullptr);
3202 ASSERT_EQ(table.stats().num_empty, 0u);
3203 ASSERT_EQ(table.size(), 9u);
3205 EXPECT_THROW(table.remove(22L), std::domain_error);
3206 EXPECT_THROW(table.remove(21L), std::domain_error);
3208 long *
const p = table.search_or_insert(21L);
3209 ASSERT_NE(p,
nullptr);
3211 EXPECT_EQ(table.size(), 10u);
3212 EXPECT_EQ(table.search_or_insert(43L),
nullptr);
3213 EXPECT_EQ(table.contains_or_insert(43L).first,
nullptr);
3216 ASSERT_EQ(table.stats().num_empty, 0u);
3217 auto [q, found] = table.contains_or_insert(32L);
3218 ASSERT_NE(q,
nullptr);
3219 EXPECT_FALSE(found);
3221 for (
const long k : {1L, 2L, 3L, 4L, 6L, 8L, 9L, 10L, 21L, 32L})
3222 EXPECT_TRUE(table.contains(
k)) <<
"key " <<
k;
3223 EXPECT_FALSE(table.contains(7L));
3228 TYPED_TEST(OpenTableEdges, ResizeRefusesTooFewBuckets)
3230 TypeParam table = this->fixed(11);
3231 for (
long k = 0;
k < 7; ++
k)
3232 ASSERT_NE(table.insert(
k),
nullptr);
3233 EXPECT_THROW(table.resize(7), std::overflow_error);
3234 EXPECT_THROW(table.resize(5), std::overflow_error);
3235 EXPECT_EQ(table.capacity(), 11u);
3236 EXPECT_EQ(table.size(), 7u);
3237 EXPECT_EQ(table.resize(0), 11u);
3238 EXPECT_EQ(table.resize(8), 8u);
3239 for (
long k = 0;
k < 7; ++
k)
3240 EXPECT_TRUE(table.contains(
k)) <<
"key " <<
k;
3247 TYPED_TEST(OpenTableEdges, RemovePtrRefusesForeignAndStalePointers)
3249 TypeParam table = this->fixed(11);
3250 long *
const p = table.insert(3L);
3251 ASSERT_NE(table.insert(4L),
nullptr);
3253 EXPECT_THROW(table.remove_ptr(&outside), std::invalid_argument);
3255 table.remove_ptr(p);
3256 EXPECT_THROW(table.remove_ptr(p), std::domain_error);
3257 EXPECT_EQ(table.size(), 1u);
3259 TypeParam other(std::move(table));
3260 EXPECT_THROW(table.remove_ptr(&outside), std::invalid_argument);
3261 EXPECT_TRUE(other.contains(4L));
3267 TypeParam table = this->fixed(11);
3268 for (
const long k : {2L, 5L, 9L})
3269 ASSERT_NE(table.insert(
k),
nullptr);
3271 auto it = table.get_it();
3272 EXPECT_EQ(it.get_pos(), 0);
3273 const auto & const_it = it;
3274 EXPECT_EQ(const_it.get_curr(), 2L);
3276 EXPECT_FALSE(it.has_curr());
3277 EXPECT_EQ(it.get_pos(), -1);
3278 EXPECT_THROW(it.get_curr(), std::underflow_error);
3279 EXPECT_THROW(it.prev(), std::underflow_error);
3282 EXPECT_EQ(it.get_curr(), 9L);
3283 EXPECT_EQ(it.get_pos(), 2);
3285 EXPECT_FALSE(it.has_curr());
3286 EXPECT_THROW(it.get_curr(), std::overflow_error);
3287 EXPECT_THROW(it.next(), std::overflow_error);
3295 for (
long k = 0;
k < 5; ++
k)
3296 ASSERT_NE(table.insert(
k * 11),
nullptr);
3297 long & second = *table.search(11L);
3298 table.remove(second);
3299 EXPECT_THROW(table.remove(second), std::domain_error);
3300 EXPECT_EQ(table.size(), 4u);
3304 TYPED_TEST(OpenMapShrinking, ValuesAndItemsByPointer)
3306 TypeParam map(11, Aleph::dft_hash_ptr_fct<long>,
3309 for (
long k = 0;
k < 5; ++
k)
3310 ASSERT_NE(map.insert(
k,
static_cast<int>(10 *
k)),
nullptr);
3312 std::set<int> values;
3313 map.values_ptr().for_each([&values] (
int * v) { values.insert(*v); });
3314 EXPECT_EQ(values, (std::set<int>{0, 10, 20, 30, 40}));
3316 std::set<long>
keys;
3317 map.items_ptr().for_each([&
keys] (
auto * item)
3319 keys.insert(item->first);
3322 EXPECT_EQ(
keys, (std::set<long>{0, 1, 2, 3, 4}));
3323 EXPECT_EQ(map.find(3L), 31);
3331 template <
class Table>
size_t busy_lists(
const Table & table)
3333 const auto stats = table.
stats();
3334 return table.
capacity() - (stats.lens.size() > 0 ? stats.lens(0) : 0);
3351 const std::pair<float, float> thresholds[] =
3352 {{0.25f, 0.9f}, {0.5f, 2.0f}, {0.01f, 0.99f}, {0.85f, 0.9f}};
3353 std::mt19937
rng(20261021);
3354 for (
int trial = 0; trial < 200; ++trial)
3356 const auto [lower, upper] = thresholds[
rng() % 4];
3357 const bool resizing =
rng() % 2 == 0;
3361 std::map<long, Bucket *>
oracle;
3362 for (
int step = 0; step < 300; ++step)
3364 const auto where = [&] { return ::testing::Message()
3365 <<
"trial " << trial <<
", step " << step; };
3366 const long k =
static_cast<long>(
rng() % 60);
3367 const unsigned op =
rng() % 9;
3369 const size_t capacity = table.
capacity();
3370 const bool present =
oracle.count(
k) > 0;
3374 auto * bucket =
new Bucket(
k);
3379 ASSERT_EQ(stored, op < 2 ?
nullptr :
oracle[
k]) << where();
3384 ASSERT_EQ(stored, bucket) << where();
3388 const auto load = [&] (
const size_t c)
3389 {
return static_cast<float>(1.0*
oracle.size()/c); };
3391 if (resizes
and load(capacity) >= upper)
3404 const size_t n =
oracle.size();
3406 const bool shrinks = resizes
3407 and static_cast<float>(1.0*n/capacity) < lower
3408 and smaller < capacity
and 1.0f*n/smaller < upper;
3409 ASSERT_EQ(table.
capacity(), shrinks ? smaller : capacity)
3413 else if (
rng() % 4 == 0)
3416 ASSERT_EQ(table.
size(),
oracle.size()) << where();
3417 ASSERT_EQ(table.get_num_busy_slots(), busy_lists(table)) << where();
3418 for (
long key = 0; key < 60; ++key)
3419 ASSERT_EQ(table.
search(key),
3421 <<
"key " << key <<
", " << where();
3423 for (
auto & [key, bucket] :
oracle)
3424 delete table.
remove(bucket);
3436 EXPECT_FALSE(empty_it.has_curr());
3437 empty_it.reset_last();
3438 EXPECT_FALSE(empty_it.has_curr());
3440 for (
const long k : {2L, 5L, 9L})
3442 EXPECT_EQ(table.
resize(0), 11u);
3445 EXPECT_EQ(it.get_curr()->get_key(), 2L);
3447 EXPECT_FALSE(it.has_curr());
3448 EXPECT_THROW(it.prev(), std::underflow_error);
3450 EXPECT_EQ(it.get_curr()->get_key(), 9L);
3452 EXPECT_FALSE(it.has_curr());
3453 EXPECT_THROW(it.next(), std::overflow_error);
3461 template <
class Map>
struct DynMapHashTables :
public ::testing::Test {};
3463 using Dyn_Hash_Maps = ::testing::Types<DynMapHash<long, int>,
3467 TYPED_TEST(DynMapHashTables, ValuesAndItemsByPointerAndSelfMove)
3470 for (
long k = 0;
k < 5; ++
k)
3471 map.insert(
k,
static_cast<int>(10 *
k));
3473 std::set<int> values;
3474 map.values_ptr().for_each([&values] (
int * v) { values.insert(*v); });
3475 EXPECT_EQ(values, (std::set<int>{0, 10, 20, 30, 40}));
3477 std::set<long>
keys;
3478 map.items_ptr().for_each([&
keys] (
auto * item)
3480 keys.insert(item->first);
3483 EXPECT_EQ(
keys, (std::set<long>{0, 1, 2, 3, 4}));
3484 EXPECT_EQ(map.find(3L), 31);
3486 TypeParam & same = map;
3487 map = std::move(same);
3488 EXPECT_EQ(map.size(), 5u);
3489 EXPECT_EQ(map.find(4L), 41);
3501 for (
int i = 0; i < 20; ++i)
3503 const size_t capacity = src.
capacity();
3507 ASSERT_EQ(dst.
size(), 20u);
3508 EXPECT_EQ(dst.
capacity(), capacity);
3509 EXPECT_EQ(dst.
search(7), where);
3510 EXPECT_EQ(src.
size(), 0u);
3513 EXPECT_EQ(src.
search(3),
nullptr);
3519 EXPECT_NE(src.
search(3),
nullptr);
3520 EXPECT_EQ(src.
search(4),
nullptr);
3522 src = std::move(dst);
3523 EXPECT_EQ(src.
size(), 20u);
3524 EXPECT_EQ(src.
search(7), where);
3525 EXPECT_EQ(dst.
size(), 0u);
3529 EXPECT_EQ(dst.
search(8), mine);
3530 EXPECT_EQ(dst.
resize(31), 31u);
3531 EXPECT_EQ(dst.
search(8), mine);
3537 size_t first_hash_calls = 0;
3538 size_t second_hash_calls = 0;
3540 size_t first_counted_hash(
const int &
k)
noexcept
3543 return static_cast<size_t>(
k);
3546 size_t second_counted_hash(
const int &
k)
noexcept
3548 ++second_hash_calls;
3549 return static_cast<size_t>(
k) * 31u;
3559 for (
int i = 0; i < 20; ++i)
3563 first_hash_calls = second_hash_calls = 0;
3564 for (
int i = 0; i < 20; ++i)
3565 ASSERT_NE(dst.
search(i),
nullptr);
3566 EXPECT_EQ(first_hash_calls, 20u);
3568 first_hash_calls = 0;
3570 EXPECT_NE(src.
search(7),
nullptr);
3571 EXPECT_EQ(first_hash_calls, 2u);
3582 for (
int i = 0; i < 20; ++i)
3586 dst = std::move(src);
3587 ASSERT_EQ(dst.
size(), 20u);
3588 EXPECT_EQ(dst.
search(100),
nullptr);
3589 EXPECT_EQ(src.
size(), 0u);
3592 first_hash_calls = second_hash_calls = 0;
3593 for (
int i = 0; i < 20; ++i)
3594 ASSERT_NE(dst.
search(i),
nullptr);
3595 EXPECT_EQ(first_hash_calls, 20u);
3596 EXPECT_EQ(second_hash_calls, 0u);
3599 EXPECT_NE(src.
search(7),
nullptr);
3600 EXPECT_EQ(second_hash_calls, 2u);
3601 EXPECT_EQ(first_hash_calls, 20u);
3611 for (
int i = 0; i < 5; ++i)
3618 for (
Bucket * bucket : mine)
3619 ASSERT_NE(dst.
insert(bucket),
nullptr);
3620 ASSERT_EQ(dst.
size(), 3u);
3622 dst = std::move(src);
3623 for (
Bucket * bucket : mine)
3626 ASSERT_EQ(dst.
size(), 5u);
3627 for (
int i = 0; i < 5; ++i)
3628 EXPECT_NE(dst.
search(i),
nullptr);
3629 EXPECT_EQ(dst.
search(100),
nullptr);
3630 EXPECT_EQ(src.
size(), 0u);
3633 EXPECT_EQ(src.
search(100),
nullptr);
3640 struct Copied_Not_Moved_Eq
3644 Copied_Not_Moved_Eq() =
default;
3646 Copied_Not_Moved_Eq(
const Copied_Not_Moved_Eq &) =
default;
3648 Copied_Not_Moved_Eq & operator = (
const Copied_Not_Moved_Eq &) =
default;
3650 Copied_Not_Moved_Eq(Copied_Not_Moved_Eq &&) =
delete;
3652 bool operator () (
int a,
int b)
const noexcept {
return a == b; }
3655 static_assert(EqualityComparator<Copied_Not_Moved_Eq, int>);
3656 static_assert(not std::is_move_constructible_v<Copied_Not_Moved_Eq>);
3665# if !defined(_MSC_VER) || defined(__clang__)
3670 template <
class Table>
3671 void expect_that_the_comparators_are_swapped()
3684 ASSERT_EQ(a.
size(), 2u);
3685 ASSERT_EQ(b.
size(), 1u);
3686 EXPECT_NE(a.
search(3),
nullptr);
3687 EXPECT_NE(b.
search(1),
nullptr);
3692 ASSERT_EQ(a.
size(), 1u);
3693 EXPECT_NE(a.
search(1),
nullptr);
3694 EXPECT_EQ(b.
size(), 0u);
3698 EXPECT_NE(b.
search(5),
nullptr);
3704# if !defined(_MSC_VER) || defined(__clang__)
3710 expect_that_the_comparators_are_swapped<LhashTable<int, Copied_Not_Moved_Eq>>();
3711 expect_that_the_comparators_are_swapped<LhashTableVtl<int, Copied_Not_Moved_Eq>>();
3719 ASSERT_NE(a.
insert(1, 10),
nullptr);
3720 ASSERT_NE(b.
insert(2, 20),
nullptr);
3721 ASSERT_NE(b.
insert(3, 30),
nullptr);
3726 ASSERT_NE(a.
search(3),
nullptr);
3727 EXPECT_EQ(*a.
search(3), 30);
3732 ASSERT_NE(a.
search(1),
nullptr);
3733 EXPECT_EQ(*a.
search(1), 10);
3734 EXPECT_EQ(b.
size(), 0u);
3735 ASSERT_NE(b.
insert(5, 50),
nullptr);
3736 EXPECT_NE(b.
search(5),
nullptr);
3744 b.get_compare().id = 2;
3745 ASSERT_NE(a.insert(1),
nullptr);
3746 ASSERT_NE(b.insert(2),
nullptr);
3747 ASSERT_NE(b.insert(3),
nullptr);
3750 EXPECT_EQ(a.get_compare().id, 2);
3751 EXPECT_EQ(b.get_compare().id, 1);
3752 EXPECT_NE(a.search(3),
nullptr);
3755 EXPECT_EQ(a.get_compare().id, 1);
3756 EXPECT_EQ(b.get_compare().id, 2);
3757 EXPECT_NE(a.search(1),
nullptr);
3758 EXPECT_EQ(b.size(), 0u);
3759 ASSERT_NE(b.insert(5),
nullptr);
3760 EXPECT_NE(b.search(5),
nullptr);
3762 Set c(std::move(a));
3763 EXPECT_EQ(c.get_compare().id, 1);
3764 EXPECT_NE(c.search(1),
nullptr);
3772TEST(HashTables, TheyAreMovedWithAComparatorThatCannotBeMoved)
3774 auto check_table = [] (
auto a)
3776 using Table =
decltype(a);
3780 ASSERT_NE(a.
insert(1),
nullptr);
3781 ASSERT_NE(b.
insert(2),
nullptr);
3786 EXPECT_NE(a.
search(2),
nullptr);
3787 EXPECT_NE(b.
search(1),
nullptr);
3789 Table c(std::move(a));
3791 EXPECT_NE(c.
search(2),
nullptr);
3795 EXPECT_NE(b.
search(2),
nullptr);
3801 auto check_map = [] (
auto a)
3803 using Map =
decltype(a);
3804 ASSERT_NE(a.
insert(1, 10),
nullptr);
3805 Map b(std::move(a));
3806 ASSERT_NE(b.search(1),
nullptr);
3810 ASSERT_NE(a.
search(1),
nullptr);
3811 EXPECT_EQ(a.
search(1)->second, 10);
3815# if !defined(_MSC_VER) || defined(__clang__)
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
TYPED_TEST(ArcHeapLifecycle, ClearNullsTheHandles)
size_t size_t int32_t value
size_t size_t int32_t * out
Dlink * del() noexcept
Remove this from the list. this must not be a header node.
void insert(Dlink *node) noexcept
Insert node after this.
bool contains(const Key &key) const noexcept
Return true if the key exists in the table.
Key * insert(const Key &key)
Inserts key into the hash set.
Dynamic hash table mapping keys to records with separate chaining.
Record * search(const Key &key)
Search for a key in the table.
void swap(DynLhashTable &table) noexcept
Swap contents with another table.
Record * insert(const Key &key, const Record &record)
Insert a key-record pair into the table.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
void empty() noexcept
empty the list
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table.
Bucket * insert(Bucket *bucket)
Inserts bucket into the table and returns its address if the key is not already in the table; otherwi...
bool contains(const Key &key) const noexcept
Checks if a key exists in the table.
float current_alpha() const noexcept
return the current table load; zero for a table without lists
const size_t & size() const noexcept
Returns the number of elements contained in the table.
Bucket * search_or_insert(Bucket *bucket)
const size_t & capacity() const noexcept
Returns the table capacity.
void swap(GenLhashTable &other) noexcept
Swaps this table with other in constant time.
Bucket * search(const Key &key) const noexcept
Search in the table for a bucket with key.
constexpr bool is_empty() const noexcept
size_t resize(const size_t new_size)
Resizes the table to new_size lists and places every bucket again.
Generic linear hash table.
const size_t & capacity() const noexcept
Returns the table capacity.
void swap(GenLinearHashTable &other) noexcept
Swaps this table with other in constant time.
DynArray< BucketList > table
Cmp & get_compare() noexcept
const size_t & size() const noexcept
Returns the number of elements in the table.
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table, without freeing it.
BucketType< Key > Bucket
The bucket type.
bool contains(const Key &key) const noexcept
Checks if a key exists in the table.
Bucket * insert(Bucket *bucket)
Insert bucket in the table.
Hash_Fct get_hash_fct() const noexcept
Bucket * search_or_insert(Bucket *bucket)
void set_hash_fct(Hash_Fct fct)
Sets the hash function of an empty table.
Bucket * search(const Key &key) const noexcept
Search for key in the linear hash table.
size_t resize(size_t) noexcept
Provided for generic programming compatibility.
Number Theoretic Transform over Z / MOD Z.
Open addressing hash table with double hashing collision resolution.
void set_second_hash_fct(Hash_Fct fct)
Sets the hash function of the second probe.
void swap(ODhashTable &other) noexcept
Swaps this table with other in constant time.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Open addressing hash table with linear probing collision resolution.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
void swap(OLhashTable &other) noexcept
Swaps this table with other in constant time.
Minimal std::expected-style result type for C++20.
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps(Operation &op) const
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
constexpr float get_upper_alpha() const noexcept
Returns the upper load factor threshold.
void set_with_resize(const bool with_resize) noexcept
Turns the automatic resizing on or off.
Stats stats() const
Computes statistics about chain length distribution.
constexpr bool get_with_resize() const noexcept
Returns whether the table resizes itself.
constexpr float get_lower_alpha() const noexcept
Returns the lower load factor threshold.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Types< MapODhash< size_t, string >, MapOLhash< size_t, string >, DynMapLinHash< size_t, string >, DynMapHash< size_t, string > > HashTypes
REGISTER_TYPED_TEST_CASE_P(EmptyOHashTest, With_exception)
TYPED_TEST_P(EmptyOHashTest, With_exception)
INSTANTIATE_TYPED_TEST_CASE_P(Empty, EmptyOHashTest, HashTypes)
TYPED_TEST_CASE_P(OHashTest)
::testing::Types< OLhashTable< long >, ODhashTable< long > > Open_Tables
MapOLhash< int, Foo > tbl
LinearHashTable< int > Table
Main namespace for Aleph-w library functions.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
const float hash_default_upper_alpha
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
bool contains(const std::string_view &str, const std::string_view &substr)
Check if substr appears inside str.
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
and
Check uniqueness with explicit hash + equality functors.
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
const float hash_default_lower_alpha
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
unsigned long next_prime(unsigned long n)
Find a prime >= n in the database, for hash table sizing.
Hash-based dynamic set (defined in tpl_dynSetHash.H).
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
Open addressing hash map using double hashing.
Open addressing hash map using linear probing.
ValueArg< size_t > num_keys
Dynamic hash table mapping keys to records with separate chaining.
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
Unified hash table interface.
Hash tables with collision resolution by separate chaining.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.