-
Notifications
You must be signed in to change notification settings - Fork 0
/
double.cpp
341 lines (262 loc) · 7.76 KB
/
double.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//======================================================
//Author: James Anthony Ortiz
//File: double.cpp
//Assignment: Programming Assignment #2 (Part B)
//Description:
//An open-address hash-map implemented in C++, that accepts randomized keys in
//the closed range [-2^31, 2^31].
//This program detects the amount of collisions based on the
//.5, .6, .7, .8, .9 load factor values.
//Compile: g++ -std=c++11 double.cpp -o double
//=======================================================
//Libraries used in program:
#include <iostream>
#include <vector>
#include <random>
#include <cstddef>
#include <time.h>
#include <chrono>
using namespace std;
//Used macro in second hash-function:
#define PRIME 7
//Global Hash-Table Array for 1,000,000 long-long int values:
long long int arr[1000000];
int collisions = 0;
//Function: hash_function1()
//Hashes value directly
//Returns an int value
int hash_function1(long long int key)
{
if(key >= 0)
{
int hash;
hash = (key % 1000000);
return hash;
}
int hash;
hash = (key & 0x7FFFFFFFFFFFFFFF) % 1000000;
return hash;
}//hash_function1()
int hash_function2(long long int key)
{
if(key >= 0)
{
int hash;
hash = (PRIME - ((key) % PRIME));
return hash;
}
int hash;
hash = (PRIME - ((key & 0x7FFFFFFFFFFFFFFF) % PRIME));
return hash;
}//hash_function2()
//Function: insert()
//Description: Inserts a random key into an unoccupied
//space inside of the hashtable, if the currently searched space
//is occupied it will look for an empty one:
//Returns: void
void insert(long long int key, int keys)
{
int index = hash_function1(key);
//If the spot is not empty:
//goto the next adjacent location
//until a vacant area in the Table is found
if(arr[index] != 0)
{
int index2 = hash_function2(key);
int i = 1;
while (1)
{
//obtain new index:
int newIndex = (index + i * index2) % 1000000;
//if no collision occurs, store key into index:
if(arr[newIndex] == 0)
{
arr[newIndex] = key;
break;
}
i++;
}
}
else
{
//if no collision occurs, insert directly:
arr[index] = key;
}
}//end function insert()
//Function: insert_with_collision_count()
//Description: This function calculates the number of collisions
//that are taken to insert a value into the hashtable. Essentially,
//it is a version of the function above but without actual insertion
//involved.
//Returns: void
void insert_with_collision_count(long long int key)
{
int index = hash_function1(key);
//If there is a collision, create a new index and add
//displacement until an unoccupied location is found:
if(arr[index] != 0)
{
//Obtain a new index value:
int index2 = hash_function2(key);
int i = 1;
while(1)
{
//obtain new index:
int newIndex = (index + i * index2) % 1000000;
//if no collision occurs, store key at index:
if(arr[newIndex] == 0)
{
collisions++;
break;
}
else
{
//if a collision still occurs we will still count this:
collisions++;
}
i++;
}
}
else
{
//if there are no collisions, place key directly into index:
collisions++;
}
}//end insert_with_collision_count()
//================================
//MAIN Driver:
//================================
int main()
{
//Lower-Bound: [-2^31]
long long lower = -2147483648;
//Upper-Bound: [2^31]
long long upper = 2147483648;
//Zero-out global array:
for(int i = 0; i < 1000000; i++)
{
arr[i] = 0;
}
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::uniform_int_distribution<long long int> dist(lower, upper);
//Character value used for selection:
char value;
//Selection from user:
cout << "======================================="<< endl;
cout << "Welcome to the Double probing program! " << endl;
cout << "=======================================" << endl;
cout << "Please enter a letter that corresponds to a load factor: " << endl;
cout << "Enter A for 0.5" << endl;
cout << "Enter B for 0.6" << endl;
cout << "Enter C for 0.7" << endl;
cout << "Enter D for 0.8" << endl;
cout << "Enter E for 0.9" << endl;
cout << "Select a value: " ;
//Recieve char value
cin >> value;
float load_factor = 0.0;
long long int keys = 0;
switch(toupper(value))
{
case 'A':
{
keys = 1000000 * 0.5;
load_factor = 0.5;
cout << "The load factor you have selected is: " << 0.5 << endl;
cout << "The number of keys you have selected to use is: " << keys << endl;
break;
}
case 'B':
{
keys = 1000000 * 0.6;
load_factor = 0.6;
cout << "The load factor you have selected is: " << 0.6 << endl;
cout << "The number of keys you have selected to use is: " << keys << endl;
break;
}
case 'C':
{
keys = 1000000 * 0.7;
load_factor = 0.7;
cout << "The load factor you have selected is: " << 0.7 << endl;
cout << "The number of keys you have selected to use is: " << keys << endl;
break;
}
case 'D':
{
keys = 1000000 * 0.8;
load_factor = 0.8;
cout << "The load factor you have selected is: " << 0.8 << endl;
cout << "The number of keys you have selected to use is: " << keys << endl;
break;
}
case 'E':
{
keys = 1000000 * 0.9;
load_factor = 0.9;
cout << "The load factor you have selected is: " << 0.9 << endl;
cout << "The number of keys you have selected to use is: " << keys << endl;
break;
}
default:
{
cout << "Sorry, you have entered the wrong character value. Exiting Program...." << endl;
break;
}
}//end switch
//==================================================================================
//Insert random values into array arr:
//For-loop:
int counter = 0;
while(counter < keys)
{
//Create a random value as a key:
long long int rand_value;
rand_value = dist(generator);
//Attempt to insert value:
insert(rand_value, keys);
//Increment while-loop counter:
counter++;
}//end for-loop:
//Currently at designated size...:
//Now, create a for loop with 10,000 random keys, without actual insertion
//to calculate collisions:
int new_counter = 0;
while(new_counter < 10000)
{
//Create a random value:
long long int rand_value;
rand_value = dist(generator);
//Count collisions at the load factor: .5
insert_with_collision_count(rand_value);
//increment counter for for-loop:
new_counter++;
}
//Calculate average amt. of collisions dividing collisions by 10000:
double avg_collisions = collisions/10000.0;
cout << "Calculating results...please wait..." << endl;
cout << "====================================================" << endl;
cout << "RESULTS FOR LOAD-FACTOR: " << load_factor << endl;
cout << "The total amount of collisions is: " << collisions << endl;
cout << "The total amount of keys were: " << keys << endl;
cout << "The average amount of collisions with " << keys << " keys were: " << avg_collisions << endl;
int occupied = 0;
int empty = 0;
for(int i = 0; i < 1000000; i++)
{
if(arr[i] != 0)
{
//cout << "[ " << arr[i] << " , " << "Index: " << i << "]" << endl;
occupied++;
}
else if(arr[i] == 0)
{
//cout << "[EMPTY" << ", " << " Index:" << i << " ]" << endl;
empty++;
}
}
cout << "Occupied Slots: " << occupied << " Empty Slots: " << empty << endl;
cout << "====================================================" << endl;
return 0;
}//end MAIN