-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpQueue.h
More file actions
271 lines (219 loc) · 5.63 KB
/
Copy pathpQueue.h
File metadata and controls
271 lines (219 loc) · 5.63 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
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
#ifndef PQUEUE_H
#define PQUEUE_H
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <fstream>
#include "pQueueNode.h"
using namespace std;
enum PQUEUE_ERRORS {PQUEUE_BAD_SIZE, PQUEUE_FULL, PQUEUE_EMPTY};
enum PRIORITY_TYPE {MAXPRIORITY,MINPRIORITY};
template<typename T, typename U>
class pQueue
{
public:
pQueue(int s = 1024);
~pQueue();
pQueue(const pQueue<T,U> &other);
pQueue<T,U>& operator=(const pQueue<T,U> &other);
bool empty();
bool full();
void resize(int s = 1024);
int size() const;
int capacity() const;
void clear();
void makeQueue(int s);
void enqueue(const T &d, const U &p);
T dequeue();
pQueue<T,U>& operator>>(T &d);
T& front();
U& frontRanking();
template<typename A, typename B>
friend
ostream& operator<<(ostream &out, const pQueue<A,B> &pQ);
template<typename A, typename B>
friend
istream& operator>>(istream &in, pQueue<A,B> &pQ);
private:
int boq, eoq;
int mySize, myCapacity;
pQueueNode<T,U> *pQ;
void nukem();
void copy(const pQueue<T,U>&other);
};
template<typename T, typename U>
pQueue<T,U>::pQueue(int s)
{
makeQueue(s);
}
template<typename T, typename U>
pQueue<T,U>::~pQueue()
{
nukem();
}
template<typename T, typename U>
void pQueue<T,U>::makeQueue(int s)
{
boq = eoq = 0;
mySize = 0;
myCapacity = s;
pQ = new pQueueNode<T,U>[s]; //plus one because eoq needs an empty spot to point at
}
template<typename T, typename U>
pQueue<T,U>::pQueue(const pQueue<T,U> &other)
{
copy(other);
}
template<typename T, typename U>
pQueue<T,U>& pQueue<T,U>::operator=(const pQueue<T,U> &other)
{
if(this != &other)
{
nukem();
copy(other);
}
return *this;
}
template<typename T, typename U>
bool pQueue<T,U>::empty()
{
return !mySize;
}
template<typename T, typename U>
bool pQueue<T,U>::full()
{
return mySize == myCapacity;
}
template<typename T, typename U>
void pQueue<T,U>::resize(int s)
{
if(s <= 0)
throw PQUEUE_BAD_SIZE;
nukem();
makeQueue(s);
}
template<typename T, typename U>
int pQueue<T,U>::size() const
{
return mySize;
}
template<typename T, typename U>
int pQueue<T,U>::capacity() const
{
return myCapacity;
}
template<typename T, typename U>
void pQueue<T,U>::clear()
{
nukem();
}
template<typename T, typename U>
void pQueue<T,U>::enqueue(const T &d, const U &p)
{
pQueueNode<T,U> tempnode(d,p);
if(full())
throw PQUEUE_FULL;
if(empty())
{
pQ[eoq%myCapacity].theData()= tempnode.theData();
pQ[eoq%myCapacity].thePriority()= tempnode.thePriority();
++eoq;
++mySize;
return;
}
//start of sorted insert (Greater number = greater priority)
for (int i = eoq; i>=boq;--i)
{
if(i != boq) //if i is not at boq
{
if(higherPriority(pQ[(i-1)%myCapacity],tempnode)
|| pQ[(i-1)%myCapacity].thePriority() == tempnode.thePriority()) //if a is greater than or equal to b.
{
pQ[i%myCapacity].theData()= tempnode.theData();
pQ[i%myCapacity].thePriority()= tempnode.thePriority();
++eoq;
++mySize;
return;
}
else
{
pQ[i%myCapacity].theData()= pQ[(i-1)%myCapacity].theData();
pQ[i%myCapacity].thePriority()= pQ[(i-1)%myCapacity].thePriority();
}
}
else if(i == boq)//when i == boq
{
pQ[i%myCapacity].theData()= tempnode.theData();
pQ[i%myCapacity].thePriority()= tempnode.thePriority();
++eoq;
++mySize;
return;
}
}
}
template<typename T, typename U>
T pQueue<T,U>::dequeue()
{
if(empty())
throw PQUEUE_EMPTY;
T temp;
temp = pQ[boq%capacity()].theData(); //POSTFIX!!
pQ[boq%capacity()].theData()=T();
pQ[boq%capacity()].thePriority()=U();
++boq;
--mySize;
return temp;
}
template<typename T, typename U>
T& pQueue<T,U>::front()
{
if(empty())
throw PQUEUE_EMPTY;
return pQ[boq%myCapacity].theData();//POSTFIX
}
template<typename T,typename U>
U& pQueue<T,U>::frontRanking()
{
if(empty())
throw PQUEUE_EMPTY;
return pQ[boq%myCapacity].thePriority();
}
template<typename T, typename U>
void pQueue<T,U>::nukem()
{
delete [] pQ;
boq = eoq = 0;
mySize = 0;
myCapacity = 0;
}
template<typename T, typename U>
void pQueue<T,U>::copy(const pQueue<T,U>&other)
{
nukem(); //POSTFIX
mySize = other.mySize;
myCapacity = other.myCapacity;
boq = other.boq;
eoq = other.eoq;
makeQueue(other.myCapacity); //POSTFIX
for(int i = 0; i < myCapacity; ++i)
pQ[i] = other.pQ[i];
}
template<typename A, typename B>
ostream& operator<<(ostream &out, const pQueue<A,B> &pQ)
{
for(int i =pQ.boq; i< pQ.eoq;++i)
{
out<<pQ.pQ[i%pQ.capacity()];
}
return out;
}
template<typename A, typename B>
istream& operator>>(istream &in, pQueue<A,B> &pQ)
{
pQueueNode<A,B> newNode;
while(in>>newNode)
pQ.enqueue(newNode.theData(),newNode.thePriority());
return in;
}
#endif // PQUEUE_H