-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashMapOA.h
200 lines (173 loc) · 5.21 KB
/
HashMapOA.h
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
#ifndef ASD2_2_HASHMAPOA_H
#define ASD2_2_HASHMAPOA_H
#include <iostream>
#include <cmath>
using namespace std;
struct Data
{
char name;
short treatsPerDay;
float fluffiness;
Data()
{
char names[] = "QWERTYUIOPASDFGHJKLZXCVBNM";
name = names[rand() % sizeof(names)];
treatsPerDay = rand() % 75 + 15;
fluffiness = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
}
~Data() = default;
void print() const
{
std::cout.precision(2);
std::cout << "Name: " << name << " Treats per day: " << treatsPerDay << " Fluffiness: " << fluffiness << "\n";
}
};
struct HashNode
{
int status = 0; // 0 - empty; 1 - removed; 2 - occupied
Data data;
unsigned long long key;
};
struct HashMapOA
{
HashNode* bucketsArray;
size_t hash_size, hash_capacity;
float MaxLoadFactor;
HashMapOA()
{
hash_size = 0;
hash_capacity = 7;
bucketsArray = new HashNode[hash_capacity];
MaxLoadFactor = 0.5;
}
~HashMapOA()
{
delete[] bucketsArray;
}
static size_t nearest_prime(size_t number)
{
for(int i = number + 1;;i++)
{
bool null_rest = false;
for(size_t j = 2; j <= round(sqrt(i)); j++)
{
if(i % j == 0)
{
null_rest = true;
break;
}
}
if(!null_rest)
return i;
}
}
size_t hash1(unsigned long long key) const
{
return ((key * 18 - 41) % 4194610382161);
}
size_t hash2(unsigned long long key) const
{
return 982137816722392 - (key * 21988211);
}
void reallocate(float times)
{
if(times < 1.0)
times = 2.0;
size_t old_capacity = hash_capacity;
hash_capacity = nearest_prime(old_capacity * times);
auto* new_bucketsArray = new HashNode[hash_capacity];
for(size_t i = 0; i < old_capacity; i++)
{
HashNode& copy = bucketsArray[i];
if(copy.status == 2)
{
size_t first_hash = hash1(copy.key) % hash_capacity;
size_t second_hash = hash2(copy.key) % hash_capacity;
size_t index = first_hash;
if(second_hash == 0)
second_hash = 1;
for(int j = 1; new_bucketsArray[index].status != 0; j++)
index = (first_hash + j * second_hash) % hash_capacity;
new_bucketsArray[index] = copy;
}
}
delete[] bucketsArray;
bucketsArray = new_bucketsArray;
}
void insert(unsigned long long key, const Data& value)
{
float loadFactor = static_cast<float>(hash_size) / static_cast<float>(hash_capacity);
if(MaxLoadFactor <= loadFactor)
{
float times = 2.0;
reallocate(times);
}
size_t first_hash = hash1(key) % hash_capacity;
size_t second_hash = hash2(key) % hash_capacity;
size_t index = first_hash;
if(second_hash == 0)
second_hash = 1;
size_t i = 1;
while(bucketsArray[index].status == 2)
{
HashNode& copy = bucketsArray[index];
if(copy.key == key)
{
copy.data = value;
return;
}
index = (first_hash + i * second_hash) % hash_capacity;
i++;
}
bucketsArray[index] = {2, value, key};
hash_size++;
}
Data* find(unsigned long long key) const
{
size_t first_hash = hash1(key) % hash_capacity;
size_t second_hash = hash2(key) % hash_capacity;
size_t index = first_hash;
if(second_hash == 0)
second_hash = 1;
int i = 1;
while(bucketsArray[index].status != 0)
{
if(bucketsArray[index].key == key)
{
Data* pointer = nullptr;
if(bucketsArray[index].status == 2)
pointer = &bucketsArray[index].data;
return pointer;
}
index = (first_hash + i * second_hash) % hash_capacity;
i++;
}
return nullptr;
}
void erase(unsigned long long key)
{
size_t first_hash = hash1(key) % hash_capacity;
size_t second_hash = hash2(key) % hash_capacity;
size_t index = first_hash;
if(second_hash == 0)
second_hash = 1;
int i = 1;
while(bucketsArray[index].status != 0)
{
HashNode& copy = bucketsArray[index];
if(copy.key == key)
{
if(copy.status == 2)
{
copy.status = 1;
hash_size--;
}
return;
}
index = (first_hash + i * second_hash) % hash_capacity;
i++;
}
}
size_t size() const {return hash_size;}
};
#endif //ASD2_2_HASHMAPOA_H