lua table面向对象扩展
一 、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
lua table面向对象扩展的更多相关文章
- Lua 之面向对象编程
Lua 之面向对象编程 Lua并不是为面向对象而设计的一种语言,因此,仅从原生态语法上并不直接支持面向对象编程,但Lua的设计中仍然包含了很多面向对象的思想,理解它们也更有助于理解Lua自身的一些高级 ...
- lua table integer index 特性
table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the t ...
- 树形打印lua table表
为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据.以下罗列了三种种关于树形打印lua table的方法;法一 local print = print local tconca ...
- lua table 排序--满足多条件排序
前提 假设 一个小怪 有三种属性,等级(level).品质(quality).id(pid) 我们需要对他们进行排序,两种排序情况,第一是单一属性排序,比如按照等级进行排序,或者多种属性进行优先级排序 ...
- cocos2d-x lua table数据存储
cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...
- cocos2d-x lua table与json的转换
cocos2d-x lua table与json的转换 version: cocos2d-x 3.6 1.引入json库 require("src/cocos/cocos2d/json&qu ...
- Lua table使用
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th ...
- lua table表
lua table表 语法结构 创建一个 table 直接使用 "{}" 即可 table1 = {} -- 赋值 table1["name"] = " ...
- lua table表判断是否为空
官方手册里早已经给了答案,那就是靠lua内置的next函数 即如此用: a = {} if next(a) == nil then next其实就是pairs遍历table时用来取下一个内容的函数. ...
随机推荐
- 看动画学算法之:排序-count排序
目录 简介 count排序的例子 count排序的java实现 count排序的第二种方法 count排序的时间复杂度 简介 今天我们介绍一种不需要作比较就能排序的算法:count排序. count排 ...
- IOT(esp8266)
今日工具: 硬件: esp8266 DHT11温湿度传感器 软件: Arduino ESP8266 是一款由乐鑫 Espressif 公司制作的低成本的 Wi-Fi 芯片,具有完整的 TCP / IP ...
- IDEA搭建多模块maven项目
目录 新建maven项目作为多模块的parent项目 新建模块 在parent项目中配置 pom api 模块搭建restful风格api Mybatis Generator的使用(mybatis 逆 ...
- 中秋国庆8天挑战赛 之 挑战8天掌握微信小程序
中秋国庆8天挑战赛 挑战8天掌握微信小程序 当前学习进度: // 10.1// 学习内容:// 10.2// 学习内容:// 10.3// 学习内容:// 10.4// 学习内容:// ...
- sort函数居然能改变元素值?记一次有趣的Bug——四数之和
坐标leetcode: 我想都不想直接深度优先搜索暴力求解: class Solution { public: vector<vector<int>> res; //答案 in ...
- >>8) & 0xFF中的 >> 和 &0xFF 的作用
参考:https://blog.csdn.net/iamgamer/article/details/79354617 其中有两个位运算,一个是>>,一个是&. 0xff的作用一: ...
- 简单区间dp
题目链接 对于基本区间dp,设dp[l][r]是区间l到r的最大价值. 我们可以枚举区间的长度,在枚举左端点,判断即可. 当右端点大于n,就break. dp[l][r]=max(dp[l+1][r] ...
- 成理信安协会反序列化01-利用fastcoll实现md5碰撞
虽然是反序列化的题目,但主要考点在利用fastcoll实现md5碰撞. 直接上源码 <?php show_source(__FILE__); class CDUTSEC { public $va ...
- 每日一题 LeetCode 491. 递增子序列 【递推】【递增子序列】【动态规划】
题目链接 https://leetcode-cn.com/problems/increasing-subsequences/ 题目说明 题解 主要方法:递推:动态规划 解释说明: 数据表示:观察数据范 ...
- python之线程了解部分
一.死锁(了解) 死锁产生的4个必要条件: 互斥:一个资源同一时刻只允许一个线程进行访问 占有未释放:一个线程占有资源,且没有释放资源 不可抢占:一个已经占有资源的线程无法抢占到其他线程拥有的资源 循 ...