Editing Module:TableTools

Jump to navigation Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
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
elseif not exists[v] then
else
ret[#ret + 1] = v
if not exists[v] then
exists[v] = true
ret[#ret + 1] = v
exists[v] = true
end
end
end
end
end
Line 378: Line 380:
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
local function _deepCopy(orig, includeMetatable, already_seen)
local function _deepCopy(orig, includeMetatable, already_seen)
if type(orig) ~= "table" then
-- Stores copies of tables indexed by the original table.
return orig
already_seen = already_seen or {}
end
 
-- already_seen stores copies of tables indexed by the original table.
local copy = already_seen[orig]
local copy = already_seen[orig]
if copy ~= nil then
if copy ~= nil then
return copy
return copy
end
end
 
copy = {}
if type(orig) == 'table' then
already_seen[orig] = copy -- memoize before any recursion, to avoid infinite loops
copy = {}
for orig_key, orig_value in pairs(orig) do
for orig_key, orig_value in pairs(orig) do
copy[_deepCopy(orig_key, includeMetatable, already_seen)] = _deepCopy(orig_value, includeMetatable, already_seen)
copy[_deepCopy(orig_key, includeMetatable, already_seen)] = _deepCopy(orig_value, includeMetatable, already_seen)
end
end
already_seen[orig] = copy
 
if includeMetatable then
if includeMetatable then
local mt = getmetatable(orig)
local mt = getmetatable(orig)
if mt ~= nil then
if mt ~= nil then
setmetatable(copy, _deepCopy(mt, true, already_seen))
local mt_copy = _deepCopy(mt, includeMetatable, already_seen)
setmetatable(copy, mt_copy)
already_seen[mt] = mt_copy
end
end
end
else -- number, string, boolean, etc
copy = orig
end
end
return copy
return copy
end
end
Line 407: Line 411:
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 or {})
return _deepCopy(orig, not noMetatable, already_seen)
end
end


Line 460: Line 464:
-- inArray
-- inArray
--
--
-- Returns true if searchElement is a member of the array, and false otherwise.
-- Returns true if valueToFind is a member of the array, and false otherwise.
-- Equivalent to JavaScript array.includes(searchElement) or
-- array.includes(searchElement, fromIndex), except fromIndex is 1 indexed
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
function p.inArray(array, searchElement, fromIndex)
function p.inArray(arr, valueToFind)
checkType("inArray", 1, array, "table")
checkType("inArray", 1, arr, "table")
-- if searchElement is nil, error?
-- if valueToFind is nil, error?


fromIndex = tonumber(fromIndex)
for _, v in ipairs(arr) do
if fromIndex then
if v == valueToFind then
if (fromIndex < 0) then
return true
fromIndex = #array + fromIndex + 1
end
if fromIndex < 1 then fromIndex = 1 end
for _, v in ipairs({unpack(array, fromIndex)}) do
if v == searchElement then
return true
end
end
else
for _, v in pairs(array) do
if v == searchElement then
return true
end
end
end
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
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


return p
return p
Please note that all contributions to American Election Wiki are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) (see American Election Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)

Template used on this page: