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