

local Queue = {}
Queue.__index = Queue

-- Creates a new entity queue
--
-- Entity queues store the properties of
-- the entities with not the entity themselves.
function new()
    local queue = {
        entries = {},
        length = 0,
    }
    setmetatable(queue, Queue)
    return queue
end

function Queue:add(e)
    self.length = self.length + 1
    self.entries[self.length] = e
    e.properties['queue_pos'] = self.length
end

function Queue:remove(e)
    local pos = e.properties['queue_pos']
    if pos == nil then
        error("Entity not in the queue")
    end
    for i=pos + 1,self.length do
        local ee = self.entries[i]
        ee.properties['queue_pos'] = i - 1
        self.entries[i - 1] = ee
    end
    e.properties['queue_pos'] = nil
    -- Gc out of bounds entries
    self.entries[0] = nil
    self.entries[self.length] = nil
    self.length = self.length - 1
end
