forked from ATinyBanana233/HashTable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.cpp
More file actions
executable file
·248 lines (217 loc) · 5.4 KB
/
hashtable.cpp
File metadata and controls
executable file
·248 lines (217 loc) · 5.4 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//CMPT 225 assignment 5
//Author: Bei Bei Li
//implementation for hashtable.h
#pragma once
#include <iostream>
#include <cstdlib>
#include <math.h> // needed for Hash function
#include <string>
#include <vector>
#include "linkedlist.h"
#include "hashtable.h"
using namespace std;
// hash function, uses Horner's method
// Assume input string consists only of lower-case a to z
int HashTable::Hash(string input) const //hash correct
{
int h = 0;
int i, val;
int len = input.length();
//cout << "len is " << len << endl;
for (i = 0; i < len; i++) {
h = (32 * h + (input[i] - 'a' + 1 )) % maxsize; //set a-z to 1-26
}
//cout << "hash is " << h << endl;
return h;
}
// helper function to find smallest prime number greater than supplied parameter
int HashTable::SmallestPrime(int n) const
{
int i;
i = n + 1;
while (IsPrime(i) != true) {
i++;
}
return i;
}
// helper function to determine whether a number is prime
bool HashTable::IsPrime(int n) const
{
if (n < 2) {
return false;
}
int i;
for (i = 2; i < n; i++) { //prime is divisible by 1 and itself only
if (n % i == 0) {
return false;
}
}
return true;
}
//LinkedList* table; // array of separately chained binary search trees
//unsigned int size; // number of items stored
//unsigned int maxsize; // size of underlying array
// default constructor
// creates an array of size 101
HashTable::HashTable()
{
maxsize = 101;
table = new LinkedList[maxsize];
//for (int i = 0; i<maxsize; i++)
//{
// table[i] = NULL;
//}
size = 0;
}
// parameterized constructor
// creates an array of size = smallest prime number > 2n
HashTable::HashTable(int n) //might be wrong
{
maxsize = SmallestPrime(2 * n); //smallest prime incorrect???
table = new LinkedList[maxsize];
//for (int i = 0; i<maxsize; i++)
//{
// table[i] = NULL;
//}
size = 0;
}
// copy constructor
// Creates deep copy of sourceht
HashTable::HashTable(const HashTable& sourceht)
{
if (maxsize) { //clear
delete[] table;
}
table = NULL;
if (sourceht.maxsize && sourceht.table) {
table = new LinkedList[sourceht.maxsize];
//for (int i = 0; i<maxsize; i++)
//{
// table[i] = NULL;
//}
int i;
for (i = 0; i < sourceht.maxsize; i++) {
table[i] = sourceht.table[i]; //copying using operator=
}
}
maxsize = sourceht.maxsize;
size = sourceht.size;
}
// destructor
HashTable::~HashTable()
{
if (table != NULL) {
delete[] table;
table = NULL;
}
maxsize = 0;
size = 0;
}
// overloaded assignment operator
HashTable& HashTable::operator=(const HashTable& sourceht) ///incorrect
{
if (this != &sourceht) { // if not equal then change
if (table != NULL) {
delete[] table;
table = NULL;
}
maxsize = 0;
size = 0;
if (sourceht.maxsize && sourceht.table) { //code from copy constructor
table = new LinkedList[sourceht.maxsize]; //cannot call HashTable(sourceht) directly....does not work
int i;
for (i = 0; i < sourceht.maxsize; i++) {
table[i] = sourceht.table[i];
}
}
maxsize = sourceht.maxsize;
size = sourceht.size;
}
return *this;
}
// Insertion
// If item does not already exist, inserts and returns true
// otherwise returns false
bool HashTable::Insert(string value) //problem here....
{
if (Search(value) == true) {
return false;
}
size++;
double ld = LoadFactor();
double compare = (float)2/3;
while (ld >= compare){ //resize if efficiency lowered
Resize(maxsize);
}
int key = Hash(value);
//cout << "key is " << key << endl;
table[key].Insert(value);
return true;
}
// Removal
// If item exists, removes and returns true
// otherwise returns false
bool HashTable::Remove(string value)
{
if (Search(value) == false) {
return false;
}
int key = Hash(value);
table[key].Remove(value);
size--;
return true;
}
// Search
// Returns true if item exists, false otherwise
bool HashTable::Search(string value) const
{
int key = Hash(value);
return table[key].Contains(value);
}
// Returns the number of items stored in the hash table
int HashTable::Size() const
{
return size;
}
// Returns the size of the underlying array
int HashTable::MaxSize() const
{
return maxsize;
}
// Returns the load factor as size / maxsize.
// Note that due to separate chaining, load factor can be > 1.
double HashTable::LoadFactor() const
{
double result = size / maxsize;
return result;
//for insertion
}
// Resizes the hashtable into a larger array.
// Return false if n is smaller than current array size.
// Else, set array size to the smallest prime number larger than n
// and re-hash all contents into the new array, delete the old array and return true.
bool HashTable::Resize(unsigned int n)
{
if (n < maxsize) {
return false;
}
else {
int max = maxsize;
maxsize = SmallestPrime(n);
LinkedList* oldtable = table;
table = new LinkedList[maxsize];
int i, j;
size = 0;
for (i = 0; i < max; i++){ //every linked list
vector<string> oldvalue = oldtable[i].Dump(); //using vector to rehash
for (j = 0; j < oldvalue.size(); j++){ //linked list contents rehash
Insert(oldvalue[j]);
}
}
//for (i = 0; i < max; i++) {
// table[i] = oldtable[i];
//}
delete[] oldtable;
return true;
}
}