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