Debian Codegraph @

core/templates/ring_buffer.h

raw symbols SCIP
1 /**************************************************************************/
2 /* ring_buffer.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/templates/local_vector.h"
34
35 template <typename T>
36 class RingBuffer {
37 LocalVector<T> data;
38 int read_pos = 0;
39 int write_pos = 0;
40 int size_mask;
41
42 inline int inc(int &p_var, int p_size) const {
43 int ret = p_var;
44 p_var += p_size;
45 p_var = p_var & size_mask;
46 return ret;
47 }
48
49 public:
50 T read() {
51 ERR_FAIL_COND_V(data_left() < 1, T());
52 return data.ptr()[inc(read_pos, 1)];
53 }
54
55 int read(T *p_buf, int p_size, bool p_advance = true) {
56 int left = data_left();
57 p_size = MIN(left, p_size);
58 int pos = read_pos;
59 int to_read = p_size;
60 int dst = 0;
61 while (to_read) {
62 int end = pos + to_read;
63 end = MIN(end, size());
64 int total = end - pos;
65 const T *read = data.ptr();
66 for (int i = 0; i < total; i++) {
67 p_buf[dst++] = read[pos + i];
68 }
69 to_read -= total;
70 pos = 0;
71 }
72 if (p_advance) {
73 inc(read_pos, p_size);
74 }
75 return p_size;
76 }
77
78 int copy(T *p_buf, int p_offset, int p_size) const {
79 int left = data_left();
80 if ((p_offset + p_size) > left) {
81 p_size = left - p_offset;
82 if (p_size <= 0) {
83 return 0;
84 }
85 }
86 p_size = MIN(left, p_size);
87 int pos = read_pos;
88 inc(pos, p_offset);
89 int to_read = p_size;
90 int dst = 0;
91 while (to_read) {
92 int end = pos + to_read;
93 end = MIN(end, size());
94 int total = end - pos;
95 for (int i = 0; i < total; i++) {
96 p_buf[dst++] = data[pos + i];
97 }
98 to_read -= total;
99 pos = 0;
100 }
101 return p_size;
102 }
103
104 int find(const T &t, int p_offset, int p_max_size) const {
105 int left = data_left();
106 if ((p_offset + p_max_size) > left) {
107 p_max_size = left - p_offset;
108 if (p_max_size <= 0) {
109 return 0;
110 }
111 }
112 p_max_size = MIN(left, p_max_size);
113 int pos = read_pos;
114 inc(pos, p_offset);
115 int to_read = p_max_size;
116 while (to_read) {
117 int end = pos + to_read;
118 end = MIN(end, size());
119 int total = end - pos;
120 for (int i = 0; i < total; i++) {
121 if (data[pos + i] == t) {
122 return i + (p_max_size - to_read);
123 }
124 }
125 to_read -= total;
126 pos = 0;
127 }
128 return -1;
129 }
130
131 inline int advance_read(int p_n) {
132 p_n = MIN(p_n, data_left());
133 inc(read_pos, p_n);
134 return p_n;
135 }
136
137 inline int decrease_write(int p_n) {
138 p_n = MIN(p_n, data_left());
139 inc(write_pos, size_mask + 1 - p_n);
140 return p_n;
141 }
142
143 Error write(const T &p_v) {
144 ERR_FAIL_COND_V(space_left() < 1, FAILED);
145 data[inc(write_pos, 1)] = p_v;
146 return OK;
147 }
148
149 int write(const T *p_buf, int p_size) {
150 int left = space_left();
151 p_size = MIN(left, p_size);
152
153 int pos = write_pos;
154 int to_write = p_size;
155 int src = 0;
156 while (to_write) {
157 int end = pos + to_write;
158 end = MIN(end, size());
159 int total = end - pos;
160
161 for (int i = 0; i < total; i++) {
162 data[pos + i] = p_buf[src++];
163 }
164 to_write -= total;
165 pos = 0;
166 }
167
168 inc(write_pos, p_size);
169 return p_size;
170 }
171
172 inline int space_left() const {
173 int left = read_pos - write_pos;
174 if (left < 0) {
175 return size() + left - 1;
176 }
177 if (left == 0) {
178 return size() - 1;
179 }
180 return left - 1;
181 }
182 inline int data_left() const {
183 return size() - space_left() - 1;
184 }
185
186 inline int size() const {
187 return data.size();
188 }
189
190 inline void clear() {
191 read_pos = 0;
192 write_pos = 0;
193 }
194
195 void resize(int p_power) {
196 int old_size = size();
197 int new_size = 1 << p_power;
198 int mask = new_size - 1;
199 data.resize(int64_t(1) << int64_t(p_power));
200 if (old_size < new_size && read_pos > write_pos) {
201 for (int i = 0; i < write_pos; i++) {
202 data[(old_size + i) & mask] = data[i];
203 }
204 write_pos = (old_size + write_pos) & mask;
205 } else {
206 read_pos = read_pos & mask;
207 write_pos = write_pos & mask;
208 }
209
210 size_mask = mask;
211 }
212
213 RingBuffer(int p_power = 0) {
214 resize(p_power);
215 }
216 };