Refactor double ended queue

commit d5b67d3499
Author: Hadrien Croubois <hadrien.croubois@gmail.com>
Date:   Sat Jul 8 15:40:04 2023 +0200

    Update strong-poems-thank.md

commit 909af00946
Author: Hadrien Croubois <hadrien.croubois@gmail.com>
Date:   Sat Jul 8 15:39:09 2023 +0200

    add changeset

commit 2201f657b9
Author: Hadrien Croubois <hadrien.croubois@gmail.com>
Date:   Fri Jul 7 16:08:48 2023 +0200

    remove declaration of max_uint48 with is not available by default in CVL2

commit 0b3da8c14c
Author: Hadrien Croubois <hadrien.croubois@gmail.com>
Date:   Fri Jul 7 15:19:30 2023 +0200

    update DoubleEndedQueue specs to run with certora 4.3.1

commit 734bf8e85a
Merge: 1294d4bc 7ccea54d
Author: Hadrien Croubois <hadrien.croubois@gmail.com>
Date:   Fri Jul 7 14:28:11 2023 +0200

    Merge branch 'master' into refactor/DoubleEndedQueue

commit 1294d4bc10
Author: Hadrien Croubois <hadrien.croubois@gmail.com>
Date:   Fri Jul 7 14:27:41 2023 +0200

    Update DoubleEndedQueue.sol

commit 1199e602d1
Merge: fd880a59 f29307cf
Author: Hadrien Croubois <hadrien.croubois@gmail.com>
Date:   Wed Jun 28 14:01:21 2023 +0200

    Merge branch 'master' into refactor/DoubleEndedQueue

commit fd880a598e
Author: Hadrien Croubois <hadrien.croubois@gmail.com>
Date:   Fri Mar 31 20:43:11 2023 +0200

    remove signed integers from DoubleEndedQueue
This commit is contained in:
ernestognw
2023-07-19 13:13:00 -06:00
parent ba8e296915
commit b027c3541c
7 changed files with 104 additions and 158 deletions

View File

@ -22,6 +22,11 @@ library DoubleEndedQueue {
*/
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.
*/
@ -40,18 +45,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 QueueFull();
deque._data[backIndex] = value;
deque._end = backIndex + 1;
}
}
@ -62,26 +68,26 @@ library DoubleEndedQueue {
* Reverts with `QueueEmpty` if the queue is empty.
*/
function popBack(Bytes32Deque storage deque) internal returns (bytes32 value) {
if (empty(deque)) revert QueueEmpty();
int128 backIndex;
unchecked {
backIndex = deque._end - 1;
uint128 backIndex = deque._end;
if (backIndex == deque._begin) revert QueueEmpty();
--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 QueueFull();
deque._data[frontIndex] = value;
deque._begin = frontIndex;
}
deque._data[frontIndex] = value;
deque._begin = frontIndex;
}
/**
@ -90,11 +96,11 @@ library DoubleEndedQueue {
* Reverts with `QueueEmpty` if the queue is empty.
*/
function popFront(Bytes32Deque storage deque) internal returns (bytes32 value) {
if (empty(deque)) revert QueueEmpty();
int128 frontIndex = deque._begin;
value = deque._data[frontIndex];
delete deque._data[frontIndex];
unchecked {
uint128 frontIndex = deque._begin;
if (frontIndex == deque._end) revert QueueEmpty();
value = deque._data[frontIndex];
delete deque._data[frontIndex];
deque._begin = frontIndex + 1;
}
}
@ -106,8 +112,7 @@ library DoubleEndedQueue {
*/
function front(Bytes32Deque storage deque) internal view returns (bytes32 value) {
if (empty(deque)) revert QueueEmpty();
int128 frontIndex = deque._begin;
return deque._data[frontIndex];
return deque._data[deque._begin];
}
/**
@ -117,11 +122,9 @@ library DoubleEndedQueue {
*/
function back(Bytes32Deque storage deque) internal view returns (bytes32 value) {
if (empty(deque)) revert QueueEmpty();
int128 backIndex;
unchecked {
backIndex = deque._end - 1;
return deque._data[deque._end - 1];
}
return deque._data[backIndex];
}
/**
@ -131,10 +134,10 @@ library DoubleEndedQueue {
* Reverts with `QueueOutOfBounds` 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 QueueOutOfBounds();
return deque._data[idx];
if (index >= length(deque)) revert QueueOutOfBounds();
unchecked {
return deque._data[deque._begin + SafeCast.toUint128(index)];
}
}
/**
@ -152,10 +155,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);
}
}
@ -163,6 +165,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;
}
}