リスト操作をサポートするスタック(lua)


多くの場合、リストは私たちが想像していたほど使いやすいものではありません.例を挙げます.

        local t = {}
        table.insert(t,{k=1})
        table.insert(t,{k=2})
        table.insert(t,{k=3})
        table.insert(t,{k=4})
        table.insert(t,{k=5})
        table.insert(t,{k=6})
        table.insert(t,{k=7})
        table.insert(t,{k=8})
        for i=1,table.getn(t) do
            print(t[i].k)
            if t[i].k == 8 then
                table.insert(t,{k=9})
            end
        end
        print("===========================")
        for i=1,table.getn(t) do
            print(t[i].k)
        end

上の例を見ると、遍歴リストの終了時にもう一つの値を挿入しました.本来、テーブルの長さは9で、テーブルは遍歴するべきですが、テーブル遍歴はここで終わります.出力を見てください:[LUA-print]1[LUA-print]2[LUA-print]3[LUA-print]4[LUA-print]5[LUA-print]6[LUA-print]7[LUA-print]8[LUA-print]==========================[LUA-print]1[LUA-print]2[LUA-print]3[LUA-print]4[LUA-print]5[LUA-print]6[LUA-print]7[LUA-print]8[LUA-print]9テーブルは巡回していません.後ろの出力から分かるように、テーブルは確かに長くなりました.同様のテーブル操作はまだ多く、デフォルトのテーブル構造は私の要求を満たすことができません.だから私は新しいキューを書きました.コードを見てください:

        local stack = util.TableStack.new()
        stack:pushElement({k = 1})
        stack:pushElement({k = 2})
        stack:pushElement({k = 3})
        local v1 = {k = 4}
        stack:pushElement(v1)
        stack:pushElement({k = 5})
        stack:pushElement({k = 6})
        stack:pushElement({k = 7})
        stack:pushElement({k = 8})
        stack:pushElement({k = 9})
        stack:pushElement({k = 10})
        stack:pushElement({k = 11})

        function trace(v_k)
            print(" k= "..v_k.k)
            if v_k.k == 11 then
                stack:pushElement({ k = 12})
                stack:delElement(v_k)
            end
        end
        stack:checkElement(trace)
        print("trace2________________")
        function trace2(v_k)
            print(v_k.k)
        end
        stack:checkElement(trace2)

出力を見る:
[LUA-print] k= 1 [LUA-print] k= 2 [LUA-print] k= 3 [LUA-print] k= 4 [LUA-print] k= 5 [LUA-print] k= 6 [LUA-print] k= 7 [LUA-print] k= 8 [LUA-print] k= 9 [LUA-print] k= 10 [LUA-print] k= 11 [LUA-print] k= 12 [LUA-print] trace2________________ [LUA-print] 1 [LUA-print] 2 [LUA-print] 3 [LUA-print] 4 [LUA-print] 5 [LUA-print] 6 [LUA-print] 7 [LUA-print] 8 [LUA-print] 9 [LUA-print] 10 [LUA-print] 12
新しいオブジェクトはテーブルの任意の操作をサポートし、次のソースコードを貼り付けます.
 --
-- Author: xiaowa
-- Date: 2015-10-04 08:33:36
--
--    table stack 

local HashMap = import(".HashMap")
local TableStack = TableStack or class("TableStack")

function TableStack:ctor()
    self.stack_ = nil
    self.stack_end_ = nil --      
    self.stack_num_ = 0 --     
    self.hash_ = HashMap.new()  --    
    self.PTR_NEXT = nil --          
    self.CHECK = false --       
end
--[[
    @function         
    @return Element#      
--]]
function TableStack:pushElement(value)
    assert(type(value) == "table", string.format("%s:pushElement() - invalid value", self.class.__cname))
    self.stack_num_ = self.stack_num_ + 1
    local ptr = self.stack_
    if ptr then
        self.stack_end_.next = {}
        self.stack_end_.next.value = value
        self.stack_end_.next.prev = self.stack_end_ --   ,  pop  
        self.stack_end_ = self.stack_end_.next
    else --      
        self.stack_ = {}
        self.stack_.value = value
        self.stack_end_ = self.stack_
    end
    self.hash_:setElement(value,self.stack_end_)    --    ,    

    --        
    if self.CHECK then
        if self.PTR_NEXT == nil then
            self.PTR_NEXT = self.stack_end_
        end
    end
    return value
end
--[[
    @function         
    @return Element#      
--]]
function TableStack:insertFirstElement(value)
    assert(type(value) == "table", string.format("%s:insertFirstElement() - invalid value", self.class.__cname))
    self.stack_num_ = self.stack_num_ + 1
    local ptr = self.stack_
    if ptr then
        self.stack_.prev = {}
        self.stack_.prev.value = value
        self.stack_.prev.next = self.stack_
        self.stack_ = self.stack_.prev
    else --      
        self.stack_ = {}
        self.stack_.value = value
        self.stack_end_ = self.stack_
    end
    self.hash_:setElement(value,self.stack_)    --    ,    
    return value
end
--[[
    @function       
    @return Element#      
--]]
function TableStack:delElement(value)
    assert(value ~= nil, string.format("%s:delElement() - invalid value", self.class.__cname))
    local v = self.hash_:getValue(value)
    if v == nil then
        return
    end
    --        
    if self.CHECK then
        if self.PTR_NEXT == v then
            self.PTR_NEXT = self.PTR_NEXT.next
        end
    end

    self.stack_num_ = self.stack_num_ - 1   --   1(         )
    if v.prev and v.next then --       
        v.prev.next = v.next
        v.next.prev = v.prev
        v.prev = nil
        v.next = nil
        self.hash_:delElement(value)
        return v.value
    elseif v.prev == nil and v.next then --         
        self.stack_ = self.stack_.next
        self.stack_.prev = nil
        v.next = nil
        self.hash_:delElement(value)
        return v.value
    elseif v.prev and v.next == nil then --        
        self.stack_end_ = self.stack_end_.prev
        self.stack_end_.next = nil
        v.prev = nil
        self.hash_:delElement(value)
        return v.value
    elseif v.prev == nil and v.next == nil then --      
        self.stack_ = nil
        self.stack_end_ = nil
        self.hash_:delElement(value)
        return v.value
    else
        printLog("error","this is No way!") --    
    end
end
--[[
    @function     
    @return Element#value
--]]
function TableStack:popElement()
    if self.stack_end_ == nil then
        return nil
    end
    --        
    if self.CHECK then
        if self.PTR_NEXT == self.stack_end_ then
            self.PTR_NEXT = self.PTR_NEXT.next
        end
    end

    self.stack_num_ = self.stack_num_ - 1   --   1
    local ptr_prev
    ptr_prev = self.stack_end_.prev
    if ptr_prev then
        ptr_prev.next = nil
        self.stack_end_.prev = nil
        local value = self.stack_end_.value
        self.stack_end_ = ptr_prev
        self.hash_:delElement(value) -- hash    key
        return value
    else --self.stack_end_       ,    1    
        self.stack_ = nil
        local value = self.stack_end_.value
        self.stack_end_ = nil
        self.hash_:delElement(value) -- hash    key
        return value
    end
end
--[[
    @function        
    @return Element#value
--]]
function TableStack:removeFirstElement()
    if self.stack_ == nil then
        return nil
    end
    local ptr,ptr_next = self.stack_,self.stack_.next
    if ptr then
        --        
        if self.CHECK then
            if self.PTR_NEXT == ptr then
                self.PTR_NEXT = self.PTR_NEXT.next
            end
        end

        self.stack_num_ = self.stack_num_ - 1   --   1
        if ptr_next then
            ptr_next.prev = ptr.prev
            self.stack_ = ptr_next
            self.hash_:delElement(ptr.value) -- hash    key
            return ptr.value
        else
            self.stack_ = nil
            self.stack_end_ = nil --      
            self.hash_:delElement(ptr.value) -- hash    key
            return ptr.value
        end
    end
    return nil
end
--[[
    @function     ,             ,              ,          
    @param function#    ,  true      
    @return nil
--]]
function TableStack:checkElement(fun)
    assert(type(fun) == "function", string.format("%s:checkElement() - invalid value", self.class.__cname))
    self.CHECK = true
    local ptr = self.stack_
    while ptr do
        self.PTR_NEXT = ptr.next
        if fun(ptr.value) then
            break 
        end
        ptr = self.PTR_NEXT
    end
    self.CHECK = false
end
--[[
    @function       
    @return number#      
--]]
function TableStack:Len()
    return self.stack_num_
end

return TableStack