OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
915 B

-- Simple FIFO Queue
local math = math or require("math")
local Queue = {}
Queue.__index = Queue
setmetatable(Queue, {
__call = function (cls)
return cls.new()
end,
__len = function (cls)
return self.length
end,
})
function Queue.new()
local self = setmetatable({}, Queue)
self.first = 1
self.last = 0
self.length = 0
return self
end
function Queue:push(e)
local last = self.last + 1
self.last = last
self[last] = e
self.length = self.length + 1
end
function Queue:pop()
-- To save some accesses
local first = self.first
if first > self.last then return nil end
local value = self[first]
self[first] = nil
self.first = first + 1
self.length = self.length - 1
return value
end
function Queue:get() return self[self.first] end
return Queue
-- wget -f http://mc.bashed.rocks:13699/lib/queue.lua lib/queue.lua