Repository navigation
Expand file tree
/
Copy pathHashTable.cpp
More file actions
205 lines (186 loc) · 3.9 KB
/
Copy pathHashTable.cpp
File metadata and controls
205 lines (186 loc) · 3.9 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
#include <iostream>
#include <iomanip>
#include "HashTable.hpp"
#include "HashFunc.hpp"
HashN::HashN() : key(-1), val(-1), del(false) {}
HashN::HashN(int k, int v) : key(k), val(v), del(false) {}
HashTable::HashTable(int tableSize, HashF func) : size(tableSize), count(0), currHF(func)
{
table.resize(size);
}
HashTable::~HashTable() {}
int HashTable::hash(int key)
{
switch (currHF)
{
case DIVISION:
return hashFunctions.divisionMethod(key, size);
case MID_SQUARE:
return hashFunctions.midSquareMethod(key, size);
case MULTIPLICATION:
return hashFunctions.multiplicationMethod(key, size);
default:
return hashFunctions.divisionMethod(key, size);
}
}
int HashTable::findSlot(int key, bool forInsertion)
{
int index = hash(key);
int orgIndex = index;
do {
if (forInsertion)
{
if (table[index].key == -1 || table[index].del)
{
return index;
}
if (table[index].key == key && !table[index].del)
{
return index;
}
}
else
{
if (table[index].key == -1)
{
return -1;
}
if (table[index].key == key && !table[index].del)
{
return index;
}
}
index = (index + 1) % size;
} while (index != orgIndex);
return -1;
}
pair<bool, int> HashTable::insert(int key, int val)
{
if(count >= size * 0.75)
{
return { false, 0 };
}
int steps = 0;
int index = hash(key);
int orgIndex = index;
do {
steps++;
if (table[index].key == -1 || table[index].del)
{
if (table[index].del)
{
table[index].del = false;
}
else
{
count++;
}
table[index].key = key;
table[index].val = val;
return { true, steps };
}
if (table[index].key == key && !table[index].del)
{
table[index].val = val;
return { true, steps };
}
index = (index + 1) % size;
} while (index != orgIndex);
return { false, steps };
}
pair<bool, int> HashTable::remove(int key)
{
int steps = 0;
int index = hash(key);
int orgIndex = index;
do {
steps++;
if (table[index].key == -1)
{
return { false, steps };
}
if (table[index].key == key && !table[index].del)
{
table[index].del = true;
return { true, steps };
}
index = (index + 1) % size;
} while (index != orgIndex);
return { false, steps };
}
pair<bool, int> HashTable::search(int key)
{
int steps = 0;
int index = hash(key);
int orgIndex = index;
do {
steps++;
if (table[index].key == -1)
{
return{ false, steps };
}
if (table[index].key == key && !table[index].del)
{
return { true, steps };
}
index = (index + 1) % size;
} while (index != orgIndex);
return { false, steps };
}
void HashTable::clear() {
table.clear();
table.resize(size);
count = 0;
}
void HashTable::printTable() {
std::cout << "\n=== ZAWARTOŚĆ TABLICY ===" << std::endl;
for (int i = 0; i < size; i++) {
std::cout << "[" << std::setw(3) << i << "] ";
if (table[i].key == -1) {
std::cout << "PUSTE";
}
else if (table[i].del) {
std::cout << "USUNIĘTE (key: " << table[i].key << ")";
}
else {
std::cout << "key: " << table[i].key << ", value: " << table[i].val;
}
std::cout << std::endl;
}
}
void HashTable::printStats() {
int active = 0, deleted = 0, empty = 0;
for (const auto& node : table) {
if (node.key == -1) {
empty++;
}
else if (node.del) {
deleted++;
}
else {
active++;
}
}
std::cout << "\n=== STATYSTYKI TABLICY ===" << std::endl;
std::cout << "Rozmiar tablicy: " << size << std::endl;
std::cout << "Aktywne elementy: " << active << std::endl;
std::cout << "Usunięte elementy: " << deleted << std::endl;
std::cout << "Puste miejsca: " << empty << std::endl;
std::cout << "Load factor: " << std::fixed << std::setprecision(3)
<< (double)active / size << std::endl;
}
double HashTable::getLoadFactor() {
return (double)count / size;
}
int HashTable::getSize() {
return size;
}
int HashTable::getCount() {
return count;
}
void HashTable::setHF(HashF func) {
currHF = func;
}
HashTable::HashF HashTable::getCurrHF() {
return currHF;
}