summaryrefslogtreecommitdiff
path: root/include/queue.lua
blob: 2eb77c697dcd2b95deb7bb64b8dfcd13a0ed1bd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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