-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6888c9b
commit 6f4a8fc
Showing
3 changed files
with
150 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
/* | ||
TypedDoubleEndedQueue.sol - Paymaster | ||
Copyright (C) 2023-Present SKALE Labs | ||
@author Dmytro Stebaiev | ||
Paymaster is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License as published | ||
by the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
Paymaster is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with Paymaster. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
// cspell:words deque structs | ||
|
||
import {DoubleEndedQueue} from "@openzeppelin/contracts/utils/structs/DoubleEndedQueue.sol"; | ||
|
||
import {SequenceLibrary} from "../Sequence.sol"; | ||
|
||
|
||
library TypedDoubleEndedQueue { | ||
|
||
struct NodeIdDeque { | ||
DoubleEndedQueue.Bytes32Deque inner; | ||
} | ||
|
||
/** | ||
* @dev Inserts an item at the end of the queue. | ||
* | ||
* Reverts with {QueueFull} if the queue is full. | ||
*/ | ||
function pushBack(NodeIdDeque storage deque, SequenceLibrary.NodeId value) internal { | ||
DoubleEndedQueue.pushBack(deque.inner, bytes32(SequenceLibrary.unwrapNodeId(value))); | ||
} | ||
|
||
/** | ||
* @dev Resets the queue back to being empty. | ||
* | ||
* NOTE: The current items are left behind in storage. This does not affect the functioning of the queue, but misses | ||
* out on potential gas refunds. | ||
*/ | ||
function clear(NodeIdDeque storage deque) internal { | ||
DoubleEndedQueue.clear(deque.inner); | ||
} | ||
|
||
/** | ||
* @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. | ||
*/ | ||
function at(NodeIdDeque storage deque, uint256 index) internal view returns (SequenceLibrary.NodeId value) { | ||
return SequenceLibrary.wrapNodeId( | ||
uint256( | ||
DoubleEndedQueue.at(deque.inner, index) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @dev Returns the number of items in the queue. | ||
*/ | ||
function length(NodeIdDeque storage deque) internal view returns (uint256 lengthValue) { | ||
return DoubleEndedQueue.length(deque.inner); | ||
} | ||
|
||
/** | ||
* @dev Returns true if the queue is empty. | ||
*/ | ||
function empty(NodeIdDeque storage deque) internal view returns (bool isEmpty) { | ||
return DoubleEndedQueue.empty(deque.inner); | ||
} | ||
} |