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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add priority queue as a data structure to be able to implement max/min heaps #3410

Open
Rassska opened this issue May 15, 2022 · 8 comments 路 May be fixed by #5084
Open

Add priority queue as a data structure to be able to implement max/min heaps #3410

Rassska opened this issue May 15, 2022 · 8 comments 路 May be fixed by #5084
Labels
feature New contracts, functions, or helpers. idea
Milestone

Comments

@Rassska
Copy link

Rassska commented May 15, 2022

Hi, there! Thank you so much for such great source!

馃 Motivation

I'm operating with huge amount of data in my smart contracts and sometimes it's pretty useful to handle queries by simply implementing common data structures in terms of saving more gas. We already do have a dequeue/bitmaps impls and now I want to add a priority queue if it's possible.

Let me know, what do you think about it <3

馃摑 Details

@frangio frangio changed the title [FR] Add priority queue as a data structure to be able to implement max/min heaps Add priority queue as a data structure to be able to implement max/min heaps May 15, 2022
@frangio
Copy link
Contributor

frangio commented May 15, 2022

A heap data structure would be a pretty cool addition. O(log n) queue and dequeue operations are acceptable.

Can you please give example use cases?

@Rassska
Copy link
Author

Rassska commented May 15, 2022

A heap data structure would be a pretty cool addition. O(log n) queue and dequeue operations are acceptable.

Can you please give example use cases?

Okay, I'll provide it a bit later.
Also, I do have an extra question about sorting algorithms. Why they are not presented in OZ? At least some quick sort or etc?

@frangio
Copy link
Contributor

frangio commented May 15, 2022

Sorting algorithms are O(n log n) so they may run out of gas with large arrays, which may be manipulable by users and become an attack vector. Specific contracts may have reasonable bounds on array length but we err on the conservative side to help people avoid creating an attack vector unknowingly.

A min/max heap will be a good alternative for use cases that require sorting.

@frangio
Copy link
Contributor

frangio commented May 16, 2022

Hm, I'm not sure Fibonacci heaps are acceptable for this context. I have to admit I'm not super familiar with them but they apparently can have high overhead which will probably translate to expensive storage operations, and worst case complexity is linear.

@Bijan-Massoumi
Copy link

This would be fantastic addition

@frangio
Copy link
Contributor

frangio commented Oct 5, 2023

@Bijan-Massoumi Please share your use case so we can consider it for prioritization!

@Amxx Amxx added idea feature New contracts, functions, or helpers. labels Oct 6, 2023
@rocketman-21
Copy link

I just implemented this w/associated tests. Will open a PR / can share code if y'all want.

Use case is an open submission box that anyone can submit to, and a specific set of accounts can vote on submissions, and gas-efficiently pull off the top voted submission.

@rocketman-21
Copy link

This contract has been audited by c4

https://github.com/collectivexyz/revolution-protocol/blob/main/packages/revolution/src/culture-index/MaxHeap.sol

@ernestognw ernestognw modified the milestones: 5.x, 5.1 Jan 11, 2024
@cairoeth cairoeth mentioned this issue Jun 11, 2024
3 tasks
@Amxx Amxx linked a pull request Jun 16, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New contracts, functions, or helpers. idea
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants