-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQueue.h
91 lines (75 loc) · 1.47 KB
/
Queue.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
#ifndef QUEUE_H
#define QUEUE_H
#include <WProgram.h>
typedef struct {
us8 exit;
char side;
} SortPosition;
#define QueueSize 20
#define QueueType SortPosition
#define QueueTypeInit { 255, 'C' }
typedef enum {
qeNONE = 0,
qeOUT_OF_RANGE = 1
} QueueError;
class Queue{
public:
QueueType table[QueueSize];
us8 head;
us8 tail;
Queue() {
clear();
}
inline void index_rollover(us8 &index) {
index++;
if(index >= QueueSize) {
index = 0;
}
}
inline us8 size() {
return QueueSize;
}
inline us8 count() {
if( head >= tail ) {
return (head - tail);
}
else {
return (head + QueueSize - tail);
}
}
void remove(us8 index) {
if(index < QueueSize) {
table[index] = QueueTypeInit;
}
}
void clear() {
for(us8 i = 0; i < QueueSize; i++) {
remove(i);
}
head = 0;
tail = 0;
}
QueueType at(us8 index) {
QueueType rval = QueueTypeInit;
if(index < QueueSize) {
return table[index];
}
return rval;
}
QueueError push(QueueType position) {
if( this->count() >= (QueueSize-1) ) {
return qeOUT_OF_RANGE;
}
table[head] = position;
index_rollover(head);
return qeNONE;
}
QueueType pop() {
QueueType temp = QueueTypeInit;
temp = at(tail);
remove(tail);
index_rollover(tail);
return temp;
}
};
#endif // QUEUE_H