CircularBuffer.sol 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0-rc.0) (utils/structs/CircularBuffer.sol)
  3. pragma solidity ^0.8.20;
  4. import {Math} from "../math/Math.sol";
  5. import {Arrays} from "../Arrays.sol";
  6. import {Panic} from "../Panic.sol";
  7. /**
  8. * @dev A fixed-size buffer for keeping `bytes32` items in storage.
  9. *
  10. * This data structure allows for pushing elements to it, and when its length exceeds the specified fixed size,
  11. * new items take the place of the oldest element in the buffer, keeping at most `N` elements in the
  12. * structure.
  13. *
  14. * Elements can't be removed but the data structure can be cleared. See {clear}.
  15. *
  16. * Complexity:
  17. * - insertion ({push}): O(1)
  18. * - lookup ({last}): O(1)
  19. * - inclusion ({includes}): O(N) (worst case)
  20. * - reset ({clear}): O(1)
  21. *
  22. * * The struct is called `Bytes32CircularBuffer`. Other types can be cast to and from `bytes32`. This data structure
  23. * can only be used in storage, and not in memory.
  24. *
  25. * Example usage:
  26. *
  27. * ```solidity
  28. * contract Example {
  29. * // Add the library methods
  30. * using CircularBuffer for CircularBuffer.Bytes32CircularBuffer;
  31. *
  32. * // Declare a buffer storage variable
  33. * CircularBuffer.Bytes32CircularBuffer private myBuffer;
  34. * }
  35. * ```
  36. */
  37. library CircularBuffer {
  38. /**
  39. * @dev Counts the number of items that have been pushed to the buffer. The residuo modulo _data.length indicates
  40. * where the next value should be stored.
  41. *
  42. * Struct members have an underscore prefix indicating that they are "private" and should not be read or written to
  43. * directly. Use the functions provided below instead. Modifying the struct manually may violate assumptions and
  44. * lead to unexpected behavior.
  45. *
  46. * The last item is at data[(index - 1) % data.length] and the last item is at data[index % data.length]. This
  47. * range can wrap around.
  48. */
  49. struct Bytes32CircularBuffer {
  50. uint256 _count;
  51. bytes32[] _data;
  52. }
  53. /**
  54. * @dev Initialize a new CircularBuffer of given size.
  55. *
  56. * If the CircularBuffer was already setup and used, calling that function again will reset it to a blank state.
  57. *
  58. * NOTE: The size of the buffer will affect the execution of {includes} function, as it has a complexity of O(N).
  59. * Consider a large buffer size may render the function unusable.
  60. */
  61. function setup(Bytes32CircularBuffer storage self, uint256 size) internal {
  62. clear(self);
  63. Arrays.unsafeSetLength(self._data, size);
  64. }
  65. /**
  66. * @dev Clear all data in the buffer without resetting memory, keeping the existing size.
  67. */
  68. function clear(Bytes32CircularBuffer storage self) internal {
  69. self._count = 0;
  70. }
  71. /**
  72. * @dev Push a new value to the buffer. If the buffer is already full, the new value replaces the oldest value in
  73. * the buffer.
  74. */
  75. function push(Bytes32CircularBuffer storage self, bytes32 value) internal {
  76. uint256 index = self._count++;
  77. uint256 modulus = self._data.length;
  78. Arrays.unsafeAccess(self._data, index % modulus).value = value;
  79. }
  80. /**
  81. * @dev Number of values currently in the buffer. This value is 0 for an empty buffer, and cannot exceed the size of
  82. * the buffer.
  83. */
  84. function count(Bytes32CircularBuffer storage self) internal view returns (uint256) {
  85. return Math.min(self._count, self._data.length);
  86. }
  87. /**
  88. * @dev Length of the buffer. This is the maximum number of elements kepts in the buffer.
  89. */
  90. function length(Bytes32CircularBuffer storage self) internal view returns (uint256) {
  91. return self._data.length;
  92. }
  93. /**
  94. * @dev Getter for the i-th value in the buffer, from the end.
  95. *
  96. * Reverts with {Panic-ARRAY_OUT_OF_BOUNDS} if trying to access an element that was not pushed, or that was
  97. * dropped to make room for newer elements.
  98. */
  99. function last(Bytes32CircularBuffer storage self, uint256 i) internal view returns (bytes32) {
  100. uint256 index = self._count;
  101. uint256 modulus = self._data.length;
  102. uint256 total = Math.min(index, modulus); // count(self)
  103. if (i >= total) {
  104. Panic.panic(Panic.ARRAY_OUT_OF_BOUNDS);
  105. }
  106. return Arrays.unsafeAccess(self._data, (index - i - 1) % modulus).value;
  107. }
  108. /**
  109. * @dev Check if a given value is in the buffer.
  110. */
  111. function includes(Bytes32CircularBuffer storage self, bytes32 value) internal view returns (bool) {
  112. uint256 index = self._count;
  113. uint256 modulus = self._data.length;
  114. uint256 total = Math.min(index, modulus); // count(self)
  115. for (uint256 i = 0; i < total; ++i) {
  116. if (Arrays.unsafeAccess(self._data, (index - i - 1) % modulus).value == value) {
  117. return true;
  118. }
  119. }
  120. return false;
  121. }
  122. }