2018-01-26 01:31:09 +02:00
|
|
|
local Queue = {}
|
|
|
|
|
|
|
|
function Queue.new()
|
2018-11-20 12:46:42 +02:00
|
|
|
local queue = {_head = 0, _tail = 0}
|
2018-01-26 01:31:09 +02:00
|
|
|
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
|
|
|
|
|
2019-01-11 20:25:54 +02:00
|
|
|
if element then
|
|
|
|
queue._tail = index - 1
|
|
|
|
end
|
2018-01-26 01:31:09 +02:00
|
|
|
return element
|
|
|
|
end
|
|
|
|
|
2018-11-20 12:46:42 +02:00
|
|
|
return Queue
|