1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00
RedMew/utils/queue.lua
Valansch 7fa83bae6c Add Tetris (#605)
Somehow nailed everything on the first try. What a pro.
2019-01-11 13:31:23 -05:00

35 lines
570 B
Lua

local Queue = {}
function Queue.new()
local queue = {_head = 0, _tail = 0}
return queue
end
function Queue.size(queue)
return queue._tail - queue._head
end
function Queue.push(queue, element)
local index = queue._head
queue[index] = element
queue._head = index - 1
end
function Queue.peek(queue)
return queue[queue._tail]
end
function Queue.pop(queue)
local index = queue._tail
local element = queue[index]
queue[index] = nil
if element then
queue._tail = index - 1
end
return element
end
return Queue