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