ring_buffer.h 5.8 KB

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