Lua Table Cloning method induction

Source: Internet
Author: User

Lua table clones

Make a table of Lua, and clone a copy of it as a separate table.

For a module, if the table object obtained after require is not directly modified, for example LUA caches this table, but executes several times logically, all using the original module,

In this case, you need to use clone.

Clone instance, for example cloning {1} out of a copy of {1},

You can see different addresses by printing two tables using ToString.

support for shallow and deep copy methods

Deep copy, that is, when encountering a table key or value is also a table, the table is also copied one copy, otherwise not copy, otherwise the method is a shallow copy.

For general data types, such as strings and numbers and Boolean values, are deep copies and are copied in a separate copy.

The method given by Luaci

------------------------Clones the given object and return it ' s copy.--@param object Table value to clone--@param deep Boolean indicating whether to do recursive cloning--@return Cloned Table ValuefunctionClone (object, deep)Localcopy = {}     forKvinch Pairs(object) Do        ifDeep and type(v) = ="Table"  Thenv=Clone (V, deep)EndCopy[k]=vEnd    return setmetatable(Copy,getmetatable(object))End

a deep copy method that supports shared links

A table of key and value, is multiple, if these links, the referenced table, there is sharing behavior, using the previous method, it will be copied out two different addresses of the table, the content of the table is consistent.

This is not desirable.

For example a = {1}, b={["AA"]=a, ["BB"]=a}

An open source software platform gives a class solution

-[[Function:cocos2dx_cloneDescription:lua table Cloning interface, COCOS2DX project Open source supply.            Copy policy: Deep copy. Network explanation See: Http://blog.csdn.net/u013174689/article/details/41959011Parm:object (Object): Lua object to be copied. Return: A Lua object that has been copied. ]]functionCocos2dx_clone (object)LocalLookup_table = {}    Local function _copy(object)if type(object) ~="Table"  Then            returnObjectElseIfLookup_table[object] Then            returnLookup_table[object]End        LocalNew_table ={} Lookup_table[object]=new_table forKey, Valueinch Pairs(object) Donew_table[_copy(key)] =_copy(value)Print(Key,value)--This sentence adds the print function to demonstrate the process of output replication        End        return setmetatable(New_table,getmetatable(object))End    return _copy(object)--returns the object table pointer/address of cloneEnd  

Lua Table Cloning method induction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.