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
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 38 additions & 3 deletions lib/std/priority_queue.zig
@@ -1,8 +1,10 @@
const std = @import("std.zig");
const Allocator = std.mem.Allocator;
const debug = std.debug;
const assert = debug.assert;
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 {
Expand Down Expand Up @@ -77,13 +79,23 @@ 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!

const first = self.items[0];
return self.removeIndex(0);
}

/// Remove and return element at index. Indices are in the
/// same order as iterator, which is not necessarily priority
/// order.
pub fn removeIndex(self: *Self, idx: usize) T {
nmichaels marked this conversation as resolved.
Show resolved Hide resolved
assert(self.len > idx);
nmichaels marked this conversation as resolved.
Show resolved Hide resolved
const last = self.items[self.len - 1];
self.items[0] = last;
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 first;
return item;
}

/// Return the number of elements remaining in the priority
Expand Down Expand Up @@ -388,3 +400,26 @@ 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);

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.removeIndex(two_idx), 2);
expectEqual(queue.remove(), 1);
expectEqual(queue.remove(), 3);
expectEqual(queue.removeOrNull(), null);
}