-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
add priority queue implementation (binary heap) #4146
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
|
That looks pretty good. I'm not sure if you need the copy constructor though. I think the only function that require it are |
|
I can see two ways of getting rid of the remaining copies: A) using unsafe blocks, and moving around 1 junk location After that, the only thing keeping the Copy trait as a requirement is not being able to move out of self at the moment. |
|
@thestinger: As far as I know, moves don't have any overhead, they are just for tracking who owns the pointers. So I suggest using swaps. |
|
After the conversations about this on IRC and some testing, I decided to go with using unsafe blocks. I think it's worth using it for a data structure that will likely be fairly widely used in algorithms where performance is critical. For some data types (strings), the comparisons dominate the time - but for some the difference between compare + move and compare + swap is pretty big. I documented the reasoning behind doing it in a comment to make it clear why it's safe, and why it was done for anyone looking at the code in the future. |
|
Here's the diff for the alternate implementation without unsafe (using swaps), for the record: https://gist.github.com/4299825 |
|
Merged. Thanks! |
This adds a priority queue implemented as a binary heap to std. The API is heavily inspired by python's heapq module except that it's encapsulated in a container and uses a max-heap to get the default of high values as the max priority and sorting in ascending order.
The naming (priority_queue::PriorityQueue) is quite verbose, so it could be heapq::HeapQ instead if that's preferred.