Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Queue Table
- -- Uses a table as stack, use <table>:push(value) and <table>:pop()
- -- Lua 5.1 compatible
- -- Create a Table with queue functions
- function Create()
- -- queue table
- local t = {first = 0, last = -1}
- -- push a value on to the queue
- function t:push(...)
- if ... then
- local last
- local targs = {...}
- -- add values
- for _,v in ipairs(targs) do
- last = self.last + 1
- self.last = last
- self[last] = v
- end
- end
- end
- -- pop a value from the queue
- function t:pop(num)
- -- get num values from queue
- local num = num or 1
- -- return table
- local entries = {}
- local first
- -- get values into entries
- for i = 1, num do
- first = self.first
- -- get first entry
- if first <= self.last then
- table.insert(entries, self[first])
- -- remove last value
- self[first] = nil
- self.first = first + 1
- else
- break
- end
- end
- -- return unpacked entries
- return unpack(entries)
- end
- -- get entries
- function t:size()
- return self.last - self.first + 1
- end
- function t:empty()
- return self.first > self.last
- end
- return t
- end
Advertisement
Add Comment
Please, Sign In to add comment