Aleph-w 3.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 <gsl/gsl_rng.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] = gsl_rng_get(r);
62 if (not table.has(keys(i)))
63 table.insert(keys(i));
64 else
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>
82(const HashTable<unsigned long, Aleph::equal_to<unsigned long>> & other)
83{
85 SetType table;
86 for (typename SetType::Iterator it(other); 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{
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));
120 }
121
123 assert(table.size() == 0);
124
125 cout << removed_counter << " items removed" << endl;
126
127 cout << "testing empty() method ...." << endl;
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)
139
140 cout << repeated_counter << " duplicated numbers" << endl
141 << dup_counter << " was the previus value" << endl;
142
144
145 cout << "Done!" << endl;
146
147 {
148 cout << "Testing iterator and map ...." << endl;
149
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 = gsl_rng_get(r);
175 while (aux.has(key))
176 key = gsl_rng_get(r);
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;
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>
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] = gsl_rng_get(r);
273 if (not table.has(keys(i)))
274 assert(table.insert(keys(i), i));
275 else
276 ++dup_counter;
277 }
278
279 cout << n << " tries " << endl
280 << dup_counter << " duplicated" << endl
281 << "size = " << table.size() << endl
282 << endl
283 << "Performing map search test" << endl
284 << endl;
285
286 cout << "keys =";
287 sort(keys).for_each([] (auto k) { cout << " " << k; });
288 cout << endl
289 << "table = ";
290 sort(table.keys()).for_each([] (auto k) { cout << " " << k; });
291 cout << endl;
292
293 for (auto i = 0; i < n; i++)
294 assert(table.search(keys(i)));
295
296 assert(keys.all([&table] (auto k) { return table.search(k) != nullptr; }));
297 cout << "Passed" << endl
298 << endl;
299
300 return dup_counter;
301}
302
303template <template <typename, typename, class> class HashTable>
304void test_DynMapLinHash(size_t n)
305{
307 MapType table;
309 unsigned int dup_counter = insert_n_random_items_in_map(table, keys, n);
310
311 typename MapType::Stats stats = table.stats();
312 table.print_stats(stats);
313
314 cout << table.size() << " items inserted" << endl
315 << dup_counter << " duplicated numbers" << endl
316 << endl
317 << "testing deletions ...." << endl;
318
319 unsigned long removed_counter = 0;
320 size_t num_inserted = table.size();
321 for (int i = 0; i < n; i++)
322 if (table.search(keys(i)) != nullptr)
323 {
324 table.remove(keys(i));
326 }
327 cout << removed_counter << " items removed" << endl;
328
330 assert(table.size() == 0);
331 //keys.cut();
332
333 cout << "testing empty() method ...." << endl;
335
336 table.empty();
337
338 assert(table.size() == 0);
339
340 unsigned long repeated_counter = 0;
341 cout << "Reinserting keys ...." << endl;
342 for (size_t i = 0; i < n; ++i)
343 if (table.insert(keys(i), i) == NULL)
345
346 cout << repeated_counter << " duplicated numbers" << endl
347 << dup_counter << " was the previus value" << endl;
348
350
351 cout << "Done!" << endl
352 << endl
353 << "Testing for_each and a battery of other tests ...." << endl;
354
355 assert(table.all(/* Lambda */ [&table]
356 (const std::pair<const unsigned long, long> & p)
357 {
358 auto ptr = table.search(p.first);
359 assert(ptr != nullptr);
360 assert(table.get_data(p.first) == ptr->second);
361 return table.has(p.first);
362 })
363 );
364
365 cout << "done!" << endl
366 << endl
367 << "testing keys() method and other tests ...." << endl;
369 assert(all(the_keys, /* Lambda */ [&table] (unsigned long k)
370 { return table.has(k); }));
371
372 cout << "done!" << endl
373 << endl
374 /* << "Testing values() method ...." << endl;
375 DynList<long> values = table.values();
376 assert(all(values, [&table] (long & val)
377 { return table.search(table.get_key(ptr)) == ptr; }));
378 cout << "done!" << endl
379 << endl */
380 << "Testing items() method and othet stuff ...." << endl;
382 assert(all(items, /* Lambda */ [&table]
383 (std::pair<unsigned long, long> p)
384 { return table.find(p.first) == p.second; } ));
385 cout << "done!" << endl
386 << endl
387 << "Testing remove by data pointer ...." << endl;
388 removed_counter = 0;
389 for_each(keys, [&table, &removed_counter] (const unsigned long & k)
390 {
391 auto ptr = table.search(k);
392 if (ptr == nullptr)
393 return;
394
395 table.remove_by_data(ptr->second);
397 });
398 assert(table.is_empty());
399
400 cout << endl
401 << "Reinserting keys for doing other tests ...." << endl;
402 for (int i = 0; i < n; ++i)
403 table.insert(keys(i), i);
404 assert(table.size() == removed_counter);
405 cout << "done!" << endl
406 << endl;
407
408}
409
410int main(int argc, char * argv[])
411{
413 assert(next_prime(5) == 5);
414
415 unsigned long n = NumItems;
416 if (argc > 1)
417 {
418 try { n = stoul(argv[1]); } catch (...) { n = NumItems; }
419 }
420
421 if (n <= 0)
422 {
423 cerr << "n must be positive" << endl;
424 return 1;
425 }
426
427 unsigned int t = std::time(NULL);
428 if (argc > 2)
429 {
430 try { t = stoul(argv[2]); } catch (...) { t = std::time(NULL); }
431 }
432
433 cout << argv[0] << " " << n << " " << t << endl;
434
436 gsl_rng_set(r, t % gsl_rng_max(r));
437
440
443
444 cout << "testing of ODhash based set ...." << endl
445 << endl;
447 cout << endl
448 << "Done all test of ODhash based set!" << endl
449 << endl
450 << endl
451 << "testing of OLhash based set ...." << endl
452 << endl;
454 cout << endl
455 << "Done all test of OLhash based set!" << endl
456 << endl
457 << endl
458 << "Testing all tests of OLhash based map" << endl
459 << endl;
460 // test_DynMapLinHash<DynMapODHash>(n);
461 // cout << "Done all tests of OD hash based map" << endl
462 // << endl
463 // << endl
464 // << "Testing of OLhash based map" << endl
465 // << endl;
466 // test_DynMapLinHash<DynMapOLHash>(n);
467 // cout << "Done all tests of OL hash based map" << endl
468 // << endl
469 // << endl;
471}
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
int main()
T & append()
Allocate a new entry to the end of array.
Iterator on the items of list.
Definition htlist.H:1420
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
bool has_curr() const noexcept
Definition htlist.H:930
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
Definition ah-dry.H:1437
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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.
and
Check uniqueness with explicit hash + equality functors.
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
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 integrity of the prime database.
Definition primes.C:411
unsigned long next_prime(unsigned long n)
Find the smallest prime number >= n from the database.
Definition primes.C:383
STL namespace.
Aleph::DynList< T > keys() const
Definition ah-dry.H:1863
Aleph::DynList< T > items() const
Return a list of all the elements of a container sorted by traversal order.
Definition ah-dry.H:1854
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)
gsl_rng * r
void test_DynSetLinHash(size_t n)
void test_DynMapLinHash(size_t n)
#define NumItems
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