-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Add CircularBuffer data structure #4913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ernestognw
merged 16 commits into
OpenZeppelin:master
from
Amxx:structure/circular-buffer
Apr 26, 2024
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0067ec7
add circular buffer
Amxx 585db62
inline documentation & testing
Amxx 42db594
update README.adoc
Amxx b56796f
fix lint
Amxx 5cd33bc
Update contracts/utils/structs/CircularBuffer.sol
Amxx a2b343c
update Stateless.sol
Amxx f160bc7
Apply trivial PR suggestions
ernestognw 4492360
Merge branch 'master' into structure/circular-buffer
ernestognw 63ff766
Update contracts/utils/structs/CircularBuffer.sol
Amxx 72a3794
rename internal vars to avoid name conflicts
Amxx 53d6aac
Update contracts/utils/structs/CircularBuffer.sol
Amxx e416a73
Merge branch 'master' into structure/circular-buffer
Amxx e5458fd
Nit
ernestognw f6d92b4
Improve example usage
ernestognw 5775190
Merge branch 'master' into structure/circular-buffer
Amxx 64351ed
fix generation
Amxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| 'openzeppelin-solidity': minor | ||
| --- | ||
|
|
||
| `CircularBuffer`: add a datastructure that stored the last N values pushed to it. | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,114 @@ | ||||||||||
| // SPDX-License-Identifier: MIT | ||||||||||
| pragma solidity ^0.8.20; | ||||||||||
|
|
||||||||||
| import {Math} from "../math/Math.sol"; | ||||||||||
| import {Arrays} from "../Arrays.sol"; | ||||||||||
| import {Panic} from "../Panic.sol"; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @dev A buffer of items of fixed size. When a new item is pushed, it takes the place of the oldest one in the buffer | ||||||||||
| * so that at all times, only the last N elements are kept. Items cannot be removed. The entier buffer can be reset. | ||||||||||
| * Last N elements can be accessed using their index from the end. | ||||||||||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| * | ||||||||||
| * Complexity: | ||||||||||
| * - insertion (`push`): O(1) | ||||||||||
ernestognw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| * - lookup (`last`): O(1) | ||||||||||
ernestognw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| * - inclusion (`includes`): O(N) (worst case) | ||||||||||
ernestognw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| * - reset (`clear`): O(1) | ||||||||||
ernestognw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| * | ||||||||||
| * * The struct is called `Bytes32CircularBuffer`. Other types can be cast to and from `bytes32`. This data structure | ||||||||||
| * can only be used in storage, and not in memory. | ||||||||||
|
Comment on lines
+23
to
+24
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the description I'm proposing, I don't think it's needed to specify these apply only to storage.
Suggested change
|
||||||||||
| * ```solidity | ||||||||||
| * CircularBuffer.Bytes32CircularBuffer buffer; | ||||||||||
| * ``` | ||||||||||
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| */ | ||||||||||
| library CircularBuffer { | ||||||||||
| /** | ||||||||||
| * @dev Counts the number of items that have been pushed to the buffer. The residu modulo _data.length indicates | ||||||||||
| * where the next value should be stored. | ||||||||||
|
||||||||||
| * @dev Counts the number of items that have been pushed to the buffer. The residu modulo _data.length indicates | |
| * where the next value should be stored. | |
| * @dev Keeps track of the items pushed to the buffer. New items should be added at `(_count + 1) % _data.length` |
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
ernestognw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ernestognw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.