-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBPQ.cpp
More file actions
107 lines (73 loc) · 1.43 KB
/
Copy pathBPQ.cpp
File metadata and controls
107 lines (73 loc) · 1.43 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
#include "stdlib.h"
#include "BPQ.h"
#include "BPQPoint.h"
#include <iostream>
BPQ::BPQ(int k) {
// alloc array
_k = k;
_values = new BPQPoint*[k];
_size = 0;
}
BPQ::~BPQ() {
int i;
delete [] _values;
_size = 0;
}
BPQPoint* BPQ::getValue(int index) {
// DEBUG -- if not OOB or throws
return _values[index];
}
void BPQ::enqueue(BPQPoint* point) {
std::cout << "queueing \n";
if(getSize() < _k) {
std::cout << "adding new bp\n";
_size++;
std::cout << "size " << _size << "\n";
_values[_size-1]=point;
} else {
std::cout << "overflowing queue - adjust priority\n";
//remove highest value n as it represents the lowest priority n
int max = 0;
int pos = -1;
int i;
for(i=0; i<_size;i++){
int cp = _values[i]->getP();
if(cp > max) {
max = cp;
pos = i;
}
}
// now pos is the minimum
//printf("max is %d at pos %d\n", max, pos);
if(point->getP() < max && pos > -1) {
delete _values[pos];
_values[pos] = point;
}
}
}
BPQPoint* BPQ::max_priority_elem() {
int min = 9999999;
int pos = 0;
int i;
for(i=0; i<_size;i++ ){
int cp = _values[i]->getP();
if(cp < min) {
min = cp;
pos = i;
}
}
return _values[pos];
}
BPQPoint* BPQ::min_priority_elem() {
int min = -99999;
int pos = 0;
int i;
for(i=0; i<_size;i++ ){
int cp = _values[i]->getP();
if(cp > min) {
min = cp;
pos = i;
}
}
return _values[pos];
}