| 1 |
-- Code from http://lua-users.org/wiki/SimpleStack |
| 2 |
--module(...,package.seeall) |
| 3 |
local M={} |
| 4 |
local Stack = {} |
| 5 |
M.Stack = Stack |
| 6 |
local unpack = table.unpack |
| 7 |
-- Create a Table with stack functions |
| 8 |
function Stack:Create() |
| 9 |
|
| 10 |
-- stack table |
| 11 |
local t = {} |
| 12 |
-- entry table |
| 13 |
t._et = {} |
| 14 |
|
| 15 |
-- push a value on to the stack |
| 16 |
function t:push(...) |
| 17 |
if ... then |
| 18 |
local targs = {...} |
| 19 |
-- add values |
| 20 |
for _,v in pairs(targs) do |
| 21 |
table.insert(self._et, v) |
| 22 |
end |
| 23 |
end |
| 24 |
end |
| 25 |
|
| 26 |
-- pop a value from the stack |
| 27 |
function t:pop(num) |
| 28 |
|
| 29 |
-- get num values from stack |
| 30 |
local num = num or 1 |
| 31 |
|
| 32 |
-- return table |
| 33 |
local entries = {} |
| 34 |
|
| 35 |
-- get values into entries |
| 36 |
for i = 1, num do |
| 37 |
-- get last entry |
| 38 |
if #self._et ~= 0 then |
| 39 |
table.insert(entries, self._et[#self._et]) |
| 40 |
-- remove last value |
| 41 |
table.remove(self._et) |
| 42 |
else |
| 43 |
break |
| 44 |
end |
| 45 |
end |
| 46 |
-- return unpacked entries |
| 47 |
return unpack(entries) |
| 48 |
end |
| 49 |
|
| 50 |
-- get entries |
| 51 |
function t:getn() |
| 52 |
return #self._et |
| 53 |
end |
| 54 |
|
| 55 |
-- list values |
| 56 |
function t:list() |
| 57 |
for i,v in pairs(self._et) do |
| 58 |
print(i, v) |
| 59 |
end |
| 60 |
end |
| 61 |
function t:join(s) |
| 62 |
return table.concat(self._et,s) |
| 63 |
end |
| 64 |
return t |
| 65 |
end |
| 66 |
|
| 67 |
return M |