Module:TableTools: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
m (44 revisions imported from wikipedia:Module:TableTools) |
(Update from sandbox per request) |
||
Line 72: | Line 72: | ||
-- NaNs can't be table keys, and they are also unique, so we don't need to check existence. |
-- NaNs can't be table keys, and they are also unique, so we don't need to check existence. |
||
ret[#ret + 1] = v |
ret[#ret + 1] = v |
||
⚫ | |||
else |
|||
ret[#ret + 1] = v |
|||
exists[v] = true |
|||
⚫ | |||
⚫ | |||
end |
end |
||
end |
end |
||
Line 380: | Line 378: | ||
------------------------------------------------------------------------------------ |
------------------------------------------------------------------------------------ |
||
local function _deepCopy(orig, includeMetatable, already_seen) |
local function _deepCopy(orig, includeMetatable, already_seen) |
||
⚫ | |||
⚫ | |||
⚫ | |||
already_seen = already_seen or {} |
|||
⚫ | |||
⚫ | |||
local copy = already_seen[orig] |
local copy = already_seen[orig] |
||
if copy ~= nil then |
if copy ~= nil then |
||
return copy |
return copy |
||
end |
end |
||
⚫ | |||
⚫ | |||
already_seen[orig] = copy -- memoize before any recursion, to avoid infinite loops |
|||
⚫ | |||
for orig_key, orig_value in pairs(orig) do |
|||
copy[_deepCopy(orig_key, includeMetatable, already_seen)] = _deepCopy(orig_value, includeMetatable, already_seen) |
|||
⚫ | |||
end |
|||
already_seen[orig] = copy |
|||
if includeMetatable then |
|||
local mt = getmetatable(orig) |
|||
if mt ~= nil then |
|||
setmetatable(copy, _deepCopy(mt, true, already_seen)) |
|||
setmetatable(copy, mt_copy) |
|||
already_seen[mt] = mt_copy |
|||
⚫ | |||
end |
end |
||
else -- number, string, boolean, etc |
|||
⚫ | |||
end |
end |
||
return copy |
return copy |
||
end |
end |
||
Line 411: | Line 407: | ||
function p.deepCopy(orig, noMetatable, already_seen) |
function p.deepCopy(orig, noMetatable, already_seen) |
||
checkType("deepCopy", 3, already_seen, "table", true) |
checkType("deepCopy", 3, already_seen, "table", true) |
||
return _deepCopy(orig, not noMetatable, already_seen) |
return _deepCopy(orig, not noMetatable, already_seen or {}) |
||
end |
end |
||
Line 476: | Line 472: | ||
end |
end |
||
return false |
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 |
end |
||