Module:TableTools: Difference between revisions
Content deleted Content added
m 44 revisions imported from wikipedia:Module:TableTools |
m 1 revision imported from wikipedia:Module:TableTools |
||
(One intermediate revision by one other user not shown) | |||
Line 72:
-- NaNs can't be table keys, and they are also unique, so we don't need to check existence.
ret[#ret + 1] = v
▲ exists[v] = true
end▼
end
end
Line 380 ⟶ 378:
------------------------------------------------------------------------------------
local function _deepCopy(orig, includeMetatable, already_seen)
-- Stores copies of tables indexed by the original table.▼
local copy = already_seen[orig]
if copy ~= nil then
return copy
end
▲ if type(orig) == 'table' then
already_seen[orig] = copy -- memoize before any recursion, to avoid infinite loops
▲ copy = {}
▲ end
end
▲ end
end
▲ copy = orig
end
return copy
end
Line 411 ⟶ 407:
function p.deepCopy(orig, noMetatable, already_seen)
checkType("deepCopy", 3, already_seen, "table", true)
return _deepCopy(orig, not noMetatable, already_seen or {})
end
Line 476 ⟶ 472:
end
return false
end
------------------------------------------------------------------------------------
-- merge
--
-- Given the arrays, returns an array containing the elements of each input array
-- in sequence.
------------------------------------------------------------------------------------
function p.merge(...)
local arrays = {...}
local ret = {}
for i, arr in ipairs(arrays) do
checkType('merge', i, arr, 'table')
for _, v in ipairs(arr) do
ret[#ret + 1] = v
end
return ret
end
------------------------------------------------------------------------------------
-- extend
--
-- Extends the first array in place by appending all elements from the second
-- array.
------------------------------------------------------------------------------------
function p.extend(arr1, arr2)
checkType('extend', 1, arr1, 'table')
checkType('extend', 2, arr2, 'table')
for _, v in ipairs(arr2) do
arr1[#arr1 + 1] = v
end
end
|