-
Notifications
You must be signed in to change notification settings - Fork 139
Feat: Add order_by
parameter to @priority_queue
#2753
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
Conversation
Pull Request Test Coverage Report for Build 1305Details
💛 - Coveralls |
// Errors | ||
|
||
// Types and methods | ||
type OrderBy |
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 make this type opaque/abstract?
In both Rust and Haskell, there is a common idiom called a “newtype” wrapper. The idea is to create a lightweight wrapper around an existing type, without adding runtime overhead, but allowing you to give it different traits/instances.
They are zero-cost abstractions: at runtime, the wrapper disappears. If moonbit's newtype is zero-cost, I suggest adding a similar newtype in |
Newtype is too tricky for newcomer. It should be considered as last resort. |
It doesn't really matter to me whether or not there is an ORDER BY in actual user usage, but I am more concerned about the API consistency issues it brings. The current export method is definitely not suitable. Even if this thing is added, it should be placed in a more generic place. I am actually thinking about whether it can be changed to a Boolean value? The main issue is that our previous approach was similar to the implementation of overloading operators, which involved declaring a new type and overriding its Compare trait to reverse the order. I don't think this is intuitive either. |
I think the problem with newtype is the trade off between code size & performance @bobzhang The code size will be doubled due to monofy, but the performance would be greater. |
We would follow @quake 's suggestion and provide the |
Summary
Fixed #2613, this PR enhances the
@priority_queue
by adding anorder_by
parameter, enabling it to function as both a max-heap (DESC, the default behavior) and a min-heap(ASC).Key Changes:
Added
order_by
Parameter: Thenew()
,from_array()
, andof()
functions now accept an optionalorder_by
parameter. This allows users to specify the ordering of elements.@priority_queue.desc
(default): The queue operates as a max-heap, with the largest element at the top.@priority_queue.asc
: The queue operates as a min-heap, with the smallest element at the top.Updated Internal Logic: The internal functions
meld()
andmerges()
have been modified to respect theorder_by
parameter, ensuring that elements are compared and ordered correctly.Exported
asc
anddesc
: To provide a clean public API, asc and desc values of typeOrderBy
are now exported from the@priority_queue
package.Added Tests: New test cases have been added to
priority_queue_test.mbt
to verify the functionality of both ascending and descending order modes, ensuring that pop and peek return the correct elements in each case.Example