-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathringIndex.cpp
More file actions
89 lines (58 loc) · 1.26 KB
/
ringIndex.cpp
File metadata and controls
89 lines (58 loc) · 1.26 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
#include <ringIndex.h>
ringIndex::ringIndex(int inNumItems) {
numItems = inNumItems;
head = 0;
tail = 0;
}
ringIndex::~ringIndex(void) { }
// Write to this index, if not INDEX_ERR.
int ringIndex::addItem(void) {
int outIndex;
outIndex = INDEX_ERR;
if (!full()) {
outIndex = head;
head = increment(head);
}
return outIndex;
}
// Read at this index, if not INDEX_ERR.
int ringIndex::readItem(void) {
int outIndex;
outIndex = INDEX_ERR;
if (!empty()) {
outIndex = tail;
tail = increment(tail);
}
return outIndex;
}
// Are we empty?
bool ringIndex::empty(void) { return head == tail; }
// Are we full?
bool ringIndex::full(void) {
int anIndex;
anIndex = head;
anIndex = increment(anIndex);
return anIndex == tail;
}
// How many items do we have?
int ringIndex::itemCount(void) {
if (empty()) return 0;
if (full()) return maxItems();
if (head>tail) return head - tail;
return numItems - tail + head;
}
// How many items can we store?
int ringIndex::maxItems(void) { return numItems-1; }
// Reset to zero zero.
void ringIndex::flushItems(void) {
head = 0;
tail = 0;
}
// Increment a pointer. Head or tail.
int ringIndex::increment(int inIndex) {
inIndex++;
if (inIndex>=numItems) {
inIndex=0;
}
return inIndex;
}