| author | |
| committer | |
| log | f8ea292d0989ac8245399a8dad6f27c8b28cd696 |
| tree | 2f5bb0e1304a645e820e7552af445c83b17ebab0 |
| parent | b5222f86eec68088b9bd83ad2f648654242082ec |
Previously the update() method would iterate over its capacity, which may contain uninitialized memory or already removed elements.2 files changed, 26 insertions(+), 4 deletions(-)
lib/std/priority_dequeue.zig+13-2| ... | ... | @@ -390,8 +390,10 @@ pub fn PriorityDequeue(comptime T: type, comptime Context: type, comptime compar |
| 390 | 390 | |
| 391 | 391 | pub fn update(self: *Self, elem: T, new_elem: T) !void { |
| 392 | 392 | const old_index = blk: { |
| 393 | for (self.items) |item, idx| { | |
| 394 | if (compareFn(self.context, item, elem).compare(.eq)) break :blk idx; | |
| 393 | var idx: usize = 0; | |
| 394 | while (idx < self.len) : (idx += 1) { | |
| 395 | const item = self.items[idx]; | |
| 396 | if (compareFn(self.context, item, elem) == .eq) break :blk idx; | |
| 395 | 397 | } |
| 396 | 398 | return error.ElementNotFound; |
| 397 | 399 | }; |
| ... | ... | @@ -778,6 +780,15 @@ test "std.PriorityDequeue: update same max queue" { |
| 778 | 780 | try expectEqual(@as(u32, 1), queue.removeMax()); |
| 779 | 781 | } |
| 780 | 782 | |
| 783 | test "std.PriorityDequeue: update after remove" { | |
| 784 | var queue = PDQ.init(testing.allocator, {}); | |
| 785 | defer queue.deinit(); | |
| 786 | ||
| 787 | try queue.add(1); | |
| 788 | try expectEqual(@as(u32, 1), queue.removeMin()); | |
| 789 | try expectError(error.ElementNotFound, queue.update(1, 1)); | |
| 790 | } | |
| 791 | ||
| 781 | 792 | test "std.PriorityDequeue: iterator" { |
| 782 | 793 | var queue = PDQ.init(testing.allocator, {}); |
| 783 | 794 | var map = std.AutoHashMap(u32, void).init(testing.allocator); |
lib/std/priority_queue.zig+13-2| ... | ... | @@ -218,8 +218,10 @@ pub fn PriorityQueue(comptime T: type, comptime Context: type, comptime compareF |
| 218 | 218 | |
| 219 | 219 | pub fn update(self: *Self, elem: T, new_elem: T) !void { |
| 220 | 220 | const update_index = blk: { |
| 221 | for (self.items) |item, idx| { | |
| 222 | if (compareFn(self.context, item, elem).compare(.eq)) break :blk idx; | |
| 221 | var idx: usize = 0; | |
| 222 | while (idx < self.len) : (idx += 1) { | |
| 223 | const item = self.items[idx]; | |
| 224 | if (compareFn(self.context, item, elem) == .eq) break :blk idx; | |
| 223 | 225 | } |
| 224 | 226 | return error.ElementNotFound; |
| 225 | 227 | }; |
| ... | ... | @@ -591,6 +593,15 @@ test "std.PriorityQueue: update same max heap" { |
| 591 | 593 | try expectEqual(@as(u32, 1), queue.remove()); |
| 592 | 594 | } |
| 593 | 595 | |
| 596 | test "std.PriorityQueue: update after remove" { | |
| 597 | var queue = PQlt.init(testing.allocator, {}); | |
| 598 | defer queue.deinit(); | |
| 599 | ||
| 600 | try queue.add(1); | |
| 601 | try expectEqual(@as(u32, 1), queue.remove()); | |
| 602 | try expectError(error.ElementNotFound, queue.update(1, 1)); | |
| 603 | } | |
| 604 | ||
| 594 | 605 | test "std.PriorityQueue: siftUp in remove" { |
| 595 | 606 | var queue = PQlt.init(testing.allocator, {}); |
| 596 | 607 | defer queue.deinit(); |