-
Notifications
You must be signed in to change notification settings - Fork 1
/
HashTable.cpp
53 lines (41 loc) · 1.33 KB
/
HashTable.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
// -----------------------------------------------------------------
// Basic hash table as a chained list of nodes
#include "HashTable.hh"
#include <vector>
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// Constructors and destructor
HashTable::HashTable(unsigned int numberOfTables) {
size = 0;
tableNumber = numberOfTables;
std::vector<node*> *temp = new std::vector<node*>(tableNumber, NULL);
table = *temp;
mod = tableNumber - 1;
}
HashTable::HashTable() {
HashTable(4093);
}
HashTable::~HashTable() {}
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// Member functions: Insertion and searching
void HashTable::insert(unsigned int site) {
unsigned int index = (17 * site - 97) & mod;
node *toAdd = new node;
toAdd->value = site;
toAdd->next = table[index];
table[index] = toAdd;
size++;
}
// Return whether or not site is in cluster
bool HashTable::find(unsigned int site) {
unsigned int index = (17 * site - 97) & mod;
node *temp = table[index];
while (temp != NULL) {
if (site == temp->value)
return true;
temp = temp->next;
}
return false;
}
// -----------------------------------------------------------------