-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.cpp
More file actions
57 lines (46 loc) · 1.2 KB
/
PriorityQueue.cpp
File metadata and controls
57 lines (46 loc) · 1.2 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
#include "PriorityQueue.h"
// Just use regular arrays
PriorityQueue::PriorityQueue() : length(0), maxItems(MAX_HEAP_SIZE) {
}
PriorityQueue::PriorityQueue(int max) : length(0), maxItems((max <= MAX_HEAP_SIZE) ? max : MAX_HEAP_SIZE) {
}
PriorityQueue::PriorityQueue(const PriorityQueue& other) {
copyQueue(other);
}
PriorityQueue& PriorityQueue::operator=(const PriorityQueue& other) {
if (this != &other) {
copyQueue(other);
}
return *this;
}
void PriorityQueue::enqueue(Order newItem) {
if (isFull()) {
throw FullPQ();
}
else {
newItem.calculateCustomerCost();
items.elements[length] = newItem;
length++;
items.reheapUp(0, length - 1);
}
}
void PriorityQueue::dequeue(Order& item) {
if (isEmpty()) {
throw EmptyPQ();
}
else {
item = items.elements[0];
items.elements[0] = items.elements[length - 1];
length--;
items.reheapDown(0, length - 1);
}
}
void PriorityQueue::copyQueue(const PriorityQueue& other) {
maxItems = other.maxItems;
length = other.length;
// Ensure that we do not copy more elements than the allocated space
int elementsToCopy = (length < maxItems) ? length : maxItems;
for (int i = 0; i < elementsToCopy; i++) {
items.elements[i] = other.items.elements[i];
}
}