summaryrefslogtreecommitdiff
path: root/include/queue.lua
diff options
context:
space:
mode:
Diffstat (limited to 'include/queue.lua')
-rw-r--r--include/queue.lua29
1 files changed, 29 insertions, 0 deletions
diff --git a/include/queue.lua b/include/queue.lua
new file mode 100644
index 0000000..2eb77c6
--- /dev/null
+++ b/include/queue.lua
@@ -0,0 +1,29 @@
+Queue = {}
+
+function Queue:new()
+ o = { first = 1, top = 0, data = {} }
+ self.__index = self
+ return setmetatable(o, self)
+end
+
+function Queue:push(val)
+ local top = self.top + 1
+ self.top = top
+ self.data[top] = val
+end
+
+function Queue:pop()
+ if self:empty() then
+ return nil
+ end
+ local val = self.data[self.first]
+ self.data[self.first] = nil
+ self.first = self.first + 1
+ return val
+end
+
+function Queue:empty()
+ return self.top < self.first
+end
+
+return Queue