-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex_queue.cc
57 lines (46 loc) · 1019 Bytes
/
index_queue.cc
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
// index_queue.cc
// Copyright (c) 2014 Jinglei Ren <jinglei@ren.systems>
#include "index_queue.h"
using namespace std;
void IndexQueue::Remove(int i) {
assert(i >= 0);
const int prev = array_[i].prev;
const int next = array_[i].next;
if (prev == -EINVAL) {
assert(Front() == i);
SetFront(next);
} else {
array_[prev].next = next;
}
if (next == -EINVAL) {
assert(Back() == i);
SetBack(prev);
} else {
array_[next].prev = prev;
}
array_[i].prev = -EINVAL;
array_[i].next = -EINVAL;
--length_;
}
void IndexQueue::PushBack(int i) {
assert(i >= 0);
assert(array_[i].prev == -EINVAL && array_[i].next == -EINVAL);
if (Empty()) {
SetFront(i);
SetBack(i);
} else {
array_[i].prev = Back();
BackNode().next = i;
SetBack(i);
}
++length_;
}
int IndexQueue::Accept(QueueVisitor* visitor) {
int num = 0, tmp;
for (int i = Front(); i != -EINVAL; ++num) {
tmp = array_[i].next;
visitor->Visit(i);
i = tmp;
}
return num;
}