-- // NOTE: This module is not written by me, but slightly modified \\ --


function PriorityQueue.new()
	return setmetatable({
		heap = {},
		current_size = 0
	}, PriorityQueue)
end

function PriorityQueue:isEmpty()
	return self.current_size == 0
end

function PriorityQueue:size()
	return self.current_size
end

function PriorityQueue:swim()
	-- Swim up on the tree and fix the order heap property.
	local floor = floor
	local heap = self.heap
	local i = self.current_size
	
	local half = floor(i / 2)
	
	while half > 0 do
		if heap[i][2] < heap[half][2] then
			heap[i], heap[half] = heap[half], heap[i]
		end

		i = half
		half = floor(i / 2)
	end
end

function PriorityQueue:Push(v, p)
    --[[ Put an item on the queue.
    Args:
        v: the item to be stored
        p(number): the priority of the item
    ]]--
	--
	table.insert(self.heap, {v, p})
	self.current_size += 1
	self:swim()
end
