Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
hash_resize.C
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
159# include <tclap/CmdLine.h>
160# include <tpl_dynMapOhash.H>
161# include <iostream>
162# include <cassert>
163
164using namespace std;
165using namespace Aleph;
166
167struct Foo
168{
169 string val;
170 int i;
171
172 Foo() : val(), i(0) {}
173 Foo(int num) : val(to_string(num)), i(num) {}
174
175 bool operator == (const Foo & foo) const
176 {
177 return i == foo.i and val == foo.val;
178 }
179};
180
183
184void fill(size_t n)
185{
186 cout << "Inserting " << n << " elements into hash table..." << endl;
187 for (size_t i = 0; i < n; ++i)
188 {
189 Foo foo(i);
190 auto ptr = tbl.insert(i, foo); // Insert copy
191 assert(ptr != nullptr);
192 (void)ptr;
193 backup.append(foo); // Append another copy
194
195 if (i % 100 == 0)
196 cout << " Inserted " << i << " elements" << endl;
197 }
198 cout << "Insertion complete. Table size: " << tbl.size() << endl;
199}
200
201void verify()
202{
203 cout << "Verifying all elements..." << endl;
204 size_t count = 0;
205 for (auto it = backup.get_it(); it.has_curr(); it.next())
206 {
207 const Foo & foo = it.get_curr();
208 auto ptr = tbl.search(foo.i);
209 assert(ptr != nullptr);
210 assert(ptr->first == foo.i);
211 assert(ptr->second == foo);
212 (void)ptr;
213 ++count;
214 }
215 cout << "Verification complete. " << count << " elements verified." << endl;
216}
217
218int main(int argc, char **argv)
219{
220 try
221 {
222 TCLAP::CmdLine cmd("Hash table resize example", ' ', "1.0");
223
224 TCLAP::ValueArg<size_t> nArg("n", "count",
225 "Number of keys to insert",
226 false, 1000, "size_t");
227 cmd.add(nArg);
228
229 cmd.parse(argc, argv);
230
231 size_t n = nArg.getValue();
232
233 cout << "Hash Resize Example" << endl;
234 cout << "===================" << endl;
235 cout << "Testing with " << n << " elements" << endl << endl;
236
237 fill(n);
238 cout << endl;
239 verify();
240
241 cout << endl << "Test passed successfully!" << endl;
242 }
243 catch (TCLAP::ArgException &e)
244 {
245 cerr << "Error: " << e.error() << " for arg " << e.argId() << endl;
246 return 1;
247 }
248
249 return 0;
250}
int main()
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
MapOLhash< int, Foo > tbl
DynArray< Foo > backup
void verify()
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
Definition ahAlgo.H:707
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
Open addressing hash map using linear probing.
int i
Foo(int num)
string val
bool operator==(const Foo &foo) const
Dynamic map with open hashing.