From a5140cc9020f4e3649ccc14ca4317829ea289306 Mon Sep 17 00:00:00 2001 From: Bhargav Srinivasan Date: Tue, 22 Sep 2020 02:12:35 -0700 Subject: [PATCH] implemented efficient heapreplace --- lib/std/priority_queue.zig | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/std/priority_queue.zig b/lib/std/priority_queue.zig index 6e0e47dd05..9c6545c4e1 100644 --- a/lib/std/priority_queue.zig +++ b/lib/std/priority_queue.zig @@ -204,8 +204,13 @@ pub fn PriorityQueue(comptime T: type) type { pub fn update(self: *Self, elem: T, new_elem: T) !void { var update_index: usize = linearSearch(elem, self.items); assert (update_index >= 0 and update_index < self.items.len); - _ = self.removeIndex(update_index); - try self.add(new_elem); + // Heapreplace: + // replace the item: self.items[update_index]= new_elem; + // swap the new item to the top of the heap: std.mem.swap(heap[0], heap[update_index]); + // sift up or down: which has been generically implemented as sift down: siftDown(self, 0) + self.items[update_index] = new_elem; + std.mem.swap(T, &self.items[0], &self.items[update_index]); + siftDown(self, 0); } pub const Iterator = struct {