DoxigAlpha

update

Function parameters

Parameters

#
self:*Self
elem:T
new_elem:T

Priority queue for storing generic data.

Functions

#
PriorityQueue
Priority queue for storing generic data.

Source

Implementation

#
pub fn update(self: *Self, elem: T, new_elem: T) !void {
    const update_index = blk: {
        var idx: usize = 0;
        while (idx < self.items.len) : (idx += 1) {
            const item = self.items[idx];
            if (compareFn(self.context, item, elem) == .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)) {
        .lt => siftUp(self, update_index),
        .gt => siftDown(self, update_index),
        .eq => {}, // Nothing to do as the items have equal priority
    }
}