forked from uli/cascade
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathring.h
More file actions
74 lines (65 loc) · 1.17 KB
/
ring.h
File metadata and controls
74 lines (65 loc) · 1.17 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
/*
* ring.h
*
* (C) Copyright 2014 Ulrich Hecht
*
* This file is part of CASCADE. CASCADE is almost free software; you can
* redistribute it and/or modify it under the terms of the Cascade Public
* License 1.0. Read the file "LICENSE" for details.
*/
#ifndef _RING_H
#define _RING_H
#include "state.h"
template <class T>
class Ring {
public:
Ring(int s) {
size = s;
start = end = 0;
ring = new T[size];
}
~Ring() {
delete ring;
}
void add(T data) {
ring[end] = data;
end = (end + 1) % size;
}
void prepend(T data) {
if (!start)
start = size - 1;
else
start--;
ring[start] = data;
}
T consume() {
T ret = ring[start];
start = (start + 1) % size;
return ret;
}
T snoop() {
return ring[start];
}
void flush() {
start = end;
}
bool empty() {
return start == end;
}
int count() {
if (start > end)
return size - end + start;
else
return end - start;
}
void loadSaveState(statefile_t fp, bool write) {
STATE_RW(start);
STATE_RW(end);
STATE_RWBUF(ring, sizeof(T) * size);
}
private:
T *ring;
int size;
int start, end;
};
#endif