-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
295 lines (263 loc) · 8.62 KB
/
main.cpp
File metadata and controls
295 lines (263 loc) · 8.62 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
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
#include <iostream>
#include <string>
#include <climits>
#include <cstdlib>
#include "BalancedTreeK.h"
#define MAX_KEY 3.0
#define MIN_KEY -1.0
#define CHECK_SIZE 113
#define INITIAL_INSERT_NUM 70
#define INITIAL_DELETED_SIZE 50
#define OPERATIONS_SIZE 150
#define VALUES_LENGTH 10
#define SEED 22568
#define NUM_OF_OPERATIONS 6
#define INSERT 1
#define DELETE 2
#define SEARCH 3
#define RANK 4
#define SELECT 5
#define GETMAXVALUE 6
using namespace std;
class MyKey :public Key
{
public:
MyKey(double k) : key(k) {}
MyKey(MyKey const &mk) :key(mk.key) {}
~MyKey() {}
MyKey* clone() const
{
return new MyKey(*this);
}
bool operator<(const Key &rhs) const { return key <((MyKey&)rhs).key; }
void print() const { cout << key; }
private:
double key;
};
class MyValue : public Value
{
public:
MyValue(string val) : value(val) {}
MyValue(MyValue const &v) { value = v.value; }
~MyValue() {}
MyValue* clone() const
{
return new MyValue(*this);
}
bool operator<(const Value &rhs) const { return value <((MyValue&)rhs).value; }
void print() const { cout << value; }
private:
string value;
};
string generate_random_string(const unsigned len)
/*returns a random string of length len*/
{
const char alphanum[] ="abcdefghijklmnopqrstuvwxyz";
string s;
for (int i = 0; i < len; ++i) {
s =s+alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}
unsigned generate_random_index(const unsigned max_ind)
/*generate random number in range [0,max_ind-1]*/
{
return rand() % max_ind;
}
void operation_execution(unsigned operation, MyKey* my_keys_array[], MyValue* my_values_array[], bool inserted_keys[], BalancedTreeK* T)
{
MyValue* val = NULL;
MyKey* key = NULL;
MyKey* key1 = NULL;
unsigned rank;
unsigned index;
double new_key;
bool delete_keys=false;
switch (operation)
{
case INSERT:
index = generate_random_index(CHECK_SIZE);
if (inserted_keys[index]==false)
{
T->Insert(my_keys_array[index], my_values_array[index]);
inserted_keys[index]=true;
}
break;
case DELETE:
index = generate_random_index(CHECK_SIZE);
T->Delete(my_keys_array[index]);
inserted_keys[index] = false;
break;
case SEARCH:
index = generate_random_index(CHECK_SIZE);
key = my_keys_array[index];
val = (MyValue*)T->Search(key);
cout << "Search for key: ";
key->print();
cout << ", resulted in value: ";
if (val != NULL)
{
val->print();
}
else
{
cout << "NULL";
}
cout << "\n";
break;
case RANK:
index = generate_random_index(CHECK_SIZE);
key = my_keys_array[index];
rank = T->Rank(key);
cout << "Rank for key: ";
key->print();
cout << ", resulted in: ";
cout << rank << "\n";
break;
case SELECT:
index = generate_random_index(CHECK_SIZE);
key = (MyKey*)T->Select(index);
cout << "Select for index: " << index;
cout << ", resulted in key: ";
if (key != NULL)
{
key->print();
}
else
{
cout << "NULL";
}
cout << "\n";
break;
case GETMAXVALUE:
index = generate_random_index(2);
if (index == 0)
{
key = my_keys_array[generate_random_index(CHECK_SIZE)];
key1 = my_keys_array[generate_random_index(CHECK_SIZE)];
}
else
{
new_key = (double) rand() / RAND_MAX;
key = new MyKey(new_key);
key1 = new MyKey(new_key + (double) rand() / RAND_MAX);
delete_keys = true;
}
val = (MyValue*)T->GetMaxValue(key, key1);
cout << "GetMaxValue for interval [";
key->print();
cout << ",";
key1->print();
cout << "]";
cout << ", resulted in value: ";
if (val != NULL)
{
val->print();
}
else
{
cout << "NULL";
}
cout << "\n";
if (delete_keys)
{
delete key;
delete key1;
delete_keys = false;
}
break;
}
}
int main()
{
srand(SEED);
MyKey* max_key = new MyKey(MAX_KEY);
MyKey* min_key = new MyKey(MIN_KEY);
bool inserted_keys[CHECK_SIZE];
BalancedTreeK* T = new BalancedTreeK(min_key,max_key);
double keys_array[CHECK_SIZE];
string values_array[CHECK_SIZE];
MyKey* my_keys_array[CHECK_SIZE];
MyValue* my_values_array[CHECK_SIZE];
for (unsigned i = 0;i < CHECK_SIZE;i++)
{
inserted_keys[i] = false;
keys_array[i] = (double) rand()/RAND_MAX;
values_array[i] = generate_random_string(VALUES_LENGTH);
my_keys_array[i] = new MyKey(keys_array[i]);
my_values_array[i] = new MyValue(values_array[i]);
}
operation_execution(DELETE, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(SEARCH, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(RANK, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(SELECT, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(GETMAXVALUE, my_keys_array, my_values_array, inserted_keys, T);
for (unsigned i = 0;i < CHECK_SIZE;i++)
{
T->Insert(my_keys_array[i], my_values_array[i]);
inserted_keys[i] = true;
}
operation_execution(INSERT, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(DELETE, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(SEARCH, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(RANK, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(SELECT, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(GETMAXVALUE, my_keys_array, my_values_array, inserted_keys, T);
unsigned del_index;
for (unsigned i = 0;i < INITIAL_DELETED_SIZE;i++)
{
del_index = generate_random_index(CHECK_SIZE);
T->Delete(my_keys_array[del_index]);
inserted_keys[i] = false;
}
operation_execution(INSERT, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(DELETE, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(SEARCH, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(RANK, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(SELECT, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(GETMAXVALUE, my_keys_array, my_values_array, inserted_keys, T);
delete T;
T = new BalancedTreeK(min_key,max_key);
delete max_key;
delete min_key;
for (unsigned i = 0;i < CHECK_SIZE;i++)
{
inserted_keys[i] = false;
}
unsigned index;
for (unsigned i = 0;i < CHECK_SIZE;i++)
{
index = generate_random_index(CHECK_SIZE);
if (inserted_keys[index]==false)
{
T->Insert(my_keys_array[index], my_values_array[index]);
inserted_keys[index] = true;
}
}
unsigned operations_array[OPERATIONS_SIZE];
for (unsigned i = 0;i < OPERATIONS_SIZE;i++)
{
operations_array[i] = generate_random_index(NUM_OF_OPERATIONS) + 1;
}
for (unsigned i = 0;i < OPERATIONS_SIZE;i++)
{
operation_execution(operations_array[i], my_keys_array, my_values_array, inserted_keys, T);
}
for (unsigned i = 0;i < CHECK_SIZE;i++)
{
T->Delete(my_keys_array[i]);
inserted_keys[i] = false;
}
operation_execution(INSERT, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(DELETE, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(SEARCH, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(RANK, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(SELECT, my_keys_array, my_values_array, inserted_keys, T);
operation_execution(GETMAXVALUE, my_keys_array, my_values_array, inserted_keys, T);
for (unsigned i = 0;i < CHECK_SIZE;i++)
{
delete my_keys_array[i];
delete my_values_array[i];
}
delete T;
}