ring_buffer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*************************************************************************/
  2. /* ring_buffer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 RINGBUFFER_H
  31. #define RINGBUFFER_H
  32. #include "core/vector.h"
  33. template <typename T>
  34. class RingBuffer {
  35. Vector<T> data;
  36. int read_pos;
  37. int write_pos;
  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. 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. p_max_size = MIN(left, p_max_size);
  104. int pos = read_pos;
  105. inc(pos, p_offset);
  106. int to_read = p_max_size;
  107. while (to_read) {
  108. int end = pos + to_read;
  109. end = MIN(end, size());
  110. int total = end - pos;
  111. for (int i = 0; i < total; i++) {
  112. if (data[pos + i] == t)
  113. return i + (p_max_size - to_read);
  114. };
  115. to_read -= total;
  116. pos = 0;
  117. }
  118. return -1;
  119. }
  120. inline int advance_read(int p_n) {
  121. p_n = MIN(p_n, data_left());
  122. inc(read_pos, p_n);
  123. return p_n;
  124. };
  125. inline int decrease_write(int p_n) {
  126. p_n = MIN(p_n, data_left());
  127. inc(write_pos, size_mask + 1 - p_n);
  128. return p_n;
  129. }
  130. Error write(const T &p_v) {
  131. ERR_FAIL_COND_V(space_left() < 1, FAILED);
  132. data.write[inc(write_pos, 1)] = p_v;
  133. return OK;
  134. };
  135. int write(const T *p_buf, int p_size) {
  136. int left = space_left();
  137. p_size = MIN(left, p_size);
  138. int pos = write_pos;
  139. int to_write = p_size;
  140. int src = 0;
  141. while (to_write) {
  142. int end = pos + to_write;
  143. end = MIN(end, size());
  144. int total = end - pos;
  145. for (int i = 0; i < total; i++) {
  146. data.write[pos + i] = p_buf[src++];
  147. };
  148. to_write -= total;
  149. pos = 0;
  150. };
  151. inc(write_pos, p_size);
  152. return p_size;
  153. };
  154. inline int space_left() const {
  155. int left = read_pos - write_pos;
  156. if (left < 0) {
  157. return size() + left - 1;
  158. };
  159. if (left == 0) {
  160. return size() - 1;
  161. };
  162. return left - 1;
  163. };
  164. inline int data_left() const {
  165. return size() - space_left() - 1;
  166. };
  167. inline int size() const {
  168. return data.size();
  169. };
  170. inline void clear() {
  171. read_pos = 0;
  172. write_pos = 0;
  173. }
  174. void resize(int p_power) {
  175. int old_size = size();
  176. int new_size = 1 << p_power;
  177. int mask = new_size - 1;
  178. data.resize(1 << p_power);
  179. if (old_size < new_size && read_pos > write_pos) {
  180. for (int i = 0; i < write_pos; i++) {
  181. data.write[(old_size + i) & mask] = data[i];
  182. };
  183. write_pos = (old_size + write_pos) & mask;
  184. } else {
  185. read_pos = read_pos & mask;
  186. write_pos = write_pos & mask;
  187. };
  188. size_mask = mask;
  189. };
  190. RingBuffer(int p_power = 0) {
  191. read_pos = 0;
  192. write_pos = 0;
  193. resize(p_power);
  194. };
  195. ~RingBuffer() {}
  196. };
  197. #endif