-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.lua
More file actions
26 lines (26 loc) · 844 Bytes
/
Stack.lua
File metadata and controls
26 lines (26 loc) · 844 Bytes
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
local Set = require('./AI/USER_AI/Set')
local Stack = {}
Stack.new = function(class)
return setmetatable({}, {__index=class, __tostring=class.tostring})
end
Stack.enque = function(self, value)
table.insert(self, 1, value)
end
Stack.deque = table.remove
Stack.push = table.insert
Stack.pop = table.remove
Stack.tostring = function(self, visit)
local check = visit or Set:new()
local index = check:index(self)
if index then
return string.format('<table %d>', index)
else
local result = nil
local index = check:insert(self)
table.foreach(self, function(index, value)
result = ( result and result .. ', ' or '' ) .. ( type(value) == type(self) and getmetatable(value).__tostring or tostring ) ( value, check )
end)
return string.format('Stack<table %d> {%s}', index, result or '')
end
end
return Stack