ring_buffer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*************************************************************************/
  2. /* ring_buffer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "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) {
  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[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. for (int i = 0; i < total; i++) {
  61. p_buf[dst++] = data[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) {
  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. p_size = MIN(left, p_size);
  79. int pos = read_pos;
  80. inc(pos, p_offset);
  81. int to_read = p_size;
  82. int dst = 0;
  83. while (to_read) {
  84. int end = pos + to_read;
  85. end = MIN(end, size());
  86. int total = end - pos;
  87. for (int i = 0; i < total; i++) {
  88. p_buf[dst++] = data[pos + i];
  89. };
  90. to_read -= total;
  91. pos = 0;
  92. };
  93. return p_size;
  94. };
  95. inline int advance_read(int p_n) {
  96. p_n = MIN(p_n, data_left());
  97. inc(read_pos, p_n);
  98. return p_n;
  99. };
  100. Error write(const T &p_v) {
  101. ERR_FAIL_COND_V(space_left() < 1, FAILED);
  102. data[inc(write_pos, 1)] = p_v;
  103. return OK;
  104. };
  105. int write(const T *p_buf, int p_size) {
  106. int left = space_left();
  107. p_size = MIN(left, p_size);
  108. int pos = write_pos;
  109. int to_write = p_size;
  110. int src = 0;
  111. while (to_write) {
  112. int end = pos + to_write;
  113. end = MIN(end, size());
  114. int total = end - pos;
  115. for (int i = 0; i < total; i++) {
  116. data[pos + i] = p_buf[src++];
  117. };
  118. to_write -= total;
  119. pos = 0;
  120. };
  121. inc(write_pos, p_size);
  122. return p_size;
  123. };
  124. inline int space_left() {
  125. int left = read_pos - write_pos;
  126. if (left < 0) {
  127. return size() + left - 1;
  128. };
  129. if (left == 0) {
  130. return size() - 1;
  131. };
  132. return left - 1;
  133. };
  134. inline int data_left() {
  135. return size() - space_left() - 1;
  136. };
  137. inline int size() {
  138. return data.size();
  139. };
  140. inline void clear() {
  141. read_pos = 0;
  142. write_pos = 0;
  143. }
  144. void resize(int p_power) {
  145. int old_size = size();
  146. int new_size = 1 << p_power;
  147. int mask = new_size - 1;
  148. data.resize(1 << p_power);
  149. if (old_size < new_size && read_pos > write_pos) {
  150. for (int i = 0; i < write_pos; i++) {
  151. data[(old_size + i) & mask] = data[i];
  152. };
  153. write_pos = (old_size + write_pos) & mask;
  154. } else {
  155. read_pos = read_pos & mask;
  156. write_pos = write_pos & mask;
  157. };
  158. size_mask = mask;
  159. };
  160. RingBuffer<T>(int p_power = 0) {
  161. read_pos = 0;
  162. write_pos = 0;
  163. resize(p_power);
  164. };
  165. ~RingBuffer<T>(){};
  166. };
  167. #endif