-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashtable.cpp
More file actions
224 lines (218 loc) · 5.8 KB
/
Copy pathHashtable.cpp
File metadata and controls
224 lines (218 loc) · 5.8 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
#include "Hashtable.h"
#include <math.h>
int Hashtable::hash(int v) const{
return v % capacity();
}
//Hashtable() \Constructs and empty hash table with a default capacity of 17 and a default load factor threshold of .65
Hashtable::Hashtable()
{
htableCapacity = 17;
loadFactor = 0.65;
htable = new int[htableCapacity]();
count = 0;
}
//Hashtable(int size)\Constructs and empty hash table with the given capacity and a default load factor threshold of .65
Hashtable::Hashtable(int capacity)
{
htableCapacity = capacity;
loadFactor = 0.65;
htable = new int[htableCapacity]();
count = 0;
}
//Hashtable(int, double)\Constructs and empty hash table with the given capacity and load factor threshold values
Hashtable::Hashtable(int capacity, double threshold)
{
htableCapacity = capacity;
loadFactor = threshold;
htable = new int[capacity]();
count = 0;
}
//Hashtable(const Hashtable& other)\creates a deep-copy of the given hashtable
Hashtable::Hashtable(const Hashtable& other)
{
if (!empty()) {
delete[] htable;
count = 0;
}
htableCapacity = other.capacity();
loadFactor = other.loadFactor;
htable = new int[other.capacity()]();
for (int i = 0; i < htableCapacity; i++) {
if (other.htable[i] != 0) {
htable[i] = other.htable[i];
count++;
}
}
}
//Hashtable& operator=(const Hashtable& other)\replaces the current hashtable with a deep-copy of the given hash table
Hashtable& Hashtable::operator=(const Hashtable& other)
{
if (!empty()) {
delete[] htable;
}
count = 0;
htableCapacity = other.capacity();
loadFactor = other.loadFactor;
htable = new int[other.capacity()]();
for (int i = 0; i < htableCapacity; i++) {
if (other.htable[i] != 0) {
htable[i] = other.htable[i];
count++;
}
}
return *this;
}
//~Hashtable()\cleans up all allocated memory of the hashtable
Hashtable::~Hashtable()
{
delete[] htable;
}
//int size() const\retunrs the number of items currently in the hashtable
int Hashtable::size() const
{
return count;
}
// double the size of the array if too small. ensure hashing is updated
void Hashtable::resize() {
int* currTable = htable;
int currCapacity = capacity();
// the tests expect this growth rate
htableCapacity = nextPrime(currCapacity*2);
htable = new int[htableCapacity]();
count = 0; // debugging this line not being here took me all day to solve :)
for (int i = 0; i < currCapacity; i++) {
if (currTable[i] != 0) {
insert(currTable[i]);
}
}
delete[] currTable;
}
// int capacity() const\\returns the capacity of the hashtable (i.e. the size of the array)
int Hashtable::capacity() const
{
return htableCapacity;
}
//double getLoadFactorThreshold() const\\returns the load factor threshold used to determine when to resize the hashtable.
double Hashtable::getLoadFactorThreshold() const
{
return loadFactor;
}
//bool empty() const\\returns true if the table is empty or false otherwise
bool Hashtable::empty() const
{
return count == 0;
}
//void insert(const int)\\Inserts the given value into the hashtable. Automatically resizes the table as necessary.
void Hashtable::insert(int value)
{
//check load factor with the new item
if (size()+1 > getLoadFactorThreshold()*capacity()) {
resize();
}
int H = hash(value);
int N = capacity();
for (int i = 0; i < N; i++) {
//H + 0*i + 1*i^2
int index = (H + i*i) % N;
if (htable[index] <= 0) {
htable[index] = value;
count++;
return;
}
}
}
//Removes the given value from the hashtable. If the value is not present it takes no action and throws no errors.
void Hashtable::remove(int value)
{
if (empty()) {
return;
}
int H = hash(value);
int N = capacity();
for (int i = 0; i < N; i++) {
//H + 0*i + 1*i^2
int index = (H + i*i) % N;
if (htable[index] != 0) {
if (htable[index] == value){
htable[index] = -1;
count--;
return;
}
}
else {
return;
}
}
}
//Returns true if the given value is contained in the hashtable or false if the value is not in the hashtable.
bool Hashtable::contains(int value) const{
int H = hash(value);
int N = capacity();
for (int i = 0; i < N; i++) {
//H + 0*i + 1*i^2
int index = (H + i*i) % N;
if (htable[index] == value) {
return true;
}
if (htable[index] == 0) {
return false;
}
}
return false;
}
//Returns the index of the given value. If the value is not in the hashtable, returns -1.
int Hashtable::indexOf(int value) const {
int H = hash(value);
int N = capacity();
int index = H;
for (int i = 0; i < N; i++) {
//H + 0*i + 1*i^2
int index = (H + i*i) % N;
if (htable[index] == 0) {
return -1;
}
if (htable[index] == value){
return index;
}
}
return -1;
}
//Removes all values from the hashtable, resetting it to an empty state
void Hashtable::clear()
{
for (int i = 0; i < capacity(); i++) {
htable[i] = 0;
}
count = 0;
}
//Returns true if the value is a prime number
bool Hashtable::isPrime(int n) {
//Returns true if the value is a prime number. One way to implement this method is to check if the number is divisible by 2-sqrt(n).
//If it is then it is not prime. If it is not then it is prime. 2 and 3 must be tested separately.
if (n <= 1) {
return false;
}
if (n == 2 || n == 3) {
return true;
}
if (n % 2 == 0) {
return false;
}
// check remaining ods
for (int i = 3; i <= sqrt(n); i+=2) {
if (n % i == 0) {
return false;
}
}
return true;
}
//Returns the next prime number greater than or equal to n.
int Hashtable::nextPrime(int n){
n++;
//Returns the next prime number greater than or equal to n.
//One way to implement this method is to test if n is prime and then repeatedly increment n and test again if it is prime.
while (!isPrime(n)) {
n++;
}
return n;
}