-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubicProbing.cpp
More file actions
180 lines (164 loc) · 3.87 KB
/
Copy pathCubicProbing.cpp
File metadata and controls
180 lines (164 loc) · 3.87 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
#include "CubicProbing.h"
using namespace std;
CubicProbing::CubicProbing(){
Account newAccount;
newAccount.id="z";
for(int i=0 ;i<100000;i++){
bankStorage1d.push_back(newAccount);
}
}
void M(vector<int> &v, int l, int m, int r){
int i, j, k;
int n1 = m-l+1;
int n2 = r-m;
int L[n1], R[n2];
for (i=0; i<n1; i++){
L[i] = v[l+i];
}
for (j=0; j<n2; j++){
R[j] = v[m+1+j];
}
i = 0;
j = 0;
k = l;
while (i<n1 && j<n2){
if (L[i] <= R[j]){
v[k] = L[i];
i++;
}
else {
v[k] = R[j];
j++;
}
k++;
}
while (i<n1){
v[k] = L[i];
i++;
k++;
}
while (j<n2){
v[k] = R[j];
j++;
k++;
}
}
void MS(vector<int> &v, int l, int r){
if (l<r){
int m = (l+r)/2;
MS(v, l, m);
MS(v, m+1, r);
M(v, l, m, r);
}
}
/*Taking the concept further, cubic probing employs cubic increments to search for open slots. This strategy
adds an extra layer of complexity to the probe sequence, aiming to distribute data evenly and mitigate
clustering issues*/
void CubicProbing::createAccount(std::string id, int count) {
// IMPLEMENT YOUR CODE HERE
long long int index=hash(id);
Account newAccount;
newAccount.id=id;
newAccount.balance=count;
long long int j=1;
while(bankStorage1d[index].id!="z"){
index=(index+(j*j*j+j*j+j))%100000;
j++;
}
bankStorage1d[index]=newAccount;
}
std::vector<int> CubicProbing::getTopK(int k) {
// IMPLEMENT YOUR CODE HERE
vector<int>TopK;
vector<int>TopK1;
for(int i=0;i<bankStorage1d.size();i++){
if(bankStorage1d[i].id!="z"){
TopK.push_back(bankStorage1d[i].balance);
}
}
MS(TopK,0,TopK.size()-1);
if(TopK.size()<k){
for(int i=0;i<TopK.size();i++){
TopK1.push_back(TopK[TopK.size()-1-i]);
}
}
else{
for(int i=0;i<k;i++){
TopK1.push_back(TopK[TopK.size()-1-i]);
}
}
return TopK1;
}
int CubicProbing::getBalance(std::string id) {
if(!doesExist(id)){
return -1;
}
long long int index=hash(id);
long long int i=index;
long long int j=1;
while(bankStorage1d[i].id!=id){
i=(i+(j*j+j+j*j*j))%100000;
j++;
}
return bankStorage1d[i].balance;
}
void CubicProbing::addTransaction(std::string id, int count) {
// IMPLEMENT YOUR CODE HERE
if(!doesExist(id)){
createAccount(id,count);
return;
}
long long int index=hash(id);
long long int i=index;
long long int j=1;
while(bankStorage1d[i].id!=id){
i=(i+(j*j*j+j*j+j))%100000;
j++;
}
bankStorage1d[i].balance+=count;
return;
}
bool CubicProbing::doesExist(std::string id) {
long long int index=hash(id);
long long int i=index;
long long int j=1;
while(bankStorage1d[i].id!=id){
i=(i+(j*j*j+j*j+j))%100000;
j++;
if(i==index){
return false;
}}
return true;
}
bool CubicProbing::deleteAccount(std::string id) {
if(!doesExist(id)){
return false;
}
long long int index=hash(id);
long long int i=index;
long long int j=1;
while(bankStorage1d[i].id!=id){
i=(i+(j*j*j+j*j+j))%100000;
j++;
}
bankStorage1d[i].id="z";
return true;
}
int CubicProbing::databaseSize() {
int count=0;
for(int i=0 ;i<bankStorage1d.size();i++){
if(bankStorage1d[i].id!="z"){
count++;
}
}
return count;
}
int CubicProbing::hash(std::string id) {
unsigned h = 0x17FFF; // Initialize with 20 bits set to 1
for (char c : id) {
h ^= static_cast<unsigned>(c);
h *= 0x104C11DB7;
h &= 0x17FFF;
}
return static_cast<int>(h);
}