Skip to content

Commit

Permalink
PriorityQueue: use compareFn in update()
Browse files Browse the repository at this point in the history
update() calls mem.indexOfScalar() which uses `==` for comparing items,
which fails when the operator is not supported.
As PirorityQueue needs a comparing function anyway we can use `.eq` results
to identify matching objects.

closes ziglang#9918
  • Loading branch information
voroskoi authored and wooster0 committed Jul 24, 2022
1 parent 77a3e8f commit b3af416
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/std/priority_queue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
}

pub fn update(self: *Self, elem: T, new_elem: T) !void {
var update_index: usize = std.mem.indexOfScalar(T, self.items[0..self.len], elem) orelse return error.ElementNotFound;
const update_index = blk: {
for (self.items) |item, idx| {
if (compareFn(self.context, item, elem).compare(.eq)) break :blk idx;
}
return error.ElementNotFound;
};
const old_elem: T = self.items[update_index];
self.items[update_index] = new_elem;
switch (compareFn(self.context, new_elem, old_elem)) {
Expand Down

0 comments on commit b3af416

Please sign in to comment.