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 removeIdx function to PriorityQueue. #4070

Merged
merged 3 commits into from Jan 8, 2020
Merged

Add removeIdx function to PriorityQueue. #4070

merged 3 commits into from Jan 8, 2020

Conversation

nmichaels
Copy link
Contributor

It's awkward to use, but lets me cancel events in an event queue.

It's awkward to use, but lets me cancel events in an event queue.
@daurnimator daurnimator added the standard library This issue involves writing Zig code for the standard library. label Jan 6, 2020
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

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

Thanks for the patch! Got a few review comments for you.

Comment on lines 94 to 100
/// Remove and return element at index. If there aren't enough
/// elements to remove from idx, returns null. Indices are in
/// the same order as iterator, which is not necessarily
/// priority order.
pub fn removeIdx(self: *Self, idx: usize) !T {
if (self.len <= idx)
return Self.Error.BoundsError;
Copy link
Member

Choose a reason for hiding this comment

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

The docs says "returns null", but it's an error union. Also, should this be an assertion rather than an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, caught the docs and code disagreeing. You're probably right about it being an assertion. It saves a branch, and remove doesn't return an error.

@@ -77,6 +78,8 @@ pub fn PriorityQueue(comptime T: type) type {
return if (self.len > 0) self.remove() else null;
}

/// Remove and return the highest priority element from the
/// queue.
pub fn remove(self: *Self) T {
Copy link
Member

Choose a reason for hiding this comment

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

Should remove be changed to call removeIdx(0) now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!

/// elements to remove from idx, returns null. Indices are in
/// the same order as iterator, which is not necessarily
/// priority order.
pub fn removeIdx(self: *Self, idx: usize) !T {
Copy link
Member

Choose a reason for hiding this comment

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

We have precedence in the std lib for spelling "Index" out:

fn shelfIndex(list_index: usize) ShelfIndex {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also a good point.

Rename to removeIndex, change return type and use an assertion, fix
the comment, and have remove call removeIndex.
lib/std/priority_queue.zig Outdated Show resolved Hide resolved
lib/std/priority_queue.zig Outdated Show resolved Hide resolved
lib/std/priority_queue.zig Outdated Show resolved Hide resolved
lib/std/priority_queue.zig Outdated Show resolved Hide resolved
Co-Authored-By: Dmitry Atamanov <data-man@users.noreply.github.com>
@andrewrk andrewrk merged commit 38ce7f6 into ziglang:master Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants