Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testDynHash.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 <tpl_dynLhash.H>
29# include <tpl_dynArray.H>
30# include <iostream>
31# include <string>
32# include <ctime>
33# include <cstdlib>
34
35# define NumItems 10000
36
37# include <cassert>
38using namespace std;
39
41
42static size_t hashFct(const unsigned & key)
43{
44 return key;
45}
46
47static void testResize(HTable& table)
48{
49 if (table.get_num_busy_slots() > (99*table.capacity())/100
50 and table.size()/table.capacity() > 3)
51 {
52 unsigned long currSize = table.capacity();
53 cout << "Resizing hash table from " << currSize << " ... ";
54 cout << table.resize(1.5 * table.capacity()) << endl;
55 }
56}
57
58static void printPars(const HTable& table)
59{
60 cout << "Table length = " << table.capacity() << endl
61 << "Busy slots = " << table.get_num_busy_slots() << endl
62 << "Num items = " << table.size() << endl;
63}
64
65int main(int argc, char *argv[])
66{
68
69 int n = NumItems;
70 unsigned int t = std::time(NULL);
71
72 try
73 {
74 if (argc > 1)
75 n = std::stoi(argv[1]);
76
77 if (argc > 2)
78 t = std::stoi(argv[2]);
79 }
80 catch (...)
81 {
82 // ignore
83 }
84
85 if (n <= 0)
86 {
87 cout << "n must be positive" << endl;
88 return 1;
89 }
90
91 srand(t);
92
93 cout << argv[0] << " " << n << " " << t << endl;
94
95 HTable table(1.15*n, hashFct);
97 unsigned int i;
98 unsigned int foundCounter = 0;
99
100 for (i = 0; i < n/2; i++)
101 {
102 keys[i] = rand();
103 testResize(table);
104 if (table.search(keys(i)) == NULL)
105 assert(table.insert(keys[i], i) != NULL);
106 else
107 foundCounter++;
108 }
109
110 cout << foundCounter << " duplicated numbers" << endl;
111
112 assert( table.size() + foundCounter == n/2);
113 printPars(table);
114
115 for (i = n/2; i < n; i++)
116 {
117 keys[i] = rand();
118 testResize(table);
119 table[keys[i]] = i;
120 table[keys[i]] = table[keys[i]];
121 }
122
123 printPars(table);
124
125 unsigned * ptr;
126 foundCounter = 0;
127 for (i = 0; i < n; i++)
128 {
129 ptr = table.search(keys[i]);
130 if (ptr != NULL)
131 table.remove(ptr);
132 else
133 foundCounter++;
134 }
135}
Dynamic hash table mapping keys to records with separate chaining.
Record * search(const Key &key)
Search for a key in the table.
void remove(Record *record)
Remove an entry from the table.
Record * insert(const Key &key, const Record &record)
Insert a key-record pair into the table.
const size_t & get_num_busy_slots() const noexcept
Returns the number of occupied entries in the array.
Definition tpl_lhash.H:531
const size_t & size() const noexcept
Returns the number of elements contained in the table.
Definition tpl_lhash.H:527
const size_t & capacity() const noexcept
Returns the table capacity.
Definition tpl_lhash.H:524
size_t resize(const size_t new_size)
Resizes the hash table to new_size and re-locates keys.
Definition tpl_lhash.H:466
and
Check uniqueness with explicit hash + equality functors.
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 check_primes_database()
Verify the integrity of the prime database.
Definition primes.C:411
STL namespace.
int keys[]
DynLhashTable< unsigned, unsigned > HTable
Definition testDynHash.C:40
#define NumItems
Definition testDynHash.C:35
static size_t hashFct(const unsigned &key)
Definition testDynHash.C:42
static void testResize(HTable &table)
Definition testDynHash.C:47
static void printPars(const HTable &table)
Definition testDynHash.C:58
Lazy and scalable dynamic array implementation.
Dynamic hash table mapping keys to records with separate chaining.