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 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27# include <ctime>
28# include <gsl/gsl_rng.h>
29# include <cassert>
30# include <iostream>
31# include <ahFunctional.H>
32# include <ahSort.H>
33# include <tpl_sort_utils.H>
34# include <tpl_dynSetHash.H>
35# include <tpl_dynMapOhash.H>
36# include <string>
37
38# define NumItems 10000
39
40using namespace std;
41
43
44template <template <typename, class> class SetType> unsigned long
46(SetType<unsigned long, Aleph::equal_to<unsigned long>> & table,
48 unsigned long n)
49{
50 unsigned long dup_counter = 0;
51
52 cout << "Testing simple insertions and searches ...." << endl;
53
54 for (int i = 0; i < n; i++)
55 {
56 keys[i] = gsl_rng_get(r);
57 if (not table.has(keys(i)))
58 table.insert(keys(i));
59 else
61
62 if (table.current_alpha() > 1.1)
63 {
64 cout << "Resizing table to " << 1.5*table.size() << endl;
65 table.resize(1.5*table.size());
66 cout << "done!" << endl;
67 }
68 }
69 cout << "done" << endl;
70
71 return dup_counter;
72}
73
74template <template <typename, class> class HashTable>
77(const HashTable<unsigned long, Aleph::equal_to<unsigned long>> & other)
78{
80 SetType table;
81 for (typename SetType::Iterator it(other); it.has_curr(); it.next())
82 table.insert(it.get_curr());
83
84 return table;
85}
86
87template <template <typename, class> class HashTable>
88void test_DynSetLinHash(size_t n)
89{
91 SetType table;
93 unsigned int dup_counter = insert_n_random_items_in_set(table, keys, n);
94
95 typename SetType::Stats stats = table.stats();
96 table.print_stats(stats);
97
98 cout << table.size() << " items inserted" << endl
99 << dup_counter << " duplicated numbers" << endl
100 << endl
101 << "testing deletions ...." << endl;
102
103 {
104 const auto ctable = table;
105 assert(table.all([&ctable] (auto k) { return ctable.find(k) == k; }));
106 }
107
108 unsigned long removed_counter = 0;
109 size_t num_inserted = table.size();
110 for (size_t i = 0; i < n; i++)
111 if (table.search(keys(i)) != NULL)
112 {
113 table.remove(keys(i));
115 }
116
118 assert(table.size() == 0);
119
120 cout << removed_counter << " items removed" << endl;
121
122 cout << "testing empty() method ...." << endl;
124
125 table.empty();
126
127 assert(table.size() == 0);
128
129 unsigned long repeated_counter = 0;
130 cout << "Reinserting keys ...." << endl;
131 for (size_t i = 0; i < n; ++i)
132 if (table.insert(keys(i)) == NULL)
134
135 cout << repeated_counter << " duplicated numbers" << endl
136 << dup_counter << " was the previus value" << endl;
137
139
140 cout << "Done!" << endl;
141
142 {
143 cout << "Testing iterator and map ...." << endl;
144
146 ([] (unsigned long k) -> unsigned long
147 {
148 return k;
149 });
150
151 for (DynList<unsigned long>::Iterator it(l); it.has_curr(); it.next())
152 assert(table.search(it.get_curr()) != NULL);
153
154 cout << "done!" << endl;
155 }
156
157 {
158 cout << "testing lvalue copy constructor ...." << endl;
159 SetType tmp = table;
160
161 assert(table.equal_to(tmp));
162 }
163
164 {
165 cout << "testing lvalue assigment...." << endl;
166 SetType aux;
167 for (size_t i = 0; i < n/2; ++i)
168 {
169 unsigned long key = gsl_rng_get(r);
170 while (aux.has(key))
171 key = gsl_rng_get(r);
172 aux.insert(key);
173 }
174
175 aux = table;
176
177 assert(aux == table);
178 }
179
180 {
181 cout << "Testing rvalue constructor ...." << endl;
182 SetType tmp = create_table(table);
183 assert(tmp == table);
184 cout << "done!" << endl
185 << endl
186 << "Testing rvalue assign = .... " << endl
187 << endl;
188 tmp = create_table(table);
189 cout << "done!" << endl
190 << endl;
191 }
192
193 {
194 cout << "testing del() of Iterator ...." << endl
195 << "Deleting all entries through del() ...." << endl;
196
197 for (typename SetType::Iterator it(table); it.has_curr(); /* empty */)
198 it.del();
199
200 assert(table.is_empty());
201
202 DynArray<size_t> dups; // stores indexes of duplicated keys of keys array
203
204 cout << "done" << endl
205 << "Reinserting ...." << endl;
206 for (int i = 0; i < n; ++i)
207 if (table.insert(keys(i)) == NULL)
208 dups.append(i);
209
210 cout << "Searching inserted keys ...." << endl;
211 for (size_t i = 0; i < n; ++i)
212 {
213 unsigned long * ptr = table.search(keys(i));
214 assert(ptr != NULL);
215 if (dups.size() > 0)
216 {
217 const auto dup_idx = binary_search(dups, i);
218 if (dup_idx < dups.size() and dups(dup_idx) == i)
219 continue; // keys[] contains a dup entry
220 }
221 }
222 }
223
224 {
225 cout << "Testing keys() in set ...." << endl
226 << endl;
228 assert(the_keys.size() == table.size());
229 assert(all(the_keys, /* Lambda */ [&table] (const size_t & key)
230 {
231 return table.has(key);
232 }));
233 }
234
235 {
236 cout << endl
237 << "Testing filter of keys multiples of 13" << endl;
238
240 filter(table, [] (const unsigned long & key)
241 {
242 return key % 13 == 0;
243 });
244
245 table.filter(/* Lambda */ [] (const unsigned long & key)
246 {
247 return key % 13 == 0;
248 }).for_each(/* Lambda */ [&v13] (const unsigned long & key)
249 {
250 cout << key << " ";
251 assert(contains(v13, key));
252 });
253 cout << endl;
254 }
255}
256
257
258template <class HashTable>
261 unsigned long n)
262{
263 unsigned long dup_counter = 0;
264 cout << "Testing simple insertions and searches ...." << endl;
265 for (long i = 0; i < n; i++)
266 {
267 keys[i] = gsl_rng_get(r);
268 if (not table.has(keys(i)))
269 assert(table.insert(keys(i), i));
270 else
271 ++dup_counter;
272 }
273
274 cout << n << " tries " << endl
275 << dup_counter << " duplicated" << endl
276 << "size = " << table.size() << endl
277 << endl
278 << "Performing map search test" << endl
279 << endl;
280
281 cout << "keys =";
282 sort(keys).for_each([] (auto k) { cout << " " << k; });
283 cout << endl
284 << "table = ";
285 sort(table.keys()).for_each([] (auto k) { cout << " " << k; });
286 cout << endl;
287
288 for (auto i = 0; i < n; i++)
289 assert(table.search(keys(i)));
290
291 assert(keys.all([&table] (auto k) { return table.search(k) != nullptr; }));
292 cout << "Passed" << endl
293 << endl;
294
295 return dup_counter;
296}
297
298template <template <typename, typename, class> class HashTable>
299void test_DynMapLinHash(size_t n)
300{
302 MapType table;
304 unsigned int dup_counter = insert_n_random_items_in_map(table, keys, n);
305
306 typename MapType::Stats stats = table.stats();
307 table.print_stats(stats);
308
309 cout << table.size() << " items inserted" << endl
310 << dup_counter << " duplicated numbers" << endl
311 << endl
312 << "testing deletions ...." << endl;
313
314 unsigned long removed_counter = 0;
315 size_t num_inserted = table.size();
316 for (int i = 0; i < n; i++)
317 if (table.search(keys(i)) != nullptr)
318 {
319 table.remove(keys(i));
321 }
322 cout << removed_counter << " items removed" << endl;
323
325 assert(table.size() == 0);
326 //keys.cut();
327
328 cout << "testing empty() method ...." << endl;
330
331 table.empty();
332
333 assert(table.size() == 0);
334
335 unsigned long repeated_counter = 0;
336 cout << "Reinserting keys ...." << endl;
337 for (size_t i = 0; i < n; ++i)
338 if (table.insert(keys(i), i) == NULL)
340
341 cout << repeated_counter << " duplicated numbers" << endl
342 << dup_counter << " was the previus value" << endl;
343
345
346 cout << "Done!" << endl
347 << endl
348 << "Testing for_each and a battery of other tests ...." << endl;
349
350 assert(table.all(/* Lambda */ [&table]
351 (const std::pair<const unsigned long, long> & p)
352 {
353 auto ptr = table.search(p.first);
354 assert(ptr != nullptr);
355 assert(table.get_data(p.first) == ptr->second);
356 return table.has(p.first);
357 })
358 );
359
360 cout << "done!" << endl
361 << endl
362 << "testing keys() method and other tests ...." << endl;
364 assert(all(the_keys, /* Lambda */ [&table] (unsigned long k)
365 { return table.has(k); }));
366
367 cout << "done!" << endl
368 << endl
369 /* << "Testing values() method ...." << endl;
370 DynList<long> values = table.values();
371 assert(all(values, [&table] (long & val)
372 { return table.search(table.get_key(ptr)) == ptr; }));
373 cout << "done!" << endl
374 << endl */
375 << "Testing items() method and othet stuff ...." << endl;
377 assert(all(items, /* Lambda */ [&table]
378 (std::pair<unsigned long, long> p)
379 { return table.find(p.first) == p.second; } ));
380 cout << "done!" << endl
381 << endl
382 << "Testing remove by data pointer ...." << endl;
383 removed_counter = 0;
384 for_each(keys, [&table, &removed_counter] (const unsigned long & k)
385 {
386 auto ptr = table.search(k);
387 if (ptr == nullptr)
388 return;
389
390 table.remove_by_data(ptr->second);
392 });
393 assert(table.is_empty());
394
395 cout << endl
396 << "Reinserting keys for doing other tests ...." << endl;
397 for (int i = 0; i < n; ++i)
398 table.insert(keys(i), i);
399 assert(table.size() == removed_counter);
400 cout << "done!" << endl
401 << endl;
402
403}
404
405int main(int argc, char * argv[])
406{
408 assert(next_prime(5) == 5);
409
410 unsigned long n = NumItems;
411 if (argc > 1)
412 {
413 try { n = stoul(argv[1]); } catch (...) { n = NumItems; }
414 }
415
416 if (n <= 0)
417 {
418 cerr << "n must be positive" << endl;
419 return 1;
420 }
421
422 unsigned int t = std::time(NULL);
423 if (argc > 2)
424 {
425 try { t = stoul(argv[2]); } catch (...) { t = std::time(NULL); }
426 }
427
428 cout << argv[0] << " " << n << " " << t << endl;
429
431 gsl_rng_set(r, t % gsl_rng_max(r));
432
435
438
439 cout << "testing of ODhash based set ...." << endl
440 << endl;
442 cout << endl
443 << "Done all test of ODhash based set!" << endl
444 << endl
445 << endl
446 << "testing of OLhash based set ...." << endl
447 << endl;
449 cout << endl
450 << "Done all test of OLhash based set!" << endl
451 << endl
452 << endl
453 << "Testing all tests of OLhash based map" << endl
454 << endl;
455 // test_DynMapLinHash<DynMapODHash>(n);
456 // cout << "Done all tests of OD hash based map" << endl
457 // << endl
458 // << endl
459 // << "Testing of OLhash based map" << endl
460 // << endl;
461 // test_DynMapLinHash<DynMapOLHash>(n);
462 // cout << "Done all tests of OL hash based map" << endl
463 // << endl
464 // << endl;
466}
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
T & append()
Allocate a new entry to the end of array.
Iterator on the items of list.
Definition htlist.H:1420
Dynamic singly linked list with functional programming support.
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:1319
and
Check uniqueness with explicit hash + equality functors.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
Container2< typename Container1::Item_Type > filter(Container1 &container, Operation &operation)
Filter elements that satisfy operation.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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:227
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
size_t 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:1729
Aleph::DynList< T > items() const
Return a list of all the elements of a container sorted by traversal order.
Definition ah-dry.H:1722
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