ring_buffer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #ifndef RING_BUFFER_H
  31. #define RING_BUFFER_H
  32. #include "core/templates/vector.h"
  33. template <typename T>
  34. class RingBuffer {
  35. Vector<T> data;
  36. int read_pos = 0;
  37. int write_pos = 0;
  38. int size_mask;
  39. inline int inc(int &p_var, int p_size) const {
  40. int ret = p_var;
  41. p_var += p_size;
  42. p_var = p_var & size_mask;
  43. return ret;
  44. }
  45. public:
  46. T read() {
  47. ERR_FAIL_COND_V(space_left() < 1, T());
  48. return data.ptr()[inc(read_pos, 1)];
  49. }
  50. int read(T *p_buf, int p_size, bool p_advance = true) {
  51. int left = data_left();
  52. p_size = MIN(left, p_size);
  53. int pos = read_pos;
  54. int to_read = p_size;
  55. int dst = 0;
  56. while (to_read) {
  57. int end = pos + to_read;
  58. end = MIN(end, size());
  59. int total = end - pos;
  60. const T *read = data.ptr();
  61. for (int i = 0; i < total; i++) {
  62. p_buf[dst++] = read[pos + i];
  63. }
  64. to_read -= total;
  65. pos = 0;
  66. }
  67. if (p_advance) {
  68. inc(read_pos, p_size);
  69. }
  70. return p_size;
  71. }
  72. int copy(T *p_buf, int p_offset, int p_size) const {
  73. int left = data_left();
  74. if ((p_offset + p_size) > left) {
  75. p_size -= left - p_offset;
  76. if (p_size <= 0) {
  77. return 0;
  78. }
  79. }
  80. p_size = MIN(left, p_size);
  81. int pos = read_pos;
  82. inc(pos, p_offset);
  83. int to_read = p_size;
  84. int dst = 0;
  85. while (to_read) {
  86. int end = pos + to_read;
  87. end = MIN(end, size());
  88. int total = end - pos;
  89. for (int i = 0; i < total; i++) {
  90. p_buf[dst++] = data[pos + i];
  91. }
  92. to_read -= total;
  93. pos = 0;
  94. }
  95. return p_size;
  96. }
  97. int find(const T &t, int p_offset, int p_max_size) const {
  98. int left = data_left();
  99. if ((p_offset + p_max_size) > left) {
  100. p_max_size -= left - p_offset;
  101. if (p_max_size <= 0) {
  102. return 0;
  103. }
  104. }
  105. p_max_size = MIN(left, p_max_size);
  106. int pos = read_pos;
  107. inc(pos, p_offset);
  108. int to_read = p_max_size;
  109. while (to_read) {
  110. int end = pos + to_read;
  111. end = MIN(end, size());
  112. int total = end - pos;
  113. for (int i = 0; i < total; i++) {
  114. if (data[pos + i] == t) {
  115. return i + (p_max_size - to_read);
  116. }
  117. }
  118. to_read -= total;
  119. pos = 0;
  120. }
  121. return -1;
  122. }
  123. inline int advance_read(int p_n) {
  124. p_n = MIN(p_n, data_left());
  125. inc(read_pos, p_n);
  126. return p_n;
  127. }
  128. inline int decrease_write(int p_n) {
  129. p_n = MIN(p_n, data_left());
  130. inc(write_pos, size_mask + 1 - p_n);
  131. return p_n;
  132. }
  133. Error write(const T &p_v) {
  134. ERR_FAIL_COND_V(space_left() < 1, FAILED);
  135. data.write[inc(write_pos, 1)] = p_v;
  136. return OK;
  137. }
  138. int write(const T *p_buf, int p_size) {
  139. int left = space_left();
  140. p_size = MIN(left, p_size);
  141. int pos = write_pos;
  142. int to_write = p_size;
  143. int src = 0;
  144. while (to_write) {
  145. int end = pos + to_write;
  146. end = MIN(end, size());
  147. int total = end - pos;
  148. for (int i = 0; i < total; i++) {
  149. data.write[pos + i] = p_buf[src++];
  150. }
  151. to_write -= total;
  152. pos = 0;
  153. }
  154. inc(write_pos, p_size);
  155. return p_size;
  156. }
  157. inline int space_left() const {
  158. int left = read_pos - write_pos;
  159. if (left < 0) {
  160. return size() + left - 1;
  161. }
  162. if (left == 0) {
  163. return size() - 1;
  164. }
  165. return left - 1;
  166. }
  167. inline int data_left() const {
  168. return size() - space_left() - 1;
  169. }
  170. inline int size() const {
  171. return data.size();
  172. }
  173. inline void clear() {
  174. read_pos = 0;
  175. write_pos = 0;
  176. }
  177. void resize(int p_power) {
  178. int old_size = size();
  179. int new_size = 1 << p_power;
  180. int mask = new_size - 1;
  181. data.resize(int64_t(1) << int64_t(p_power));
  182. if (old_size < new_size && read_pos > write_pos) {
  183. for (int i = 0; i < write_pos; i++) {
  184. data.write[(old_size + i) & mask] = data[i];
  185. }
  186. write_pos = (old_size + write_pos) & mask;
  187. } else {
  188. read_pos = read_pos & mask;
  189. write_pos = write_pos & mask;
  190. }
  191. size_mask = mask;
  192. }
  193. RingBuffer(int p_power = 0) {
  194. resize(p_power);
  195. }
  196. ~RingBuffer() {}
  197. };
  198. #endif // RING_BUFFER_H