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 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
28# include <ctime>
29# include <cerrno>
30# include <climits>
31# include <gsl/gsl_rng.h>
32# include <cassert>
33# include <iostream>
34# include <ahFunctional.H>
35# include <ahSort.H>
36# include <tpl_sort_utils.H>
37# include <tpl_dynMapTree.H>
38
39# define NumItems 10000
40
41using namespace std;
42
43gsl_rng * r = nullptr;
44
47 unsigned long n)
48{
49 unsigned long dup_counter = 0;
50
51 cout << "Testing simple insertions and searches ...." << endl;
52
53 for (unsigned long i = 0; i < n; i++)
54 {
55 keys[i] = gsl_rng_get(r);
56 if (not table.has(keys(i)))
57 table.insert(keys(i));
58 else
60 }
61
62 return dup_counter;
63}
64
66{
68 other.for_each([&table] (unsigned long item)
69 {
70 table.insert(item);
71 });
72
73 return table;
74}
75
76void test_DynSet(size_t n)
77{
78 DynSetTreap<unsigned long> t = { 1, 2, 3, 4, 5};
81 unsigned long dup_counter = insert_n_random_items_in_set(table, keys, n);
82
83 unsigned long removed_counter = 0;
84 size_t num_inserted = table.size();
85 for (size_t i = 0; i < n; i++)
86 if (table.search(keys(i)) != nullptr)
87 {
88 table.remove(keys(i));
90 }
91
93 assert(table.size() == 0);
94
95 cout << removed_counter << " items removed" << endl;
96
97 cout << "testing empty() method ...." << endl;
99
100 table.empty();
101
102 assert(table.size() == 0);
103
104 unsigned long repeated_counter = 0;
105 cout << "Reinserting keys ...." << endl;
106 for (size_t i = 0; i < n; ++i)
107 if (table.insert(keys(i)) == nullptr)
109
110 cout << repeated_counter << " duplicated numbers" << endl
111 << dup_counter << " was the previous value" << endl;
112
114
115 cout << "Done!" << endl;
116
117 {
118 cout << "Testing iterator and map ...." << endl;
119
121 (/* Lambda */ [] (const unsigned long & k) -> unsigned long
122 {
123 return k;
124 });
125
126 for (DynList<unsigned long>::Iterator it(l); it.has_curr(); it.next())
127 assert(table.search(it.get_curr()) != nullptr);
128
129 cout << "done!" << endl;
130 }
131
132 {
133 cout << "testing lvalue copy constructor ...." << endl;
135
136 assert(table.equal_to(tmp));
137 }
138
139 {
140 cout << "testing lvalue assigment...." << endl;
142 for (size_t i = 0; i < n/2; ++i)
143 {
144 unsigned long key = gsl_rng_get(r);
145 while (aux.has(key))
146 key = gsl_rng_get(r);
147 aux.insert(key);
148 }
149
150 aux = table;
151
152 assert(aux == table);
153 }
154
155 {
156 cout << "Testing rvalue constructor ...." << endl;
158 assert(tmp == table);
159 cout << "done!" << endl
160 << endl
161 << "Testing rvalue assign = .... " << endl
162 << endl;
163 tmp = create_table(table);
164 cout << "done!" << endl
165 << endl;
166 }
167
168 {
169 DynArray<size_t> dups; // stores indexes of duplicated keys of keys array
170
171 cout << "done" << endl
172 << "Reinserting ...." << endl;
173
174 // Clear table to ensure reinsertion tests against an empty set
175 table.empty();
176
177 for (size_t i = 0; i < n; ++i)
178 if (table.insert(keys(i)) == nullptr)
179 dups.append(i);
180
181 cout << "Searching inserted keys ...." << endl;
182 for (size_t i = 0; i < n; ++i)
183 {
184 unsigned long * ptr = table.search(keys(i));
185 assert(ptr != nullptr);
186 if (dups.size() > 0)
187 {
188 const auto dup_idx = binary_search(dups, i);
189 if (dup_idx < dups.size() and dups(dup_idx) == i)
190 continue; // keys[] contains a dup entry
191 }
192 }
193 }
194
195 {
196 cout << "Testing keys() in set ...." << endl
197 << endl;
199 assert(the_keys.size() == table.size());
200 assert(all(the_keys, /* Lambda */ [&table] (const unsigned long & key)
201 {
202 return table.has(key);
203 }));
204 }
205
206 {
207 cout << endl
208 << "Testing filter of keys multiples of 13" << endl;
209
211 filter(table, [](const unsigned long & key)
212 {
213 return key % 13 == 0;
214 });
215
216 table.filter(/* Lambda */ [] (const unsigned long & key)
217 {
218 return key % 13 == 0;
219 }).for_each(/* Lambda */ [&v13] (const unsigned long & key)
220 {
221 cout << key << " ";
222 assert(contains(v13, key));
223 });
224 cout << endl;
225 }
226}
227
228
229unsigned long
232 unsigned long n)
233{
234 unsigned long dup_counter = 0;
235 cout << "Testing simple insertions and searches ...." << endl;
236 for (unsigned long i = 0; i < n; i++)
237 {
238 keys[i] = gsl_rng_get(r);
239 if (not table.has(keys(i)))
240 assert(table.insert(keys(i), i));
241 else
242 ++dup_counter;
243 }
244 return dup_counter;
245}
246
247void test_DynMap(size_t n)
248{
250 MapType table;
252 unsigned long dup_counter = insert_n_random_items_in_map(table, keys, n);
253
254 unsigned long removed_counter = 0;
255 size_t num_inserted = table.size();
256 for (size_t i = 0; i < n; i++)
257 if (table.search(keys(i)) != nullptr)
258 {
259 table.remove(keys(i));
261 }
262
264 assert(table.size() == 0);
265
266 cout << removed_counter << " items removed" << endl;
267
268 cout << "testing empty() method ...." << endl;
270
271 table.empty();
272
273 assert(table.size() == 0);
274
275 unsigned long repeated_counter = 0;
276 cout << "Reinserting keys ...." << endl;
277 for (size_t i = 0; i < n; ++i)
278 if (table.insert(keys(i), i) == nullptr)
280
281 cout << repeated_counter << " duplicated numbers" << endl
282 << dup_counter << " was the previous value" << endl;
283
285
286 cout << "Done!" << endl
287 << endl
288 << "Testing for_each and a battery of other tests ...." << endl;
289
290 assert(table.all([&table] (const std::pair<const unsigned long, long> & p)
291 {
292 auto * ptr = table.search(p.first);
293 assert(ptr != nullptr);
294 assert(table.get_data(p.first) == ptr->second);
295 return table.has(p.first);
296 })
297 );
298
299 cout << "done!" << endl
300 << endl
301 << "testing keys() method and other tests ...." << endl;
303 assert(all(the_keys, /* Lambda */ [&table] (unsigned long k)
304 { return table.has(k); }));
305
306 cout << "done!" << endl
307 << endl
308 << "Testing items() method and other stuff ...." << endl;
310 assert(all(items, /* Lambda */ [&table]
311 (std::pair<unsigned long, long> p)
312 { return table.find(p.first) == p.second; } ));
313 cout << "done!" << endl
314 << endl;
315}
316
317int main(int argc, char *argv[])
318{
319 unsigned long n = NumItems;
320 if (argc > 1)
321 {
322 if (argv[1][0] == '-')
323 {
324 cerr << "Invalid n: " << argv[1] << endl;
325 return 1;
326 }
327 char * endptr = nullptr;
328 errno = 0;
329 n = strtoul(argv[1], &endptr, 10);
330 if (errno != 0 or endptr == argv[1] or *endptr != '\0')
331 {
332 cerr << "Invalid n: " << argv[1] << endl;
333 return 1;
334 }
335 }
336
337 unsigned int t = (unsigned int) time(nullptr);
338 if (argc > 2)
339 {
340 if (argv[2][0] == '-')
341 {
342 cerr << "Invalid t: " << argv[2] << endl;
343 return 1;
344 }
345 char * endptr = nullptr;
346 errno = 0;
347 const unsigned long parsed_t = strtoul(argv[2], &endptr, 10);
348 if (errno != 0 or endptr == argv[2] or *endptr != '\0' or parsed_t > UINT_MAX)
349 {
350 cerr << "Invalid t: " << argv[2] << endl;
351 return 1;
352 }
353 t = static_cast<unsigned int>(parsed_t);
354 }
355
356 cout << argv[0] << " " << n << " " << t << endl;
357
359 gsl_rng_set(r, t % gsl_rng_max(r));
360
361 test_DynSet(n);
362
363 test_DynMap(n);
364
366}
Functional programming utilities for Aleph-w containers.
High-level sorting functions for Aleph containers.
Iterator on the items of list.
Definition htlist.H:1420
Dynamic singly linked list with functional programming support.
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:1852
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
Definition ah-dry.H:1319
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:779
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.
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: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[]
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