-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCuckooHashTable.cpp
More file actions
194 lines (166 loc) · 4.3 KB
/
CuckooHashTable.cpp
File metadata and controls
194 lines (166 loc) · 4.3 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
#include <iostream>
#include "CuckooHashTable.h"
using namespace std;
// Konstruktor klasy CuckooHashTable
CuckooHashTable::CuckooHashTable() : capacity(1), size(0), rehash_count(0)
{
table1 = new Nod[capacity];
table2 = new Nod[capacity];
}
// Destruktor klasy CuckooHashTable
CuckooHashTable::~CuckooHashTable()
{
delete[] table1;
delete[] table2;
}
// Funkcja haszująca 1
int CuckooHashTable::hash1(int key) const
{
return key % capacity;
}
// Funkcja haszująca 2
int CuckooHashTable::hash2(int key) const
{
return (key / capacity) % capacity;
}
// Funkcja rehashująca
void CuckooHashTable::rehash()
{
Nod* old_table1 = table1;
Nod* old_table2 = table2;
int old_capacity = capacity;
capacity *= 2; // Zwiększenie pojemności
table1 = new Nod[capacity];
table2 = new Nod[capacity];
size = 0;
for (int i = 0; i < old_capacity; ++i)
{
if (!old_table1[i].is_empty)
{
insert(old_table1[i].value, old_table1[i].key);
}
if (!old_table2[i].is_empty)
{
insert(old_table2[i].value, old_table2[i].key);
}
}
delete[] old_table1;
delete[] old_table2;
}
// Funkcja zamieniająca elementy
void CuckooHashTable::swap(Nod& a, Nod& b)
{
Nod temp = a;
a = b;
b = temp;
}
// Funkcja wstawiająca element do tablicy
void CuckooHashTable::insert(int v, int k)
{
//Sprawdzanie czy klucz juz istnieje
int pos1 = hash1(k);
if(!table1[pos1].is_empty && table1[pos1].key == k)
{
cout << "Klucz " << k << " juz istnieje w tablicy . Zakanczamy dzialanie programu" << endl;
return;
}
//Sprawdzanie czy klucz juz istnieje
int pos2 = hash2(k);
if(!table2[pos2].is_empty && table2[pos2].key == k)
{
cout << "Klucz " << k << " juz istnieje w tablicy. Zakanczamy dzialanie programu" << endl;
return;
}
if (size >= capacity)
{
rehash();
}
Nod newNode(v, k);
for (int count = 0; count < 2 * capacity; ++count)
{
int pos1 = hash1(k);
if (table1[pos1].is_empty)
{
table1[pos1] = newNode;
size++;
return;
}
swap(newNode, table1[pos1]);
int pos2 = hash2(newNode.key);
if (table2[pos2].is_empty)
{
table2[pos2] = newNode;
size++;
return;
}
swap(newNode, table2[pos2]);
}
rehash();
insert(newNode.value, newNode.key);
}
// Funkcja usuwająca element z tablicy
void CuckooHashTable::remove(int k)
{
int pos1 = hash1(k);
if (!table1[pos1].is_empty && table1[pos1].key == k)
{
table1[pos1] = Nod();
size--;
cout << "Usuniety element znajdowal sie pod indeksem " << pos1 << endl;
return;
}
int pos2 = hash2(k);
if (!table2[pos2].is_empty && table2[pos2].key == k)
{
table2[pos2] = Nod();
size--;
cout << "Usuniety element znajdowal sie pod indeksem " << pos2 << endl;
return;
}
cout << "Podany klucz nie istnieje w tablicy." << endl;
}
// Funkcja wyświetlająca zawartość tablic
void CuckooHashTable::show() const
{
if (size == 0)
{
cout << "Tablica jest pusta." << endl;
return;
}
cout << "Tabela 1:" << endl;
for (int i = 0; i < capacity; ++i)
{
if (!table1[i].is_empty)
{
cout << "[" << i + 1 << "] Klucz: " << table1[i].key << ", Wartosc: " << table1[i].value << endl;
}
}
cout << "Tabela 2:" << endl;
for (int i = 0; i < capacity; ++i)
{
if (!table2[i].is_empty)
{
cout << "[" << i + 1<< "] Klucz: " << table2[i].key << ", Wartosc: " << table2[i].value << endl;
}
}
}
// Funkcja zwracająca aktualną pojemność tablicy
int CuckooHashTable::capacity_() const
{
return capacity;
}
// Funkcja zwracająca aktualną liczbę elementów w tablicy
int CuckooHashTable::size_() const
{
return size;
}
// Funkcja usuwająca wszystkie elementy z tablicy i resetująca jej stan do początkowego
void CuckooHashTable::clear()
{
delete[] table1;
delete[] table2;
capacity = 1; // Resetowanie pojemności do początkowej wartości
table1 = new Nod[capacity];
table2 = new Nod[capacity];
size = 0;
}