-
Notifications
You must be signed in to change notification settings - Fork 1
/
priority_queue.cpp
140 lines (123 loc) · 3.64 KB
/
priority_queue.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
#include "priority_queue.h"
template <class T>
PriorityQueue<T>::PriorityQueue(const PriorityQueue<T> & source) : numElems(source.numElems)
{
typename std::list<heapNode *>::const_iterator it;
for (it = source.rootList.begin(); it != source.rootList.end(); it++)
{
heapNode * rootNode;
copy(rootNode, *it);
rootList.push_back(rootNode);
}
// Update highestPriority
highestPriority = rootList.begin();
typename std::list<heapNode *>::iterator it2;
for (it2 = rootList.begin(); it2 != rootList.end(); it2++)
{
if (higherPriority((*it2)->value, (*highestPriority)->value))
{
highestPriority = it2;
}
}
}
template <class T>
void PriorityQueue<T>::copy(heapNode * & currNode, heapNode * const & sourceCurrNode)
{
currNode = new heapNode(sourceCurrNode->value);
if (!sourceCurrNode->children.empty())
{
typename std::list<heapNode *>::const_iterator it;
for (it = sourceCurrNode->children.begin(); it != sourceCurrNode->children.end(); it++)
{
heapNode * child;
copy(child, *it);
currNode->children.push_back(child);
}
}
}
template <class T>
PriorityQueue<T>::~PriorityQueue()
{
typename std::list<heapNode *>::iterator it;
for (it = rootList.begin(); it != rootList.end(); it++)
{
clear(*it);
}
}
template <class T>
void PriorityQueue<T>::clear(heapNode * & currNode)
{
if (!currNode->children.empty())
{
typename std::list<heapNode *>::iterator it;
for (it = currNode->children.begin(); it != currNode->children.end(); it++)
{
clear(*it);
}
}
delete currNode;
currNode = NULL;
}
template <class T>
void PriorityQueue<T>::insert(const T & value)
{
heapNode * newNode = new heapNode(value);
rootList.push_back(newNode);
if (highestPriority == rootList.end() || higherPriority(value, (*highestPriority)->value)) {
highestPriority = --rootList.end();
}
numElems++;
}
template <class T>
T PriorityQueue<T>::pop()
{
if (isEmpty()) return T();
// Meld children into root list
rootList.splice(rootList.end(), (*highestPriority)->children);
// Delete highest priority element
T retVal = (*highestPriority)->value;
delete *highestPriority;
highestPriority = rootList.erase(highestPriority);
// Consolidate trees and update highest priority
consolidate();
numElems--;
return retVal;
}
template <class T>
const T & PriorityQueue<T>::top() const
{
return (*highestPriority)->value;
}
template <class T>
bool PriorityQueue<T>::isEmpty() const
{
return rootList.empty();
}
template <class T>
void PriorityQueue<T>::consolidate()
{
highestPriority = rootList.begin();
std::vector<typename std::list<heapNode *>::iterator> B(numElems, rootList.end());
typename std::list<heapNode *>::iterator it;
for (it = rootList.begin(); it != rootList.end(); ++it)
{
if (higherPriority((*it)->value, (*highestPriority)->value))
highestPriority = it;
typename std::list<heapNode *>::iterator v = it;
while (B[(*v)->degree()] != rootList.end())
{
typename std::list<heapNode *>::iterator w = B[(*v)->degree()];
if (v == w) break;
B[(*v)->degree()] = rootList.end();
if (higherPriority((*w)->value, (*v)->value))
{
std::swap(v, w);
}
((*v)->children).push_back(*w);
if (highestPriority == w) highestPriority = v;
w = rootList.erase(w);
}
it = v;
B[(*v)->degree()] = v;
}
}