-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.cpp
More file actions
132 lines (120 loc) · 2.71 KB
/
hashtable.cpp
File metadata and controls
132 lines (120 loc) · 2.71 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
#include "Hashtable.h"
bool Hashtable::Build(std::string filename)
{
std::fstream data(filename, std::ios::in);
if (!data)
return false;
// Count the number of elements
std::string number = "";
while (std::getline(data, number))
elements++;
capacity = 2 * elements + 100;
delete[] table; // delete for safety
table = new cell[capacity];
// Go to the start of the file
data.clear();
data.seekg(0, std::ios_base::beg);
while (std::getline(data, number))
{
int value = stoi(number);
Insert(value, table);
}
return true;
}
bool Hashtable::Insert(int number)
{
double a = (elements + 1) / capacity;
if (a > 0.5)
ResizeTable();
if (Insert(number, table))
{
elements++;
return true;
}
return false;
}
bool Hashtable::Insert(int value, cell* t)
{
int index = HashFunction(value, capacity);
if (t[index].right == nullptr)
t[index].right = new cell{ value, nullptr };
else
{
cell* currentCell = t[index].right;
while (currentCell != nullptr)
{
if (currentCell->value == value)
return false;
currentCell = currentCell->right;
}
currentCell = new cell{ value, nullptr };
}
return true;
}
bool Hashtable::Delete(int number)
{
// Going down the chain of numbers at a particular
// index, we check if there is the number we are
// looking for. If so, we remove it from the chain.
int index = HashFunction(number, capacity);
cell* currentCell = &table[index];
cell* nextCell = currentCell->right;
while (nextCell != nullptr)
{
if (nextCell->value == number)
{
elements--;
currentCell->right = nextCell->right;
delete nextCell;
return true;
}
currentCell = nextCell->right;
nextCell = currentCell->right;
}
return false;
}
bool Hashtable::Search(int number) const
{
// Going down the chain of numbers at a particular
// index, we check if there is the number we are
// looking for.
int index = HashFunction(number, capacity);
cell* currentCell = table[index].right;
while (currentCell != nullptr)
{
if (currentCell->value == number)
return true;
currentCell = currentCell->right;
}
return false;
}
int Hashtable::GetSize() const
{
return elements;
}
int Hashtable::HashFunction(int x, int cap) const
{
int i = ((17 * x + 55) % bucket) % cap;
return i;
}
void Hashtable::ResizeTable()
{
// For every element in the original table we insert
// it in a new table using the new capacity and hence
// the new result of the hash function
int oldCapacity = capacity;
capacity *= 2;
cell *temp = new cell[capacity];
for (int i = 0; i < oldCapacity; i++)
{
cell* currentCell = table[i].right;
while (currentCell != nullptr)
{
int value = currentCell->value;
Insert(value, temp);
currentCell = currentCell->right;
}
}
delete[] table;
table = temp;
}