Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
hash_function_change_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
45# include <gtest/gtest.h>
46
47# include <stdexcept>
48# include <string>
49# include <utility>
50
51# include <tpl_dynLhash.H>
52# include <tpl_dynMapOhash.H>
53# include <tpl_dynSetHash.H>
54# include <tpl_lhash.H>
55# include <tpl_linHash.H>
56# include <tpl_odhash.H>
57# include <tpl_olhash.H>
58
59using namespace Aleph;
60
61namespace
62{
63 constexpr int Keys = 300; // enough to make the tables of 101 lists grow
64
65 // Hash functions other than the default ones, of the keys and of the
66 // pairs of the maps: a search by a stale function misses
67 size_t new_hash(const int & k) noexcept
68 {
69 return static_cast<size_t>(k) * 40503u + 17;
70 }
71
72 size_t new_second_hash(const int & k) noexcept
73 {
74 return static_cast<size_t>(k) * 3 + 11;
75 }
76
77 size_t new_pair_hash(const std::pair<int, int> & p) noexcept
78 {
79 return new_hash(p.first);
80 }
81
82 size_t new_second_pair_hash(const std::pair<int, int> & p) noexcept
83 {
84 return new_second_hash(p.first);
85 }
86
87 // Each container: how it inserts the key k, whether it holds k, and how
88 // it takes the new hash functions. refuses_keys: whether set_hash_fct()
89 // refuses a container with keys, as the linear hash tables do.
90
91 struct Chained_Table
92 {
94 static constexpr bool refuses_keys = false;
95 static void insert(Container & c, int k) { c.insert(new LhashBucket<int>(k)); }
96 static bool holds(Container & c, int k) { return c.search(k) != nullptr; }
97 static void set(Container & c) { c.set_hash_fct(new_hash); }
98 };
99
100 struct Linear_Table
101 {
103 static constexpr bool refuses_keys = true;
104 static void insert(Container & c, int k) { c.insert(new Container::Bucket(k)); }
105 static bool holds(Container & c, int k) { return c.search(k) != nullptr; }
106 static void set(Container & c) { c.set_hash_fct(new_hash); }
107 };
108
109 struct Linear_Probing_Table
110 {
112 static constexpr bool refuses_keys = false;
113 static void insert(Container & c, int k) { (void) c.insert(k); }
114 static bool holds(Container & c, int k) { return c.search(k) != nullptr; }
115 static void set(Container & c) { c.set_hash_fct(new_hash); }
116 };
117
118 struct Double_Hashing_Table
119 {
121 static constexpr bool refuses_keys = false;
122 static void insert(Container & c, int k) { (void) c.insert(k); }
123 static bool holds(Container & c, int k) { return c.search(k) != nullptr; }
124 static void set(Container & c)
125 {
126 c.set_hash_fct(new_hash);
127 c.set_second_hash_fct(new_second_hash);
128 }
129 };
130
131 template <class Set, bool Linear>
132 struct A_Set
133 {
134 using Container = Set;
135 static constexpr bool refuses_keys = Linear;
136 static void insert(Container & c, int k) { (void) c.insert(k); }
137 static bool holds(Container & c, int k) { return c.search(k) != nullptr; }
138 static void set(Container & c) { c.set_hash_fct(new_hash); }
139 };
140
141 struct Chained_Records
142 {
144 static constexpr bool refuses_keys = false;
145 static void insert(Container & c, int k) { c.insert(k, 10 * k); }
146 static bool holds(Container & c, int k)
147 {
148 const int * r = c.search(k);
149 return r != nullptr and *r == 10 * k;
150 }
151 static void set(Container & c) { c.set_hash_fct(new_hash); }
152 };
153
154 // A map of tpl_dynSetHash.H, given a function of the keys or of the pairs
155 template <class Map, bool Linear, bool By_Pairs>
156 struct A_Hash_Map
157 {
158 using Container = Map;
159 static constexpr bool refuses_keys = Linear;
160 static void insert(Container & c, int k) { c.insert(k, 10 * k); }
161 static bool holds(Container & c, int k)
162 {
163 const auto * p = c.search(k);
164 return p != nullptr and p->second == 10 * k and c.has(k);
165 }
166 static void set(Container & c)
167 {
168 if constexpr (By_Pairs)
169 c.set_hash_fct(new_pair_hash);
170 else
171 c.set_hash_fct(new_hash);
172 }
173 };
174
175 // A map of tpl_dynMapOhash.H, whose functions are of the pairs
176 template <class Map, bool Double>
177 struct An_Open_Map
178 {
179 using Container = Map;
180 static constexpr bool refuses_keys = false;
181 static void insert(Container & c, int k) { (void) c.insert(k, 10 * k); }
182 static bool holds(Container & c, int k)
183 {
184 const auto * p = c.search(k);
185 return p != nullptr and p->second == 10 * k;
186 }
187 static void set(Container & c)
188 {
189 c.set_hash_fct(new_pair_hash);
190 if constexpr (Double)
191 c.set_second_hash_fct(new_second_pair_hash);
192 }
193 };
194
195 using Hash_Containers = ::testing::Types<
196 Chained_Table, Linear_Table, Linear_Probing_Table, Double_Hashing_Table,
197 A_Set<DynSetLhash<int>, false>, A_Set<DynSetHash<int>, false>,
198 A_Set<DynSetLinHash<int>, true>, Chained_Records,
199 A_Hash_Map<DynMapHash<int, int>, false, false>,
200 A_Hash_Map<DynMapHash<int, int>, false, true>,
201 A_Hash_Map<DynMapLinHash<int, int>, true, false>,
202 A_Hash_Map<DynMapLinHash<int, int>, true, true>,
203 An_Open_Map<MapOLhash<int, int>, false>,
204 An_Open_Map<MapODhash<int, int>, true>>;
205
206 template <class T>
207 class Hash_Function_Change : public ::testing::Test
208 {
209 };
210
211 TYPED_TEST_SUITE(Hash_Function_Change, Hash_Containers);
212
213 // A function set on an empty container rules where its keys go and
214 // where its searches look for them
215 TYPED_TEST(Hash_Function_Change, OnAnEmptyContainer)
216 {
217 typename TypeParam::Container c;
218 TypeParam::set(c);
219 for (int k = 0; k < Keys; ++k)
220 TypeParam::insert(c, k);
221
222 for (int k = 0; k < Keys; ++k)
223 ASSERT_TRUE(TypeParam::holds(c, k)) << "key " << k;
224 EXPECT_FALSE(TypeParam::holds(c, Keys));
225 }
226
227 // On a container with keys, set_hash_fct() places them again, so they
228 // are still found, and the new ones go by the new function; a linear
229 // table, which cannot place its keys again, refuses the function and
230 // keeps its own. The keys are looked for before the container grows,
231 // which would place them again by itself.
232 TYPED_TEST(Hash_Function_Change, OnAContainerWithKeys)
233 {
234 typename TypeParam::Container c;
235 for (int k = 0; k < Keys; ++k)
236 TypeParam::insert(c, k);
237
238 if constexpr (TypeParam::refuses_keys)
239 EXPECT_THROW(TypeParam::set(c), std::domain_error);
240 else
241 TypeParam::set(c);
242
243 for (int k = 0; k < Keys; ++k)
244 ASSERT_TRUE(TypeParam::holds(c, k)) << "key " << k << ", before the container grows";
245 for (int k = Keys; k < 2 * Keys; ++k)
246 TypeParam::insert(c, k);
247 for (int k = 0; k < 2 * Keys; ++k)
248 ASSERT_TRUE(TypeParam::holds(c, k)) << "key " << k;
249 EXPECT_FALSE(TypeParam::holds(c, 2 * Keys));
250 }
251
252 // =========================================================================
253 // The maps of tpl_dynSetHash.H search by the hash of the keys, which they
254 // keep apart from the function of the table. After a function of pairs
255 // they search by the pair; a function of keys brings the search that
256 // builds nothing back. Copies, moves and swaps keep the function of each
257 // map.
258 // =========================================================================
259
260 template <class Map>
261 class Map_Hash_Function : public ::testing::Test
262 {
263 };
264
265 using Hash_Maps = ::testing::Types<DynMapHash<int, int>, DynMapLinHash<int, int>>;
266
267 TYPED_TEST_SUITE(Map_Hash_Function, Hash_Maps);
268
269 // Whether map holds the pairs (k, 10 k) for k in [from, to), and no other
270 // key of [from - 1, to], through every way of searching
271 template <class Map>
272 void expect_pairs(Map & map, const int from, const int to)
273 {
274 const Map & const_map = map;
275 for (int k = from; k < to; ++k)
276 {
277 auto * p = map.search(k);
278 ASSERT_NE(p, nullptr) << "key " << k;
279 EXPECT_EQ(p->second, 10 * k);
280 EXPECT_EQ(const_map.search(k), p);
281 int key = k;
282 EXPECT_EQ(map.search(std::move(key)), p);
283 EXPECT_TRUE(map.has(k));
284 EXPECT_TRUE(map.contains(k));
285 EXPECT_EQ(map.find(k), 10 * k);
286 }
287 EXPECT_FALSE(map.has(from - 1));
288 EXPECT_FALSE(map.has(to));
289 }
290
291 TYPED_TEST(Map_Hash_Function, FunctionsOfPairsAndOfKeys)
292 {
293 TypeParam by_pairs, by_keys, back_to_keys;
294 by_pairs.set_hash_fct(new_pair_hash);
295 by_keys.set_hash_fct(new_hash);
296 back_to_keys.set_hash_fct(new_pair_hash);
297 back_to_keys.set_hash_fct(new_hash);
298 for (int k = 0; k < Keys; ++k)
299 {
300 by_pairs.insert(k, 10 * k);
301 by_keys.insert(k, 10 * k);
302 back_to_keys.insert(k, 10 * k);
303 }
304
305 expect_pairs(by_pairs, 0, Keys);
306 expect_pairs(by_keys, 0, Keys);
307 expect_pairs(back_to_keys, 0, Keys);
308
309 EXPECT_EQ(by_pairs.remove(7), 70);
310 EXPECT_FALSE(by_pairs.has(7));
311 EXPECT_EQ(by_keys.remove(7), 70);
312 EXPECT_FALSE(by_keys.has(7));
313 EXPECT_EQ(by_pairs[8], 80);
314 by_pairs[Keys] = 10 * Keys;
315 EXPECT_EQ(by_pairs.find(Keys), 10 * Keys);
316 }
317
318 TYPED_TEST(Map_Hash_Function, CopiesMovesAndSwapsKeepTheFunction)
319 {
320 TypeParam by_pairs, by_keys;
321 by_pairs.set_hash_fct(new_pair_hash);
322 by_keys.set_hash_fct(new_hash);
323 for (int k = 0; k < Keys; ++k)
324 {
325 by_pairs.insert(k, 10 * k);
326 by_keys.insert(Keys + k, 10 * (Keys + k));
327 }
328
329 TypeParam copy = by_pairs;
330 expect_pairs(copy, 0, Keys);
331 TypeParam moved = std::move(copy);
332 expect_pairs(moved, 0, Keys);
333 TypeParam assigned;
334 assigned = by_keys;
335 expect_pairs(assigned, Keys, 2 * Keys);
336 TypeParam move_assigned;
337 move_assigned = std::move(assigned);
338 expect_pairs(move_assigned, Keys, 2 * Keys);
339
340 by_pairs.swap(by_keys);
341 expect_pairs(by_pairs, Keys, 2 * Keys);
342 expect_pairs(by_keys, 0, Keys);
343 }
344
345 // The search by the hash of the keys builds nothing, so it does not throw
346 // with keys of any type. After a function of pairs it builds one, from a
347 // copy or a move of the key and a Data built by default: the search is
348 // noexcept as they are. A Data with no default constructor takes no
349 // function of pairs, and leaves the first search alone.
350 struct No_Default
351 {
352 int value;
353 No_Default(int v) noexcept : value(v) {}
354 };
355
356 template <class Key, class Data>
358 requires (DynMapHash<Key, Data> & m, size_t (*f)(const std::pair<Key, Data> &))
359 {
360 m.set_hash_fct(f);
361 };
362
363 static_assert(noexcept(std::declval<DynMapHash<int, int> &>().search(1)));
364 static_assert(noexcept(std::declval<DynMapLinHash<int, int> &>().has(1)));
365 static_assert(not noexcept(std::declval<DynMapHash<std::string, int> &>()
366 .search(std::declval<const std::string &>())));
367 static_assert(not noexcept(std::declval<const DynMapLinHash<std::string, int> &>()
368 .has(std::declval<const std::string &>())));
369 static_assert(noexcept(std::declval<DynMapHash<std::string, int> &>()
370 .search(std::declval<std::string>())));
371 static_assert(noexcept(std::declval<DynMapHash<std::string, No_Default> &>()
372 .search(std::declval<const std::string &>())));
375
376 TEST(Map_Hash_Function, ADataWithNoDefaultConstructorStillSearchesByTheKey)
377 {
379 map.set_hash_fct(new_hash);
380 for (int k = 0; k < Keys; ++k)
381 map.insert(k, No_Default(10 * k));
382 for (int k = 0; k < Keys; ++k)
383 {
384 const auto * p = map.search(k);
385 ASSERT_NE(p, nullptr) << "key " << k;
386 EXPECT_EQ(p->second.value, 10 * k);
387 }
388 }
389} // namespace
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
TYPED_TEST(ArcHeapLifecycle, ClearNullsTheHandles)
size_t size_t int32_t value
Definition ca-c-api.h:116
Dynamic hash table mapping keys to records with separate chaining.
Pair * insert(const Key &key, const Data &data)
Inserts into the hash map the pair (key, record) indexed by key.
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Searches for the pair whose key is key.
void set_hash_fct(Hash_Fct_Ptr fct)
Sets the hash function of the keys.
Generic linear hash table.
BucketType< Key > Bucket
The bucket type.
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:193
void set_hash_fct(Hash_Fct fct)
Definition tpl_odhash.H:653
Open addressing hash table with linear probing collision resolution.
Definition tpl_olhash.H:175
#define TEST(name)
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
and
Check uniqueness with explicit hash + equality functors.
Itor1 search(Itor1 beg, const Itor1 &end, Itor2 searchBeg, const Itor2 &searchEnd, BinaryPredicate op=BinaryPredicate())
Search for a subrange within a range.
Definition ahAlgo.H:309
Field< double > Double
Definition ahField.H:142
Generic hash table with collision resolution by separate chaining and buckets without virtual destruc...
Definition tpl_lhash.H:1008
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k
Gsl_Compatible_Rng r
Dynamic hash table mapping keys to records with separate chaining.
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
Hash tables with collision resolution by separate chaining.
Linear hashing with chaining.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.