-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Add Binary heap structure #5084
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
Merged
Changes from 3 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
4fec530
implement binary heap
Amxx 8fa2eeb
codespell & lib naming
Amxx 792fcba
tests
Amxx 0c86005
fix fuzzing tests
Amxx 248baf6
codespell
Amxx 53db2ab
update
Amxx 945e0f4
procedural generation
Amxx df82b15
testing
Amxx 8b965fc
overflow handling
Amxx e952cf6
add replace and changeset
Amxx f5fa274
rename top -> peek
Amxx 1f0fef0
internal renaming
Amxx d0972a3
codespell
Amxx 8e3dda6
regenerate
Amxx 38e1813
auto regenerate
Amxx 02f224d
Update .githooks/pre-push
Amxx 7e88481
up
Amxx a46cc63
Merge branch 'master' into struct/heap
Amxx b2fda31
up
Amxx 516f1ca
tests
Amxx cf1278e
Update test/utils/structs/Heap.test.js
Amxx 5f15d1c
Update test/utils/structs/Heap.test.js
Amxx 32e9b49
Apply suggestions from code review
Amxx c083d79
regenrate
Amxx 0e6ada0
Merge branch 'master' into struct/heap
Amxx 7c98102
update inline comments
Amxx a1767d4
update
Amxx 1c1e84b
Address comment for the PR
Amxx 0e7fe7a
rewrite Arrays.sol to use uint256[] as the default, and use Comparato…
Amxx d495859
Update scripts/generate/templates/Heap.js
Amxx fe8e902
regenerate
Amxx 3abeb84
Add docs
ernestognw 8801d98
Update scripts/generate/templates/Heap.js
Amxx f78df0c
Apply suggestions from code review
Amxx bb37dfb
fix generation + change key type
Amxx 1fb4b81
more invariant check
Amxx d3308c4
Update scripts/generate/templates/Heap.js
ernestognw 5b07512
Generate
ernestognw 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
|
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. I like the idea of having comparators so that the heap is customizable. However, this library feels odd. Is this something we see value in providing on its own file? I'd rather keep it undocumented and inside Heap.sol |
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,13 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| library Comparators { | ||
| function lt(uint256 a, uint256 b) internal pure returns (bool) { | ||
| return a < b; | ||
| } | ||
|
|
||
| function gt(uint256 a, uint256 b) internal pure returns (bool) { | ||
| return a > b; | ||
| } | ||
| } |
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,228 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| import {SafeCast} from "../math/SafeCast.sol"; | ||
| import {Comparators} from "../Comparators.sol"; | ||
| import {Panic} from "../Panic.sol"; | ||
|
|
||
| library Heap { | ||
| using SafeCast for uint256; | ||
|
|
||
| /** | ||
| * A Heap is represented as an array of Node objects. In this array we store two overlapping structures: | ||
| * - A tree structure, where index 0 is the root, and for each index i, the children are 2*i+1 and 2*i+2. | ||
| * For each index in this tree we have the `index` pointer that gives the position of the corresponding value. | ||
| * - An array of values (payload). At each index we store a uint256 `value` and `lookup`, the index of the node | ||
| * that points to this value. | ||
| * | ||
| * Some invariant: | ||
| * ``` | ||
| * i == heap.data[heap[data].index].lookup // for all index i | ||
| * i == heap.data[heap[data].lookup].index // for all index i | ||
| * ``` | ||
| * | ||
| * The structure is order so that each node is bigger then its parent. An immediate consequence is that the | ||
| * smallest value is the one at the root. It can be retrieved in O(1) at `heap.data[heap.data[0].index].value` | ||
| * | ||
| * This structure is designed for the following complexities: | ||
| * - insert: 0(log(n)) | ||
| * - pop (remove smallest value in set): O(log(n)) | ||
| * - top (get smallest value in set): O(1) | ||
| */ | ||
| struct Uint256Heap { | ||
| Uint256HeapNode[] data; | ||
| } | ||
|
|
||
| // Index and lookup are bounded by the size of the structure. We could reasonably limit that to uint20 (1 billion entries) | ||
| // Then could also limit the value to uint216 so that the entier structure fits into a single slot. | ||
| struct Uint256HeapNode { | ||
| uint256 value; | ||
| uint32 index; // position -> value | ||
| uint32 lookup; // value -> position | ||
| } | ||
|
|
||
| /** | ||
| * @dev Lookup the root element of the heap. | ||
| */ | ||
| function top(Uint256Heap storage self) internal view returns (uint256) { | ||
| return self.data[self.data[0].index].value; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Remove (and return) the root element for the heap using the default comparator. | ||
| * | ||
| * Note: All inserting and removal from a heap should always be done using the same comparator. Mixing comparator | ||
| * during the lifecycle of a heap will result in undefined behavior. | ||
| */ | ||
| function pop(Uint256Heap storage self) internal returns (uint256) { | ||
| return pop(self, Comparators.lt); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Remove (and return) the root element for the heap using the provided comparator. | ||
| * | ||
| * Note: All inserting and removal from a heap should always be done using the same comparator. Mixing comparator | ||
| * during the lifecycle of a heap will result in undefined behavior. | ||
| */ | ||
| function pop( | ||
| Uint256Heap storage self, | ||
| function(uint256, uint256) view returns (bool) comp | ||
| ) internal returns (uint256) { | ||
| uint32 size = length(self); | ||
|
|
||
| if (size == 0) Panic.panic(Panic.EMPTY_ARRAY_POP); | ||
|
|
||
| uint32 last = size - 1; // could be unchecked (check above) | ||
|
|
||
| // get root location (in the data array) and value | ||
| uint32 rootIdx = self.data[0].index; | ||
| uint256 rootValue = self.data[rootIdx].value; | ||
|
|
||
| // if root is not the last element of the data array (that will get pop-ed), reorder the data array. | ||
| if (rootIdx != last) { | ||
| // get details about the value stored in the last element of the array (that will get pop-ed) | ||
| uint32 lastDataIdx = self.data[last].lookup; | ||
| uint256 lastDataValue = self.data[last].value; | ||
| // copy these values to the location of the root (that is safe, and that we no longer use) | ||
| self.data[rootIdx].value = lastDataValue; | ||
| self.data[rootIdx].lookup = lastDataIdx; | ||
| // update the tree node that used to point to that last element (value now located where the root was) | ||
| self.data[lastDataIdx].index = rootIdx; | ||
| } | ||
|
|
||
| // get last leaf location (in the data array) and value | ||
| uint32 lastIdx = self.data[last].index; | ||
| uint256 lastValue = self.data[lastIdx].value; | ||
|
|
||
| // move the last leaf to the root, pop last leaf ... | ||
| self.data[0].index = lastIdx; | ||
| self.data[lastIdx].lookup = 0; | ||
| self.data.pop(); | ||
|
|
||
| // ... and heapify | ||
| _heapifyDown(self, last, 0, lastValue, comp); | ||
|
|
||
| // return root value | ||
| return rootValue; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Insert a new element in the heap using the default comparator. | ||
| * | ||
| * Note: All inserting and removal from a heap should always be done using the same comparator. Mixing comparator | ||
| * during the lifecycle of a heap will result in undefined behavior. | ||
| */ | ||
| function insert(Uint256Heap storage self, uint256 value) internal { | ||
| insert(self, value, Comparators.lt); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Insert a new element in the heap using the provided comparator. | ||
| * | ||
| * Note: All inserting and removal from a heap should always be done using the same comparator. Mixing comparator | ||
| * during the lifecycle of a heap will result in undefined behavior. | ||
| */ | ||
| function insert( | ||
| Uint256Heap storage self, | ||
| uint256 value, | ||
| function(uint256, uint256) view returns (bool) comp | ||
| ) internal { | ||
| uint32 size = length(self); | ||
| self.data.push(Uint256HeapNode({index: size, lookup: size, value: value})); | ||
| _heapifyUp(self, size, value, comp); | ||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * @dev Returns the number of elements in the heap. | ||
| */ | ||
| function length(Uint256Heap storage self) internal view returns (uint32) { | ||
| return self.data.length.toUint32(); | ||
| } | ||
|
|
||
| function clear(Uint256Heap storage self) internal { | ||
| Uint256HeapNode[] storage data = self.data; | ||
| /// @solidity memory-safe-assembly | ||
| assembly { | ||
| sstore(data.slot, 0) | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * @dev Swap node `i` and `j` in the tree. | ||
| */ | ||
| function _swap(Uint256Heap storage self, uint32 i, uint32 j) private { | ||
| uint32 ii = self.data[i].index; | ||
| uint32 jj = self.data[j].index; | ||
| // update pointers to the data (swap the value) | ||
| self.data[i].index = jj; | ||
| self.data[j].index = ii; | ||
| // update lookup pointers for consistency | ||
| self.data[ii].lookup = j; | ||
| self.data[jj].lookup = i; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Perform heap maintenance on `self`, starting at position `pos` (with the `value`), using `comp` as a | ||
| * comparator, and moving toward the leafs of the underlying tree. | ||
| * | ||
| * Note: This is a private function that is called in a trusted context with already cached parameters. `lesizength` | ||
| * and `value` could be extracted from `self` and `pos`, but that would require redundant storage read. These | ||
| * parameters are not verified. It is the caller role to make sure the parameters are correct. | ||
| */ | ||
| function _heapifyDown( | ||
| Uint256Heap storage self, | ||
| uint32 size, | ||
| uint32 pos, | ||
| uint256 value, | ||
| function(uint256, uint256) view returns (bool) comp | ||
| ) private { | ||
| uint32 left = 2 * pos + 1; | ||
| uint32 right = 2 * pos + 2; | ||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (right < size) { | ||
| uint256 lValue = self.data[self.data[left].index].value; | ||
| uint256 rValue = self.data[self.data[right].index].value; | ||
| if (comp(lValue, value) || comp(rValue, value)) { | ||
| if (comp(lValue, rValue)) { | ||
| _swap(self, pos, left); | ||
| _heapifyDown(self, size, left, value, comp); | ||
| } else { | ||
| _swap(self, pos, right); | ||
| _heapifyDown(self, size, right, value, comp); | ||
| } | ||
| } | ||
| } else if (left < size) { | ||
| uint256 lValue = self.data[self.data[left].index].value; | ||
| if (comp(lValue, value)) { | ||
| _swap(self, pos, left); | ||
| _heapifyDown(self, size, left, value, comp); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev Perform heap maintenance on `self`, starting at position `pos` (with the `value`), using `comp` as a | ||
| * comparator, and moving toward the root of the underlying tree. | ||
| * | ||
| * Note: This is a private function that is called in a trusted context with already cached parameters. `value` | ||
| * could be extracted from `self` and `pos`, but that would require redundant storage read. This parameters is not | ||
| * verified. It is the caller role to make sure the parameters are correct. | ||
| */ | ||
| function _heapifyUp( | ||
| Uint256Heap storage self, | ||
| uint32 pos, | ||
| uint256 value, | ||
| function(uint256, uint256) view returns (bool) comp | ||
| ) private { | ||
| unchecked { | ||
| while (pos > 0) { | ||
| uint32 parent = (pos - 1) / 2; | ||
| uint256 parentValue = self.data[self.data[parent].index].value; | ||
| if (comp(parentValue, value)) break; | ||
| _swap(self, pos, parent); | ||
| pos = parent; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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,79 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| import {Test} from "forge-std/Test.sol"; | ||
| import {Math} from "@openzeppelin/contracts/utils/math/Math.sol"; | ||
| import {Heap} from "@openzeppelin/contracts/utils/structs/Heap.sol"; | ||
| import {Comparators} from "@openzeppelin/contracts/utils/Comparators.sol"; | ||
|
|
||
| contract HeapTest is Test { | ||
| using Heap for *; | ||
|
|
||
| Heap.Uint256Heap internal heap; | ||
|
|
||
| function _validateHeap(function(uint256, uint256) view returns (bool) comp) internal { | ||
| for (uint32 i = 0; i < heap.length(); ++i) { | ||
| // lookups | ||
| assertEq(i, heap.data[heap.data[i].index].lookup); | ||
|
|
||
| // ordering: each node has a value bigger then its parent | ||
| if (i > 0) | ||
| assertFalse(comp(heap.data[heap.data[i].index].value, heap.data[heap.data[(i - 1) / 2].index].value)); | ||
| } | ||
| } | ||
|
|
||
| function testFuzz(uint256[] calldata input) public { | ||
| vm.assume(input.length < 0x20); | ||
| assertEq(heap.length(), 0); | ||
|
|
||
| uint256 min = type(uint256).max; | ||
| for (uint256 i; i < input.length; ++i) { | ||
| heap.insert(input[i]); | ||
| assertEq(heap.length(), i); | ||
| _validateHeap(Comparators.lt); | ||
|
|
||
| min = Math.min(min, input[i]); | ||
| assertEq(heap.top(), min); | ||
| } | ||
|
|
||
| uint256 max = 0; | ||
| for (uint256 i; i < input.length; ++i) { | ||
| uint256 top = heap.top(); | ||
| uint256 pop = heap.pop(); | ||
| assertEq(heap.length(), input.length - i - 1); | ||
| _validateHeap(Comparators.lt); | ||
|
|
||
| assertEq(pop, top); | ||
| assertGe(pop, max); | ||
| max = pop; | ||
| } | ||
| } | ||
|
|
||
| function testFuzzGt(uint256[] calldata input) public { | ||
| vm.assume(input.length < 0x20); | ||
| assertEq(heap.length(), 0); | ||
|
|
||
| uint256 max = 0; | ||
| for (uint256 i; i < input.length; ++i) { | ||
| heap.insert(input[i], Comparators.gt); | ||
| assertEq(heap.length(), i); | ||
| _validateHeap(Comparators.gt); | ||
|
|
||
| max = Math.max(max, input[i]); | ||
| assertEq(heap.top(), max); | ||
| } | ||
|
|
||
| uint256 min = type(uint256).max; | ||
| for (uint256 i; i < input.length; ++i) { | ||
| uint256 top = heap.top(); | ||
| uint256 pop = heap.pop(Comparators.gt); | ||
| assertEq(heap.length(), input.length - i - 1); | ||
| _validateHeap(Comparators.gt); | ||
|
|
||
| assertEq(pop, top); | ||
| assertLe(pop, min); | ||
| min = pop; | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not adding Comparators here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was overlooked, and since there are currently no tests for the
Comparatorslibrary, it missing did not triger any issue.Lets start by discussing weither we want the comparator library or not ... then we can add tests and solve that