-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.cpp
148 lines (118 loc) · 4.42 KB
/
test2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// File: test2.cpp
//
// Insertions trigger rehashing.
//
// Clusters are moved until old hash table is empty
// and we revert to having only 1 hash table.
//
#include <iostream>
#include <cstdlib>
using namespace std ;
#include "HashTable.h"
int main() {
HashTable T(107) ;
T.insert("undulation") ; // 9
T.insert("impertinent") ; // 10
T.insert("maladies") ; // 10 -> 11
T.insert("dominions") ; // 12
T.insert("waspish") ; // 52
T.insert("wildebeest") ; // 52 -> 53
T.insert("reaction") ; // 52 -> 54
T.insert("pawns") ; // 43
T.insert("vacuously") ; // 43 -> 44
T.insert("firth") ; // 43 -> 45
T.insert("manipulator") ; // 43 -> 46
T.insert("dreariness") ; // 43 -> 47
T.insert("insanity") ; // 105
T.insert("enthronement") ; // 105 -> 106
T.insert("falsifiers") ; // 105 -> 0
T.insert("ignominious") ; // 105 -> 1
T.insert("mummified") ; // 105 -> 2
T.insert("tributes") ; // 21
T.insert("skulduggery") ; // 22
T.insert("convulse") ; // 23
T.insert("frothed") ; // 24
T.insert("horrify") ; // 25
T.insert("blackmailers") ; // 26
T.insert("defenestrated") ; // 27
T.insert("garrison") ; // 23 -> 28
T.insert("lidless") ; // 22 -> 29
cout << "----------------------------------------------------\n" ;
cout << "Original hash table\n" ;
cout << "----------------------------------------------------\n" ;
T.dump() ;
cout << "----------------------------------------------------\n" ;
cout << "\n\nInserting \"eye\" should trigger rehash\n\n" ;
T.insert("eye") ; // 21 -> 30, should trigger inc. rehash
cout << "----------------------------------------------------\n" ;
cout << "Hash table after rehash triggered\n" ;
cout << "----------------------------------------------------\n" ;
T.dump() ;
cout << "----------------------------------------------------\n" ;
cout << "\n\nSearch for \"manipulator\" should move cluster in slots 43-47.\n\n" ;
T.find("manipulator") ; // 21 -> 30, should trigger inc. rehash
cout << "----------------------------------------------------\n" ;
cout << "Hash table after cluster 43-47 moved.\n" ;
cout << "----------------------------------------------------\n" ;
T.dump() ;
cout << "----------------------------------------------------\n" ;
cout << "\n\nDo some finds, inserts and removes\n\n" ;
const char *str ;
char *ptr ;
if( T.find(str="zip") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
if( T.find(str="spaceflight") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
/*
ptr = T.remove(str="maladies") ;
if (ptr == NULL) {
cout << "String " << str << " not found, not removed\n" ;
} else {
cout << "Removed string = " << ptr << endl ;
free(ptr) ;
}
ptr = T.remove(str="wildebeest") ;
if (ptr == NULL) {
cout << "String " << str << " not found, not removed\n" ;
} else {
cout << "Removed string = " << ptr << endl ;
free(ptr) ;
}
*/
if( T.find(str="frothed") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
T.insert("wildcat") ; // 18 (new table)
T.insert("weightlessness") ; // 69 (new table)
T.insert("sorceress") ; // 33 (new table)
T.insert("enchantress") ; // 33 (new table) really.
cout << "----------------------------------------------------\n" ;
cout << "Hash table after more insertions.\n" ;
cout << "----------------------------------------------------\n" ;
T.dump() ;
cout << "----------------------------------------------------\n" ;
cout << "\n\nA find on \"ignominious\" + 1 more operation should cause the tables to consolidate down to one table. \n\n" ;
if( T.find(str="ignominious") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
if( T.find(str="reaction") ) {
cout << "Found " << str << endl ;
} else {
cout << "Did not find " << str << endl ;
}
cout << "----------------------------------------------------\n" ;
cout << "Hash table after wrap up.\n" ;
cout << "----------------------------------------------------\n" ;
T.dump() ;
cout << "----------------------------------------------------\n" ;
}