ring_buffer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* ring_buffer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef RINGBUFFER_H
  30. #define RINGBUFFER_H
  31. #include "vector.h"
  32. template <typename T>
  33. class RingBuffer {
  34. Vector<T> data;
  35. int read_pos;
  36. int write_pos;
  37. int size_mask;
  38. inline int inc(int& p_var, int p_size) {
  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[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. for (int i=0; i<total; i++) {
  60. p_buf[dst++] = data[pos + i];
  61. };
  62. to_read -= total;
  63. pos = 0;
  64. };
  65. if (p_advance) {
  66. inc(read_pos, p_size);
  67. };
  68. return p_size;
  69. };
  70. int copy(T* p_buf, int p_offset, int p_size) {
  71. int left = data_left();
  72. if ((p_offset+p_size)>left) {
  73. p_size-=left-p_offset;
  74. if (p_size<=0)
  75. return 0;
  76. }
  77. p_size = MIN(left, p_size);
  78. int pos = read_pos;
  79. inc(pos,p_offset);
  80. int to_read = p_size;
  81. int dst = 0;
  82. while(to_read) {
  83. int end = pos + to_read;
  84. end = MIN(end, size());
  85. int total = end - pos;
  86. for (int i=0; i<total; i++) {
  87. p_buf[dst++] = data[pos + i];
  88. };
  89. to_read -= total;
  90. pos = 0;
  91. };
  92. return p_size;
  93. };
  94. inline int advance_read(int p_n) {
  95. p_n = MIN(p_n, data_left());
  96. inc(read_pos, p_n);
  97. return p_n;
  98. };
  99. Error write(const T& p_v) {
  100. ERR_FAIL_COND_V( space_left() < 1, FAILED);
  101. data[inc(write_pos, 1)] = p_v;
  102. return OK;
  103. };
  104. int write(const T* p_buf, int p_size) {
  105. int left = space_left();
  106. p_size = MIN(left, p_size);
  107. int pos = write_pos;
  108. int to_write = p_size;
  109. int src = 0;
  110. while (to_write) {
  111. int end = pos + to_write;
  112. end = MIN(end, size());
  113. int total = end - pos;
  114. for (int i=0; i<total; i++) {
  115. data[pos+i] = p_buf[src++];
  116. };
  117. to_write -= total;
  118. pos = 0;
  119. };
  120. inc(write_pos, p_size);
  121. return p_size;
  122. };
  123. inline int space_left() {
  124. int left = read_pos - write_pos;
  125. if (left < 0) {
  126. return size() + left;
  127. };
  128. if (left == 0) {
  129. return size();
  130. };
  131. return left;
  132. };
  133. inline int data_left() {
  134. return size() - space_left();
  135. };
  136. inline int size() {
  137. return data.size();
  138. };
  139. void resize(int p_power) {
  140. int old_size = size();
  141. int new_size = 1<<p_power;
  142. int mask = new_size - 1;
  143. data.resize(1<<p_power);
  144. if (old_size < new_size && read_pos > write_pos) {
  145. for (int i=0; i<write_pos; i++) {
  146. data[(old_size + i)&mask] = data[i];
  147. };
  148. write_pos = (old_size + write_pos) & mask;
  149. } else {
  150. read_pos = read_pos & mask;
  151. write_pos = write_pos & mask;
  152. };
  153. size_mask = mask;
  154. };
  155. RingBuffer<T>(int p_power=0) {
  156. read_pos = 0;
  157. write_pos = 0;
  158. resize(p_power);
  159. };
  160. ~RingBuffer<T>() {};
  161. };
  162. #endif