Deepcopy: Difference between revisions

m
→‎Depth-first: Small changes mainly in comments.
m (→‎Non-recursive: Moved text from sub-category to parent category.)
m (→‎Depth-first: Small changes mainly in comments.)
Line 815:
-- and now we have to copy the value table
local v = t[k] -- get the original value
-- if we want to deep-copy valueskeys, then get its copy from the list
-- of known tables. if it's not there, then it isn't a table,
-- so keep its original value. same goes for the keyvalue.
if deep_values then v = tables[v] or v end
if deep_keys then k = tables[k] or k end
if deep_values then v = tables[v] or v end
new_t[k] = v -- put value into the new table
end
Line 830:
while k ~= nil do
-- we need to deep-copy the key/value only if
-- 1. we want to do it (eg. `mode` specifies to deep-copy keys/values), AND
-- 2. it is a table, AND
-- 3. we haven't copied it already (and are not copying it right now)
local copy_key = deep_keys and type(k) == 'table' and not tables[k]
Line 839:
-- if either `deep_keys` is `nil` (we don't want to deep-copy keys)
-- or `tables[k]` is `nil` (the key is not a table), then keep the key's original value,
-- otherwise use the value saved in `tables`. same goes for `deep_values`the and `tables[v]`value.
local new_k = deep_keys and tables[k] or k
local new_v = deep_values and tables[v] or v