Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
4fec530
implement binary heap
Amxx Jun 16, 2024
8fa2eeb
codespell & lib naming
Amxx Jun 16, 2024
792fcba
tests
Amxx Jun 16, 2024
0c86005
fix fuzzing tests
Amxx Jun 16, 2024
248baf6
codespell
Amxx Jun 16, 2024
53db2ab
update
Amxx Jun 17, 2024
945e0f4
procedural generation
Amxx Jun 17, 2024
df82b15
testing
Amxx Jun 17, 2024
8b965fc
overflow handling
Amxx Jun 21, 2024
e952cf6
add replace and changeset
Amxx Jun 21, 2024
f5fa274
rename top -> peek
Amxx Jun 21, 2024
1f0fef0
internal renaming
Amxx Jun 21, 2024
d0972a3
codespell
Amxx Jun 21, 2024
8e3dda6
regenerate
Amxx Jun 21, 2024
38e1813
auto regenerate
Amxx Jun 21, 2024
02f224d
Update .githooks/pre-push
Amxx Jun 21, 2024
7e88481
up
Amxx Jun 21, 2024
a46cc63
Merge branch 'master' into struct/heap
Amxx Jun 21, 2024
b2fda31
up
Amxx Jun 21, 2024
516f1ca
tests
Amxx Jun 21, 2024
cf1278e
Update test/utils/structs/Heap.test.js
Amxx Jun 21, 2024
5f15d1c
Update test/utils/structs/Heap.test.js
Amxx Jun 21, 2024
32e9b49
Apply suggestions from code review
Amxx Jun 27, 2024
c083d79
regenrate
Amxx Jun 27, 2024
0e6ada0
Merge branch 'master' into struct/heap
Amxx Jul 3, 2024
7c98102
update inline comments
Amxx Jul 15, 2024
a1767d4
update
Amxx Jul 15, 2024
1c1e84b
Address comment for the PR
Amxx Jul 16, 2024
0e7fe7a
rewrite Arrays.sol to use uint256[] as the default, and use Comparato…
Amxx Jul 17, 2024
d495859
Update scripts/generate/templates/Heap.js
Amxx Jul 18, 2024
fe8e902
regenerate
Amxx Jul 18, 2024
3abeb84
Add docs
ernestognw Jul 23, 2024
8801d98
Update scripts/generate/templates/Heap.js
Amxx Jul 23, 2024
f78df0c
Apply suggestions from code review
Amxx Jul 23, 2024
bb37dfb
fix generation + change key type
Amxx Jul 23, 2024
1fb4b81
more invariant check
Amxx Jul 23, 2024
d3308c4
Update scripts/generate/templates/Heap.js
ernestognw Jul 23, 2024
5b07512
Generate
ernestognw Jul 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update inline comments
  • Loading branch information
Amxx committed Jul 15, 2024
commit 7c981028b4235ea4c296e2a4d4f268cc19a3de2f
71 changes: 31 additions & 40 deletions contracts/utils/structs/Heap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,40 @@ import {SafeCast} from "../math/SafeCast.sol";
import {Comparators} from "../Comparators.sol";
import {Panic} from "../Panic.sol";

/**
* @dev Library for managing https://en.wikipedia.org/wiki/Binary_heap[binary heap] that can be used as
* https://en.wikipedia.org/wiki/Priority_queue[priority queue].
*
* Heaps are 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 `value` and `lookup`. Type of `value` depends on the
* variant you chose. `lookup` is the index of the node (in the tree) that points to this value.
*
* Some invariants:
* ```
* i == heap.data[heap.data[i].index].lookup // for all indices i
* i == heap.data[heap.data[i].lookup].index // for all indices i
* ```
*
* The structure is ordered so that each node is bigger than its parent. An immediate consequence is that the
* smallest value is the one at the root. This value can be lookup up in constant time (O(1)) at
* `heap.data[heap.data[0].index].value`
*
* The structure is designed to perform the following operations with the corresponding complexities:
*
* * peek (get the smallest value in set): O(1)
* * insert (insert a value in the set): 0(log(n))
* * pop (remove the smallest value in set): O(log(n))
* * replace (replace the smallest value in set with a new value): O(log(n))
*/
library Heap {
using SafeCast for *;

/**
* 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.
* @dev Binary heap that support values of type uint256.
*
* Some invariants:
* ```
* 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 ordered so that each node is bigger than 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`
*
* The structure is designed to perform the following operations with the corresponding complexities:
*
* * peek (get the smallest value in set): O(1)
* * insert (insert a value in the set): 0(log(n))
* * pop (remove the smallest value in set): O(log(n))
* * replace (replace the smallest value in set with a new value): O(log(n))
* Each element of that structures uses 2 storage slots.
*/
struct Uint256Heap {
Uint256HeapNode[] data;
Expand Down Expand Up @@ -294,27 +303,9 @@ library Heap {
}

/**
* 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 uint208 `value` and `lookup`, the index of the node
* that points to this value.
*
* Some invariants:
* ```
* 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 ordered so that each node is bigger than 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`
*
* The structure is designed to perform the following operations with the corresponding complexities:
* @dev Binary heap that support values of type uint208.
*
* * peek (get the smallest value in set): O(1)
* * insert (insert a value in the set): 0(log(n))
* * pop (remove the smallest value in set): O(log(n))
* * replace (replace the smallest value in set with a new value): O(log(n))
* Each element of that structures uses 1 storage slots.
*/
struct Uint208Heap {
Uint208HeapNode[] data;
Expand Down
28 changes: 19 additions & 9 deletions scripts/generate/templates/Heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ pragma solidity ^0.8.20;
import {SafeCast} from "../math/SafeCast.sol";
import {Comparators} from "../Comparators.sol";
import {Panic} from "../Panic.sol";
`;

const generate = ({ struct, node, valueType, indexType, blockSize }) => `\
/**
* A Heap is represented as an array of Node objects. In this array we store two overlapping structures:
* @dev Library for managing https://en.wikipedia.org/wiki/Binary_heap[binary heap] that can be used as
* https://en.wikipedia.org/wiki/Priority_queue[priority queue].
*
* Heaps are 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 ${valueType} \`value\` and \`lookup\`, the index of the node
* that points to this value.
* - An array of values (payload). At each index we store a \`value\` and \`lookup\`. Type of \`value\` depends on the
* variant you chose. \`lookup\` is the index of the node (in the tree) that points to this value.
*
* Some invariants:
* \`\`\`
* i == heap.data[heap[data].index].lookup // for all index i
* i == heap.data[heap[data].lookup].index // for all index i
* i == heap.data[heap.data[i].index].lookup // for all indices i
* i == heap.data[heap.data[i].lookup].index // for all indices i
* \`\`\`
*
* The structure is ordered so that each node is bigger than 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\`
* smallest value is the one at the root. This value can be lookup up in constant time (O(1)) at
* \`heap.data[heap.data[0].index].value\`
*
* The structure is designed to perform the following operations with the corresponding complexities:
*
Expand All @@ -35,6 +37,14 @@ const generate = ({ struct, node, valueType, indexType, blockSize }) => `\
* * pop (remove the smallest value in set): O(log(n))
* * replace (replace the smallest value in set with a new value): O(log(n))
*/
`;

const generate = ({ struct, node, valueType, indexType, blockSize }) => `\
/**
* @dev Binary heap that support values of type ${valueType}.
*
* Each element of that structures uses ${blockSize} storage slots.
*/
struct ${struct} {
${node}[] data;
}
Expand Down Expand Up @@ -298,7 +308,7 @@ function _unsafeNodeAccess(

// GENERATE
module.exports = format(
header,
header.trimEnd(),
'library Heap {',
format(
[].concat(
Expand Down