You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
3.3KB

  1. #pragma once
  2. #include <string.h>
  3. #include "math.hpp"
  4. namespace rack {
  5. /** A simple cyclic buffer.
  6. S must be a power of 2.
  7. push() is constant time O(1)
  8. */
  9. template <typename T, int S>
  10. struct RingBuffer {
  11. T data[S];
  12. int start = 0;
  13. int end = 0;
  14. int mask(int i) const {
  15. return i & (S - 1);
  16. }
  17. void push(T t) {
  18. int i = mask(end++);
  19. data[i] = t;
  20. }
  21. T shift() {
  22. return data[mask(start++)];
  23. }
  24. void clear() {
  25. start = end;
  26. }
  27. bool empty() const {
  28. return start >= end;
  29. }
  30. bool full() const {
  31. return end - start >= S;
  32. }
  33. int size() const {
  34. return end - start;
  35. }
  36. int capacity() const {
  37. return S - size();
  38. }
  39. };
  40. /** A cyclic buffer which maintains a valid linear array of size S by keeping a copy of the buffer in adjacent memory.
  41. S must be a power of 2.
  42. push() is constant time O(2) relative to RingBuffer
  43. */
  44. template <typename T, int S>
  45. struct DoubleRingBuffer {
  46. T data[S*2];
  47. int start = 0;
  48. int end = 0;
  49. int mask(int i) const {
  50. return i & (S - 1);
  51. }
  52. void push(T t) {
  53. int i = mask(end++);
  54. data[i] = t;
  55. data[i + S] = t;
  56. }
  57. T shift() {
  58. return data[mask(start++)];
  59. }
  60. void clear() {
  61. start = end;
  62. }
  63. bool empty() const {
  64. return start >= end;
  65. }
  66. bool full() const {
  67. return end - start >= S;
  68. }
  69. int size() const {
  70. return end - start;
  71. }
  72. int capacity() const {
  73. return S - size();
  74. }
  75. /** Returns a pointer to S consecutive elements for appending.
  76. If any data is appended, you must call endIncr afterwards.
  77. Pointer is invalidated when any other method is called.
  78. */
  79. T *endData() {
  80. return &data[mask(end)];
  81. }
  82. void endIncr(int n) {
  83. int e = mask(end);
  84. int e1 = e + n;
  85. int e2 = mini(e1, S);
  86. // Copy data forward
  87. memcpy(data + S + e, data + e, sizeof(T) * (e2 - e));
  88. if (e1 > S) {
  89. // Copy data backward from the doubled block to the main block
  90. memcpy(data, data + S, sizeof(T) * (e1 - S));
  91. }
  92. end += n;
  93. }
  94. /** Returns a pointer to S consecutive elements for consumption
  95. If any data is consumed, call startIncr afterwards.
  96. */
  97. const T *startData() const {
  98. return &data[mask(start)];
  99. }
  100. void startIncr(int n) {
  101. start += n;
  102. }
  103. };
  104. /** A cyclic buffer which maintains a valid linear array of size S by sliding along a larger block of size N.
  105. The linear array of S elements are moved back to the start of the block once it outgrows past the end.
  106. This happens every N - S pushes, so the push() time is O(1 + S / (N - S)).
  107. For example, a float buffer of size 64 in a block of size 1024 is nearly as efficient as RingBuffer.
  108. */
  109. template <typename T, size_t S, size_t N>
  110. struct AppleRingBuffer {
  111. T data[N];
  112. size_t start = 0;
  113. size_t end = 0;
  114. void push(T t) {
  115. data[end++] = t;
  116. if (end >= N) {
  117. // move end block to beginning
  118. memmove(data, &data[N - S], sizeof(T) * S);
  119. start -= N - S;
  120. end = S;
  121. }
  122. }
  123. T shift() {
  124. return data[start++];
  125. }
  126. bool empty() const {
  127. return start >= end;
  128. }
  129. bool full() const {
  130. return end - start >= S;
  131. }
  132. size_t size() const {
  133. return end - start;
  134. }
  135. /** Returns a pointer to S consecutive elements for appending, requesting to append n elements.
  136. */
  137. T *endData(size_t n) {
  138. // TODO
  139. return &data[end];
  140. }
  141. /** Returns a pointer to S consecutive elements for consumption
  142. If any data is consumed, call startIncr afterwards.
  143. */
  144. const T *startData() const {
  145. return &data[start];
  146. }
  147. void startIncr(size_t n) {
  148. // This is valid as long as n < S
  149. start += n;
  150. }
  151. };
  152. } // namespace rack