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

Add MaxHeap #5076

Closed
wants to merge 1 commit into from
Closed

Add MaxHeap #5076

wants to merge 1 commit into from

Conversation

cairoeth
Copy link
Contributor

Fixes #3410 — WIP

PR Checklist

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

Copy link

changeset-bot bot commented Jun 11, 2024

⚠️ No Changeset found

Latest commit: f028cb4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Comment on lines +60 to +63
struct Node {
uint256 value;
uint256 heapIndex;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we can do something like:

  • assuming the heapIndex fits into something smaller. I'm not expecting any onchain structure to have more than type(uint32).max elements. Maybe just type(uint16).max is enough (that is 65k elements).
  • limit the value to uint224

So that Node fits into a single slot.

Comment on lines +66 to +67
mapping(uint256 => uint256) tree;
mapping(uint256 => Node) items;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm expecting one of these mapping (maybe both) can be implemented using array. That would result in better data locality when we move to verkle.

Copy link
Collaborator

Choose a reason for hiding this comment

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

also, using array with a value smaller than uint256 (for tree) would compress the data (shared slots)

Comment on lines +77 to +78
(heap.tree[fpos], heap.tree[spos]) = (heap.tree[spos], heap.tree[fpos]);
(heap.items[heap.tree[fpos]].heapIndex, heap.items[heap.tree[spos]].heapIndex) = (fpos, spos);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
(heap.tree[fpos], heap.tree[spos]) = (heap.tree[spos], heap.tree[fpos]);
(heap.items[heap.tree[fpos]].heapIndex, heap.items[heap.tree[spos]].heapIndex) = (fpos, spos);
(
heap.tree[fpos], heap.items[heap.tree[fpos]].heapIndex,
heap.tree[spos], heap.items[heap.tree[spos]].heapIndex
) = (
heap.tree[spos], spos,
heap.tree[fpos], fpos
);

}

function heapify(MaxHeap storage heap, uint256 pos) internal {
if (pos >= (heap.size / 2) && pos <= heap.size) return;
Copy link
Collaborator

@Amxx Amxx Jun 12, 2024

Choose a reason for hiding this comment

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

I think pos <= heap.size should be pos < heap.size.
Also pos >= heap.size should be an error if that is a user call ... but that would require changes to the recursive calls

uint256 heapIndex;
}

struct MaxHeap {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Random though: maybe we want a Heap, instead of a MaxHeap, with a function pointer for the comparator.

}
}

function insert(MaxHeap storage heap, uint256 itemId, uint256 value) internal {
Copy link
Collaborator

Choose a reason for hiding this comment

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

My understand is that itemId must be unique, so that all items are clearly identified ... but is then useless to the user. Maybe it should be automatically generated ... so that the user doesn't have to worry about it, and can just push a value.

@Amxx Amxx mentioned this pull request Jun 16, 2024
4 tasks
@cairoeth
Copy link
Contributor Author

Closing in favor of improved implementation at #5084

@cairoeth cairoeth closed this Jun 27, 2024
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