Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testLinHash.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 <iostream>
34# include <string>
35# include <ctime>
36# include <cstdlib>
37# include <aleph.H>
38# include <tpl_linHash.H>
39
40/* TODO:
41 1-. Test del() del iterador
42
43*/
44
45# include <cassert>
46using namespace std;
47
48# include <cassert>
49using namespace Aleph;
50
51struct Entry : public LinearHashTableVtl<unsigned long>::Bucket
52{
53 unsigned long val;
54
55 Entry(unsigned long k, unsigned long v)
57 {
58 /* EMPTY */
59 }
60};
61
63{
64 cout << "Capacity = " << table.capacity() << endl
65 << "size = " << table.size() << endl
66 << "busy slots = " << table.busy_slots() << endl
67 << "expansions = " << table.expansions() << endl
68 << "alpha = " << 1.0*table.size()/table.capacity() << endl;
69}
70
71int main(int argc, char *argv[])
72{
73 const unsigned long numNodes = 10000;
74
75 unsigned long i, n = numNodes;
76
77 unsigned long value;
78
79 unsigned int t = std::time(NULL);
80
81 try
82 {
83 if (argc > 1)
84 n = std::stoul(argv[1]);
85
86 if (argc > 2)
87 t = std::stoul(argv[2]);
88 }
89 catch (...)
90 {
91 // ignore
92 }
93
94 if (n <= 0)
95 {
96 cout << "n must be positive" << endl;
97 return 1;
98 }
99
101
102 cout << "testDynamicHash " << n << " " << t << endl;
103
104 srand(t);
105
107 Entry* bucket;
108
109 print_stats(table);
110
111 cout << "Inserting..." << endl;
112
113 for (i = 0; i < n; i++)
114 {
115 do
116 {
117 value = keys[i] = (unsigned long) (10*n*(rand() /(RAND_MAX + 1.0)));
118 }
119 while (table.search(value) != NULL);
120
121 cout << value << " ";
122
123 bucket = new Entry (keys[i], i);
124 table.insert(bucket);
125 }
126
127 cout << endl;
128
129 table.print();
130
131 print_stats(table);
132
133 cout << endl << "Searching..." << endl;
134
135 for (i = 0; i < n; i++)
136 {
137 value = keys[i];
138 bucket = static_cast<Entry*>(table.search(value));
139 if (bucket == NULL)
140 {
141 cout << endl << "Error key " << keys[i] << " not found" << endl;
142 abort();
143 }
144 }
145
146 cout << "Testing iterator" << endl;
147
148 {
149 long count = 0;
151 it.next(), ++count)
152 cout << it.get_curr()->get_key() << " ";
153 if (count != table.size())
154 AH_ERROR("Test not passed count = %ld != %ld", count, table.size());
155 }
156
157 cout << endl << "testing deleting ..." << endl;
158
159 try
160 {
161 for (i = 0; i < n; i++)
162 {
163 value = keys[i];
164 bucket = static_cast<Entry*>(table.search(value));
165 if (bucket != NULL)
166 {
167 table.remove(bucket);
168 delete bucket;
169 }
170 else
171 AH_ERROR("%u th key %u not found\n", (int) i, (int) keys[i]);
172 }
173 print_stats(table);
174 }
175 catch (exception& exc)
176 {
177 cout << exc.what() << " exception has been thrown" << endl;
178 }
179 catch (...)
180 {
181 cout << " unknown exception has been thrown" << endl;
182 }
183
184 assert(table.size() == 0);
185}
186
187
#define AH_ERROR(...)
Print an error message (always enabled).
Definition ahDefs.H:270
Core header for the Aleph-w library.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Generic linear hash table.
const size_t & capacity() const noexcept
Returns the table capacity.
const size_t & size() const noexcept
Returns the number of elements in the table.
const size_t & expansions() const noexcept
Returns the expansion level performed on the table.
Bucket * remove(Bucket *bucket) noexcept
Remove bucket from table.
const size_t & busy_slots() const noexcept
Returns the number of busy slots in the table.
Bucket * insert(Bucket *bucket)
Insert bucket in the table.
Bucket * search(const Key &key) const noexcept
Search for key in the linear hash table.
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
Table::Bucket Bucket
Definition lin-hash.cc:54
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
Entry(unsigned long k, unsigned long v)
Definition testLinHash.C:55
unsigned long val
Definition testLinHash.C:53
int keys[]
void print_stats(LinearHashTableVtl< unsigned long > &table)
Definition testLinHash.C:62
static int * k
Linear hashing with chaining.