lua中表table的拷贝而不是引用的操作方法
来源:互联网 发布:淘宝店铺怎么快速升钻 编辑:程序博客网 时间:2024/05/16 16:09
lua中的table类型相互赋值都是相互引用,也就是修改被赋值或者赋值的table都会影响双方的内容,相当于两个指针的指向是一样的,修改其中一个指针的指向内容,另一个所指向的内容也相应的变化,想通过赋值得到一个copy的想法看来有点天真了。
其实这个特性是官方刻意为之的,官方也给出了足够的理由和解决方法,偶然之前搜索的时候找到了他们的解决方法,如下所述,如果不想看内容,那哥们就告诉你结论得了,那就是直接拷贝deepcopy这个函数用就行了!!!!!
原文链接:http://lua-users.org/wiki/CopyTable
The Lua standard libraries do not provide a function to copy a table. However, it is relatively simple to implement such a function.
A generic table.copy function is not guaranteed to suit all use-cases, as there are many different aspects which must be selected for the specific situation. For example: should metatables be shared or copied? Should we check userdata for a __copy metamethod? These questions (as well as many others) must be asked and answered by the developer.
Some of the Lua "extended standard libraries", such as Penlight and stdlib provide ready-made copy functions for convenience. Check if they suit your use-case before implementing your own.
The following functions provide a base to work off of:
Quick & Dirty
function table.clone(org) return {table.unpack(org)}endlocal abc = {5,12,1}local def = table.clone(abc)table.sort(def)print(abc[2], def[2]) -- 125
Shallow Copy
This a simple, naive implementation. It only copies the top level value and its direct children; there is no handling of deeper children, metatables or special types such as userdata or coroutines. It is also susceptible to influence by the __pairs metamethod.function shallowcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table' then copy = {} for orig_key, orig_value in pairs(orig) do copy[orig_key] = orig_value end else -- number, string, boolean, etc copy = orig end return copyend
Deep Copy
A deep copy copies all levels (or a specific subset of levels).Here is a simple recursive implementation that additionally handles metatables and avoid the __pairs metamethod.
function deepcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table' then copy = {} for orig_key, orig_value in next, orig, nil do copy[deepcopy(orig_key)] = deepcopy(orig_value) end setmetatable(copy, deepcopy(getmetatable(orig))) else -- number, string, boolean, etc copy = orig end return copyend
Additionally, it is recursive which means it may overflow the stack for large tables.
Non-recursive Deep Copy
A more flexible (non-recursive) deepcopy implementation is available from [this GitHub gist]. It allows for varying rules on how to copy special types, metatables, and function upvalues (including joining). Please see the comments for usage.- lua中表table的拷贝而不是引用的操作方法
- lua table传值是传的引用
- MYSQL中表的拷贝
- Lua Table表的深度拷贝
- Lua中table的拷贝问题
- 小心List.subList---返回的引用视图而不是拷贝
- lua脚本中表的用法
- LUA 中实现table表的深拷贝实例
- 拷贝构造函数的形参应是类对象的引用,而不是对象的值
- Lua编程中遇到的table类型传递引用问题
- lua table 拷贝
- lua 拷贝table
- quick-lua中表的排序问题
- Smartform中表(table)的行间距设置
- lua的TABLE性质
- lua的table排序
- lua 的 table 处理
- lua的table排序
- 语音合成技术
- 小米手机测试安装失败Installation failed with message:INSTALL_CANCELED_BY_USER
- 分类模型与算法--距离判别法
- 第一个Hibernate程序
- Atom beautify不可用,提示uncrustify未安装错误
- lua中表table的拷贝而不是引用的操作方法
- JavaScript中location.hash详解
- Python-3.6.2
- C++:指针函数与函数指针
- Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 4. 函数
- JSON数组对象和对象数组
- [整理]WebUploader + SpringMVC 实现多文件断点续传之二 多文件断点续传
- rsync
- 机器学习之线性回归 Linear Regression(一)