-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyHashTable.cpp
More file actions
114 lines (95 loc) · 1.76 KB
/
myHashTable.cpp
File metadata and controls
114 lines (95 loc) · 1.76 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
#pragma once
#include "myHashTable.h"
template<class V>
myHashTable<V>::myHashTable()
{
entries = new V*[MAXSIZE];
for (int i = 0; i < MAXSIZE; i++)
{
entries[i] = NULL;
}
size = 0;
b = MAXSIZE + 1;
}
template<class V>
int myHashTable<V>::getSize()
{
return size;
}
template<class V>
int myHashTable<V>::getCapacity()
{
return b;
}
template<class V>
int myHashTable<V>::getSpace()
{
double space = 1.0 * b / size;
//rounding is negligible
return static_cast<int>(100 * space);
}
template<class V>
bool myHashTable<V>::add(V* entry)
{
int i = 0; //colisions
int hash;
while (true)
{
hash = Hash::hash(entry, i, b, R);
if (entries[hash] == NULL) //open space
{
size++;
entries[hash] = entry;
return true;
}
if (entries[hash] != NULL) {
cout << "COLLISION!!!!" << endl;
}
i++; //collision found, find next space
}
}
template<class V>
bool myHashTable<V>::contains(V * entry)
{
int i = 0; //collisions
int hash;
while (true)
{
hash = Hash::hash(entry, i, b, R);
if (entries[hash] == NULL) //open space
{
return false; //no match found
}
if (*entry == *entries[hash])
{
entry = entries[hash];
return true;
}
i++; //collision found, continue search
}
}
//THIS ONE MAKES NO SENSE
//*********
//*********
template<class V>
V * myHashTable<V>::getEntry(int id)
{
int i = 0;
int hash;
hash = Hash::hash(id, i, b, R);
if (entries[hash] == NULL) //open slot
{
return NULL; //not found
}
return entries[hash];
}
template<class V>
myHashTable<V>::~myHashTable()
{
//destroy all entries in hash table
for (int i = 0; i < MAXSIZE; i++)
{
entries[i] = NULL;
}
delete[] entries; //free memory
}