Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testOhash.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 <gsl/gsl_rng.h>
34
35# include <cassert>
36# include <iostream>
37# include <string>
38# include <aleph.H>
39# include <tpl_dynArray.H>
40# include <tpl_sort_utils.H>
41# include <tpl_olhash.H>
42# include <tpl_odhash.H>
43
44# include <ctime>
45using namespace std;
46
47static unsigned long tbl_size = 0;
48
50{
58 operator gsl_rng*() { return r; }
59};
60
61constexpr int DEFAULT_N = 100;
62
63template <template <typename,class> class HashTable, typename Key,
66{
69 for (typename Hset::Iterator it(s); it.has_curr(); it.next())
70 ret_val.insert(it.get_curr());
71
72 return ret_val;
73}
74
75template <template <typename, class> class HashTable, typename Key,
77void test_hash_table(size_t n, gsl_rng * r)
78{
81
82 Hset table;
83
84 tbl_size = table.size();
85
86 for (int k = 0; k < 4; k++)
87 {
88 cout << "k = " << k << endl
89 << "testing insertions and initial searches" << endl;
90
91 for (int i = 0; i < n; i++)
92 {
93 while (true)
94 {
95 keys[i] = gsl_rng_get(r);
96 if (table.search(keys(i)) == nullptr)
97 break;
98 }
99
100 table.insert(keys(i));
101 }
102
103 cout << "done" << endl
104 << endl;
105
106 table.print_stats(table.stats());
107
108 cout << endl
109 << "testing searches or previous inserted keys" << endl;
110 Key * ptr;
111 for (int i = 0; i < n; i++)
112 {
113 const Key k = keys(i);
114
115 ptr = table.search(k);
116
117 assert(ptr != nullptr);
118 assert(*ptr == keys(i));
119 }
120 cout << "done!" << endl
121 << endl
122 << "testing deletion ...." << endl;
123 for (int i = 0; i < n; i += 2)
124 {
125 ptr = table.search(keys(i));
126
127 assert(ptr != nullptr);
128
129 table.remove_ptr(ptr);
130 }
131 cout << "done!" << endl
132 << endl
133 << "Reinserting others keys ...." << endl;
134 for (int i = 0; i < n; i += 2)
135 {
136 while (true)
137 {
138 keys[i] = gsl_rng_get(r);
139 if (table.search(keys(i)) == nullptr)
140 break;
141 }
142 table.insert(keys(i));
143 }
144 cout << "done!" << endl
145 << endl
146 << "Removing all the keys ...." << endl;
147 for (int i = 0; i < n; i++)
148 {
149 const Key & key = keys(i);
150 ptr = table.search(key);
151 assert(ptr != nullptr);
152 table.remove_ptr(ptr);
153 }
154
155 assert(table.size() == 0);
156 cout << "done! k = " << k << endl
157 << endl;
158 }
159
160 cout << "Sorting keys backup ...." << endl;
162
163 cout << "done!" << endl
164 << endl
165 << "Testing iterator ...." << endl
166 << endl
167 << "Reinserting the keys ...."
168 << endl;
169 for (int i = 0; i < n; ++i)
170 table.insert(keys(i));
171
172 int count = 0;
173 for (typename Hset::Iterator it(table);
174 it.has_curr(); it.next(), ++count)
175 {
176 const Key & curr = it.get_curr();
177 int idx = binary_search(keys, curr);
178 assert(idx >= 0);
179 assert(curr == keys(idx));
180 }
181 assert(count == table.size());
182 cout << "done!" << endl
183 << endl
184 << "Testing backward iterator ...." << endl;
185 {
186 typename Hset::Iterator it(table);
187 count = 0;
188 it.reset_last();
189 for (int i = 0; it.has_curr(); it.prev(), ++i, ++count)
190 {
191 const Key & curr = it.get_curr();
192 int idx = binary_search(keys, curr);
193 assert(idx >= 0);
194 assert(curr == keys(idx));
195 }
196 assert(count == table.size());
197 cout << "done!" << endl
198 << endl;
199 }
200 cout << "done!" << endl
201 << endl
202 << "Testing del() of iterator ...." << endl
203 << "Deleting all the keys via del() of iterator"
204 << endl;
205 count = 0;
206 for (typename Hset::Iterator it(table); it.has_curr(); ++count)
207 it.del();
208
209 cout << "done!. Deleted " << count << " entries " << endl
210 << endl;
211
212 assert(count == n);
213 assert(table.is_empty());
214
215 cout << "Inserting again all keys ...." << endl
216 << endl;
217 for (int i = 0; i < n; ++i)
218 table.insert(keys(i));
219
220 cout << "done!" << endl
221 << endl
222 << "Deleting a 10% of keys for causing deleted entries ...." << endl
223 << endl;
224 for (int i = 0; i < n/10; ++i)
225 {
226 unsigned long idx = gsl_rng_uniform_int(r, keys.size());
227 const Key & key = keys(idx);
228
229 Key * ptr = table.search(key);
230 if (ptr == nullptr)
231 continue;
232
233 table.remove_ptr(ptr);
234 }
235
236 table.print_stats(table.stats());
237
238 {
239 cout << "Testing copy constructor" << endl;
240 Hset aux = table;
241 assert(aux.size() == table.size());
242 for (typename Hset::Iterator it(table); it.has_curr(); it.next())
243 {
244 Key * key_ptr = aux.search(it.get_curr());
245 assert(key_ptr != nullptr);
246 assert(*key_ptr == it.get_curr());
247 }
248 cout << "done!" << endl;
249 }
250
251 {
252 cout << "Testing rvalue copy constructor ...." << endl;
253 Hset aux = create_table(table);
254
255 assert(aux == table);
256
257 aux = create_table(table);
258
259 assert(aux == table);
260
261 cout << "done!" << endl
262 << endl;
263 }
264}
265
266int main(int argc, char *argv[])
267{
268 int n = DEFAULT_N;
269 unsigned int t = std::time(0);
270
271 try
272 {
273 if (argc > 1)
274 n = std::stoi(argv[1]);
275
276 if (argc > 2)
277 t = std::stoi(argv[2]);
278 }
279 catch (...)
280 {
281 // ignore
282 }
283
284 if (n <= 0)
285 {
286 cout << "n must be positive" << endl;
287 return 1;
288 }
289
290 cout << "testOhash " << n << " " << t << endl;
291
292 gsl_rng_holder r(t);
293
295
296 //test_hash_table<OLhashTable, unsigned int>(n, r);
297}
Core header for the Aleph-w library.
int main()
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
bool binary_search(Itor beg, Itor end, const T &value)
Binary search for a value.
Definition ahAlgo.H:1284
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
STL namespace.
gsl_rng * r
Definition testOhash.C:51
gsl_rng_holder(unsigned int seed)
Definition testOhash.C:52
int keys[]
ValueArg< size_t > seed
Definition testHash.C:53
void test_hash_table(size_t n, gsl_rng *r)
Definition testOhash.C:77
constexpr int DEFAULT_N
Definition testOhash.C:61
HashTable< Key, Cmp > create_table(const HashTable< Key, Cmp > &s)
Definition testOhash.C:65
static unsigned long tbl_size
Definition testOhash.C:47
static int * k
gsl_rng * r
Lazy and scalable dynamic array implementation.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.
Comprehensive sorting algorithms and search utilities for Aleph-w.