mirror of
https://github.com/OpenDriver2/REDRIVER2.git
synced 2024-11-22 18:32:42 +01:00
79 lines
1.4 KiB
Lua
79 lines
1.4 KiB
Lua
-- Stack Table
|
|
-- Uses a table as stack, use <table>:push(value) and <table>:pop()
|
|
-- Lua 5.1 compatible
|
|
|
|
unpack = table.unpack or unpack
|
|
|
|
-- GLOBAL
|
|
Stack = {}
|
|
|
|
-- Create a Table with stack functions
|
|
function Stack:Create()
|
|
|
|
-- stack table
|
|
local t = {}
|
|
-- entry table
|
|
t._et = {}
|
|
|
|
-- push a value on to the stack
|
|
function t:push(...)
|
|
if ... then
|
|
local targs = {...}
|
|
-- add values
|
|
for _,v in ipairs(targs) do
|
|
table.insert(self._et, v)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- pop a value from the stack
|
|
function t:pop(num)
|
|
|
|
-- get num values from stack
|
|
local num = num or 1
|
|
|
|
-- return table
|
|
local entries = {}
|
|
|
|
-- get values into entries
|
|
for i = 1, num do
|
|
-- get last entry
|
|
if #self._et ~= 0 then
|
|
table.insert(entries, self._et[#self._et])
|
|
-- remove last value
|
|
table.remove(self._et)
|
|
else
|
|
break
|
|
end
|
|
end
|
|
-- return unpacked entries
|
|
return unpack(entries)
|
|
end
|
|
|
|
-- get entries
|
|
function t:getn()
|
|
return #self._et
|
|
end
|
|
|
|
-- get entry form index
|
|
function t:get(index)
|
|
return self._et[index]
|
|
end
|
|
|
|
-- list values
|
|
function t:list()
|
|
for i,v in pairs(self._et) do
|
|
print(i, v)
|
|
end
|
|
end
|
|
-- list values
|
|
function t:find_value(value)
|
|
for i,v in pairs(self._et) do
|
|
if v == value then
|
|
return i
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
return t
|
|
end |