Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testDynSetHash.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32# include <ctime>
33# include <ah-random.H>
34# include <cassert>
35# include <iostream>
36# include <ahFunctional.H>
37# include <ahSort.H>
38# include <tpl_sort_utils.H>
39# include <tpl_dynSetHash.H>
40# include <tpl_dynMapOhash.H>
41# include <string>
42
43# define NumItems 10000
44
45using namespace std;
46
48
49template <template <typename, class> class SetType> unsigned long
51(SetType<unsigned long, Aleph::equal_to<unsigned long>> & table,
53 unsigned long n)
54{
55 unsigned long dup_counter = 0;
56
57 cout << "Testing simple insertions and searches ...." << endl;
58
59 for (int i = 0; i < n; i++)
60 {
61 keys[i] = r.get();
62 if (not table.has(keys(i)))
63 table.insert(keys(i));
64 else
65 ++dup_counter;
66
67 if (table.current_alpha() > 1.1)
68 {
69 cout << "Resizing table to " << 1.5*table.size() << endl;
70 table.resize(1.5*table.size());
71 cout << "done!" << endl;
72 }
73 }
74 cout << "done" << endl;
75
76 return dup_counter;
77}
78
79template <template <typename, class> class HashTable>
80HashTable<unsigned long, Aleph::equal_to<unsigned long>>
82(const HashTable<unsigned long, Aleph::equal_to<unsigned long>> & other)
83{
84 typedef HashTable<unsigned long, Aleph::equal_to<unsigned long>> SetType;
85 SetType table;
86 for (auto it = other.get_it(); it.has_curr(); it.next())
87 table.insert(it.get_curr());
88
89 return table;
90}
91
92template <template <typename, class> class HashTable>
93void test_DynSetLinHash(size_t n)
94{
95 typedef HashTable<unsigned long, Aleph::equal_to<unsigned long>> SetType;
96 SetType table;
98 unsigned int dup_counter = insert_n_random_items_in_set(table, keys, n);
99
100 typename SetType::Stats stats = table.stats();
101 table.print_stats(stats);
102
103 cout << table.size() << " items inserted" << endl
104 << dup_counter << " duplicated numbers" << endl
105 << endl
106 << "testing deletions ...." << endl;
107
108 {
109 const auto ctable = table;
110 assert(table.all([&ctable] (auto k) { return ctable.find(k) == k; }));
111 }
112
113 unsigned long removed_counter = 0;
114 size_t num_inserted = table.size();
115 for (size_t i = 0; i < n; i++)
116 if (table.search(keys(i)) != NULL)
117 {
118 table.remove(keys(i));
119 ++removed_counter;
120 }
121
122 assert(removed_counter == num_inserted);
123 assert(table.size() == 0);
124
125 cout << removed_counter << " items removed" << endl;
126
127 cout << "testing empty() method ...." << endl;
128 dup_counter = insert_n_random_items_in_set(table, keys, n);
129
130 table.empty();
131
132 assert(table.size() == 0);
133
134 unsigned long repeated_counter = 0;
135 cout << "Reinserting keys ...." << endl;
136 for (size_t i = 0; i < n; ++i)
137 if (table.insert(keys(i)) == NULL)
138 ++repeated_counter;
139
140 cout << repeated_counter << " duplicated numbers" << endl
141 << dup_counter << " was the previus value" << endl;
142
143 assert(dup_counter == repeated_counter);
144
145 cout << "Done!" << endl;
146
147 {
148 cout << "Testing iterator and map ...." << endl;
149
150 DynList<unsigned long> l = table.template maps<unsigned long>
151 ([] (unsigned long k) -> unsigned long
152 {
153 return k;
154 });
155
156 for (DynList<unsigned long>::Iterator it(l); it.has_curr(); it.next())
157 assert(table.search(it.get_curr()) != NULL);
158
159 cout << "done!" << endl;
160 }
161
162 {
163 cout << "testing lvalue copy constructor ...." << endl;
164 SetType tmp = table;
165
166 assert(table.equal_to(tmp));
167 }
168
169 {
170 cout << "testing lvalue assigment...." << endl;
171 SetType aux;
172 for (size_t i = 0; i < n/2; ++i)
173 {
174 unsigned long key = r.get();
175 while (aux.has(key))
176 key = r.get();
177 aux.insert(key);
178 }
179
180 aux = table;
181
182 assert(aux == table);
183 }
184
185 {
186 cout << "Testing rvalue constructor ...." << endl;
187 SetType tmp = create_table(table);
188 assert(tmp == table);
189 cout << "done!" << endl
190 << endl
191 << "Testing rvalue assign = .... " << endl
192 << endl;
193 tmp = create_table(table);
194 cout << "done!" << endl
195 << endl;
196 }
197
198 {
199 cout << "testing del() of Iterator ...." << endl
200 << "Deleting all entries through del() ...." << endl;
201
202 for (typename SetType::Iterator it(table); it.has_curr(); /* empty */)
203 it.del();
204
205 assert(table.is_empty());
206
207 DynArray<size_t> dups; // stores indexes of duplicated keys of keys array
208
209 cout << "done" << endl
210 << "Reinserting ...." << endl;
211 for (int i = 0; i < n; ++i)
212 if (table.insert(keys(i)) == NULL)
213 dups.append(i);
214
215 cout << "Searching inserted keys ...." << endl;
216 for (size_t i = 0; i < n; ++i)
217 {
218 unsigned long * ptr = table.search(keys(i));
219 assert(ptr != NULL);
220 if (dups.size() > 0)
221 {
222 const auto dup_idx = binary_search(dups, i);
223 if (dup_idx < dups.size() and dups(dup_idx) == i)
224 continue; // keys[] contains a dup entry
225 }
226 }
227 }
228
229 {
230 cout << "Testing keys() in set ...." << endl
231 << endl;
232 DynList<unsigned long> the_keys = table.keys();
233 assert(the_keys.size() == table.size());
234 assert(all(the_keys, /* Lambda */ [&table] (const size_t & key)
235 {
236 return table.has(key);
237 }));
238 }
239
240 {
241 cout << endl
242 << "Testing filter of keys multiples of 13" << endl;
243
245 filter(table, [] (const unsigned long & key)
246 {
247 return key % 13 == 0;
248 });
249
250 table.filter(/* Lambda */ [] (const unsigned long & key)
251 {
252 return key % 13 == 0;
253 }).for_each(/* Lambda */ [&v13] (const unsigned long & key)
254 {
255 cout << key << " ";
256 assert(contains(v13, key));
257 });
258 cout << endl;
259 }
260}
261
262
263template <class HashTable>
264unsigned long insert_n_random_items_in_map(HashTable & table,
266 unsigned long n)
267{
268 unsigned long dup_counter = 0;
269 cout << "Testing simple insertions and searches ...." << endl;
270 for (long i = 0; i < n; i++)
271 {
272 keys[i] = r.get();
273 if (not table.has(keys(i)))
274 {
275 // Keep the insertion out of assert(): NDEBUG would remove it.
276 [[maybe_unused]] const auto inserted = table.insert(keys(i), i);
277 assert(inserted != nullptr);
278 }
279 else
280 ++dup_counter;
281 }
282
283 cout << n << " tries " << endl
284 << dup_counter << " duplicated" << endl
285 << "size = " << table.size() << endl
286 << endl
287 << "Performing map search test" << endl
288 << endl;
289
290 cout << "keys =";
291 sort(keys).for_each([] (auto k) { cout << " " << k; });
292 cout << endl
293 << "table = ";
294 sort(table.keys()).for_each([] (auto k) { cout << " " << k; });
295 cout << endl;
296
297 for (auto i = 0; i < n; i++)
298 assert(table.search(keys(i)));
299
300 assert(keys.all([&table] (auto k) { return table.search(k) != nullptr; }));
301 cout << "Passed" << endl
302 << endl;
303
304 return dup_counter;
305}
306
307template <template <typename, typename, class> class HashTable>
308void test_DynMapLinHash(size_t n)
309{
310 using MapType = HashTable<unsigned long, long, Aleph::equal_to<unsigned long>>;
311 MapType table;
313 unsigned int dup_counter = insert_n_random_items_in_map(table, keys, n);
314
315 typename MapType::Stats stats = table.stats();
316 table.print_stats(stats);
317
318 cout << table.size() << " items inserted" << endl
319 << dup_counter << " duplicated numbers" << endl
320 << endl
321 << "testing deletions ...." << endl;
322
323 unsigned long removed_counter = 0;
324 size_t num_inserted = table.size();
325 for (int i = 0; i < n; i++)
326 if (table.search(keys(i)) != nullptr)
327 {
328 table.remove(keys(i));
329 ++removed_counter;
330 }
331 cout << removed_counter << " items removed" << endl;
332
333 assert(removed_counter == num_inserted);
334 assert(table.size() == 0);
335 //keys.cut();
336
337 cout << "testing empty() method ...." << endl;
338 dup_counter = insert_n_random_items_in_map(table, keys, n);
339
340 table.empty();
341
342 assert(table.size() == 0);
343
344 unsigned long repeated_counter = 0;
345 cout << "Reinserting keys ...." << endl;
346 for (size_t i = 0; i < n; ++i)
347 if (table.insert(keys(i), i) == NULL)
348 ++repeated_counter;
349
350 cout << repeated_counter << " duplicated numbers" << endl
351 << dup_counter << " was the previus value" << endl;
352
353 assert(dup_counter == repeated_counter);
354
355 cout << "Done!" << endl
356 << endl
357 << "Testing for_each and a battery of other tests ...." << endl;
358
359 assert(table.all(/* Lambda */ [&table]
360 (const std::pair<const unsigned long, long> & p)
361 {
362 auto ptr = table.search(p.first);
363 assert(ptr != nullptr);
364 assert(table.get_data(p.first) == ptr->second);
365 return table.has(p.first);
366 })
367 );
368
369 cout << "done!" << endl
370 << endl
371 << "testing keys() method and other tests ...." << endl;
372 DynList<unsigned long> the_keys = table.keys();
373 assert(all(the_keys, /* Lambda */ [&table] (unsigned long k)
374 { return table.has(k); }));
375
376 cout << "done!" << endl
377 << endl
378 /* << "Testing values() method ...." << endl;
379 DynList<long> values = table.values();
380 assert(all(values, [&table] (long & val)
381 { return table.search(table.get_key(ptr)) == ptr; }));
382 cout << "done!" << endl
383 << endl */
384 << "Testing items() method and othet stuff ...." << endl;
386 assert(all(items, /* Lambda */ [&table]
387 (std::pair<unsigned long, long> p)
388 { return table.find(p.first) == p.second; } ));
389 cout << "done!" << endl
390 << endl
391 << "Testing remove by data pointer ...." << endl;
392 removed_counter = 0;
393 for_each(keys, [&table, &removed_counter] (const unsigned long & k)
394 {
395 auto ptr = table.search(k);
396 if (ptr == nullptr)
397 return;
398
399 table.remove_by_data(ptr->second);
400 ++removed_counter;
401 });
402 assert(table.is_empty());
403
404 cout << endl
405 << "Reinserting keys for doing other tests ...." << endl;
406 for (int i = 0; i < n; ++i)
407 table.insert(keys(i), i);
408 assert(table.size() == removed_counter);
409 cout << "done!" << endl
410 << endl;
411
412}
413
414int main(int argc, char * argv[])
415{
417 assert(next_prime(5) == 5);
418
419 unsigned long n = NumItems;
420 if (argc > 1)
421 {
422 try { n = stoul(argv[1]); } catch (...) { n = NumItems; }
423 }
424
425 if (n <= 0)
426 {
427 cerr << "n must be positive" << endl;
428 return 1;
429 }
430
431 unsigned int t = std::time(NULL);
432 if (argc > 2)
433 {
434 try { t = stoul(argv[2]); } catch (...) { t = std::time(NULL); }
435 }
436
437 cout << argv[0] << " " << n << " " << t << endl;
438
440
441 test_DynSetLinHash<DynSetLhash>(n);
442 test_DynMapLinHash<DynMapHash>(n);
443
444 test_DynSetLinHash<DynSetLinHash>(n);
445 test_DynMapLinHash<DynMapLinHash>(n);
446
447 cout << "testing of ODhash based set ...." << endl
448 << endl;
449 test_DynSetLinHash<SetODhash>(n);
450 cout << endl
451 << "Done all test of ODhash based set!" << endl
452 << endl
453 << endl
454 << "testing of OLhash based set ...." << endl
455 << endl;
456 test_DynSetLinHash<SetOLhash>(n);
457 cout << endl
458 << "Done all test of OLhash based set!" << endl
459 << endl
460 << endl
461 << "Testing all tests of OLhash based map" << endl
462 << endl;
463 // test_DynMapLinHash<DynMapODHash>(n);
464 // cout << "Done all tests of OD hash based map" << endl
465 // << endl
466 // << endl
467 // << "Testing of OLhash based map" << endl
468 // << endl;
469 // test_DynMapLinHash<DynMapOLHash>(n);
470 // cout << "Done all tests of OL hash based map" << endl
471 // << endl
472 // << endl;
473}
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
int main()
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
Iterator on the items of list.
Definition htlist.H:1614
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
Definition ah-random.H:277
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:289
bool has_curr() const noexcept
Definition htlist.H:969
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
Definition ah-dry.H:2356
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
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.
Definition ahSort.H:234
and
Check uniqueness with explicit hash + equality functors.
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
Definition ahAlgo.H:76
bool binary_search(Itor beg, Itor end, const T &value)
Binary search for a value.
Definition ahAlgo.H:1284
bool check_primes_database()
Verify the order of the prime database.
Definition primes.C:414
unsigned long next_prime(unsigned long n)
Find a prime >= n in the database, for hash table sizing.
Definition primes.C:383
STL namespace.
Aleph::DynList< T > items() const
Return a list of all the elements of a container sorted by traversal order.
Definition ah-dry.H:2904
Aleph::DynList< T > keys() const
See items().
Definition ah-dry.H:2916
int keys[]
unsigned long insert_n_random_items_in_set(SetType< unsigned long, Aleph::equal_to< unsigned long > > &table, DynArray< unsigned long > &keys, unsigned long n)
HashTable< unsigned long, Aleph::equal_to< unsigned long > > create_table(const HashTable< unsigned long, Aleph::equal_to< unsigned long > > &other)
void test_DynSetLinHash(size_t n)
void test_DynMapLinHash(size_t n)
#define NumItems
Gsl_Compatible_Rng r
unsigned long insert_n_random_items_in_map(HashTable &table, DynArray< unsigned long > &keys, unsigned long n)
static int * k
Dynamic map with open hashing.
Dynamic set implementations based on hash tables.
Comprehensive sorting algorithms and search utilities for Aleph-w.
DynList< int > l