|
@@ -2,6 +2,8 @@
|
|
|
// OpenZeppelin Contracts (last updated v5.0.0) (utils/structs/DoubleEndedQueue.sol)
|
|
|
pragma solidity ^0.8.20;
|
|
|
|
|
|
+import {Panic} from "../Panic.sol";
|
|
|
+
|
|
|
/**
|
|
|
* @dev A sequence of items with the ability to efficiently push and pop items (i.e. insert and remove) on both ends of
|
|
|
* 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;
|
|
|
* ```
|
|
|
*/
|
|
|
library DoubleEndedQueue {
|
|
|
- /**
|
|
|
- * @dev An operation (e.g. {front}) couldn't be completed due to the queue being empty.
|
|
|
- */
|
|
|
- error QueueEmpty();
|
|
|
-
|
|
|
- /**
|
|
|
- * @dev A push operation couldn't be completed due to the queue being full.
|
|
|
- */
|
|
|
- error QueueFull();
|
|
|
-
|
|
|
- /**
|
|
|
- * @dev An operation (e.g. {at}) couldn't be completed due to an index being out of bounds.
|
|
|
- */
|
|
|
- error QueueOutOfBounds();
|
|
|
-
|
|
|
/**
|
|
|
* @dev Indices are 128 bits so begin and end are packed in a single storage slot for efficient access.
|
|
|
*
|
|
@@ -48,12 +35,12 @@ library DoubleEndedQueue {
|
|
|
/**
|
|
|
* @dev Inserts an item at the end of the queue.
|
|
|
*
|
|
|
- * Reverts with {QueueFull} if the queue is full.
|
|
|
+ * Reverts with {Panic-RESOURCE_ERROR} if the queue is full.
|
|
|
*/
|
|
|
function pushBack(Bytes32Deque storage deque, bytes32 value) internal {
|
|
|
unchecked {
|
|
|
uint128 backIndex = deque._end;
|
|
|
- if (backIndex + 1 == deque._begin) revert QueueFull();
|
|
|
+ if (backIndex + 1 == deque._begin) Panic.panic(Panic.RESOURCE_ERROR);
|
|
|
deque._data[backIndex] = value;
|
|
|
deque._end = backIndex + 1;
|
|
|
}
|
|
@@ -62,12 +49,12 @@ library DoubleEndedQueue {
|
|
|
/**
|
|
|
* @dev Removes the item at the end of the queue and returns it.
|
|
|
*
|
|
|
- * Reverts with {QueueEmpty} if the queue is empty.
|
|
|
+ * Reverts with {Panic-EMPTY_ARRAY_POP} if the queue is empty.
|
|
|
*/
|
|
|
function popBack(Bytes32Deque storage deque) internal returns (bytes32 value) {
|
|
|
unchecked {
|
|
|
uint128 backIndex = deque._end;
|
|
|
- if (backIndex == deque._begin) revert QueueEmpty();
|
|
|
+ if (backIndex == deque._begin) Panic.panic(Panic.EMPTY_ARRAY_POP);
|
|
|
--backIndex;
|
|
|
value = deque._data[backIndex];
|
|
|
delete deque._data[backIndex];
|
|
@@ -78,12 +65,12 @@ library DoubleEndedQueue {
|
|
|
/**
|
|
|
* @dev Inserts an item at the beginning of the queue.
|
|
|
*
|
|
|
- * Reverts with {QueueFull} if the queue is full.
|
|
|
+ * Reverts with {Panic-RESOURCE_ERROR} if the queue is full.
|
|
|
*/
|
|
|
function pushFront(Bytes32Deque storage deque, bytes32 value) internal {
|
|
|
unchecked {
|
|
|
uint128 frontIndex = deque._begin - 1;
|
|
|
- if (frontIndex == deque._end) revert QueueFull();
|
|
|
+ if (frontIndex == deque._end) Panic.panic(Panic.RESOURCE_ERROR);
|
|
|
deque._data[frontIndex] = value;
|
|
|
deque._begin = frontIndex;
|
|
|
}
|
|
@@ -92,12 +79,12 @@ library DoubleEndedQueue {
|
|
|
/**
|
|
|
* @dev Removes the item at the beginning of the queue and returns it.
|
|
|
*
|
|
|
- * Reverts with `QueueEmpty` if the queue is empty.
|
|
|
+ * Reverts with {Panic-EMPTY_ARRAY_POP} if the queue is empty.
|
|
|
*/
|
|
|
function popFront(Bytes32Deque storage deque) internal returns (bytes32 value) {
|
|
|
unchecked {
|
|
|
uint128 frontIndex = deque._begin;
|
|
|
- if (frontIndex == deque._end) revert QueueEmpty();
|
|
|
+ if (frontIndex == deque._end) Panic.panic(Panic.EMPTY_ARRAY_POP);
|
|
|
value = deque._data[frontIndex];
|
|
|
delete deque._data[frontIndex];
|
|
|
deque._begin = frontIndex + 1;
|
|
@@ -107,20 +94,20 @@ library DoubleEndedQueue {
|
|
|
/**
|
|
|
* @dev Returns the item at the beginning of the queue.
|
|
|
*
|
|
|
- * Reverts with `QueueEmpty` if the queue is empty.
|
|
|
+ * Reverts with {Panic-ARRAY_OUT_OF_BOUNDS} if the queue is empty.
|
|
|
*/
|
|
|
function front(Bytes32Deque storage deque) internal view returns (bytes32 value) {
|
|
|
- if (empty(deque)) revert QueueEmpty();
|
|
|
+ if (empty(deque)) Panic.panic(Panic.ARRAY_OUT_OF_BOUNDS);
|
|
|
return deque._data[deque._begin];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @dev Returns the item at the end of the queue.
|
|
|
*
|
|
|
- * Reverts with `QueueEmpty` if the queue is empty.
|
|
|
+ * Reverts with {Panic-ARRAY_OUT_OF_BOUNDS} if the queue is empty.
|
|
|
*/
|
|
|
function back(Bytes32Deque storage deque) internal view returns (bytes32 value) {
|
|
|
- if (empty(deque)) revert QueueEmpty();
|
|
|
+ if (empty(deque)) Panic.panic(Panic.ARRAY_OUT_OF_BOUNDS);
|
|
|
unchecked {
|
|
|
return deque._data[deque._end - 1];
|
|
|
}
|
|
@@ -130,10 +117,10 @@ library DoubleEndedQueue {
|
|
|
* @dev Return the item at a position in the queue given by `index`, with the first item at 0 and last item at
|
|
|
* `length(deque) - 1`.
|
|
|
*
|
|
|
- * Reverts with `QueueOutOfBounds` if the index is out of bounds.
|
|
|
+ * Reverts with {Panic-ARRAY_OUT_OF_BOUNDS} if the index is out of bounds.
|
|
|
*/
|
|
|
function at(Bytes32Deque storage deque, uint256 index) internal view returns (bytes32 value) {
|
|
|
- if (index >= length(deque)) revert QueueOutOfBounds();
|
|
|
+ if (index >= length(deque)) Panic.panic(Panic.ARRAY_OUT_OF_BOUNDS);
|
|
|
// By construction, length is a uint128, so the check above ensures that index can be safely downcast to uint128
|
|
|
unchecked {
|
|
|
return deque._data[deque._begin + uint128(index)];
|