Skip to content

Instantly share code, notes, and snippets.

@creationix
Created October 17, 2012 15:27
Show Gist options
  • Save creationix/3906143 to your computer and use it in GitHub Desktop.
Save creationix/3906143 to your computer and use it in GitHub Desktop.
local Queue = Object:extend()
continuable.Queue = Queue
function Queue:initialize()
self.head = {}
self.tail = {}
self.index = 1
self.headLength = 0
end
function Queue:shift()
if self.index > self.headLength then
self.head, self.tail = self.tail, self.head
self.index = 1
self.headLength = #self.head
if self.headLength == 0 then
return
end
}
local value = self.head[self.index]
self.head[self.index] = nil
self.index = self.index + 1
return value
end
function Queue:push(item)
return table.insert(self.tail, item)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment