|
|
|
@ -24,6 +24,11 @@ library DoubleEndedQueue { |
|
|
|
|
*/ |
|
|
|
|
error Empty(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dev A push operation couldn't be completed due to the queue being full. |
|
|
|
|
*/ |
|
|
|
|
error Full(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dev An operation (e.g. {at}) couldn't be completed due to an index being out of bounds. |
|
|
|
|
*/ |
|
|
|
@ -42,18 +47,19 @@ library DoubleEndedQueue { |
|
|
|
|
* data[end - 1]. |
|
|
|
|
*/ |
|
|
|
|
struct Bytes32Deque { |
|
|
|
|
int128 _begin; |
|
|
|
|
int128 _end; |
|
|
|
|
mapping(int128 => bytes32) _data; |
|
|
|
|
uint128 _begin; |
|
|
|
|
uint128 _end; |
|
|
|
|
mapping(uint128 => bytes32) _data; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dev Inserts an item at the end of the queue. |
|
|
|
|
*/ |
|
|
|
|
function pushBack(Bytes32Deque storage deque, bytes32 value) internal { |
|
|
|
|
int128 backIndex = deque._end; |
|
|
|
|
deque._data[backIndex] = value; |
|
|
|
|
unchecked { |
|
|
|
|
uint128 backIndex = deque._end; |
|
|
|
|
if (backIndex + 1 == deque._begin) revert Full(); |
|
|
|
|
deque._data[backIndex] = value; |
|
|
|
|
deque._end = backIndex + 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -64,26 +70,26 @@ library DoubleEndedQueue { |
|
|
|
|
* Reverts with `Empty` if the queue is empty. |
|
|
|
|
*/ |
|
|
|
|
function popBack(Bytes32Deque storage deque) internal returns (bytes32 value) { |
|
|
|
|
if (empty(deque)) revert Empty(); |
|
|
|
|
int128 backIndex; |
|
|
|
|
unchecked { |
|
|
|
|
backIndex = deque._end - 1; |
|
|
|
|
uint128 backIndex = deque._end; |
|
|
|
|
if (backIndex == deque._begin) revert Empty(); |
|
|
|
|
--backIndex; |
|
|
|
|
value = deque._data[backIndex]; |
|
|
|
|
delete deque._data[backIndex]; |
|
|
|
|
deque._end = backIndex; |
|
|
|
|
} |
|
|
|
|
value = deque._data[backIndex]; |
|
|
|
|
delete deque._data[backIndex]; |
|
|
|
|
deque._end = backIndex; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dev Inserts an item at the beginning of the queue. |
|
|
|
|
*/ |
|
|
|
|
function pushFront(Bytes32Deque storage deque, bytes32 value) internal { |
|
|
|
|
int128 frontIndex; |
|
|
|
|
unchecked { |
|
|
|
|
frontIndex = deque._begin - 1; |
|
|
|
|
uint128 frontIndex = deque._begin - 1; |
|
|
|
|
if (frontIndex == deque._end) revert Full(); |
|
|
|
|
deque._data[frontIndex] = value; |
|
|
|
|
deque._begin = frontIndex; |
|
|
|
|
} |
|
|
|
|
deque._data[frontIndex] = value; |
|
|
|
|
deque._begin = frontIndex; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -92,11 +98,11 @@ library DoubleEndedQueue { |
|
|
|
|
* Reverts with `Empty` if the queue is empty. |
|
|
|
|
*/ |
|
|
|
|
function popFront(Bytes32Deque storage deque) internal returns (bytes32 value) { |
|
|
|
|
if (empty(deque)) revert Empty(); |
|
|
|
|
int128 frontIndex = deque._begin; |
|
|
|
|
value = deque._data[frontIndex]; |
|
|
|
|
delete deque._data[frontIndex]; |
|
|
|
|
unchecked { |
|
|
|
|
uint128 frontIndex = deque._begin; |
|
|
|
|
if (frontIndex == deque._end) revert Empty(); |
|
|
|
|
value = deque._data[frontIndex]; |
|
|
|
|
delete deque._data[frontIndex]; |
|
|
|
|
deque._begin = frontIndex + 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -108,8 +114,7 @@ library DoubleEndedQueue { |
|
|
|
|
*/ |
|
|
|
|
function front(Bytes32Deque storage deque) internal view returns (bytes32 value) { |
|
|
|
|
if (empty(deque)) revert Empty(); |
|
|
|
|
int128 frontIndex = deque._begin; |
|
|
|
|
return deque._data[frontIndex]; |
|
|
|
|
return deque._data[deque._begin]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -119,11 +124,9 @@ library DoubleEndedQueue { |
|
|
|
|
*/ |
|
|
|
|
function back(Bytes32Deque storage deque) internal view returns (bytes32 value) { |
|
|
|
|
if (empty(deque)) revert Empty(); |
|
|
|
|
int128 backIndex; |
|
|
|
|
unchecked { |
|
|
|
|
backIndex = deque._end - 1; |
|
|
|
|
return deque._data[deque._end - 1]; |
|
|
|
|
} |
|
|
|
|
return deque._data[backIndex]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -133,10 +136,10 @@ library DoubleEndedQueue { |
|
|
|
|
* Reverts with `OutOfBounds` if the index is out of bounds. |
|
|
|
|
*/ |
|
|
|
|
function at(Bytes32Deque storage deque, uint256 index) internal view returns (bytes32 value) { |
|
|
|
|
// int256(deque._begin) is a safe upcast |
|
|
|
|
int128 idx = SafeCast.toInt128(int256(deque._begin) + SafeCast.toInt256(index)); |
|
|
|
|
if (idx >= deque._end) revert OutOfBounds(); |
|
|
|
|
return deque._data[idx]; |
|
|
|
|
if (index >= length(deque)) revert OutOfBounds(); |
|
|
|
|
unchecked { |
|
|
|
|
return deque._data[deque._begin + SafeCast.toUint128(index)]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -154,10 +157,9 @@ library DoubleEndedQueue { |
|
|
|
|
* @dev Returns the number of items in the queue. |
|
|
|
|
*/ |
|
|
|
|
function length(Bytes32Deque storage deque) internal view returns (uint256) { |
|
|
|
|
// The interface preserves the invariant that begin <= end so we assume this will not overflow. |
|
|
|
|
// We also assume there are at most int256.max items in the queue. |
|
|
|
|
// We also assume there are at most uint128.max items in the queue. |
|
|
|
|
unchecked { |
|
|
|
|
return uint256(int256(deque._end) - int256(deque._begin)); |
|
|
|
|
return uint256(deque._end - deque._begin); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -165,6 +167,6 @@ library DoubleEndedQueue { |
|
|
|
|
* @dev Returns true if the queue is empty. |
|
|
|
|
*/ |
|
|
|
|
function empty(Bytes32Deque storage deque) internal view returns (bool) { |
|
|
|
|
return deque._end <= deque._begin; |
|
|
|
|
return deque._end == deque._begin; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|