authorgravatar for voroskoi@gmail.comVÖRÖSKŐI András <voroskoi@gmail.com> 2022-07-17 22:42:31+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-22 19:25:38+03:00
logd925d19cfcabd96fdc4459e11ecb85a4f42ec655
tree40a6e32e492f72d72b21f38cba694d3e253e0956
parentd1d892c83ca7beaf235147341b7e68d3619dd829

PriorityQueue: use compareFn in update()

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 #9918

1 files changed, 6 insertions(+), 1 deletions(-)

lib/std/priority_queue.zig+6-1
...@@ -219,7 +219,12 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF...@@ -219,7 +219,12 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF
219 }219 }
220220
221 pub fn update(self: *Self, elem: T, new_elem: T) !void {221 pub fn update(self: *Self, elem: T, new_elem: T) !void {
222 var update_index: usize = std.mem.indexOfScalar(T, self.items[0..self.len], elem) orelse return error.ElementNotFound;222 const update_index = blk: {
223 for (self.items) |item, idx| {
224 if (compareFn(self.context, item, elem).compare(.eq)) break :blk idx;
225 }
226 return error.ElementNotFound;
227 };
223 const old_elem: T = self.items[update_index];228 const old_elem: T = self.items[update_index];
224 self.items[update_index] = new_elem;229 self.items[update_index] = new_elem;
225 switch (compareFn(self.context, new_elem, old_elem)) {230 switch (compareFn(self.context, new_elem, old_elem)) {