Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testDynSetTree.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
33# include <ctime>
34# include <cerrno>
35# include <climits>
36# include <gsl/gsl_rng.h>
37# include <cassert>
38# include <iostream>
39# include <ahFunctional.H>
40# include <ahSort.H>
41# include <tpl_sort_utils.H>
42# include <tpl_dynMapTree.H>
43
44# define NumItems 10000
45
46using namespace std;
47
48gsl_rng * r = nullptr;
49
52 unsigned long n)
53{
54 unsigned long dup_counter = 0;
55
56 cout << "Testing simple insertions and searches ...." << endl;
57
58 for (unsigned long i = 0; i < n; i++)
59 {
60 keys[i] = gsl_rng_get(r);
61 if (not table.has(keys(i)))
62 table.insert(keys(i));
63 else
65 }
66
67 return dup_counter;
68}
69
71{
73 other.for_each([&table] (unsigned long item)
74 {
75 table.insert(item);
76 });
77
78 return table;
79}
80
81void test_DynSet(size_t n)
82{
83 DynSetTreap<unsigned long> t = { 1, 2, 3, 4, 5};
86 unsigned long dup_counter = insert_n_random_items_in_set(table, keys, n);
87
88 unsigned long removed_counter = 0;
89 size_t num_inserted = table.size();
90 for (size_t i = 0; i < n; i++)
91 if (table.search(keys(i)) != nullptr)
92 {
93 table.remove(keys(i));
95 }
96
98 assert(table.size() == 0);
99
100 cout << removed_counter << " items removed" << endl;
101
102 cout << "testing empty() method ...." << endl;
104
105 table.empty();
106
107 assert(table.size() == 0);
108
109 unsigned long repeated_counter = 0;
110 cout << "Reinserting keys ...." << endl;
111 for (size_t i = 0; i < n; ++i)
112 if (table.insert(keys(i)) == nullptr)
114
115 cout << repeated_counter << " duplicated numbers" << endl
116 << dup_counter << " was the previous value" << endl;
117
119
120 cout << "Done!" << endl;
121
122 {
123 cout << "Testing iterator and map ...." << endl;
124
126 (/* Lambda */ [] (const unsigned long & k) -> unsigned long
127 {
128 return k;
129 });
130
131 for (DynList<unsigned long>::Iterator it(l); it.has_curr(); it.next())
132 assert(table.search(it.get_curr()) != nullptr);
133
134 cout << "done!" << endl;
135 }
136
137 {
138 cout << "testing lvalue copy constructor ...." << endl;
140
141 assert(table.equal_to(tmp));
142 }
143
144 {
145 cout << "testing lvalue assigment...." << endl;
147 for (size_t i = 0; i < n/2; ++i)
148 {
149 unsigned long key = gsl_rng_get(r);
150 while (aux.has(key))
151 key = gsl_rng_get(r);
152 aux.insert(key);
153 }
154
155 aux = table;
156
157 assert(aux == table);
158 }
159
160 {
161 cout << "Testing rvalue constructor ...." << endl;
163 assert(tmp == table);
164 cout << "done!" << endl
165 << endl
166 << "Testing rvalue assign = .... " << endl
167 << endl;
168 tmp = create_table(table);
169 cout << "done!" << endl
170 << endl;
171 }
172
173 {
174 DynArray<size_t> dups; // stores indexes of duplicated keys of keys array
175
176 cout << "done" << endl
177 << "Reinserting ...." << endl;
178
179 // Clear table to ensure reinsertion tests against an empty set
180 table.empty();
181
182 for (size_t i = 0; i < n; ++i)
183 if (table.insert(keys(i)) == nullptr)
184 dups.append(i);
185
186 cout << "Searching inserted keys ...." << endl;
187 for (size_t i = 0; i < n; ++i)
188 {
189 unsigned long * ptr = table.search(keys(i));
190 assert(ptr != nullptr);
191 if (dups.size() > 0)
192 {
193 const auto dup_idx = binary_search(dups, i);
194 if (dup_idx < dups.size() and dups(dup_idx) == i)
195 continue; // keys[] contains a dup entry
196 }
197 }
198 }
199
200 {
201 cout << "Testing keys() in set ...." << endl
202 << endl;
204 assert(the_keys.size() == table.size());
205 assert(all(the_keys, /* Lambda */ [&table] (const unsigned long & key)
206 {
207 return table.has(key);
208 }));
209 }
210
211 {
212 cout << endl
213 << "Testing filter of keys multiples of 13" << endl;
214
216 filter(table, [](const unsigned long & key)
217 {
218 return key % 13 == 0;
219 });
220
221 table.filter(/* Lambda */ [] (const unsigned long & key)
222 {
223 return key % 13 == 0;
224 }).for_each(/* Lambda */ [&v13] (const unsigned long & key)
225 {
226 cout << key << " ";
227 assert(contains(v13, key));
228 });
229 cout << endl;
230 }
231}
232
233
234unsigned long
237 unsigned long n)
238{
239 unsigned long dup_counter = 0;
240 cout << "Testing simple insertions and searches ...." << endl;
241 for (unsigned long i = 0; i < n; i++)
242 {
243 keys[i] = gsl_rng_get(r);
244 if (not table.has(keys(i)))
245 assert(table.insert(keys(i), i));
246 else
247 ++dup_counter;
248 }
249 return dup_counter;
250}
251
252void test_DynMap(size_t n)
253{
255 MapType table;
257 unsigned long dup_counter = insert_n_random_items_in_map(table, keys, n);
258
259 unsigned long removed_counter = 0;
260 size_t num_inserted = table.size();
261 for (size_t i = 0; i < n; i++)
262 if (table.search(keys(i)) != nullptr)
263 {
264 table.remove(keys(i));
266 }
267
269 assert(table.size() == 0);
270
271 cout << removed_counter << " items removed" << endl;
272
273 cout << "testing empty() method ...." << endl;
275
276 table.empty();
277
278 assert(table.size() == 0);
279
280 unsigned long repeated_counter = 0;
281 cout << "Reinserting keys ...." << endl;
282 for (size_t i = 0; i < n; ++i)
283 if (table.insert(keys(i), i) == nullptr)
285
286 cout << repeated_counter << " duplicated numbers" << endl
287 << dup_counter << " was the previous value" << endl;
288
290
291 cout << "Done!" << endl
292 << endl
293 << "Testing for_each and a battery of other tests ...." << endl;
294
295 assert(table.all([&table] (const std::pair<const unsigned long, long> & p)
296 {
297 auto * ptr = table.search(p.first);
298 assert(ptr != nullptr);
299 assert(table.get_data(p.first) == ptr->second);
300 return table.has(p.first);
301 })
302 );
303
304 cout << "done!" << endl
305 << endl
306 << "testing keys() method and other tests ...." << endl;
308 assert(all(the_keys, /* Lambda */ [&table] (unsigned long k)
309 { return table.has(k); }));
310
311 cout << "done!" << endl
312 << endl
313 << "Testing items() method and other stuff ...." << endl;
315 assert(all(items, /* Lambda */ [&table]
316 (std::pair<unsigned long, long> p)
317 { return table.find(p.first) == p.second; } ));
318 cout << "done!" << endl
319 << endl;
320}
321
322int main(int argc, char *argv[])
323{
324 unsigned long n = NumItems;
325 if (argc > 1)
326 {
327 if (argv[1][0] == '-')
328 {
329 cerr << "Invalid n: " << argv[1] << endl;
330 return 1;
331 }
332 char * endptr = nullptr;
333 errno = 0;
334 n = strtoul(argv[1], &endptr, 10);
335 if (errno != 0 or endptr == argv[1] or *endptr != '\0')
336 {
337 cerr << "Invalid n: " << argv[1] << endl;
338 return 1;
339 }
340 }
341
342 unsigned int t = (unsigned int) time(nullptr);
343 if (argc > 2)
344 {
345 if (argv[2][0] == '-')
346 {
347 cerr << "Invalid t: " << argv[2] << endl;
348 return 1;
349 }
350 char * endptr = nullptr;
351 errno = 0;
352 const unsigned long parsed_t = strtoul(argv[2], &endptr, 10);
353 if (errno != 0 or endptr == argv[2] or *endptr != '\0' or parsed_t > UINT_MAX)
354 {
355 cerr << "Invalid t: " << argv[2] << endl;
356 return 1;
357 }
358 t = static_cast<unsigned int>(parsed_t);
359 }
360
361 cout << argv[0] << " " << n << " " << t << endl;
362
364 gsl_rng_set(r, t % gsl_rng_max(r));
365
366 test_DynSet(n);
367
368 test_DynMap(n);
369
371}
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
int main()
Iterator on the items of list.
Definition htlist.H:1420
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1155
Generic key-value map implemented on top of a binary search tree.
bool has(const Key &key) const noexcept
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Dynamic set implemented using randomized treap binary search trees of type Treap<Key>.
Dynamic set backed by balanced binary search trees with automatic memory management.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
bool has(const Key &key) const
size_t remove(const Key &key)
Removes a key from the dynamic set.
Key * search(const Key &key) const
Find an element in the set.
void empty()
remove all elements from the set
bool has_curr() const noexcept
Definition htlist.H:930
bool equal_to(const Container &r) const noexcept
Test if elements of this are exactly contained in another container.
Definition ah-dry.H:1984
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
Definition ah-dry.H:1437
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:796
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.
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
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[]
DynSetTree< unsigned long > create_table(const DynSetTree< unsigned long > &other)
void test_DynSet(size_t n)
unsigned long insert_n_random_items_in_set(DynSetTree< unsigned long > &table, DynArray< unsigned long > &keys, unsigned long n)
gsl_rng * r
unsigned long insert_n_random_items_in_map(DynMapTree< unsigned long, long > &table, DynArray< unsigned long > &keys, unsigned long n)
void test_DynMap(size_t n)
#define NumItems
static int * k
Dynamic key-value map based on balanced binary search trees.
Comprehensive sorting algorithms and search utilities for Aleph-w.
DynList< int > l