authorgravatar for bsvasan92@icloud.comBhargav Srinivasan <bsvasan92@icloud.com> 2020-09-22 02:12:35-07:00
committergravatar for bsvasan92@icloud.comBhargav Srinivasan <bsvasan92@icloud.com> 2020-09-22 02:12:35-07:00
loga5140cc9020f4e3649ccc14ca4317829ea289306
tree7fdddc09806c08110b1d2b59ecc29712dd404f0a
parent1345f87f4efb7444516fceb76b1d60ec18ddebc1

implemented efficient heapreplace


1 files changed, 7 insertions(+), 2 deletions(-)

lib/std/priority_queue.zig+7-2
......@@ -204,8 +204,13 @@ pub fn PriorityQueue(comptime T: type) type {
204204 pub fn update(self: *Self, elem: T, new_elem: T) !void {
205205 var update_index: usize = linearSearch(elem, self.items);
206206 assert (update_index >= 0 and update_index < self.items.len);
207 _ = self.removeIndex(update_index);
208 try self.add(new_elem);
207 // Heapreplace:
208 // replace the item: self.items[update_index]= new_elem;
209 // swap the new item to the top of the heap: std.mem.swap(heap[0], heap[update_index]);
210 // sift up or down: which has been generically implemented as sift down: siftDown(self, 0)
211 self.items[update_index] = new_elem;
212 std.mem.swap(T, &self.items[0], &self.items[update_index]);
213 siftDown(self, 0);
209214 }
210215
211216 pub const Iterator = struct {