On the source code interpretation of the clone function in Cocos2dx Lua

Source: Internet
Author: User

COCOS2DX's clone function, is a deep copy, a complete copy, this code content is not much, but the first time to see or a little dizzy, I read down to share. Source code Interpretation:
function Clone (object)--clone functions    Local lookup_table = {}--New table is used to record    local function _copy (object)--_copy ( Object) function is used to implement the copy if        type (object) ~= "table" then return             object   ---If the content is not directly returned by the Table object (for example, if it is a number \ String directly returns the number \ The string)        ElseIf Lookup_table[object] then            return lookup_table[object]--here is used for recursion drop time, If the table has already been copied, return to        end        local new_table = {}        lookup_table[object] = new_table--The two-level child table that the new new_table record needs to replicate , and put it in lookup_table[object].        For key, value in pairs (object) do            new_table[_copy (key)] = _copy (value)--traverse object and recursive _copy (value) to copy the data from each table        End        return setmetatable (new_table, Getmetatable (object))--Sets the MetaTable key value end for the specified table after each traversal        


Example Demonstration:
function Clone (object)    local lookup_table = {}    local function _copy (object)        if Type (object) ~= "table" then< C3/>return object         ElseIf Lookup_table[object] then            return lookup_table[object]        End        Local new_table = {}        Lookup_table[object] = new_table        for key, value in pairs (object) do            new_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)--return clone out of Object table pointer/Address End local A = {[{], +}] = {+, +}, c = "abc"}local MyPolygon = {color= "blue", thickness=2, Npoints=4,{x=0, y=0}, {x=-10, y=0}, {x=-5, y=4}, {x=0, y=4}}loca L newtable = Clone (a) Local newtable2 = Clone (MyPolygon)

-----------Output Content 1200
1 300
2 500
2 table:0x7ffa43d06930
1 100
2 200
1 300
2 400
table:0x7ffa43d06610 Table:0x7ffa43d068f0
Abc ABC---above is the output content of table MyPolygon after copying the output of table A
X 0
Y 0
1 Table:0x7ffa43d064f0
X -10
Y 0
2 Table:0x7ffa43d06a60
X -5
Y 4
3 Table:0x7ffa43d06af0
X 0
Y 4
4 Table:0x7ffa43d06b80
Npoints 4
Thickness 2
Color Blue

On the source code interpretation of the clone function in Cocos2dx Lua

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.