22// OpenZeppelin Contracts (last updated v5.0.0) (utils/structs/DoubleEndedQueue.sol)
33pragma solidity ^ 0.8.20 ;
44
5+ import {Panic} from "../Panic.sol " ;
6+
57/**
68 * @dev A sequence of items with the ability to efficiently push and pop items (i.e. insert and remove) on both ends of
79 * the sequence (called front and back). Among other access patterns, it can be used to implement efficient LIFO and
@@ -15,21 +17,6 @@ pragma solidity ^0.8.20;
1517 * ```
1618 */
1719library DoubleEndedQueue {
18- /**
19- * @dev An operation (e.g. {front}) couldn't be completed due to the queue being empty.
20- */
21- error QueueEmpty ();
22-
23- /**
24- * @dev A push operation couldn't be completed due to the queue being full.
25- */
26- error QueueFull ();
27-
28- /**
29- * @dev An operation (e.g. {at}) couldn't be completed due to an index being out of bounds.
30- */
31- error QueueOutOfBounds ();
32-
3320 /**
3421 * @dev Indices are 128 bits so begin and end are packed in a single storage slot for efficient access.
3522 *
@@ -48,12 +35,12 @@ library DoubleEndedQueue {
4835 /**
4936 * @dev Inserts an item at the end of the queue.
5037 *
51- * Reverts with {QueueFull } if the queue is full.
38+ * Reverts with {Panic-RESOURCE_ERROR } if the queue is full.
5239 */
5340 function pushBack (Bytes32Deque storage deque , bytes32 value ) internal {
5441 unchecked {
5542 uint128 backIndex = deque._end;
56- if (backIndex + 1 == deque._begin) revert QueueFull ( );
43+ if (backIndex + 1 == deque._begin) Panic. panic (Panic.RESOURCE_ERROR );
5744 deque._data[backIndex] = value;
5845 deque._end = backIndex + 1 ;
5946 }
@@ -62,12 +49,12 @@ library DoubleEndedQueue {
6249 /**
6350 * @dev Removes the item at the end of the queue and returns it.
6451 *
65- * Reverts with {QueueEmpty } if the queue is empty.
52+ * Reverts with {Panic-EMPTY_ARRAY_POP } if the queue is empty.
6653 */
6754 function popBack (Bytes32Deque storage deque ) internal returns (bytes32 value ) {
6855 unchecked {
6956 uint128 backIndex = deque._end;
70- if (backIndex == deque._begin) revert QueueEmpty ( );
57+ if (backIndex == deque._begin) Panic. panic (Panic.EMPTY_ARRAY_POP );
7158 -- backIndex;
7259 value = deque._data[backIndex];
7360 delete deque._data[backIndex];
@@ -78,12 +65,12 @@ library DoubleEndedQueue {
7865 /**
7966 * @dev Inserts an item at the beginning of the queue.
8067 *
81- * Reverts with {QueueFull } if the queue is full.
68+ * Reverts with {Panic-RESOURCE_ERROR } if the queue is full.
8269 */
8370 function pushFront (Bytes32Deque storage deque , bytes32 value ) internal {
8471 unchecked {
8572 uint128 frontIndex = deque._begin - 1 ;
86- if (frontIndex == deque._end) revert QueueFull ( );
73+ if (frontIndex == deque._end) Panic. panic (Panic.RESOURCE_ERROR );
8774 deque._data[frontIndex] = value;
8875 deque._begin = frontIndex;
8976 }
@@ -92,12 +79,12 @@ library DoubleEndedQueue {
9279 /**
9380 * @dev Removes the item at the beginning of the queue and returns it.
9481 *
95- * Reverts with `QueueEmpty` if the queue is empty.
82+ * Reverts with {Panic-EMPTY_ARRAY_POP} if the queue is empty.
9683 */
9784 function popFront (Bytes32Deque storage deque ) internal returns (bytes32 value ) {
9885 unchecked {
9986 uint128 frontIndex = deque._begin;
100- if (frontIndex == deque._end) revert QueueEmpty ( );
87+ if (frontIndex == deque._end) Panic. panic (Panic.EMPTY_ARRAY_POP );
10188 value = deque._data[frontIndex];
10289 delete deque._data[frontIndex];
10390 deque._begin = frontIndex + 1 ;
@@ -107,20 +94,20 @@ library DoubleEndedQueue {
10794 /**
10895 * @dev Returns the item at the beginning of the queue.
10996 *
110- * Reverts with `QueueEmpty` if the queue is empty.
97+ * Reverts with {Panic-ARRAY_OUT_OF_BOUNDS} if the queue is empty.
11198 */
11299 function front (Bytes32Deque storage deque ) internal view returns (bytes32 value ) {
113- if (empty (deque)) revert QueueEmpty ( );
100+ if (empty (deque)) Panic. panic (Panic.ARRAY_OUT_OF_BOUNDS );
114101 return deque._data[deque._begin];
115102 }
116103
117104 /**
118105 * @dev Returns the item at the end of the queue.
119106 *
120- * Reverts with `QueueEmpty` if the queue is empty.
107+ * Reverts with {Panic-ARRAY_OUT_OF_BOUNDS} if the queue is empty.
121108 */
122109 function back (Bytes32Deque storage deque ) internal view returns (bytes32 value ) {
123- if (empty (deque)) revert QueueEmpty ( );
110+ if (empty (deque)) Panic. panic (Panic.ARRAY_OUT_OF_BOUNDS );
124111 unchecked {
125112 return deque._data[deque._end - 1 ];
126113 }
@@ -130,10 +117,10 @@ library DoubleEndedQueue {
130117 * @dev Return the item at a position in the queue given by `index`, with the first item at 0 and last item at
131118 * `length(deque) - 1`.
132119 *
133- * Reverts with `QueueOutOfBounds` if the index is out of bounds.
120+ * Reverts with {Panic-ARRAY_OUT_OF_BOUNDS} if the index is out of bounds.
134121 */
135122 function at (Bytes32Deque storage deque , uint256 index ) internal view returns (bytes32 value ) {
136- if (index >= length (deque)) revert QueueOutOfBounds ( );
123+ if (index >= length (deque)) Panic. panic (Panic.ARRAY_OUT_OF_BOUNDS );
137124 // By construction, length is a uint128, so the check above ensures that index can be safely downcast to uint128
138125 unchecked {
139126 return deque._data[deque._begin + uint128 (index)];
0 commit comments