Aleph-w 7.10.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 <ah-random.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
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] = r.get();
61 if (not table.has(keys(i)))
62 table.insert(keys(i));
63 else
64 ++dup_counter;
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));
94 ++removed_counter;
95 }
96
97 assert(removed_counter == num_inserted);
98 assert(table.size() == 0);
99
100 cout << removed_counter << " items removed" << endl;
101
102 cout << "testing empty() method ...." << endl;
103 dup_counter = insert_n_random_items_in_set(table, keys, n);
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)
113 ++repeated_counter;
114
115 cout << repeated_counter << " duplicated numbers" << endl
116 << dup_counter << " was the previous value" << endl;
117
118 assert(dup_counter == repeated_counter);
119
120 cout << "Done!" << endl;
121
122 {
123 cout << "Testing iterator and map ...." << endl;
124
125 DynList<unsigned long> l = table.template maps<unsigned long>
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;
139 DynSetTree<unsigned long> tmp = table;
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 = r.get();
150 while (aux.has(key))
151 key = r.get();
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;
203 DynList<unsigned long> the_keys = table.keys();
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] = r.get();
244 if (not table.has(keys(i)))
245 {
246 // Keep the insertion out of assert(): NDEBUG would remove it.
247 [[maybe_unused]] const auto inserted = table.insert(keys(i), i);
248 assert(inserted != nullptr);
249 }
250 else
251 ++dup_counter;
252 }
253 return dup_counter;
254}
255
256void test_DynMap(size_t n)
257{
258 typedef DynMapTree<unsigned long, long> MapType;
259 MapType table;
261 unsigned long dup_counter = insert_n_random_items_in_map(table, keys, n);
262
263 unsigned long removed_counter = 0;
264 size_t num_inserted = table.size();
265 for (size_t i = 0; i < n; i++)
266 if (table.search(keys(i)) != nullptr)
267 {
268 table.remove(keys(i));
269 ++removed_counter;
270 }
271
272 assert(removed_counter == num_inserted);
273 assert(table.size() == 0);
274
275 cout << removed_counter << " items removed" << endl;
276
277 cout << "testing empty() method ...." << endl;
278 dup_counter = insert_n_random_items_in_map(table, keys, n);
279
280 table.empty();
281
282 assert(table.size() == 0);
283
284 unsigned long repeated_counter = 0;
285 cout << "Reinserting keys ...." << endl;
286 for (size_t i = 0; i < n; ++i)
287 if (table.insert(keys(i), i) == nullptr)
288 ++repeated_counter;
289
290 cout << repeated_counter << " duplicated numbers" << endl
291 << dup_counter << " was the previous value" << endl;
292
293 assert(dup_counter == repeated_counter);
294
295 cout << "Done!" << endl
296 << endl
297 << "Testing for_each and a battery of other tests ...." << endl;
298
299 assert(table.all([&table] (const std::pair<const unsigned long, long> & p)
300 {
301 auto * ptr = table.search(p.first);
302 assert(ptr != nullptr);
303 assert(table.get_data(p.first) == ptr->second);
304 return table.has(p.first);
305 })
306 );
307
308 cout << "done!" << endl
309 << endl
310 << "testing keys() method and other tests ...." << endl;
311 DynList<unsigned long> the_keys = table.keys();
312 assert(all(the_keys, /* Lambda */ [&table] (unsigned long k)
313 { return table.has(k); }));
314
315 cout << "done!" << endl
316 << endl
317 << "Testing items() method and other stuff ...." << endl;
319 assert(all(items, /* Lambda */ [&table]
320 (std::pair<unsigned long, long> p)
321 { return table.find(p.first) == p.second; } ));
322 cout << "done!" << endl
323 << endl;
324}
325
326int main(int argc, char *argv[])
327{
328 unsigned long n = NumItems;
329 if (argc > 1)
330 {
331 if (argv[1][0] == '-')
332 {
333 cerr << "Invalid n: " << argv[1] << endl;
334 return 1;
335 }
336 char * endptr = nullptr;
337 errno = 0;
338 n = strtoul(argv[1], &endptr, 10);
339 if (errno != 0 or endptr == argv[1] or *endptr != '\0')
340 {
341 cerr << "Invalid n: " << argv[1] << endl;
342 return 1;
343 }
344 }
345
346 unsigned int t = (unsigned int) time(nullptr);
347 if (argc > 2)
348 {
349 if (argv[2][0] == '-')
350 {
351 cerr << "Invalid t: " << argv[2] << endl;
352 return 1;
353 }
354 char * endptr = nullptr;
355 errno = 0;
356 const unsigned long parsed_t = strtoul(argv[2], &endptr, 10);
357 if (errno != 0 or endptr == argv[2] or *endptr != '\0' or parsed_t > UINT_MAX)
358 {
359 cerr << "Invalid t: " << argv[2] << endl;
360 return 1;
361 }
362 t = static_cast<unsigned int>(parsed_t);
363 }
364
365 cout << argv[0] << " " << n << " " << t << endl;
366
368
369 test_DynSet(n);
370
371 test_DynMap(n);
372
373}
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
Generic key-value map implemented on top of a binary search tree.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
bool has(const Key &key) const noexcept(nothrow_search)
Return whether key is in the map.
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)
Find an element in the set.
void empty()
Remove all the elements of the set.
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
bool equal_to(const Container &r) const
Equality test between this and r.
Definition ah-dry.H:3129
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
Definition ah-dry.H:2356
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
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.
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
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[]
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)
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
Gsl_Compatible_Rng r
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