-
Notifications
You must be signed in to change notification settings - Fork 0
/
egtb_queue.cpp
47 lines (41 loc) · 1.04 KB
/
egtb_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
#include <assert.h>
#include <stdlib.h>
#include "egtb_queue.h"
#include "logging.h"
EgtbQueue::EgtbQueue(int size) {
this->size = size;
head = tail = enqTotal = deqTotal = 0;
assert(queue = (EgtbQueueElement*)malloc(size * sizeof(EgtbQueueElement)));
}
EgtbQueue::~EgtbQueue() {
free(queue);
}
void EgtbQueue::enqueue(unsigned code, unsigned index) {
enqTotal++;
queue[tail].code = code;
queue[tail++].index = index;
if (tail == size) {
tail = 0;
}
if (!deqTotal && !(enqTotal & (LOG_EVERY - 1))) {
// log enqueues in the initial phase, before dequeuing starts
log(LOG_DEBUG, "%d positions enqueued", enqTotal);
}
}
void EgtbQueue::dequeue(unsigned* code, unsigned* index) {
deqTotal++;
*code = queue[head].code;
*index = queue[head++].index;
if (head == size) {
head = 0;
}
if (!(deqTotal & (LOG_EVERY - 1))) {
log(LOG_DEBUG, "%d positions dequeued, queue size %d", deqTotal, enqTotal - deqTotal);
}
}
bool EgtbQueue::isEmpty() {
return head == tail;
}
int EgtbQueue::getTotal() {
return enqTotal;
}