-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTournament.h
551 lines (350 loc) · 10.9 KB
/
Tournament.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/** \file Tournament.h
\brief Function prototypes and implementations for the Tournament class
This contains the header file and implementation for methods of Tournament class.
The following methods are implemented in this file: constructors, mutators, accessors, and helper functions.
#ifndef BplusTournamentSort_Tournament_h
#define BplusTournamentSort_Tournament_h
#include <iostream>
#include <vector>
#include <limits>
#include <assert.h>
#include <math.h>
using namespace std;
template <class Type>
class Tournament
{
private:
int HEAPSIZE;
vector<vector<Type> > master_data; // copied the input vector of vectors to be able to access data
typedef typename vector<Type>::iterator It;
vector<It> its; // iterators for each vector
vector<int> data; // Heap, containing indexes for iterators and master_data
vector<Type> run; // a vector to be used for output
int size; // size of heap
//-- Support methods --//
/** \fn int l_child(int)
* \brief gets the index of the left child
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the left child is returned
*/
int l_child(int); // index of left child: general version
/** \fn int r_child(int)
* \brief gets the index of the right child
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the right child is returned
*/
int r_child(int); // index of right child: general version
/** \fn int parent(int)
* \brief gets the index of the parent node
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the parent node is returned
*/
int parent(int); // index of parent node: general version
/** \fn bool exist_l_child(int)
* \brief verifies whether there is a left child
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the left child is returned
*/
bool exist_l_child(int);
/** \fn bool exist_r_child(int)
* \brief verifies whether there is a right child
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the right child is returned
*/
bool exist_r_child(int);
/** \fn bool exist_parent(int)
* \brief verifies whether there is a parent node
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the parent node is returned
*/
bool exist_parent(int);
/** \fn void push(int)
* \brief pushes the new item onto the heap
* \param index is int for the index of the item pushed
* \pre there should be items to push
* \post the new item is inserted into the heap and the size of heap incremented
*/
void push(int); // will increments size
/** \fn Type pop()
* \brief pops an item from the heap
* \pre there should be items to pop
* \post the item is popped from the heap and the size of heap decremented
*/
Type pop(); // pop from heap. If the vector pointed by the popped item reaches at the end of the vector,
// pop the itm from the heap. otherwise, don't remove it, just increment the iterator of vector and do heapify_down
/** \fn int next(int)
* \brief gets the next index from heap
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the next index for heap is returned
*/
int next(int); // next index for heap
/** \fn int head()
* \brief gets the head of heap
* \pre there should be a head
* \post the head of heap is returned
*/
int head(); // head of heap
/** \fn int tail()
* \brief gets the tail of heap
* \pre there should be a tail
* \post the tail of heap is returned
*/
int tail(); // tail of heap
/** \fn void heapify_up(int)
* \brief heapifies up a node in heap
* \param n is an int for the index
* \pre checks whether the item in heap's head is the root
* \post a node is heapified up in heap
*/
void heapify_up(int); // heapify up a node in heap
/** \fn void heapify_down(int)
* \brief heapifies down a node in heap
* \param n is an int for the index
* \pre checks whether the root has children
* \post a node is heapified down in heap
*/
void heapify_down(int); // heapify down a node in heap
public:
/** Default constructor */
Tournament();
/** Copy constructor */
Tournament(vector<vector<Type> >);
/** \fn void init(vector<vector<Type> >)
* \brief initialize a vector of items
* \param items is a vector of vectors to hold items
* \pre the elements of the vector items should be vectors
* \post a vector of vectors containing items is returned
*/
void init(vector<vector<Type> >);
/** \fn void sort()
* \brief sorts each list
* \pre the list to be sorted should not be empty
* \post each list is sorted
*/
void sort();
/** \fn vector<Type> retrieve()
* \brief returns list of sorted items
* \pre there should some items in the list
* \post the sorted list is returned
*/
vector<Type> retrieve(); // returns list of sorted items
/** \fn void logFile(ofstream& )
* \brief generates logfile
* \param logfile is of type ofstream for a pointer
* \pre the argument should be a pointer
* \post the logfile is returned
*/
void logFile(ofstream& );
};
// ------------------------------
// Definition of Tournament Class
// ------------------------------
//-- Public Methods --//
// Default Constructor
template <class Type>
Tournament<Type>::Tournament()
{
}
template <class Type>
Tournament<Type>::Tournament(vector<vector<Type> > items)
{
init(items);
}
template <class Type>
void Tournament<Type>::init(vector<vector<Type> > items)
{
HEAPSIZE = (int)items.size();
master_data = items;
size = 0;
data.reserve(HEAPSIZE);
for (int i=0; i < HEAPSIZE; i++)
{
its.push_back(master_data[i].begin());
}
for (int i=0; i < HEAPSIZE; i++)
{
push(i);
}
}
template <class Type>
void Tournament<Type>::sort()
{
while (size != 0)
{
run.push_back(pop());
}
}
template <class Type>
vector<Type> Tournament<Type>::retrieve()
{
return run;
}
template <class Type>
void Tournament<Type>::logFile(ofstream& outFile2)
{
outFile2 << "The number of records that can fit in memory are " << HEAPSIZE << endl;
int counter = 0;
for (typename vector<vector<Type> >::iterator outit = master_data.begin(); outit != master_data.end(); ++outit)
{
for (typename vector<Type>::iterator init = (*outit).begin(); init != (*outit).end(); ++init)
{
counter++; //keeps track of how many elements are in the vector of vectors i.e. the number of records
}
}
outFile2 << "The number of records are " << counter << endl;
outFile2 << "The number of runs are " << HEAPSIZE << endl;
int max = (int)master_data[0].size();
int min = (int)master_data[0].size();
for(int i = 0; i < HEAPSIZE; i++)
{
if(master_data[i].size() > max)
{
max = (int)master_data[i].size();
}
if(master_data[i+1].size() < min)
{
min = (int)master_data[i+1].size();
}
}
outFile2 << "The smallest number of records in all of the runs is " << min << endl;
outFile2 << "The largest number of records in all of the runs is " << max << endl;
outFile2 << "The arithmetic mean number of records in all of the runs is " << counter / HEAPSIZE << endl;
int height = ceil(log2(counter));
outFile2 << "The height of the tournament tree for the merge is " << height << endl;
}
//---- Supporter Methods ----//
//-- Private Methods --//
//-- Index Getter --//
template <class Type>
int Tournament<Type>::l_child(int n)
{
return 2 * n + 1;
}
template <class Type>
int Tournament<Type>::r_child(int n)
{
return 2 * (n + 1);
}
template <class Type>
int Tournament<Type>::parent(int n)
{
return (n-1)/2;
}
template <class Type>
int Tournament<Type>::next(int n)
{
return n+1;
}
template <class Type>
int Tournament<Type>::head()
{
return 0;
}
template <class Type>
int Tournament<Type>::tail()
{
return size - 1;
}
//-- Existence Checker --//
template <class Type>
bool Tournament<Type>::exist_parent(int n)
{
return parent(n) >= 0;
}
template <class Type>
bool Tournament<Type>::exist_l_child(int n)
{
return l_child(n) < size;
}
template <class Type>
bool Tournament<Type>::exist_r_child(int n)
{
return r_child(n) < size;
}
template <class Type>
void Tournament<Type>::push(int index)
{
data[next(tail())] = index;
size++;
heapify_up(tail());
return;
}
template <class Type>
Type Tournament<Type>::pop()
{
Type returndata = *(its[data[head()]]);
its[data[head()]] = ++ its[data[head()]];
if (its[data[head()]] == master_data[data[head()]].end())
{
// run out the item from the vector
data[head()] = data[tail()];
size--;
}
heapify_down(head());
return returndata;
}
template <class Type>
void Tournament<Type>::heapify_up(int n)
{
if (n == head())
return;
if (*(its[data[parent(n)]]) > *(its[data[n]]))
{
int tempdata = data[parent(n)];
data[parent(n)] = data[n];
data[n] = tempdata;
heapify_up(parent(n)); // recursive call to parent node
}
}
template <class Type>
void Tournament<Type>::heapify_down(int n)
{
if (!exist_l_child(n))
{ // both children not exist
// stop heapifying
return;
}
else if (!exist_r_child(n))
{
if (*(its[data[l_child(n)]]) < *(its[data[n]]))
{
int tempdata = data[l_child(n)];
data[l_child(n)] = data[n];
data[n] = tempdata;
heapify_down(l_child(n));
}
return;
}
else // both exist
// if left child is smaller than right
if (*(its[data[l_child(n)]]) < *(its[data[r_child(n)]]))
{
if (*(its[data[l_child(n)]]) < *(its[data[n]]))
{
int tempdata = data[l_child(n)];
data[l_child(n)] = data[n];
data[n] = tempdata;
heapify_down(l_child(n));
}
}
// if right child is smaller
else
{
if (*(its[data[r_child(n)]]) < *(its[data[n]]))
{
int tempdata = data[r_child(n)];
data[r_child(n)] = data[n];
data[n] = tempdata;
heapify_down(r_child(n));
}
}
return;
}
#endif