forked from ange-yaghi/engine-sim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathring_buffer.h
More file actions
151 lines (128 loc) · 3.65 KB
/
ring_buffer.h
File metadata and controls
151 lines (128 loc) · 3.65 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifndef ATG_ENGINE_SIM_RING_BUFFER_H
#define ATG_ENGINE_SIM_RING_BUFFER_H
#include "part.h"
#include <cstring>
template <typename T_Data>
class RingBuffer {
public:
RingBuffer() {
m_buffer = nullptr;
m_capacity = 0;
m_writeIndex = 0;
m_start = 0;
}
~RingBuffer() {
destroy();
}
void initialize(size_t capacity) {
m_buffer = new T_Data[capacity];
m_capacity = capacity;
m_writeIndex = 0;
m_start = 0;
}
void destroy() {
if (m_buffer != nullptr) {
delete[] m_buffer;
m_buffer = nullptr;
}
m_capacity = 0;
m_writeIndex = 0;
m_start = 0;
}
inline void write(T_Data data) {
m_buffer[m_writeIndex] = data;
if (++m_writeIndex >= m_capacity) {
m_writeIndex = 0;
}
}
inline void overwrite(T_Data data, size_t index) {
if (start + index < m_capacity) {
m_buffer[m_start + index] = data;
}
else {
m_buffer[m_start + index - m_capacity] = data;
}
}
inline size_t index(size_t base, int offset) {
if (offset == 0) return base;
else if (offset < 0) {
const size_t offset_u = -offset;
if (offset_u <= base) return base - offset_u;
else return (base + m_capacity) - offset_u;
}
else {
const size_t offset_u = offset;
const size_t rawOffset = base + offset_u;
if (rawOffset >= m_capacity) return rawOffset - m_capacity;
else return rawOffset;
}
}
inline T_Data read(size_t index) const {
return (m_start + index) >= m_capacity
? m_buffer[m_start + index - m_capacity]
: m_buffer[m_start + index];
}
inline void read(size_t n, T_Data *target) {
if (m_start + n < m_capacity) {
memcpy(target, m_buffer + m_start, n * sizeof(T_Data));
}
else {
memcpy(
target,
m_buffer + m_start,
(m_capacity - m_start) * sizeof(T_Data));
memcpy(
target + (m_capacity - m_start),
m_buffer,
(n - (m_capacity - m_start)) * sizeof(T_Data));
}
}
inline void readAndRemove(size_t n, T_Data *target) {
if (m_start + n < m_capacity) {
memcpy(target, m_buffer + m_start, n * sizeof(T_Data));
}
else {
memcpy(
target,
m_buffer + m_start,
(m_capacity - m_start) * sizeof(T_Data));
memcpy(
target + (m_capacity - m_start),
m_buffer,
(n - (m_capacity - m_start)) * sizeof(T_Data));
}
m_start += n;
if (m_start >= m_capacity) {
m_start -= m_capacity;
}
}
inline void setWriteIndex(size_t writeIndex) {
m_writeIndex = writeIndex;
}
inline void removeBeginning(size_t n) {
m_start += n;
if (m_start >= m_capacity) {
m_start -= m_capacity;
}
}
inline void setStartIndex(size_t startIndex) {
m_start = startIndex;
}
inline size_t size() const {
return (m_writeIndex < m_start)
? m_writeIndex + (m_capacity - m_start)
: m_writeIndex - m_start;
}
inline size_t writeIndex() const {
return m_writeIndex;
}
inline size_t start() const {
return m_start;
}
private:
T_Data *m_buffer;
size_t m_capacity;
size_t m_writeIndex;
size_t m_start;
};
#endif /* ATG_ENGINE_SIM_RING_BUFFER_H */