37# include <gtest/gtest.h>
50using namespace testing;
55constexpr size_t N = 1000;
57template <
class HashTbl>
70template <
class HashTbl>
97 auto it =
tbl.get_it();
102 for (; it.has_curr(); it.next_ne())
105 auto ll =
l.
maps<
size_t>([](
auto &p)
106 {
return p.first; });
111 for (; it.has_curr(); it.prev_ne())
113 ll =
l.
maps<
size_t>([](
auto &p)
114 {
return p.first; });
151 auto stats =
tbl.stats();
152 EXPECT_EQ(stats.num_busy,
static_cast<size_t>(2));
161 auto *bucket =
decltype(
tbl)::key_to_bucket(ptr);
179 auto *p1 =
tbl.insert(10);
181 auto *p2 =
tbl.insert(10);
190 auto *p =
tbl.insert(5);
202 for (
int i = 0; i < 5; ++i)
212 auto *p1 =
tbl.insert(1);
213 auto *p2 =
tbl.insert(2);
214 auto *p3 =
tbl.insert(3);
221 auto stats =
tbl.stats();
222 EXPECT_EQ(stats.num_busy,
static_cast<size_t>(2));
223 EXPECT_EQ(stats.num_deleted,
static_cast<size_t>(1));
224 EXPECT_EQ(stats.num_busy + stats.num_deleted + stats.num_empty,
225 static_cast<size_t>(
tbl.capacity()));
226 EXPECT_GE(stats.max_len,
static_cast<size_t>(1));
237 for (
int i = 0; i < 10; ++i)
240 for (
int i = 0; i < 10; ++i)
247 auto const_hash = [](
int) {
return static_cast<size_t>(0); };
250 for (
int i = 0; i < 5; ++i)
253 for (
int i = 0; i < 5; ++i)
256 auto stats =
tbl.stats();
257 EXPECT_EQ(stats.num_busy,
static_cast<size_t>(5));
258 EXPECT_GE(stats.max_len,
static_cast<size_t>(5));
269 for (
int i = 0; i < 6; ++i)
277 for (
int i = 10; i < 16; ++i)
281 for (
int i : {0, 2, 4, 5, 10, 11, 12, 13, 14, 15})
288 auto *p1 =
tbl.search_or_insert(1);
290 auto *p2 =
tbl.search_or_insert(1);
349 auto const_hash = [](
int) {
return static_cast<size_t>(0); };
352 for (
int i = 0; i < 5; ++i)
391 const auto cap0 =
tbl.capacity();
392 for (
int i = 0; i < 10; ++i)
395 for (
int i = 0; i < 10; ++i)
423 for (
int i = 0; i < 5; ++i)
433 auto *p1 =
tbl.insert(10);
435 auto *p2 =
tbl.insert(10);
438 auto *p3 =
tbl.search_or_insert(10);
483 auto stats =
tbl.stats();
484 EXPECT_EQ(stats.num_busy,
static_cast<size_t>(2));
485 EXPECT_GE(stats.num_deleted,
static_cast<size_t>(0));
491 auto h2 = [](
int k) {
return static_cast<size_t>(
k * 13); };
492 tbl.set_second_hash_fct(
h2);
500 for (
int i = 0; i < 5; ++i)
502 auto stats =
tbl.stats();
510 auto const_h2 = +[](
int) {
return static_cast<size_t>(0); };
516 auto stats =
tbl.stats();
517 EXPECT_EQ(stats.num_busy,
static_cast<size_t>(3));
518 EXPECT_GE(stats.max_len,
static_cast<size_t>(1));
530 const auto cap0 =
tbl.capacity();
531 for (
int i = 0; i < 10; ++i)
534 for (
int i = 0; i < 10; ++i)
551 for (
int i = 0; i < 7; ++i)
556 auto it =
tbl.get_it();
558 for (; it.has_curr(); it.next_ne())
569 auto const_hash = +[](
const int &) {
return static_cast<size_t>(0); };
592 0.9f, 10.0f,
true,
true);
595 auto * b1 =
new Bucket(1);
596 auto * b2 =
new Bucket(2);
611 delete tbl.remove(b2);
612 delete tbl.remove(
b3);
617 auto const_hash = +[](
const int &) {
return static_cast<size_t>(1); };
622 for (
int k : {1, 2, 3})
640 auto * b1 =
new Bucket(42);
643 auto * b2 =
new Bucket(42);
644 auto *
found =
tbl.search_or_insert(b2);
648 delete tbl.remove(b1);
655 auto * b1 =
new Bucket(1);
656 auto * b2 =
new Bucket(2);
675 auto * b1 =
new Bucket(7);
679 auto * b2 =
new Bucket(7);
686 delete tbl.remove(b1);
691 auto const_hash = +[](
const int &) {
return static_cast<size_t>(3); };
696 auto * b1 =
new Bucket(1);
697 auto * b2 =
new Bucket(2);
703 auto * first =
tbl.search(1);
707 delete tbl.remove(b1);
708 delete tbl.remove(b2);
709 delete tbl.remove(
b3);
743 for (
int k : {1, 2, 3})
756 auto const_hash = +[](
const int &) {
return static_cast<size_t>(0); };
774 auto * b1 =
new Bucket(1);
775 auto * b2 =
new Bucket(2);
805 for (
int k : {1, 2, 102})
809 tbl.set_hash_fct(+[](
const int &
k) {
return static_cast<size_t>(
k * 2); });
818 delete tbl.remove(
tbl.search(1));
819 delete tbl.remove(
tbl.search(2));
820 delete tbl.remove(
tbl.search(102));
829 const auto cap =
tbl.capacity();
830 for (
int k = 0;
k < 50; ++
k)
843 bool operator()(
int a,
int b)
const {
return (a % 10) == (b % 10); }
846 auto const_hash = +[](
const int &) {
return static_cast<size_t>(0); };
851 auto * b1 =
new Bucket(10);
852 auto * b2 =
new Bucket(20);
857 delete tbl.remove(b1);
872static_assert(!std::is_copy_constructible_v<LhashTable<int>>,
873 "LhashTable should not be copy constructible");
874static_assert(!std::is_copy_assignable_v<LhashTable<int>>,
875 "LhashTable should not be copy assignable");
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
void insert(Dlink *node) noexcept
Insert node after this.
Dynamic singly linked list with functional programming support.
T & insert(const T &item)
Insert a new item by copy.
T & append(const T &item)
Append a new item by copy.
T remove()
Remove the first item of the list.
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...
const size_t & size() const noexcept
Returns the number of elements contained in the table.
const size_t & capacity() const noexcept
Returns the table capacity.
void swap(GenLhashTable &other) noexcept
Bucket * search(const Key &key) const noexcept
Search in the table for a bucket with key.
constexpr bool is_empty() const noexcept
size_t size() const noexcept
Count the number of elements of the list.
Open addressing hash table with double hashing collision resolution.
Key * search(const Key &key) const noexcept
searches the table for the key.
void swap(ODhashTable &other) noexcept
Open addressing hash table with linear probing collision resolution.
Key * search(const Key &key) const noexcept
Finds the key and returns the associated record if key is find inside the table; otherwise,...
void swap(OLhashTable &other) noexcept
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
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)
MapOLhash< int, Foo > tbl
Main namespace for Aleph-w library functions.
const float hash_default_upper_alpha
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
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
DynList< T > maps(const C &c, Op op)
Classic map operation.
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
Open addressing hash map using linear probing.
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
Linear hashing with dynamic bucket expansion.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.