Created
October 17, 2012 15:27
-
-
Save creationix/3906143 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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