• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

lua实现List及Dictionary

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

转载: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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
寻找[nginx] 由Lua 粘合的Nginx生态环境-- agentzh - Chen Jian发布时间:2022-07-22
下一篇:
LUAOOP单例模式实现的一个方案发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap