-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashTableCuckoo.cpp
More file actions
157 lines (136 loc) · 2.6 KB
/
hashTableCuckoo.cpp
File metadata and controls
157 lines (136 loc) · 2.6 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
#include "hashTableCuckoo.h"
HashTableCuckoo::HashTableCuckoo(int capacity)
{
this->capacity = capacity;
this->size = 0;
this->table1 = new int[capacity];
this->table2 = new int[capacity];
for (int i = 0; i < capacity; i++)
{
table1[i] = -1;
table2[i] = -1;
}
}
HashTableCuckoo::~HashTableCuckoo()
{
delete[] table1;
delete[] table2;
capacity = 0;
size = 0;
}
int HashTableCuckoo::hash1(int key)
{
return key % capacity;
}
int HashTableCuckoo::hash2(int key)
{
return 1 + (key % (capacity - 1));
}
void HashTableCuckoo::rehash(int newCapacity)
{
int* oldTable1 = table1;
int* oldTable2 = table2;
int oldCapacity = capacity;
size = 0;
capacity = newCapacity;
table1 = new int[capacity];
table2 = new int[capacity];
for (int i = 0; i < capacity; i++)
{
table1[i] = -1;
table2[i] = -1;
}
for (int i = 0; i < oldCapacity; i++)
{
if (oldTable1[i] != -1)
{
insert(i, oldTable1[i]);
}
if (oldTable2[i] != -1)
{
insert(i, oldTable2[i]);
}
}
delete[] oldTable1;
delete[] oldTable2;
}
void HashTableCuckoo::insert(int key, int value) {
int index = hash1(key);
int* currentTable = table1;
for (int i = 0; i < 10; i++) {
if (currentTable[index] == -1) {
currentTable[index] = value;
size++;
return;
}
std::swap(currentTable[index], value);
if (currentTable == table1) {
currentTable = table2;
index = (index + hash2(key)) % capacity;
}
else {
currentTable = table1;
index = hash1(key);
}
}
rehash(capacity * 2);
insert(key, value);
}
void HashTableCuckoo::remove(int key) {
int index = hash1(key);
int* currentTable = table1;
for(int i = 0; i < 10; i++) {
if (currentTable[index] != -1) {
currentTable[index] = -1;
size--;
return;
}
if (currentTable == table1) {
currentTable = table2;
index = (index + hash2(key)) % capacity;
}
else {
currentTable = table1;
index = (index + hash1(key)) % capacity;
}
}
if (size <= capacity * 0.3) {
rehash(capacity / 2);
remove(key);
}
else {
cout << "Key not found" << endl;
}
}
bool HashTableCuckoo::search(int key) {
int index = hash1(key);
int* currentTable = table1;
for(int i = 0; i < 10; i++) {
if (currentTable[index] != -1) {
return true;
}
if (currentTable == table1) {
currentTable = table2;
index = hash2(key);
}
else {
currentTable = table1;
index = hash1(key);
}
}
return false;
}
void HashTableCuckoo::display() {
cout << "Table 1: ";
for (int i = 0; i < capacity; i++)
{
cout << table1[i] << " ";
}
cout << endl;
cout << "Table 2: ";
for (int i = 0; i < capacity; i++)
{
cout << table2[i] << " ";
}
cout << endl;
}