• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Fork me on GitHub
            隨筆 - 215  文章 - 13  trackbacks - 0
            <2015年11月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345


            專注即時通訊及網游服務端編程
            ------------------------------------
            Openresty 官方模塊
            Openresty 標準模塊(Opm)
            Openresty 三方模塊
            ------------------------------------
            本博收藏大部分文章為轉載,并在文章開頭給出了原文出處,如有再轉,敬請保留相關信息,這是大家對原創作者勞動成果的自覺尊重!!如為您帶來不便,請于本博下留言,謝謝配合。

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            相冊

            Awesome

            Blog

            Book

            GitHub

            Link

            搜索

            •  

            積分與排名

            • 積分 - 215445
            • 排名 - 118

            最新評論

            閱讀排行榜

            lua實現List及Dictionary

            轉載:http://www.cnblogs.com/sanyejun/p/7801432.html

            http://www.maosongliang.com/archives/122

            參考 http://blog.csdn.net/jason_520/article/details/54173685

            實現List

            List = {}
            List.__index = List
             
            function List:New(t)
                local o = {itemType = t}
                setmetatable(o, self)
                return o
            end
             
            function List:Add(item)
                table.insert(self, item)
            end
             
            function List:Clear()
                local count = self:Count()
                for i=count,1,-1 do
                    table.remove(self)
                end
            end
             
            function List:Contains(item)
                local count = self:Count()
                for i=1,count do
                    if self[i] == item then
                        return true
                    end
                end
                return false
            end
             
            function List:Count()
                return table.getn(self)
            end
             
            function List:Find(predicate)
                if (predicate == nil or type(predicate) ~= 'function') then
                    print('predicate is invalid!')
                    return
                end
                local count = self:Count()
                for i=1,count do
                    if predicate(self[i]) then 
                        return self[i] 
                    end
                end
                return nil
            end
             
            function List:ForEach(action)
                if (action == nil or type(action) ~= 'function') then
                    print('action is invalid!')
                    return
                end
                local count = self:Count()
                for i=1,count do
                    action(self[i])
                end
            end
             
            function List:IndexOf(item)
                local count = self:Count()
                for i=1,count do
                    if self[i] == item then
                        return i
                    end
                end
                return 0
            end
             
            function List:LastIndexOf(item)
                local count = self:Count()
                for i=count,1,-1 do
                    if self[i] == item then
                        return i
                    end
                end
                return 0
            end
             
            function List:Insert(index, item)
                table.insert(self, index, item)
            end
             
            function List:ItemType()
                return self.itemType
            end
             
            function List:Remove(item)
                local idx = self:LastIndexOf(item)
                if (idx > 0) then
                    table.remove(self, idx)
                    self:Remove(item)
                end
            end
             
            function List:RemoveAt(index)
                table.remove(self, index)
            end
             
            function List:Sort(comparison)
                if (comparison ~= nil and type(comparison) ~= 'function') then
                    print('comparison is invalid')
                    return
                end
                if func == nil then
                    table.sort(self)
                else
                    table.sort(self, func)
                end
            end

             

            實現Dic

            Dictionary = {}
            Dictionary.__index = Dictionary
             
            function Dictionary:New(tk, tv)
                local o = {keyType = tk, valueType = tv}
                setmetatable(o, self)
                o.keyList = {}
                return o
            end
             
            function Dictionary:Add(key, value)
                if self[key] == nil then
                    self[key] = value
                    table.insert(self.keyList, key)
                else
                    self[key] = value
                end
            end
             
            function Dictionary:Clear()
                local count = self:Count()
                for i=count,1,-1 do
                    self[self.keyList[i]] = nil
                    table.remove(self.keyList)
                end
            end
             
            function Dictionary:ContainsKey(key)
                local count = self:Count()
                for i=1,count do
                    if self.keyList[i] == key then
                        return true
                    end
                end
                return false
            end
             
            function Dictionary:ContainsValue(value)
                local count = self:Count()
                for i=1,count do
                    if self[self.keyList[i]] == value then
                        return true
                    end
                end
                return false
            end
             
            function Dictionary:Count()
                return table.getn(self.keyList)
            end
             
            function Dictionary:Iter()
                local i = 0
                local n = self:Count()
                return function ()
                    i = i + 1
                    if i <= n then
                        return self.keyList[i]
                    end
                    return nil
                end
            end
             
            function Dictionary:Remove(key)
                if self:ContainsKey(key) then
                    local count = self:Count()
                    for i=1,count do
                        if self.keyList[i] == key then
                            table.remove(self.keyList, i)
                            break
                        end
                    end
                    self[key] = nil
                end
            end
             
            function Dictionary:KeyType()
                return self.keyType
            end
             
            function Dictionary:ValueType()
                return self.valueType
            end

            其中Dictionary:Iter是用來遍歷Dictionary的,用法如下:

            Lua

            local dic = Dictionary:New('string', 'string')
            dic:Add('BeiJing', '010')
            dic:Add('ShangHai', '021')

            while true do
                local it = dic:Iter()
                if it ~= nil then
                    local key = it()
                    local value = dic[key]
                    print('key: ' .. tostring(key) .. ' value: ' .. tostring(value))
                else
                    break
                end
            end

            local dic = Dictionary:New('string', 'string')
            dic:Add('BeiJing', '010')
            dic:Add('ShangHai', '021')
             
            while true do
                local it = dic:Iter()
                if it ~= nil then
                    local key = it()
                    local value = dic[key]
                    print('key: ' .. tostring(key) .. ' value: ' .. tostring(value))
                else
                    break
                end
            end
            posted on 2018-05-08 11:07 思月行云 閱讀(1089) 評論(0)  編輯 收藏 引用 所屬分類: Nginx\Openresty
            精品久久久久久无码专区不卡 | 久久99国产亚洲高清观看首页| 丁香色欲久久久久久综合网| 亚洲色婷婷综合久久| 久久w5ww成w人免费| 久久精品国产黑森林| 99久久这里只精品国产免费 | 9191精品国产免费久久| 久久久久亚洲AV成人网| 久久综合综合久久综合| 香蕉99久久国产综合精品宅男自| 东方aⅴ免费观看久久av | 久久精品国产99久久无毒不卡 | 精品久久人人做人人爽综合| 欧美精品九九99久久在观看| 国产成人精品久久二区二区| 中文精品99久久国产| a级毛片无码兔费真人久久| 久久久久se色偷偷亚洲精品av| 99久久国产综合精品网成人影院| 久久久久亚洲AV片无码下载蜜桃| 18岁日韩内射颜射午夜久久成人| 欧美午夜精品久久久久免费视 | 久久久久人妻一区精品色| 久久久久亚洲精品男人的天堂| 国产91色综合久久免费| 久久久精品人妻一区二区三区蜜桃 | 99久久这里只精品国产免费| 久久WWW免费人成—看片| 久久91精品国产91久久小草| 亚洲中文字幕久久精品无码喷水| 久久男人中文字幕资源站| 国产精品99久久久久久猫咪| 久久电影网一区| 97久久精品人人做人人爽| 91久久成人免费| 久久精品国产72国产精福利| 久久亚洲国产成人影院网站| 久久国产成人午夜aⅴ影院| 理论片午午伦夜理片久久| 天堂无码久久综合东京热|