From c795fb754bfd5c84c1bfd7dc793c6519f01109ea Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 20 Oct 2021 13:47:26 +0800 Subject: *misc --- .../DefaultContent/Libraries/Container/stack.lua | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Resources/DefaultContent/Libraries/Container/stack.lua (limited to 'Resources/DefaultContent/Libraries/Container/stack.lua') diff --git a/Resources/DefaultContent/Libraries/Container/stack.lua b/Resources/DefaultContent/Libraries/Container/stack.lua new file mode 100644 index 0000000..d828c81 --- /dev/null +++ b/Resources/DefaultContent/Libraries/Container/stack.lua @@ -0,0 +1,67 @@ +--堆栈实现 +stack = stack or {} + +function stack.create() + local data = {} + + local function push(v) + assert(v) + table.insert(data, v) + end + + local function pop() + assert(#data > 0) + table.remove(data) + end + + local function peek() + return #data > 0 and data[#data] or nil + end + + local function clear() + for i=1,#data do + data[i] = nil + end + end + + + + local __tostring = function() + local tmp = {} + for i,v in ipairs(data) do + tmp[#data+1 - i] = v + end + return table.concat(tmp, ",") + end + + local __index = function(i_t, key) + error(">> Dee: Limited access") + end + + local __len = function() + return #data + end + + local __newindex = function(i_t, key, v) + error(">> Dee: Limited access") + end + + local __ipairs = function() + error(">> Dee: Limited access") + end + + local mt = {__tostring = __tostring, __index = __index, __newindex = __newindex, __ipairs = __ipairs, __pairs = __ipairs, __len = __len} + + local t = { + push = push, + pop = pop, + peek = peek, + clear = clear + } + + setmetatable(t, mt) + + return t +end + +return stack \ No newline at end of file -- cgit v1.1-26-g67d0