lua实现List及Dictionary
转载: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,,- do
table.remove(self)
end
end function List:Contains(item)
local count = self:Count()
for i=,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=,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=,count do
action(self[i])
end
end function List:IndexOf(item)
local count = self:Count()
for i=,count do
if self[i] == item then
return i
end
end
return
end function List:LastIndexOf(item)
local count = self:Count()
for i=count,,- do
if self[i] == item then
return i
end
end
return
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 > ) 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,,- do
self[self.keyList[i]] = nil
table.remove(self.keyList)
end
end function Dictionary:ContainsKey(key)
local count = self:Count()
for i=,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=,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 =
local n = self:Count()
return function ()
i = i +
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=,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', '')
dic:Add('ShangHai', '') 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', '')
dic:Add('ShangHai', '') 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
lua实现List及Dictionary的更多相关文章
- Lua Table转C# Dictionary
因为在游戏公司做web后台开发,经常会涉及到取游戏服务器的数据库里面读写各种操作. 昨天下午,服务器那边让我读一个配置显示到后台,让运营大佬们可以在web后台配置游戏参数. 本来以为很简单个事情,结果 ...
- 用好lua+unity,让性能飞起来——lua与c#交互篇
前言 在看了uwa之前发布的<Unity项目常见Lua解决方案性能比较>,决定动手写一篇关于lua+unity方案的性能优化文. 整合lua是目前最强大的unity热更新方案,毕竟这是唯一 ...
- LUA 运算笔记
循环 比如要实现这样的一个For for(int i=10;i>1;i—) { print(i) } lua的for循环 转换成LUA for i=10,1,-1 do print(i) end ...
- 深入xLua实现原理之C#如何调用Lua
本文主要是探讨xLua下C#调用Lua的实现原理,有关Lua如何调用C#的介绍可以查看深入xLua实现原理之Lua如何调用C# C#与Lua数据通信机制 无论是Lua调用C#,还是C#调用Lua,都需 ...
- Nginx+lua+openresty精简系列
1. CentOS系统安装openresty 你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令).运行下面的 ...
- nginx HttpLuaModule
http://wiki.nginx.org/HttpLuaModule#Directives Name ngx_lua - Embed the power of Lua into Nginx This ...
- 热更新解决方案--tolua学习笔记
一.tolua使用准备工作:从GitHub上下载tolua(说明:这篇笔记使用的Unity版本是2019.4.18f1c1,使用的tolua是2021年4月9日从GitHub上Clone的tolua工 ...
- Lua脚本之语法基础快速入门
要 1.基本数据类型 2.Lua中的常用语句结构以及函数 3.Lua中的常用语句结构介绍 4.Lua中的库函数 目录[-] 一.基本数据类型 二.Lua中的常用语句结构以及函数 1.Lua中的常用语句 ...
- redis(Remote Dictionary Server)
redis的简介和使用 简介 redis(Remote Dictionary Server)是一种Nosql技术,它是一个开源的高级kv存储和数据结构存储系统,它经常被拿来和Memcached相比 ...
随机推荐
- 快速获取.NET DLL文件编译时间
当用户现场汇报问题给我们, 我们比较关心的就有用户现场的DLL是什么版本号,是什么时候编译的. 有没有什么办法得到呢?办法是有的. 在网上找了很久终端找到这个软件非常地好用. 直接把文件拖到软件里就行 ...
- docker 镜像 容器删除
Docker 容器镜像删除 1.停止所有的container,这样才能够删除其中的images: docker stop $(docker ps -a -q) 如果想要删除所有container的 ...
- day08.2-ssh远程连接服务
在Linux环境中,部署一个服务的一般步骤: a). 准备环境,包括 关闭防火墙:service iptables stop(或chkconfig iptables off) 关闭se ...
- 设置使用的python版本
一.查看当前使用的python版本,或设置使用的python版本 二.python2中默认使用ASCII码,无法识别中文,报错如图,解决办法,设置字符集为utf-8
- Win7下C/C++跨平台开发工具IDE的安装之CodeBlocks
1. Win7下安装CodeBlocks: 下载带有mingw的CodeBlocks:http://www.codeblocks.org/downloads/26#windows 运行所下载程序: 点 ...
- Python——零基础向-四行代码下载网页上的一张图片
一.确保安装了requests模块 还没安装的可以百度一下如何安装,很简单的. 这里简单的说一下方法:win+R,输入cmd,打开命令行窗口,输入命令:pip install requests ,即可 ...
- JavaBean的概念
Java的一个特性,数据与行为相分离.数据就是成员变量,行为就是setter和getter方法 JavaBean是Java中开发的可以跨平台的重要组件,在JSP中常用来封装业务逻辑和数据库操作.Jav ...
- app.use和app.get的区别及解析
转载至:http://blog.csdn.net/wthfeng/article/details/53366169 写在前面:最近研究nodejs及其web框架express,对app.use和app ...
- 3.1、Factorization Machine模型
Factorization Machine模型 在Logistics Regression算法的模型中使用的是特征的线性组合,最终得到的分隔超平面属于线性模型,其只能处理线性可分的二分类问题,现实生活 ...
- select 插入数据 不自增列实现自增