forked from pixelmatix/SmartMatrix
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularBuffer.cpp
More file actions
Latest commit
45 lines (35 loc) · 1012 Bytes
/
Copy pathCircularBuffer.cpp
File metadata and controls
45 lines (35 loc) · 1012 Bytes
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
/* CircularBuffer.cpp
modified from example code from Wikipedia:
http://en.wikipedia.org/wiki/Circular_queue#Use_a_Fill_Count
modified to only contain indexes and not any data elements, and allow for peeking at the next read/write index
*/
#include"CircularBuffer.h"
voidcbInit(CircularBuffer *cb, int size) {
cb->size = size;
cb->start = 0;
cb->count = 0;
}
/* below from fill count mods */
intcbIsFull(CircularBuffer *cb) {
return cb->count == cb->size;
}
intcbIsEmpty(CircularBuffer *cb) {
return cb->count == 0;
}
// returns index of next free element
intcbGetNextWrite(CircularBuffer *cb) {
return (cb->start + cb->count) % cb->size;
}
voidcbRead(CircularBuffer *cb) {
cb->start = (cb->start + 1) % cb->size;
-- cb->count;
}
voidcbWrite(CircularBuffer *cb) {
if (cb->count == cb->size)
cb->start = (cb->start + 1) % cb->size; /* full, overwrite */
else
++ cb->count;
}
intcbGetNextRead(CircularBuffer *cb) {
return cb->start;
}