-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.hpp
More file actions
56 lines (50 loc) · 1.2 KB
/
queue.hpp
File metadata and controls
56 lines (50 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
# ifndef QUEUE
# define QUEUE
#include <assert.h>
#include <iostream>
template<typename T, int array_size = 32>
class queue {
// 维护的是下标[1:32]的循环队列
private:
int head, tail, size;
T a[array_size + 5];
public:
// tail指向空位置
queue() { head = tail = 0, size = array_size; }
inline bool isempty() { return head == tail; }
inline bool isfull() { return (tail + 1) % size == head; }
inline T front() { return a[head]; }
void push(const T &x) {
if(isfull()) {
std :: cerr << "queue_size_has_full\n";
assert(0);
}
a[tail] = x;
++tail; if(tail == size) tail = 0;
}
T pop() {
T tmp = a[head];
++head; if(head == size) head = 0;
return tmp;
}
void print() {
int idx = head;
while(idx != tail) {
a[idx].print();
idx = (idx + 1) % size;
}
}
// 主要用于ROB中, 返回队列的下一个空位置, 注意这里下标整体右移1位
inline int getTail() { return tail + 1; }
inline T & getfront() { return a[head]; }
T & operator [] (int pos) {
//assert(pos >= 1 && pos <= 32);
return a[pos - 1];
}
void clear() {
for(int i = 0; i < size; ++i)
a[i].clear();
head = tail = 0;
}
};
# endif