PriorityQueue = { size = 0, heap = {}, cmp = function(a,b) return a0 and self:Compare(i, parent)) do self:Swap(i, parent); i = parent; parent = floor(i/2); end end -- local next = pq:Peek(); function PriorityQueue:Peek() if (self.size <= 0) then return nil; end local node = self.heap[1]; return node.key, node.val; end -- local next = pq:Remove(); function PriorityQueue:Remove() if (self.size <= 0) then return nil; end self:Swap(1, self.size); self:PushDown(1); self.size = self.size - 1; local node = self.heap[self.size+1]; return node.key, node.val; end -- --------------------------- -- ------ Class Methods ------ -- --------------------------- function PriorityQueue:PushDown(index) local lc = index*2; local rc = lc + 1; if (lc <= self.size) then if (rc <= self.size and self:Compare(rc, lc)) then rc = lc; end if (self:Compare(lc, index)) then self:Swap(lc, index); end end end function PriorityQueue:Swap(i, j) local temp = self.heap[i]; self.heap[i] = self.heap[j]; self.heap[j] = temp; end function PriorityQueue:Compare(i, j) return self.cmp(self.heap[i].val, self.heap[j].val); end