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 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 <tpl_dynLhash.H>
34# include <tpl_dynArray.H>
35# include <iostream>
36# include <string>
37# include <ctime>
38# include <cstdlib>
39
40# define NumItems 10000
41
42# include <cassert>
43using namespace std;
44
46
47static size_t hashFct(const unsigned & key)
48{
49 return key;
50}
51
52static void testResize(HTable& table)
53{
54 if (table.get_num_busy_slots() > (99*table.capacity())/100
55 and table.size()/table.capacity() > 3)
56 {
57 unsigned long currSize = table.capacity();
58 cout << "Resizing hash table from " << currSize << " ... ";
59 cout << table.resize(1.5 * table.capacity()) << endl;
60 }
61}
62
63static void printPars(const HTable& table)
64{
65 cout << "Table length = " << table.capacity() << endl
66 << "Busy slots = " << table.get_num_busy_slots() << endl
67 << "Num items = " << table.size() << endl;
68}
69
70int main(int argc, char *argv[])
71{
73
74 int n = NumItems;
75 unsigned int t = std::time(NULL);
76
77 try
78 {
79 if (argc > 1)
80 n = std::stoi(argv[1]);
81
82 if (argc > 2)
83 t = std::stoi(argv[2]);
84 }
85 catch (...)
86 {
87 // ignore
88 }
89
90 if (n <= 0)
91 {
92 cout << "n must be positive" << endl;
93 return 1;
94 }
95
96 srand(t);
97
98 cout << argv[0] << " " << n << " " << t << endl;
99
100 HTable table(1.15*n, hashFct);
102 unsigned int i;
103 unsigned int foundCounter = 0;
104
105 for (i = 0; i < n/2; i++)
106 {
107 keys[i] = rand();
108 testResize(table);
109 if (table.search(keys(i)) == NULL)
110 assert(table.insert(keys[i], i) != NULL);
111 else
112 foundCounter++;
113 }
114
115 cout << foundCounter << " duplicated numbers" << endl;
116
117 assert( table.size() + foundCounter == n/2);
118 printPars(table);
119
120 for (i = n/2; i < n; i++)
121 {
122 keys[i] = rand();
123 testResize(table);
124 table[keys[i]] = i;
125 table[keys[i]] = table[keys[i]];
126 }
127
128 printPars(table);
129
130 unsigned * ptr;
131 foundCounter = 0;
132 for (i = 0; i < n; i++)
133 {
134 ptr = table.search(keys[i]);
135 if (ptr != NULL)
136 table.remove(ptr);
137 else
138 foundCounter++;
139 }
140}
int main()
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
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
and
Check uniqueness with explicit hash + equality functors.
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:45
#define NumItems
Definition testDynHash.C:40
static size_t hashFct(const unsigned &key)
Definition testDynHash.C:47
static void testResize(HTable &table)
Definition testDynHash.C:52
static void printPars(const HTable &table)
Definition testDynHash.C:63
Lazy and scalable dynamic array implementation.
Dynamic hash table mapping keys to records with separate chaining.