-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubleHashImpl.cpp
203 lines (193 loc) · 7.98 KB
/
doubleHashImpl.cpp
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
#include <iostream>
#include <cmath>
#include "hashTable.hpp"
using namespace std;
doubleHash::~doubleHash(){
delete[] table;
table = NULL;
}
bool doubleHash::alrExist(int size, unsigned int key){
for(int i = 0; i<size; i++){
if(table[i].get_val() == key){
return true;
}
}
return false;
}
void doubleHash::insert(unsigned int key, int memory[], int tableSize, int memorySize, int pageSize, int& countInsert, int delArr[]){
int finLoc = ( hashFunctionOne(key) + countInsert * hashFunctionTwo(key) ) % tableSize;
int store[pageSize];
for(int i = 0; i<pageSize; i++){
store[i] = i * pageSize;
}
if(countInsert >= tableSize || searchIndex(key, tableSize) != -1){ //alrExist(tableSize, key) == true node does exist
cout<< "failure" <<endl; //searchIndex(key, tableSize) != -1
return;
}
else{
//Do double hashing here
if(table[hashFunctionOne(key)].get_val() == 0 && countInsert < tableSize){
table[hashFunctionOne(key)].set_val(key);
for(int i = 0; i<tableSize; i++){
if(delArr[i] != -1){
table[hashFunctionOne(key)].set_phys_addr(delArr[i]);
delArr[i] = -1;
break;
}
}
//table[hashFunctionOne(key)].set_phys_addr(countInsert * pageSize);
table[hashFunctionOne(key)].set_flag(1);
countInsert++;
cout<< "success" <<endl;
return;
}
/*
else if(table[( hashFunctionOne(key) + countInsert * hashFunctionTwo(key) ) % tableSize].get_val() == 0 && countInsert < tableSize){
table[( hashFunctionOne(key) + countInsert * hashFunctionTwo(key) ) % tableSize].set_val(key);
table[( hashFunctionOne(key) + countInsert * hashFunctionTwo(key) ) % tableSize].set_phys_addr(countInsert * pageSize);
table[( hashFunctionOne(key) + countInsert * hashFunctionTwo(key) ) % tableSize].set_flag(1);
countInsert++;
cout << "5" << endl;
cout<< "success" <<endl;
return;
}
*/
else{
while(countInsert > 0 && countInsert < tableSize){
if(table[( hashFunctionOne(key) + countInsert * hashFunctionTwo(key) ) % tableSize].get_val() == 0){
table[( hashFunctionOne(key) + countInsert * hashFunctionTwo(key) ) % tableSize].set_val(key);
for(int i = 0; i<tableSize; i++){
if(delArr[i] != -1){
table[( hashFunctionOne(key) + countInsert * hashFunctionTwo(key) ) % tableSize].set_phys_addr(delArr[i]);
delArr[i] = -1;
break;
}
}
//table[( hashFunctionOne(key) + countInsert * hashFunctionTwo(key) ) % tableSize].set_phys_addr(countInsert * pageSize);
table[( hashFunctionOne(key) + countInsert * hashFunctionTwo(key) ) % tableSize].set_flag(1);
countInsert++;
cout<< "success" <<endl;
return;
}
}
}
cout<< "failure" << endl;
return;
}
}
void doubleHash::search(unsigned int key, int tableSize){
/*
//Using linear search
for(int i = 0; i<tableSize; i++){
if(table[i].get_val() == key){
cout << "found " + to_string(key) + " in " + to_string(i) << endl;
return;
}
}
cout << "not found" << endl;
return;
*/
int count = 0;
if(table[hashFunctionOne(key)].get_val() == key){
cout << "found " + to_string(key) + " in " + to_string(hashFunctionOne(key)) << endl;
return;
}
else{
while(count < tableSize){
count++;
if(table[( hashFunctionOne(key) + count * hashFunctionTwo(key) ) % tableSize].get_flag() == -1){
count++;
}
else{
if(table[( hashFunctionOne(key) + count * hashFunctionTwo(key) ) % tableSize].get_val() == key
&& table[( hashFunctionOne(key) + count * hashFunctionTwo(key) ) % tableSize].get_flag() != -1){
cout << "found " + to_string(key) + " in " + to_string(( hashFunctionOne(key) + count * hashFunctionTwo(key) ) % tableSize) << endl;
return;
}
}
}
}
cout << "not found" << endl;
return;
/*
if(searchIndex(key, tableSize) == -1){
cout << "not found" << endl;
return;
}
else{
cout << "found " + to_string(key) + " in " + to_string(searchIndex(key, tableSize)) << endl;
return;
}
*/
}
int doubleHash::searchIndex(unsigned int key, int tableSize){
int count = 0;
if(table[hashFunctionOne(key)].get_val() == key){
//cout << "without collision: " + to_string(hashFunctionOne(key))<< endl;
return hashFunctionOne(key);
}
else{
while(count < tableSize){
count++;
if(table[( hashFunctionOne(key) + count * hashFunctionTwo(key) ) % tableSize].get_val() == key){
return ( hashFunctionOne(key) + count * hashFunctionTwo(key) ) % tableSize;
}
else if(table[( hashFunctionOne(key) + count * hashFunctionTwo(key) ) % tableSize].get_flag() != 1){
//return -1;
} else{
//cout << "Collision: " + to_string(( hashFunctionOne(key) + count * hashFunctionTwo(key) ) % tableSize)<< endl;
}
}
}
return -1;
}
void doubleHash::write(unsigned int key, int vir_addr, int x, int pageSize, int tableSize, int memory[], int memSize){
if(searchIndex(key, tableSize) != -1 && table[searchIndex(key, tableSize)].get_val() != 0 // alrExist(tableSize, key) == true node does exist
&& table[searchIndex(key, tableSize)].get_phys_addr() + vir_addr < memSize // searchIndex(key, tableSize) != -1
&& vir_addr < pageSize){
memory[ table[searchIndex(key, tableSize)].get_phys_addr() + vir_addr ] = x;
//cout << "x: " + to_string(x) << endl;
//cout << to_string(key)+ " memory written: " + to_string(memory[ table[searchIndex(key, tableSize)].get_phys_addr() + vir_addr ]) << endl;
cout << "success" << endl;
}
else{
cout << "failure" << endl;
}
return;
}
void doubleHash::read(unsigned int key, int tableSize, int vir_addr, int pageSize, int memory[], int memSize){
if(searchIndex(key, tableSize) == -1 || //alrExist(tableSize, key) == false key doesn't exist
table[searchIndex(key, tableSize)].get_val() <= 0 || //searchIndex(key, tableSize) == -1
table[searchIndex(key, tableSize)].get_phys_addr() + vir_addr >= memSize ||
vir_addr >= pageSize){
cout << "failure" << endl;
}
else{
int pos = table[searchIndex(key, tableSize)].get_phys_addr() + vir_addr;
//cout << to_string(key) + " " + to_string(pos) << endl;
cout << to_string(vir_addr) + " " + to_string(memory[pos]) << endl;
}
return;
}
void doubleHash::del(unsigned int key, int tableSize, int memory[], int pageSize, int& countInsert, int delArr[]){
if(searchIndex(key, tableSize) != -1){ //alrExist(tableSize, key) == true node does exist, delete
int loc = searchIndex(key, tableSize); //searchIndex(key, tableSize) != -1
for(int i = 0; i < tableSize; i++){
if(delArr[i] == -1){
delArr[i] = table[loc].get_phys_addr();
break;
}
}
table[loc].set_val(0);
table[loc].set_phys_addr(-1);
table[loc].set_flag(-1); //-1 is condition flag for deletion
countInsert--;
cout << "success" << endl;
}
else{
if(searchIndex(key, tableSize) == -1 || table[searchIndex(key, tableSize)].get_flag() != 1){
cout << "failure" << endl;
}
}
return;
}