-
Notifications
You must be signed in to change notification settings - Fork 1
/
priorityqueue.h
138 lines (122 loc) · 3.78 KB
/
priorityqueue.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
/************************************************************************
*
* Copyright (C) 2017 by Peter Harrison. www.micromouseonline.com
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without l> imitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
************************************************************************/
#ifndef PRIORITYQUEUE_H
#define PRIORITYQUEUE_H
#include <cassert>
#include <cstdint>
template <class item_t>
class PriorityQueue {
public:
explicit PriorityQueue(int maxSize = 128) : MAX_ITEMS(maxSize) {
mData = new item_t[MAX_ITEMS + 1];
mHead = 0;
mTail = 0;
mItemCount = 0;
}
PriorityQueue(const PriorityQueue<item_t> &rhs) : MAX_ITEMS(rhs.MAX_ITEMS), mItemCount(rhs.mItemCount) {
mHead = rhs.mHead;
mTail = rhs.mTail;
mData = new item_t(MAX_ITEMS + 1);
for (int i = 0; i < MAX_ITEMS; i++) {
mData[i] = rhs.mData[i];
}
};
~PriorityQueue() { delete[] mData; };
int size() { return mItemCount; }
bool empty() { return size() == 0; }
void clear() {
mHead = 0;
mTail = 0;
mItemCount = 0;
}
/*
* Adds an item to the tail of the queue
*/
void push(item_t item) {
assert(mItemCount < MAX_ITEMS);
mData[mTail] = item;
increment(mTail);
++mItemCount;
}
/*
* fetch the item at the head of the queue. Using only this method
* allows use of the queue as a simple LIFO structure
*/
item_t head() {
item_t result = mData[mHead];
increment(mHead);
--mItemCount;
return result;
}
/*
* remove the item at the head of the queue.
* Provided only for compatibility with std::priority_queue
*/
void pop() {
increment(mHead);
--mItemCount;
}
/*
* return a reference to the item at the front of the queue. Using only this method
* allows use of the queue as a simple LIFO structure
*/
item_t &front() { return mData[mHead]; }
/*
* Search from the head of the queue towards the tail
* return the smallest item in the queue
* If two items are equally small, return the one that
* was in the queue the longest. That is, the first one found.
* Operation is performed by swapping the smallest item into the head
* position. Thus, the natural order of the queue is corrupted
*/
item_t fetchSmallest() {
assert(mItemCount > 0);
int posSmallest = mHead;
int index = mHead;
while (index != mTail) {
if (mData[index] < mData[posSmallest]) {
posSmallest = index;
}
increment(index);
}
item_t smallest = mData[posSmallest];
mData[posSmallest] = mData[mHead];
mData[mHead] = smallest;
return head();
}
protected:
void increment(int &ptr) {
++ptr;
if (ptr > MAX_ITEMS) {
ptr -= MAX_ITEMS;
}
}
item_t *mData;
const int MAX_ITEMS;
int mHead;
int mTail;
int mItemCount;
};
#endif // PRIORITYQUEUE_H