mirror of
https://github.com/SashLilac/cambridge.git
synced 2024-11-25 07:29:03 -06:00
Amend the copying functions
This commit is contained in:
parent
6cf6568a57
commit
0e1f40ad30
18
funcs.lua
18
funcs.lua
@ -1,10 +1,20 @@
|
|||||||
function copy(t)
|
function copy(t)
|
||||||
-- returns deep copy of t (as opposed to the shallow copy you get from var = t)
|
-- returns top-layer shallow copy of t
|
||||||
if type(t) ~= "table" then return t end
|
if type(t) ~= "table" then return t end
|
||||||
local meta = getmetatable(t)
|
|
||||||
local target = {}
|
local target = {}
|
||||||
for k, v in pairs(t) do target[k] = v end
|
for k, v in next, t do target[k] = v end
|
||||||
setmetatable(target, meta)
|
setmetatable(target, getmetatable(t))
|
||||||
|
return target
|
||||||
|
end
|
||||||
|
|
||||||
|
function deepcopy(t)
|
||||||
|
-- returns infinite-layer deep copy of t
|
||||||
|
if type(t) ~= "table" then return t end
|
||||||
|
local target = {}
|
||||||
|
for k, v in next, t do
|
||||||
|
target[deepcopy(k)] = deepcopy(v)
|
||||||
|
end
|
||||||
|
setmetatable(target, deepcopy(getmetatable(t)))
|
||||||
return target
|
return target
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user