一 、table扩展

-- 返回table大小
table.size = function(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end -- 判断table是否为空
table.empty = function(t)
return not next(t)
end -- 返回table键列表
table.keys = function(hashtable)
local keys = {}
for k, v in pairs(hashtable) do
keys[#keys + 1] = k
end
return keys
end -- 返回table值列表
table.values = function(t)
local result = {}
for k, v in pairs(t) do
table.insert(result, v)
end
return result
end -- 返回索引
table.indexof = function(array, value, begin)
for i = begin or 1, #array do
if array[i] == value then return i end
end
return -1
end table.equal = function (a, b)
if type(a)~="table" or type(b)~="table" then
return false
end
if #a ~= #b then
return false
end for k, v in pairs(a) do
if b[k] == nil then
return false
end
if type(a[k]) == "table" then
if not table.equal(a[k], b[k]) then
return false
end
end
if a[k] ~= b[k] then
return false
end
end
return true
end -- 浅拷贝
table.clone = function(t, nometa)
local result = {}
if not nometa then
setmetatable(result, getmetatable(t))
end
for k, v in pairs (t) do
result[k] = v
end
return result
end -- 深拷贝
table.copy = function(t, nometa)
local result = {} if not nometa then
setmetatable(result, getmetatable(t))
end for k, v in pairs(t) do
if type(v) == "table" then
result[k] = copy(v)
else
result[k] = v
end
end
return result
end table.merge = function(dest, src)
for k, v in pairs(src) do
dest[k] = v
end
end table.shuffle = function(t)
if type(t)~="table" then
return
end
local tab={}
local index=1
while #t~=0 do
local n=math.random(0,#t)
if t[n]~=nil then
tab[index]=t[n]
table.remove(t,n)
index=index+1
end
end
return tab
end table.find = function (tab, value)
for k,v in ipairs(tab) do
if v == value then
return k
end
end
return nil
end

二、面向对象扩展

-- lua面向对象扩展
function class(classname, super)
local superType = type(super)
local cls if superType ~= "function" and superType ~= "table" then
superType = nil
super = nil
end if superType == "function" or (super and super.__ctype == 1) then
-- inherited from native C++ Object
cls = {} if superType == "table" then
-- copy fields from super
for k,v in pairs(super) do cls[k] = v end
cls.__create = super.__create
cls.super = super
else
cls.__create = super
cls.ctor = function() end
end cls.__cname = classname
cls.__ctype = 1 function cls.new(...)
local instance = cls.__create(...)
-- copy fields from class to native object
for k,v in pairs(cls) do instance[k] = v end
instance.class = cls
instance:ctor(...)
return instance
end else
-- inherited from Lua Object
if super then
cls = {}
setmetatable(cls, {__index = super})
cls.super = super
else
cls = {ctor = function() end}
end cls.__cname = classname
cls.__ctype = 2 -- lua
cls.__index = cls function cls.new(...)
local instance = setmetatable({}, cls)
instance.class = cls
instance:ctor(...)
return instance
end
end return cls
end function iskindof(obj, classname)
local t = type(obj)
local mt
if t == "table" then
mt = getmetatable(obj)
elseif t == "userdata" then
mt = tolua.getpeer(obj)
end while mt do
if mt.__cname == classname then
return true
end
mt = mt.super
end return false
end

最新文章

  1. Write thread-safe servlets [reproduced]
  2. IntelliJ IDEA使用(二):tomcat和jetty配置
  3. call_user_func()的参数不能为引用传递 自定义替代方法
  4. ubuntu maven环境安装配置
  5. java之进制转换
  6. 宜家的幸福生活,源于K2 BPM的支撑
  7. luigi学习6--parameters详解
  8. POJ1201 差分约束
  9. hdu 4467 Graph
  10. Jdk1.7环境变量的配置
  11. WPF中MeasureOverride ArrangeOverride 的理解
  12. Jquery取属性值(复选框、下拉列表、单选按钮)、做全选按钮、JSON存储、去空格
  13. SpringIOC/DI(2)
  14. Java作业四(2017-10-8)
  15. [Windows] [VS] [C] [取得指针所指内存的二进制形式字符]
  16. JDK源码笔记--Object
  17. Linux系统重置root用户密码
  18. 根据数据库结构生成RzCheckTree
  19. MySQL 安装与使用(二)
  20. HTTP host头

热门文章

  1. Vue mustache语法
  2. 吴恩达Machine Learning学习笔记(四)--BP神经网络
  3. PHPExcel集成对数据导入和导出
  4. linux(centos)环境下安装rabbitMq
  5. Python-序列-str list tuple
  6. 第三周:java面向对象部分总结(2)
  7. 06 C语言变量
  8. 【漏洞复现】MSF添加ms17-010的exp脚本及攻击复现
  9. Jmeter之『多变量循环』
  10. ASP。NET MVC (NetCore 2.0)用于处理实体框架、DbContexts和对象的通用控制器和视图