38#include <gtest/gtest.h>
67 EXPECT_EQ(table.
size(), 0u);
80 static_assert(not std::is_copy_constructible_v<LinearHashTable<int>>);
81 static_assert(not std::is_copy_assignable_v<LinearHashTable<int>>);
82 static_assert(not std::is_copy_constructible_v<LinearHashTableVtl<int>>);
83 static_assert(not std::is_copy_assignable_v<LinearHashTableVtl<int>>);
86 static_assert(std::is_copy_constructible_v<DynSetLinHash<int>>);
90 EXPECT_NE(
copy.search(1),
nullptr);
91 EXPECT_NE(set.
search(1),
nullptr);
98 EXPECT_EQ(table.
search(10),
nullptr);
99 EXPECT_EQ(table.
search(0),
nullptr);
100 EXPECT_EQ(table.
search(-1),
nullptr);
111 auto *bucket =
new Bucket(10);
112 auto *inserted = table.
insert(bucket);
114 EXPECT_EQ(inserted, bucket);
115 EXPECT_EQ(table.
size(), 1u);
124 for (
int k : {5, 3, 7, 1, 4, 6, 8})
127 ASSERT_NE(table.
insert(bucket),
nullptr);
130 EXPECT_EQ(table.
size(), 7u);
133 for (
int k : {5, 3, 7, 1, 4, 6, 8})
134 EXPECT_NE(table.
search(
k),
nullptr) <<
"Key " <<
k <<
" not found";
141 auto *bucket1 =
new Bucket(10);
142 auto *bucket2 =
new Bucket(10);
144 EXPECT_NE(table.
insert(bucket1),
nullptr);
145 EXPECT_EQ(table.
insert(bucket2),
nullptr);
147 EXPECT_EQ(table.
size(), 1u);
155 const size_t initial_capacity = table.
capacity();
158 for (
int k = 0;
k < 100; ++
k)
161 EXPECT_EQ(table.
size(), 100u);
162 EXPECT_GT(table.
capacity(), initial_capacity);
166 for (
int k = 0;
k < 100; ++
k)
167 EXPECT_NE(table.
search(
k),
nullptr);
178 for (
int k : {1, 2, 3, 4, 5})
181 for (
int k : {1, 2, 3, 4, 5})
184 ASSERT_NE(found,
nullptr);
185 EXPECT_EQ(found->get_key(),
k);
193 for (
int k : {1, 3, 5})
196 EXPECT_EQ(table.
search(2),
nullptr);
197 EXPECT_EQ(table.
search(4),
nullptr);
198 EXPECT_EQ(table.
search(0),
nullptr);
199 EXPECT_EQ(table.
search(6),
nullptr);
206 auto *bucket1 =
new Bucket(10);
209 auto *bucket2 =
new Bucket(10);
212 EXPECT_EQ(found, bucket1);
213 EXPECT_EQ(table.
size(), 1u);
224 auto *bucket =
new Bucket(10);
227 EXPECT_EQ(result, bucket);
228 EXPECT_EQ(table.
size(), 2u);
229 EXPECT_NE(table.
search(10),
nullptr);
240 for (
int k : {1, 2, 3, 4, 5})
243 auto *found = table.
search(3);
244 ASSERT_NE(found,
nullptr);
246 auto *removed = table.
remove(found);
247 EXPECT_EQ(removed->get_key(), 3);
250 EXPECT_EQ(table.
size(), 4u);
251 EXPECT_EQ(table.
search(3),
nullptr);
258 for (
int k : {5, 3, 7, 1, 4, 6, 8})
261 for (
int k : {5, 3, 7, 1, 4, 6, 8})
264 ASSERT_NE(found,
nullptr) <<
"Key " <<
k <<
" not found";
265 delete table.
remove(found);
268 EXPECT_EQ(table.
size(), 0u);
277 for (
int k = 0;
k < 200; ++
k)
280 const size_t expanded_capacity = table.
capacity();
283 for (
int k = 0;
k < 180; ++
k)
286 ASSERT_NE(found,
nullptr);
287 delete table.
remove(found);
290 EXPECT_EQ(table.
size(), 20u);
292 EXPECT_LE(table.
capacity(), expanded_capacity);
297 size_t identity_hash(
const int &
k)
noexcept
299 return static_cast<size_t>(
k);
303 size_t busy_lists(
const Table & table)
305 const auto stats = table.
stats();
306 return table.
capacity() - (stats.lens.size() > 0 ? stats.lens(0) : 0);
320 const std::pair<float, float> thresholds[] =
321 {{0.25f, 0.9f}, {0.5f, 2.0f}, {0.01f, 0.99f}, {0.85f, 0.9f}};
322 std::mt19937
rng(20261021);
323 for (
int trial = 0; trial < 200; ++trial)
325 const auto [lower, upper] = thresholds[
rng() % 4];
326 const bool resizing =
rng() % 2 == 0;
327 const size_t len = 1 +
rng() % 13;
330 std::map<int, Bucket *>
oracle;
331 for (
int step = 0; step < 400; ++step)
333 const auto where = [&] { return ::testing::Message()
334 <<
"trial " << trial <<
", step " << step; };
335 const int k =
static_cast<int>(
rng() % 80);
336 const unsigned op =
rng() % 9;
338 const size_t capacity = table.
capacity();
339 const bool present =
oracle.count(
k) > 0;
348 ASSERT_EQ(stored, op < 2 ?
nullptr :
oracle[
k]) << where();
353 ASSERT_EQ(stored, bucket) << where();
370 table.
capacity() == len) << where();
374 else if (
rng() % 4 == 0)
379 ASSERT_EQ(table.
capacity(), capacity) << where();
381 ASSERT_GE(table.
capacity(), len) << where();
382 ASSERT_EQ(table.
size(),
oracle.size()) << where();
383 ASSERT_EQ(table.
busy_slots(), busy_lists(table)) << where();
384 for (
int key = 0; key < 80; ++key)
386 <<
"key " << key <<
", " << where();
388 for (
auto & [key, bucket] :
oracle)
389 delete table.
remove(bucket);
400 const size_t initial_capacity = table.
capacity();
402 for (
int k = 0;
k < 200; ++
k)
404 EXPECT_EQ(table.
capacity(), initial_capacity);
406 for (
int k = 0;
k < 200; ++
k)
407 EXPECT_NE(table.
search(
k),
nullptr) <<
"key " <<
k;
413 const size_t expanded_capacity = table.
capacity();
414 EXPECT_GT(expanded_capacity, initial_capacity);
418 for (
int k = 0;
k < 150; ++
k)
420 EXPECT_EQ(table.
capacity(), expanded_capacity);
424 for (
int k = 150;
k < 190; ++
k)
426 EXPECT_LT(table.
capacity(), expanded_capacity);
428 EXPECT_EQ(table.
size(), 11u);
429 for (
int k = 190;
k <= 200; ++
k)
430 EXPECT_NE(table.
search(
k),
nullptr) <<
"key " <<
k;
441 std::set<int> inserted;
442 for (
int k : {5, 3, 7, 1, 4, 6, 8})
448 std::set<int> visited;
450 visited.insert(it.get_curr()->get_key());
452 EXPECT_EQ(visited, inserted);
467 for (
int k : {1, 2, 3, 4, 5})
473 if (it.get_curr()->get_key() == 3)
480 EXPECT_EQ(table.
size(), 4u);
481 EXPECT_EQ(table.
search(3),
nullptr);
488 for (
int k : {1, 2, 3, 4, 5})
505 for (
int k : {1, 2, 3})
524 for (
int k : {1, 2, 3})
531 EXPECT_NE(bucket1,
nullptr);
535 EXPECT_NE(bucket2,
nullptr);
536 EXPECT_NE(bucket1, bucket2);
554 std::vector<Bucket*> buckets1, buckets2;
555 for (
int k : {1, 2, 3})
559 buckets1.push_back(b);
561 for (
int k : {10, 20})
565 buckets2.push_back(b);
570 EXPECT_EQ(table1.
size(), 2u);
571 EXPECT_EQ(table2.
size(), 3u);
573 EXPECT_NE(table1.
search(10),
nullptr);
574 EXPECT_NE(table1.
search(20),
nullptr);
575 EXPECT_NE(table2.
search(1),
nullptr);
576 EXPECT_NE(table2.
search(2),
nullptr);
577 EXPECT_NE(table2.
search(3),
nullptr);
580 for (
auto* b : buckets1)
delete b;
581 for (
auto* b : buckets2)
delete b;
592 for (
int k = 0;
k < 50; ++
k)
595 EXPECT_EQ(table.
size(), 50u);
599 EXPECT_EQ(table.
size(), 0u);
605 for (
int k = 0;
k < 50; ++
k)
607 EXPECT_EQ(table.
busy_slots(), busy_lists(table));
620 for (
int k = 0;
k < 50; ++
k)
623 float expected_alpha = 50.0f / table.
capacity();
633 bool operator()(
int a,
int b)
const {
return (a % 100) == (b % 100); }
637size_t mod_hash(
const int&
k) {
return static_cast<size_t>(std::abs(
k % 100)); }
648 EXPECT_EQ(table.
insert(bucket),
nullptr);
653 EXPECT_NE(table.
search(5),
nullptr);
664 for (
int k : {-5, -3, -1, 0, 1, 3, 5})
667 EXPECT_EQ(table.
size(), 7u);
669 for (
int k : {-5, -3, -1, 0, 1, 3, 5})
670 EXPECT_NE(table.
search(
k),
nullptr);
672 EXPECT_EQ(table.
search(-2),
nullptr);
679 auto *bucket =
new Bucket(42);
682 EXPECT_EQ(table.
size(), 1u);
683 EXPECT_NE(table.
search(42),
nullptr);
685 delete table.
remove(bucket);
687 EXPECT_EQ(table.
size(), 0u);
688 EXPECT_EQ(table.
search(42),
nullptr);
708 0.8f, 0.4f,
true,
true)),
713 0.5f, 0.5f,
true,
true)),
726 std::mt19937
rng(12345);
727 std::uniform_int_distribution<int> dist(0, 999);
730 for (
int i = 0; i < 500; ++i)
743 for (
int i = 0; i < 200; ++i)
748 EXPECT_NE(found,
nullptr);
750 EXPECT_EQ(found,
nullptr);
754 for (
int i = 0; i < 200; ++i)
760 ASSERT_NE(found,
nullptr);
762 delete table.
remove(found);
766 EXPECT_EQ(found,
nullptr);
774 EXPECT_NE(table.
search(
k),
nullptr);
784 for (
int k = 0;
k <
N; ++
k)
787 EXPECT_EQ(table.
size(),
static_cast<size_t>(
N));
790 for (
int k = 0;
k <
N; ++
k)
791 EXPECT_NE(table.
search(
k),
nullptr);
794 for (
int k = 0;
k <
N;
k += 2)
797 ASSERT_NE(found,
nullptr);
798 delete table.
remove(found);
801 EXPECT_EQ(table.
size(),
static_cast<size_t>(
N / 2));
804 for (
int k = 1;
k <
N;
k += 2)
805 EXPECT_NE(table.
search(
k),
nullptr);
816 for (
int k : {1, 2, 3, 4, 5})
819 EXPECT_EQ(table.
size(), 5u);
821 for (
int k : {1, 2, 3, 4, 5})
822 EXPECT_NE(table.
search(
k),
nullptr);
844 EXPECT_NE(table.
search(1),
nullptr);
845 EXPECT_NE(table.
search(2),
nullptr);
846 EXPECT_NE(table.
search(3),
nullptr);
854 EXPECT_NE(hash_fct,
nullptr);
862 EXPECT_TRUE(
cmp(5, 5));
863 EXPECT_FALSE(
cmp(5, 6));
865 const Table& const_table = table;
867 EXPECT_TRUE(const_cmp(10, 10));
879 size_t result = table.
resize(1000);
880 EXPECT_EQ(result, table.
capacity());
bool has_curr() const noexcept
Return true if the iterator has current item.
Key * insert(const Key &key)
Inserts key into the hash set.
Key * search(const Key &key) noexcept
Searches for a key in the hash table.
Bucket * get_curr_ne() noexcept
long get_pos() const noexcept
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.
std::function< size_t(const Key &)> Hash_Fct
The hash function type.
Cmp & get_compare() noexcept
const size_t & size() const noexcept
Returns the number of elements in the table.
bool is_empty() const noexcept
return true is table is empty
void empty() noexcept
Empties the table: frees every bucket and gives the table its initial size.
const size_t & expansions() const noexcept
Returns the expansion level performed on the table.
float current_alpha() const noexcept
return the current table load
Bucket * remove(Bucket *bucket) noexcept
Removes bucket from the table, without freeing it.
BucketType< Key > Bucket
The bucket type.
const size_t & busy_slots() const noexcept
Returns the number of busy slots 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.
Bucket with virtual destructor for a hash table with collision resolution by separate chaining.
Bucket without virtual destructor for a hash table with collision resolution by separate chaining.
Number Theoretic Transform over Z / MOD Z.
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.
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
size_t mod_hash(const int &k)
LinearHashTable< int > Table
Main namespace for Aleph-w library functions.
const float hash_default_upper_alpha
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
and
Check uniqueness with explicit hash + equality functors.
const float hash_default_lower_alpha
bool operator()(int a, int b) const
size_t custom_hash(const int &key)
Dynamic set implementations based on hash tables.
Linear hashing with chaining.