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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -3,6 +3,7 @@ const Allocator = std.mem.Allocator; | |||
const debug = std.debug; | ||||
const expect = std.testing.expect; | ||||
const expectEqual = std.testing.expectEqual; | ||||
const expectError = std.testing.expectError; | ||||
|
||||
/// Priority queue for storing generic data. Initialize with `init`. | ||||
pub fn PriorityQueue(comptime T: type) type { | ||||
|
@@ -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 { | ||||
const first = self.items[0]; | ||||
const last = self.items[self.len - 1]; | ||||
|
@@ -86,6 +89,23 @@ pub fn PriorityQueue(comptime T: type) type { | |||
return first; | ||||
} | ||||
|
||||
pub const Error = error{BoundsError}; | ||||
|
||||
/// 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 { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have precedence in the std lib for spelling "Index" out: zig/lib/std/segmented_list.zig Line 239 in 9390e8b
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also a good point. |
||||
if (self.len <= idx) | ||||
return Self.Error.BoundsError; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||
const last = self.items[self.len - 1]; | ||||
const item = self.items[idx]; | ||||
nmichaels marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
self.items[idx] = last; | ||||
nmichaels marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
self.len -= 1; | ||||
siftDown(self, 0); | ||||
return item; | ||||
} | ||||
|
||||
/// Return the number of elements remaining in the priority | ||||
/// queue. | ||||
pub fn count(self: Self) usize { | ||||
|
@@ -388,3 +408,27 @@ test "std.PriorityQueue: iterator" { | |||
|
||||
expectEqual(@as(usize, 0), map.count()); | ||||
} | ||||
|
||||
test "std.PriorityQueue: remove at index" { | ||||
var queue = PQ.init(debug.global_allocator, lessThan); | ||||
defer queue.deinit(); | ||||
|
||||
try queue.add(3); | ||||
try queue.add(2); | ||||
try queue.add(1); | ||||
expectError(PQ.Error.BoundsError, queue.removeIdx(5)); | ||||
|
||||
var it = queue.iterator(); | ||||
var elem = it.next(); | ||||
var idx: usize = 0; | ||||
const two_idx = while (elem != null) : (elem = it.next()) { | ||||
if (elem.? == 2) | ||||
break idx; | ||||
idx += 1; | ||||
} else unreachable; | ||||
|
||||
expectEqual(queue.removeIdx(two_idx), 2); | ||||
expectEqual(queue.remove(), 1); | ||||
expectEqual(queue.remove(), 3); | ||||
expectEqual(queue.removeOrNull(), null); | ||||
} |
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.
Should
remove
be changed to callremoveIdx(0)
now?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.
Good point!