Skip to content
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

Binary heap #5084

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open

Binary heap #5084

wants to merge 24 commits into from

Conversation

Amxx
Copy link
Collaborator

@Amxx Amxx commented Jun 16, 2024

Fixes #3410
Alternative to #5076

This PR uses names from https://en.wikipedia.org/wiki/Heap_(data_structure)#Operations

  • Basic
    • peek
    • insert
    • pop
    • replace
  • Internal
    • siftUp
    • siftDown

For the inspection part, we use length instead of size for consistencty with all other structures in the utils/struct folder.

PR Checklist

  • Tests
  • Tests Fuzzing
  • Documentation
  • Changeset entry (run npx changeset add)

Copy link

changeset-bot bot commented Jun 16, 2024

🦋 Changeset detected

Latest commit: c083d79

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openzeppelin-solidity Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

.githooks/pre-push Outdated Show resolved Hide resolved
@Amxx Amxx requested review from ernestognw and cairoeth June 21, 2024 08:39
test/utils/structs/Heap.test.js Outdated Show resolved Hide resolved
test/utils/structs/Heap.test.js Outdated Show resolved Hide resolved
@Amxx Amxx added this to the 5.1 milestone Jun 26, 2024
contracts/utils/README.adoc Outdated Show resolved Hide resolved
.changeset/great-pianos-work.md Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The 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

@@ -22,6 +22,7 @@ import {ERC165} from "../utils/introspection/ERC165.sol";
import {ERC165Checker} from "../utils/introspection/ERC165Checker.sol";
import {ERC1967Utils} from "../proxy/ERC1967/ERC1967Utils.sol";
import {ERC721Holder} from "../token/ERC721/utils/ERC721Holder.sol";
import {Heap} from "../utils/structs/Heap.sol";
Copy link
Member

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?

Suggested change
import {Heap} from "../utils/structs/Heap.sol";
import {Heap} from "../utils/structs/Heap.sol";
import {Comparators} from "../utils/Comparators.sol";

Copy link
Collaborator Author

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 Comparators library, 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

Comment on lines +16 to +20
* 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 ${valueType} \`value\` and \`lookup\`, the index of the node
* that points to this value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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 ${valueType} \`value\` and \`lookup\`, the index of the node
* that points to this value.
* A Heap is represented by an array of Node objects where two different data structures are represented:
*
* * A tree where the first element (i.e. at index 0) is the root and children are located at \`2*i+i\` and \`2*i+2\` for each index \`i\`.
* * A list of payload values where each index contains a ${valueType} \`value\` and a \`lookup\` with its index in the tree.
*
* Every tree node includes an \`index\` pointer that indicates the position of its corresponding value within the list of payloads.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this block should be in the top NatSpec and not in the struct docs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets discuss that part on our call

scripts/generate/templates/Heap.js Outdated Show resolved Hide resolved
scripts/generate/templates/Heap.js Outdated Show resolved Hide resolved
scripts/generate/templates/Heap.js Outdated Show resolved Hide resolved
@cairoeth cairoeth mentioned this pull request Jun 27, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add priority queue as a data structure to be able to implement max/min heaps
2 participants