DoubleEndedQueue.sol 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.4;
  3. import "../math/SafeCast.sol";
  4. /**
  5. * @dev A sequence of items with the ability to efficiently push and pop items (i.e. insert and remove) on both ends of
  6. * the sequence (called front and back). Among other access patterns, it can be used to implement efficient LIFO and
  7. * FIFO queues. Storage use is optimized, and all operations are O(1) constant time. This includes {clear}, given that
  8. * the existing queue contents are left in storage.
  9. *
  10. * The struct is called `Bytes32Deque`. Other types can be cast to and from `bytes32`. This data structure can only be
  11. * used in storage, and not in memory.
  12. * ```
  13. * DoubleEndedQueue.Bytes32Deque queue;
  14. * ```
  15. *
  16. * _Available since v4.6._
  17. */
  18. library DoubleEndedQueue {
  19. /**
  20. * @dev An operation (e.g. {front}) couldn't be completed due to the queue being empty.
  21. */
  22. error Empty();
  23. /**
  24. * @dev An operation (e.g. {at}) couldn't be completed due to an index being out of bounds.
  25. */
  26. error OutOfBounds();
  27. /**
  28. * @dev Indices are signed integers because the queue can grow in any direction. They are 128 bits so begin and end
  29. * are packed in a single storage slot for efficient access. Since the items are added one at a time we can safely
  30. * assume that these 128-bit indices will not overflow, and use unchecked arithmetic.
  31. *
  32. * Struct members have an underscore prefix indicating that they are "private" and should not be read or written to
  33. * directly. Use the functions provided below instead. Modifying the struct manually may violate assumptions and
  34. * lead to unexpected behavior.
  35. *
  36. * Indices are in the range [begin, end) which means the first item is at data[begin] and the last item is at
  37. * data[end - 1].
  38. */
  39. struct Bytes32Deque {
  40. int128 _begin;
  41. int128 _end;
  42. mapping(int128 => bytes32) _data;
  43. }
  44. /**
  45. * @dev Inserts an item at the end of the queue.
  46. */
  47. function pushBack(Bytes32Deque storage deque, bytes32 value) internal {
  48. int128 backIndex = deque._end;
  49. deque._data[backIndex] = value;
  50. unchecked {
  51. deque._end = backIndex + 1;
  52. }
  53. }
  54. /**
  55. * @dev Removes the item at the end of the queue and returns it.
  56. *
  57. * Reverts with `Empty` if the queue is empty.
  58. */
  59. function popBack(Bytes32Deque storage deque) internal returns (bytes32 value) {
  60. if (empty(deque)) revert Empty();
  61. int128 backIndex;
  62. unchecked {
  63. backIndex = deque._end - 1;
  64. }
  65. value = deque._data[backIndex];
  66. delete deque._data[backIndex];
  67. deque._end = backIndex;
  68. }
  69. /**
  70. * @dev Inserts an item at the beginning of the queue.
  71. */
  72. function pushFront(Bytes32Deque storage deque, bytes32 value) internal {
  73. int128 frontIndex;
  74. unchecked {
  75. frontIndex = deque._begin - 1;
  76. }
  77. deque._data[frontIndex] = value;
  78. deque._begin = frontIndex;
  79. }
  80. /**
  81. * @dev Removes the item at the beginning of the queue and returns it.
  82. *
  83. * Reverts with `Empty` if the queue is empty.
  84. */
  85. function popFront(Bytes32Deque storage deque) internal returns (bytes32 value) {
  86. if (empty(deque)) revert Empty();
  87. int128 frontIndex = deque._begin;
  88. value = deque._data[frontIndex];
  89. delete deque._data[frontIndex];
  90. unchecked {
  91. deque._begin = frontIndex + 1;
  92. }
  93. }
  94. /**
  95. * @dev Returns the item at the beginning of the queue.
  96. *
  97. * Reverts with `Empty` if the queue is empty.
  98. */
  99. function front(Bytes32Deque storage deque) internal view returns (bytes32 value) {
  100. if (empty(deque)) revert Empty();
  101. int128 frontIndex = deque._begin;
  102. return deque._data[frontIndex];
  103. }
  104. /**
  105. * @dev Returns the item at the end of the queue.
  106. *
  107. * Reverts with `Empty` if the queue is empty.
  108. */
  109. function back(Bytes32Deque storage deque) internal view returns (bytes32 value) {
  110. if (empty(deque)) revert Empty();
  111. int128 backIndex;
  112. unchecked {
  113. backIndex = deque._end - 1;
  114. }
  115. return deque._data[backIndex];
  116. }
  117. /**
  118. * @dev Return the item at a position in the queue given by `index`, with the first item at 0 and last item at
  119. * `length(deque) - 1`.
  120. *
  121. * Reverts with `OutOfBounds` if the index is out of bounds.
  122. */
  123. function at(Bytes32Deque storage deque, uint256 index) internal view returns (bytes32 value) {
  124. // int256(deque._begin) is a safe upcast
  125. int128 idx = SafeCast.toInt128(int256(deque._begin) + SafeCast.toInt256(index));
  126. if (idx >= deque._end) revert OutOfBounds();
  127. return deque._data[idx];
  128. }
  129. /**
  130. * @dev Resets the queue back to being empty.
  131. *
  132. * NOTE: The current items are left behind in storage. This does not affect the functioning of the queue, but misses
  133. * out on potential gas refunds.
  134. */
  135. function clear(Bytes32Deque storage deque) internal {
  136. deque._begin = 0;
  137. deque._end = 0;
  138. }
  139. /**
  140. * @dev Returns the number of items in the queue.
  141. */
  142. function length(Bytes32Deque storage deque) internal view returns (uint256) {
  143. // The interface preserves the invariant that begin <= end so we assume this will not overflow.
  144. // We also assume there are at most int256.max items in the queue.
  145. unchecked {
  146. return uint256(int256(deque._end) - int256(deque._begin));
  147. }
  148. }
  149. /**
  150. * @dev Returns true if the queue is empty.
  151. */
  152. function empty(Bytes32Deque storage deque) internal view returns (bool) {
  153. return deque._end <= deque._begin;
  154. }
  155. }