- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.lua
More file actions
Latest commit
76 lines (62 loc) · 1.47 KB
/
Copy pathStack.lua
File metadata and controls
76 lines (62 loc) · 1.47 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
--- Stack Table
--- Uses a table as stack, use <table>:push(value) and <table>:pop()
-- Lua 5.1 compatible
---@classStack
Stack= {}
--- Create a Table with stack functions
functionStack:Create()
-- stack table
localt= {}
-- entry table
t._et= {}
--- Push a value on to the stack
---@varargany @Values to push on the stack
functiont:push(...)
if...then
localtargs= {...}
-- add values
for_,vinipairs(targs) do
table.insert(self._et, v)
end
end
end
--- Pop a value from the stack
---@paramnum?integer Number of values to pop off the stack
---@return...any
functiont:pop(num)
-- get num values from stack
num=numor1
-- return table
localentries= {}
-- get values into entries
fori=1, numdo
-- get last entry
if#self._et~=0then
table.insert(entries, self._et[#self._et])
-- remove last value
table.remove(self._et)
else
break
end
end
-- return unpacked entries
returnunpack(entries)
end
--- The number of entries on the stack
functiont:getn()
return#self._et
end
--- Print the list of values on the stack
functiont:list()
fori,vinpairs(self._et) do
print(i, v)
end
end
--- Returs the list of values on the stack in a string from (one line per entry)
---@returnstring
functiont:tostring()
returntable.concat(self._et, "\n")
end
returnt
end
returnStack