Module:table: Difference between revisions

From Linguifex
Jump to navigation Jump to search
(Created page with "--[[ ------------------------------------------------------------------------------------ -- table (formerly TableTools) --...")
 
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 3: Line 3:
--                      table (formerly TableTools)                              --
--                      table (formerly TableTools)                              --
--                                                                                --
--                                                                                --
-- This module inclcudes a number of functions for dealing with Lua tables.        --
-- This module includes a number of functions for dealing with Lua tables.        --
-- It is a meta-module, meant to be called from other Lua modules, and should    --
-- It is a meta-module, meant to be called from other Lua modules, and should    --
-- not be called directly from #invoke.                                          --
-- not be called directly from #invoke.                                          --
Line 9: Line 9:
--]]
--]]


--[[
local export = {}
Inserting new values into a table using a local "index" variable, which is
incremented each time, is faster than using "table.insert(t, x)" or
"t[#t + 1] = x". See the talk page.
]]


local libraryUtil = require('libraryUtil')
local collation_module = "Module:collation"
local debug_track_module = "Module:debug/track"
local function_module = "Module:fun"
local math_module = "Module:math"


local export = {}
local table = table


-- Define often-used variables and functions.
local concat = table.concat
local floor = math.floor
local contains -- defined as export.contains
local infinity = math.huge
local deep_copy -- defined as export.deepCopy
local checkType = libraryUtil.checkType
local deep_equals -- defined as export.deepEquals
local checkTypeMulti = libraryUtil.checkTypeMulti
local format = string.format
local getmetatable = getmetatable
local insert = table.insert
local insert_if_not -- defined as export.insertIfNot
local invert -- defined as export.invert
local ipairs = ipairs
local ipairs_default_iter = ipairs{export}
local keys_to_list -- defined as export.keysToList
local list_to_set -- defined as export.listToSet
local next = next
local num_keys -- defined as export.numKeys
local pairs = pairs
local pcall = pcall
local raw_pairs -- defined as export.rawPairs
local rawequal = rawequal
local rawget = rawget
local require = require
local select = select
local setmetatable = setmetatable
local signed_index -- defined as export.signedIndex
local sort = table.sort
local sparse_ipairs -- defined as export.sparseIpairs
local table_len -- defined as export.length
local table_reverse -- defined as export.reverse
local type = type


local function _check(funcName, expectType)
--[==[
if type(expectType) == "string" then
Loaders for functions in other modules, which overwrite themselves with the target function when called. This ensures modules are only loaded when needed, retains the speed/convenience of locally-declared pre-loaded functions, and has no overhead after the first call, since the target functions are called directly in any subsequent calls.]==]
return function(argIndex, arg, nilOk)
local function debug_track(...)
checkType(funcName, argIndex, arg, expectType, nilOk)
debug_track = require(debug_track_module)
end
return debug_track(...)
end
local function is_callable(...)
is_callable = require(function_module).is_callable
return is_callable(...)
end
local function is_integer(...)
is_integer = require(math_module).is_integer
return is_integer(...)
end
local function is_positive_integer(...)
is_positive_integer = require(math_module).is_positive_integer
return is_positive_integer(...)
end
local function string_sort(...)
string_sort = require(collation_module).string_sort
return string_sort(...)
end
 
--[==[
Returns a clone of an object. If the object is a table, the value returned is a new table, but all subtables and functions are shared. Metamethods are respected unless the `raw` flag is set, but the returned table will have no metatable of its own.]==]
function export.shallowCopy(orig, raw)
if type(orig) ~= "table" then
return orig
end
local copy, iter, state, init = {}
if raw then
iter, state = next, orig
else
else
return function(argIndex, arg, expectType, nilOk)
iter, state, init = pairs(orig)
if type(expectType) == "table" then
-- Track instances of data loaded via `mw.loadData` being copied, which is very inefficient and usually unnecessary.
checkTypeMulti(funcName, argIndex, arg, expectType, nilOk)
-- `mw.loadData` sets the key "mw_loadData" to true in the metatable.
else
local mt = getmetatable(orig)
checkType(funcName, argIndex, arg, expectType, nilOk)
if mt and type(mt) == "table" and rawget(mt, "mw_loadData") == true then
end
debug_track("table/shallowCopy/loaded data")
end
end
end
end
for k, v in iter, state, init do
copy[k] = v
end
return copy
end
end


--[[
function export.shallowcopy(orig, raw)
------------------------------------------------------------------------------------
return export.shallowCopy(orig, raw)
-- isPositiveInteger
--
-- This function returns true if the given value is a positive integer, and false
-- if not. Although it doesn't operate on tables, it is included here as it is
-- useful for determining whether a given table key is in the array part or the
-- hash part of a table.
------------------------------------------------------------------------------------
--]]
function export.isPositiveInteger(v)
return type(v) == 'number' and v >= 1 and floor(v) == v and v < infinity
end
end


--[[
do
------------------------------------------------------------------------------------
local function make_copy(orig, seen, mt_flag, keep_loaded_data, tracked)
-- isNan
if type(orig) ~= "table" then
--
return orig
-- This function returns true if the given number is a NaN value, and false
end
-- if not. Although it doesn't operate on tables, it is included here as it is
local memoized = seen[orig]
-- useful for determining whether a value can be a valid table key. Lua will
if memoized ~= nil then
-- generate an error if a NaN is used as a table key.
return memoized
------------------------------------------------------------------------------------
end
--]]
local mt, iter, state, init = getmetatable(orig)
function export.isNan(v)
-- `mt` could be a non-table if `__metatable` has been used, but discard it in such cases.
if type(v) == 'number' and tostring(v) == '-nan' then
if not (mt and type(mt) == "table") then
return true
mt, iter, state, init = nil, next, orig, nil
else
-- Data loaded via `mw.loadData`, which sets the key "mw_loadData" to true in the metatable.
return false
elseif rawget(mt, "mw_loadData") == true then
if keep_loaded_data then
seen[orig] = orig
return orig
-- Track instances of such data being copied, which is very inefficient and usually unnecessary.
elseif not tracked then
debug_track("table/deepCopy/loaded data")
tracked = true
end
-- Discard the metatable, and use the `__pairs` metamethod.
mt, iter, state, init = nil, pairs(orig)
-- Otherwise, keep `mt`.
else
-- Track copied metatables to find any instances where it's really necessary, as it would be preferable for the default to be `pairs` instead of `next` (i.e. using __pairs if present, returning a table with no metatable).
if not tracked then
debug_track("table/deepCopy/copied metatable")
tracked = true
end
iter, state, init = next, orig, nil
end
local copy = {}
seen[orig] = copy
for k, v in iter, state, init do
copy[make_copy(k, seen, mt_flag, keep_loaded_data, tracked)] = make_copy(v, seen, mt_flag, keep_loaded_data, tracked)
end
if mt == nil or mt_flag == "none" then
return copy
elseif mt_flag ~= "keep" then
mt = make_copy(mt, seen, mt_flag, keep_loaded_data, tracked)
end
return setmetatable(copy, mt)
end
 
--[==[
Recursive deep copy function. Preserves copied identities of subtables.
A more powerful version of {mw.clone}, with customizable options.
* By default, metatables are copied, except for data loaded via {mw.loadData} (see below). If `metatableFlag` is set to "none", the copy will not have any metatables at all. Conversely, if `metatableFlag` is set to "keep", then the cloned table (and all its members) will have the exact same metatable as their original version.
* If `keepLoadedData` is true, then any data loaded via {mw.loadData} will not be copied, and the original will be used instead. This is useful in iterative contexts where it is necessary to copy data being destructively modified, because objects loaded via mw.loadData are immutable.
* Notes:
*# Protected metatables will not be copied (i.e. those hidden behind a __metatable metamethod), as they are not
  accessible by Lua's design. Instead, the output of the __metatable method will be used instead.
*# When iterating over the table, the __pairs metamethod is ignored, since this can prevent the table from being properly cloned.
*# Data loaded via mw.loadData is a special case in two ways: the metatable is stripped, because otherwise the cloned table throws errors when accessed; in addition, the __pairs metamethod is used, since otherwise the cloned table would be empty.]==]
function export.deepCopy(orig, metatableFlag, keepLoadedData)
return make_copy(orig, {}, metatableFlag, keepLoadedData)
end
end
deep_copy = export.deepCopy
end
end


--[[
--[==[
------------------------------------------------------------------------------------
Given an array and a signed index, returns the true table index. If the signed index is negative, the array will be counted from the end, where {-1} is the highest index in the array; otherwise, the returned index will be the same. To aid optimization, the first argument may be a number representing the array length instead of the array itself; this is useful when the array length is already known, as it avoids recalculating it each time this function is called.]==]
-- shallowClone
function export.signedIndex(t, k)
--
if not is_integer(k) then
-- This returns a clone of a table. The value returned is a new table, but all
error("index must be an integer")
-- subtables and functions are shared. Metamethods are respected, but the returned
-- table will have no metatable of its own.
------------------------------------------------------------------------------------
--]]
function export.shallowClone(t)
local ret = {}
for k, v in pairs(t) do
ret[k] = v
end
end
return ret
return k < 0 and (type(t) == "table" and table_len(t) or t) + k + 1 or k
end
end
signed_index = export.signedIndex


--[[
--[==[
Shallow copy
Returns the highest positive integer index of a table or array that possibly has holes in it, or otherwise 0 if no positive integer keys are found. Note that this differs from `table.maxn`, which returns the highest positive numerical index, even if it is not an integer.]==]
]]
function export.maxIndex(t)
function export.shallowcopy(orig)
local max = 0
local orig_type = type(orig)
for k in pairs(t) do
local copy
if is_positive_integer(k) and k > max then
if orig_type == 'table' then
max = k
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
end
else -- number, string, boolean, etc
copy = orig
end
end
return copy
return max
end
end


--[[
--[==[
Recursive deep copy function
Append any number of lists together and returns the result. Compare the Lisp expression {(append list1 list2 ...)}.]==]
Equivalent to mw.clone?
function export.append(...)
]]
local args, list, n = {...}, {}, 0
local function deepcopy(orig, includeMetatable, already_seen)
for i = 1, select("#", ...) do
-- Stores copies of tables indexed by the original table.
local t, j = args[i], 0
already_seen = already_seen or {}
while true do
j = j + 1
local copy = already_seen[orig]
local v = t[j]
if copy ~= nil then
if v == nil then
return copy
break
end
n = n + 1
list[n] = v
end
end
end
return list
if type(orig) == 'table' then
end
copy = {}
 
for orig_key, orig_value in pairs(orig) do
--[==[
copy[deepcopy(orig_key, includeMetatable, already_seen)] = deepcopy(orig_value, includeMetatable, already_seen)
Extend an existing list by a new list, modifying the existing list in-place. Compare the Python expression
end
{list.extend(new_items)}.
already_seen[orig] = copy
 
`options` is an optional table of additional options to control the behavior of the operation. The following options are
if includeMetatable then
recognized:
local mt = getmetatable(orig)
* `insertIfNot`: Use {export.insertIfNot()} instead of {table.insert()}, which ensures that duplicate items do not get
if mt ~= nil then
  inserted (at the cost of an O((M+N)*N) operation, where M = #list and N = #new_items).
local mt_copy = deepcopy(mt, includeMetatable, already_seen)
* `key`: As in {insertIfNot()}. Ignored otherwise.
setmetatable(copy, mt_copy)
* `pos`: As in {insertIfNot()}. Ignored otherwise.]==]
end
function export.extend(t, new_items, options)
local i, insert_if_not_option = 0, options and options.insertIfNot
while true do
i = i + 1
local item = new_items[i]
if item == nil then
return
elseif insert_if_not_option then
insert_if_not(t, item, options)
else
insert(t, item)
end
end
else -- number, string, boolean, etc
copy = orig
end
end
return copy
end
end
export.extendList = export.extend


function export.deepcopy(orig, noMetatable, already_seen)
--[==[
checkType("deepcopy", 3, already_seen, "table", true)
Given a list, returns a new list consisting of the items between the start index `i` and end index `j` (inclusive). `i` defaults to `1`, and `j` defaults to the length of the input list.]==]
function export.slice(t, i, j)
return deepcopy(orig, not noMetatable, already_seen)
local t_len = table_len(t)
i = i and signed_index(t_len, i) or 1
local list, offset = {}, i - 1
for key = i, j and signed_index(t_len, j) or t_len do
list[key - offset] = t[key]
end
return list
end
end


--[[
do
------------------------------------------------------------------------------------
local pos_nan, neg_nan
-- removeDuplicates
--[==[
--
Remove any duplicate values from a list, ignoring non-positive-integer keys. The earliest value is kept, and all subsequent duplicate values are removed, but otherwise the list order is unchanged.]==]
-- This removes duplicate values from an array. Non-positive-integer keys are
function export.removeDuplicates(t)
-- ignored. The earliest value is kept, and all subsequent duplicate values are
local list, seen, i, n = {}, {}, 0, 0
-- removed, but otherwise the array order is unchanged.
while true do
------------------------------------------------------------------------------------
i = i + 1
--]]
local v = t[i]
function export.removeDuplicates(t)
if v == nil then
checkType('removeDuplicates', 1, t, 'table')
return list
local isNan = export.isNan
end
local ret, exists = {}, {}
local memo_key
local index = 1
if v == v then
for _, v in ipairs(t) do
memo_key = v
if isNan(v) then
-- NaN
-- NaNs can't be table keys, and they are also unique, so we don't need to check existence.
elseif format("%f", v) == "nan" then
ret[index] = v
if not pos_nan then
index = index + 1
pos_nan = {}
else
end
if not exists[v] then
memo_key = pos_nan
ret[index] = v
-- -NaN
index = index + 1
else
exists[v] = true
if not neg_nan then
neg_nan = {}
end
memo_key = neg_nan
end
if not seen[memo_key] then
n = n + 1
list[n], seen[memo_key] = v, true
end
end
end
end
end
end
return ret
end
end


--[[
--[==[
------------------------------------------------------------------------------------
Given a table, return an array containing all positive integer keys, sorted in numerical order.]==]
-- numKeys
function export.numKeys(t)
--
local nums, i = {}, 0
-- This takes a table and returns an array containing the numbers of any numerical
for k in pairs(t) do
-- keys that have non-nil values, sorted in numerical order.
if is_positive_integer(k) then
------------------------------------------------------------------------------------
i = i + 1
--]]
nums[i] = k
function export.numKeys(t, checked)
if not checked then
checkType('numKeys', 1, t, 'table')
end
local isPositiveInteger = export.isPositiveInteger
local nums = {}
local index = 1
for k, _ in pairs(t) do
if isPositiveInteger(k) then
nums[index] = k
index = index + 1
end
end
end
end
table.sort(nums)
sort(nums)
return nums
return nums
end
end
num_keys = export.numKeys


function export.maxIndex(t)
--[==[
checkType('maxIndex', 1, t, 'table')
This takes a list with one or more nil values, and removes the nil values while preserving the order, so that the list can be safely traversed with ipairs.]==]
local positiveIntegerKeys = export.numKeys(t)
function export.compressSparseArray(t)
if positiveIntegerKeys[1] then
local list, keys, i = {}, num_keys(t), 0
return math.max(unpack(positiveIntegerKeys))
while true do
else
i = i + 1
return 0 -- ???
local k = keys[i]
if k == nil then
return list
end
list[i] = t[k]
end
end
end
end


--[[
--[==[
------------------------------------------------------------------------------------
An iterator which works like `pairs`, but ignores any `__pairs` metamethod.]==]
-- affixNums
function export.rawPairs(t)
--
return next, t, nil
-- This takes a table and returns an array containing the numbers of keys with the
end
-- specified prefix and suffix.
raw_pairs = export.rawPairs
-- affixNums({a1 = 'foo', a3 = 'bar', a6 = 'baz'}, "a")
 
-- ↓
--[==[
-- {1, 3, 6}.
An iterator which works like `ipairs`, but ignores any `__ipairs` metamethod.]==]
------------------------------------------------------------------------------------
function export.rawIpairs(t)
--]]
return ipairs_default_iter, t, 0
function export.affixNums(t, prefix, suffix)
end
local check = _check('affixNums')
 
check(1, t, 'table')
do
check(2, prefix, 'string', true)
local current
check(3, suffix, 'string', true)
--[==[
An iterator which works like `pairs`, except that it also respects the `__index` metamethod. This works by iterating over the input table with `pairs`, followed by the table at its `__index` metamethod (if any). This is then repeated for that table's `__index` table and so on, with any repeated keys being skipped over, until there are no more tables, or a table repeats (so as to prevent an infinite loop). If `__index` is a function, however, then it is ignored, since there is no way to iterate over its return values.
local function cleanPattern(s)
A `__pairs` metamethod will be respected for any given table instead of iterating over it directly, but these will be ignored if the `raw` flag is set.
-- Cleans a pattern so that the magic characters ()%.[]*+-?^$ are interpreted literally.
 
s = s:gsub('([%(%)%%%.%[%]%*%+%-%?%^%$])', '%%%1')
Note: this function can be used as a `__pairs` metamethod. In such cases, it does not call itself, since this would cause an infinite loop, so it treats the relevant table as having no `__pairs` metamethod. Other `__pairs` metamethods on subsequent tables will still be respected.]==]
return s
function export.indexPairs(t, raw)
end
-- If there's no metatable, result is identical to `pairs`.
-- To prevent infinite loops, act like `pairs` if `current` is set with `t`, which means this function is being used as a __pairs metamethod.
prefix = prefix or ''
if current and current[t] or getmetatable(t) == nil then
suffix = suffix or ''
return next, t, nil
prefix = cleanPattern(prefix)
end
suffix = cleanPattern(suffix)
local pattern = '^' .. prefix .. '([1-9]%d*)' .. suffix .. '$'
-- `seen_k` memoizes keys, as they should never repeat; `seen_t` memoizes tables iterated over.
local seen_k, seen_t, iter, state, k, v, success = {}, {[t] = true}
local nums = {}
local index = 1
return function()
for k, _ in pairs(t) do
while true do
if type(k) == 'string' then
if iter == nil then
local num = mw.ustring.match(k, pattern)
-- If `raw` is set, use `next`.
if num then
if raw then
nums[index] = tonumber(num)
iter, state, k = next, t, nil
index = index + 1
-- Otherwise, call `pairs`, setting `current` with `t` so that export.indexPairs knows to return `next` if it's being used as a metamethod, as this prevents infinite loops. `t` is then unset, so that `current` doesn't get polluted if the loop breaks early.
else
if not current then
current = {}
end
current[t] = true
-- Use `pcall`, so that `t` can always be unset from `current`.
success, iter, state, k = pcall(pairs, t)
current[t] = nil
-- If there was an error, raise it.
if not success then
error(iter)
end
end
end
while true do
-- It's possible for a `__pairs` metamethod to return additional values, but assume there aren't any, since this iterator specifically relates to table indexes.
k, v = iter(state, k)
if k == nil then
break
-- If a repeated key is found, skip and iterate again.
elseif not seen_k[k] then
seen_k[k] = true
return k, v
end
end
-- If there's an __index metamethod, iterate over it iff it's a table not already seen before.
local mt = getmetatable(t)
-- `mt` might not be a table if __metatable is used.
if not mt or type(mt) ~= "table" then
return nil
end
seen_t[t] = true
t = rawget(mt, "__index")
if not t or type(t) ~= "table" then
return nil
-- Throw error if it's been seen before.
elseif seen_t[t] then
error("loop in gettable")
end
iter = nil -- New `iter` will be generated on the next iteration of the while loop.
end
end
end
end
end
end
table.sort(nums)
return nums
end
end


--[[
do
------------------------------------------------------------------------------------
local function ipairs_func(t, i)
-- numData
i = i + 1
--
local v = t[i]
-- Given a table with keys like ("foo1", "bar1", "foo2", "baz2"), returns a table
if v ~= nil then
-- of subtables in the format
return i, v
-- { [1] = {foo = 'text', bar = 'text'}, [2] = {foo = 'text', baz = 'text'} }
-- Keys that don't end with an integer are stored in a subtable named "other".
-- The compress option compresses the table so that it can be iterated over with
-- ipairs.
------------------------------------------------------------------------------------
--]]
function export.numData(t, compress)
local check = _check('numData')
check(1, t, 'table')
check(2, compress, 'boolean', true)
local ret = {}
for k, v in pairs(t) do
local prefix, num = tostring(k):match('^([^0-9]*)([1-9][0-9]*)$')
if num then
num = tonumber(num)
local subtable = ret[num] or {}
if prefix == '' then
-- Positional parameters match the blank string; put them at the start of the subtable instead.
prefix = 1
end
subtable[prefix] = v
ret[num] = subtable
else
local subtable = ret.other or {}
subtable[k] = v
ret.other = subtable
end
end
end
end
if compress then
local other = ret.other
--[==[
ret = export.compressSparseArray(ret)
An iterator which works like `ipairs`, except that it also respects the `__index` metamethod. This works by looking up values in the table, iterating integers from key `1` until no value is found.]==]
ret.other = other
function export.indexIpairs(t)
-- If there's no metatable, just use the default ipairs iterator.
return getmetatable(t) == nil and ipairs_default_iter or ipairs_func, t, 0
end
end
return ret
end
end


--[[
--[==[
------------------------------------------------------------------------------------
An iterator which works like `indexIpairs`, but which only returns the value.]==]
-- compressSparseArray
function export.iterateList(t)
--
local i = 0
-- This takes an array with one or more nil values, and removes the nil values
return function(t)
-- while preserving the order, so that the array can be safely traversed with
i = i + 1
-- ipairs.
return t[i]
------------------------------------------------------------------------------------
end, t
--]]
function export.compressSparseArray(t)
checkType('compressSparseArray', 1, t, 'table')
local ret = {}
local index = 1
local nums = export.numKeys(t)
for _, num in ipairs(nums) do
ret[index] = t[num]
index = index + 1
end
return ret
end
end


--[[
--[==[
------------------------------------------------------------------------------------
This is an iterator for sparse arrays. It can be used like ipairs, but can handle nil values.]==]
-- sparseIpairs
--
-- This is an iterator for sparse arrays. It can be used like ipairs, but can
-- handle nil values.
------------------------------------------------------------------------------------
--]]
function export.sparseIpairs(t)
function export.sparseIpairs(t)
checkType('sparseIpairs', 1, t, 'table')
local keys, i = num_keys(t), 0
local nums = export.numKeys(t)
return function(t)
local i = 0
return function()
i = i + 1
i = i + 1
local key = nums[i]
local k = keys[i]
if key then
if k then
return key, t[key]
return k, t[k]
else
return nil, nil
end
end
end
end, t
end
end
sparse_ipairs = export.sparseIpairs


--[[
--[==[
------------------------------------------------------------------------------------
This returns the size of a key/value pair table. If `raw` is set, then metamethods will be ignored, giving the true table size.
-- size
 
--
For arrays, it is faster to use `export.length`.]==]
-- This returns the size of a key/value pair table. It will also work on arrays,
function export.size(t, raw)
-- but for arrays it is more efficient to use the # operator.
local i, iter, state, init = 0
------------------------------------------------------------------------------------
if raw then
--]]
iter, state, init = next, t, nil
function export.size(t)
else
checkType('size', 1, t, 'table')
iter, state, init = pairs(t)
local i = 0
end
for _ in pairs(t) do
for _ in iter, state, init do
i = i + 1
i = i + 1
end
end
Line 359: Line 451:
end
end


--[[
--[==[
-- This returns the length of a table, or the first integer key n counting from
This returns the length of a table, or the first integer key n counting from 1 such that t[n + 1] is nil. It is a more reliable form of the operator `#`, which can become unpredictable under certain circumstances due to the implementation of tables under the hood in Lua, and therefore should not be used when dealing with arbitrary tables. `#` also does not use metamethods, so will return the wrong value in cases where it is desirable to take these into account (e.g. data loaded via `mw.loadData`). If `raw` is set, then metamethods will be ignored, giving the true table length.
-- 1 such that t[n + 1] is nil. It is similar to the operator #, but may return
 
-- a different value when there are gaps in the array portion of the table.
For arrays, this function is faster than `export.size`.]==]
-- Intended to be used on data loaded with mw.loadData. For other tables, use #.
function export.length(t, raw)
--]]
local n = 0
function export.length(t)
if raw then
local i = 0
for i in ipairs_default_iter, t, 0 do
n = i
end
return n
end
repeat
repeat
i = i + 1
n = n + 1
until t[i] == nil
until t[n] == nil
return i - 1
return n - 1
end
end
table_len = export.length
do
local function is_equivalent(a, b, seen, include_mt, pairs_func)
-- Raw equality check.
if rawequal(a, b) then
return true
-- If not equal, a and b can only be equivalent if they're both tables.
elseif not (type(a) == "table" and type(b) == "table") then
return false
end
-- If `a` and `b` have been compared before, return the memoized result. This will usually be true, since failures normally fail the whole check outright, but match failures can occur during the laborious check without this happening, so it could be false.
local memo_a = seen[a]
if memo_a then
local result = memo_a[b]
if result ~= nil then
return result
end
-- To avoid recursive references causing infinite loops, assume the tables currently being compared are equivalent by memoizing them as true; this will be corrected to false if there's a match failure.
memo_a[b] = true
else
memo_a = {[b] = true}
seen[a] = memo_a
end
-- Don't bother checking `memo_b` for `a`, since if `a` and `b` had been compared before, then `b` would be in `memo_a`.
local memo_b = seen[b]
if memo_b then
memo_b[a] = true
else
memo_b = {[a] = true}
seen[b] = memo_b
end
-- If `include_mt` is set, check the metatables are equivalent.
if include_mt and not is_equivalent(getmetatable(a), getmetatable(b), seen, true, pairs_func) then
memo_a[b], memo_b[a] = false, false
return false
end
-- Copy all key/values pairs in `b` to `remaining_b`, and count the size: this uses `pairs_func`, which will also be used to iterate over `a`, ensuring that `a` and `b` are iterated over in the same way. This is necessary to ensure that `export.deepEquals(a, b)` and `export.deepEquals(b, a)` always give the same result. Simply iterating over `a` while accessing keys in `b` for comparison would ignore any `__pairs` metamethod that `b` has, which could cause non-symmetrical outputs if `__pairs` returns more or less than the complete set of key/value pairs accessible via `__index`, so using `pairs_func` for both `a` and `b` prevents this.
-- TODO: handle exotic `__pairs` methods which return the same key multiple times with different values.
local remaining_b, size_b = {}, 0
for k_b, v_b in pairs_func(b) do
remaining_b[k_b], size_b = v_b, size_b + 1
end
-- Fast check: iterate over the keys in `a`, checking if an equivalent value exists at the same key in `remaining_b`. As matches are found, key/value pairs are removed from `remaining_b`. If any keys in `a` or `remaining_b` are tables, the fast check will only work if the exact same object exists as a key in the other table. Any others from `a` that don't match anything in `remaining_b` are added to `remaining_a`, while those in `remaining_b` that weren't found will still remain once the loop ends. `remaining_a` and `remaining_b` are then compared at the end with the laborious check.
local size_a, remaining_a = 0
for k, v_a in pairs_func(a) do
local v_b = remaining_b[k]
-- If `k` isn't in `remaining_b`, `a` and `b` can't be equivalent unless it's a table.
if v_b == nil then
if type(k) ~= "table" then
memo_a[b], memo_b[a] = false, false
return false
-- Otherwise, add the `k`/`v_a` pair to `remaining_a` for the laborious check.
elseif not remaining_a then
remaining_a = {}
end
remaining_a[k], size_a = v_a, size_a + 1
-- Otherwise, if `k` exists in `a` and `remaining_b`, `v_a` and `v_b` must be equivalent for there to be a match.
elseif is_equivalent(v_a, v_b, seen, include_mt, pairs_func) then
remaining_b[k], size_b = nil, size_b - 1
else
memo_a[b], memo_b[a] = false, false
return false
end
end
-- Must be the same number of remaining keys in each table.
if size_a ~= size_b then
memo_a[b], memo_b[a] = false, false
return false
-- If the size is 0, there's nothing left to check.
elseif size_a == 0 then
return true
end
-- Laborious check: since it's not possible to use table lookups, check each key/value pair in `remaining_a` against every key/value pair in `remaining_b` until a match is found, removing the matching key/value pair from `remaining_b` each time, to ensure one-to-one correspondence.
for k_a, v_a in next, remaining_a do
local success
for k_b, v_b in next, remaining_b do
-- Keys/value pairs must be equivalent in order to match.
if (
-- Check values first for speed, since they might not be tables.
is_equivalent(v_a, v_b, seen, include_mt, pairs_func) and
is_equivalent(k_a, k_b, seen, include_mt, pairs_func)
) then
-- Remove matched key from `remaining_b`, and break the inner loop.
success, remaining_b[k_b] = true, nil
break
end
end
-- Fail if `remaining_b` runs out of keys, as the `k_a`/`v_a` pair still hasn't matched.
if not success then
memo_a[b], memo_b[a] = false, false
return false
end
end
-- If every key/value pair in `remaining_a` matched with one in `remaining_b`, `a` and `b` must be equivalent. Note that `remaining_b` will now be empty, since the laborious check only starts if `remaining_a` and `remaining_b` are the same size.
return true
end


--[[
--[==[
Takes table and a value to be found.
Recursively compare two values that may be tables, and returns true if all key-value pairs are structurally equivalent. Note that this handles arbitrary nesting of subtables (including recursive nesting) to any depth, for keys as well as values.
If the value is in the array portion of the table, return true.
 
If the value is in the hashmap or not in the table, return false.
If `include_mt` is true, then metatables are also compared. If `raw` is true, then metamethods are not used during the comparison.]==]
]]
function export.deepEquals(a, b, include_mt, raw)
function export.contains(list, x)
return is_equivalent(a, b, {}, include_mt, raw and raw_pairs or pairs)
for _, v in ipairs(list) do
if v == x then return true end
end
end
return false
deep_equals = export.deepEquals
end
end


--[[
do
Recursively compare two values that may be tables, including tables with
local function get_nested(t, k, ...)
nested tables as values. Return true if both values are structurally equal.
if t == nil then
Note that this handles arbitary levels of nesting. If all tables are known
return nil
to be lists (with only integral keys), use export.deepEqualsList, which will
elseif select("#", ...) ~= 0 then
be more efficient.
return get_nested(t[k], ...)
end
return t[k]
end
 
--[==[
Given a table and an arbitrary number of keys, will successively access subtables using each key in turn, returning the value at the final key. For example, if {t} is { {[1] = {[2] = {[3] = "foo"}}}}, {export.getNested(t, 1, 2, 3)} will return {"foo"}.


NOTE: This is *NOT* smart enough to properly handle cycles; in such a case, it
If no subtable exists for a given key value, returns nil, but will throw an error if a non-table is found at an intermediary key.]==]
will get into an infinite loop.
function export.getNested(t, ...)
]]
if t == nil or select("#", ...) == 0 then
function export.deepEquals(x, y)
error("Must provide a table and at least one key.")
    if type(x) == "table" and type(y) == "table" then
end
    -- Two tables are the same if they have the same number of elements
return get_nested(t, ...)
    -- and all keys that are present in one of the tables compare equal
end
    -- to the corresponding keys in the other table, using structural
    -- comparison.
    local sizex = 0
        for key, value in pairs(x) do
            if not export.deepEquals(value, y[key]) then
                return false
            end
            sizex = sizex + 1
        end
        local sizey = export.size(y)
        if sizex ~= sizey then
            return false
        end
        return true
    end
    return x == y
end
end


--[[
do
Recursively compare two values that may be lists (i.e. tables with integral
local function set_nested(t, v, k, ...)
keys), including lists with nested lists as values. Return true if both values
if select("#", ...) == 0 then
are structurally equal. Note that this handles arbitary levels of nesting.
t[k] = v
Results are undefined if tables with non-integral keys are present anywhere in
return
either structure; if that may be the case, use export.deepEquals, which will
end
handle such tables correctly but be less efficient on lists than
local next_t = t[k]
export.deepEqualsList.
if next_t == nil then
-- If there's no next table while setting nil, there's nothing more to do.
if v == nil then
return
end
next_t = {}
t[k] = next_t
end
return set_nested(next_t, v, ...)
end
 
--[==[
Given a table, value and an arbitrary number of keys, will successively access subtables using each key in turn, and sets the value at the final key. For example, if {t} is { {} }, {export.setNested(t, "foo", 1, 2, 3)} will modify {t} to { {[1] = {[2] = {[3] = "foo"} } } }.
 
If no subtable exists for a given key value, one will be created, but the function will throw an error if a non-table value is found at an intermediary key.


NOTE: This is *NOT* smart enough to properly handle cycles; in such a case, it
Note: the parameter order (table, value, keys) differs from functions like rawset, because the number of keys can be arbitrary. This is to avoid situations where an additional argument must be appended to arbitrary lists of variables, which can be awkward and error-prone: for example, when handling variable arguments ({{lua|...}}) or function return values.]==]
will get into an infinite loop.
function export.setNested(t, ...)
]]
if t == nil or select("#", ...) < 2 then
function export.deepEqualsList(x, y)
error("Must provide a table and at least one key.")
    if type(x) == "table" and type(y) == "table" then
end
        if #x ~= #y then
return set_nested(t, ...)
            return false
end
        end  
end
        for key, value in ipairs(x) do
 
            if not export.deepEqualsList(value, y[key]) then
--[==[
                return false
Given a list and a value to be found, return true if the value is in the array portion of the list. Comparison is by value, using `deepEquals`.]==]
            end
function export.contains(list, x, options)
        end
if options and options.key then
        return true
x = options.key(x)
    end
end
    return x == y
local i = 0
while true do
i = i + 1
local v = list[i]
if v == nil then
return false
elseif options and options.key then
v = options.key(v)
end
if deep_equals(v, x) then
return true
end
end
end
end
contains = export.contains


--[[
--[==[
Finds key for specified value in a given table.
Given a general table and a value to be found, return true if the value is in
Roughly equivalent to reversing the key-value pairs in the table –
either the array or hashmap portion of the table. Comparison is by value, using
reversed_table = { [value1] = key1, [value2] = key2, ... }
`deepEquals`.]==]
– and then returning reversed_table[valueToFind].
function export.tableContains(t, x)
for _, v in pairs(t) do
The value can only be a string or a number
if deep_equals(v, x) then
(not nil, a boolean, a table, or a function).
return true
Only reliable if there is just one key with the specified value.
Otherwise, the function returns the first key found,
and the output is unpredictable.
]]
function export.keyFor(t, valueToFind)
local check = _check('keyFor')
check(1, t, 'table')
check(2, valueToFind, { 'string', 'number' })
for key, value in pairs(t) do
if value == valueToFind then
return key
end
end
end
end
return false
return nil
end
end


--[[
--[==[
The default sorting function used in export.keysToList if no keySort
Given a `list` and a `new_item` to be inserted, append the value to the end of the list if not already present
is defined.
(or insert at an arbitrary position, if `options.pos` is given; see below). Comparison is by value, using {deepEquals}.
]]
 
local function defaultKeySort(key1, key2)
`options` is an optional table of additional options to control the behavior of the operation. The following options are
-- "number" < "string", so numbers will be sorted before strings.
recognized:
local type1, type2 = type(key1), type(key2)
* `pos`: Position at which insertion happens (i.e. before the existing item at position `pos`).
if type1 ~= type2 then
* `key`: Function of one argument to return a comparison key, as with {deepEquals}. The key function is applied to both
return type1 < type2
`item` and the existing item in `list` to compare against, and the comparison is done against the results.
This is useful when inserting a complex structure into an existing list while avoiding duplicates.
* `combine`: Function of three arguments (the existing item, the new item and the position, respectively) to combine an
existing item with `new_item`, when `new_item` is found in `list`. If unspecified, the existing item is
left alone.
 
Returns {false} if an entry is already found, or {true} if inserted.
 
For compatibility, `pos` can be specified directly as the third argument in place of `options`, but this is not
recommended for new code.
 
NOTE: This function is O(N) in the size of the existing list. If you use this function in a loop to insert several
items, you will get O(M*(M+N)) behavior, effectively O((M+N)^2). Thus it is not recommended to use this unless you are
sure the total number of items will be small. (An alternative for large lists is to insert all the items without
checking for duplicates, and use {removeDuplicates()} at the end.)]==]
function export.insertIfNot(list, new_item, options)
if type(options) == "number" then
options = {pos = options}
end
if options and options.combine then
local new_key
-- Don't use options.key and options.key(new_item) or new_item in case the key is legitimately false or nil.
if options.key then
new_key = options.key(new_item)
else
new_key = new_item
end
local i = 0
while true do
i = i + 1
local item, key = list[i]
if item == nil then
break
elseif options.key then
key = options.key(item)
else
key = item
end
if deep_equals(key, new_key) then
local retval = options.combine(item, new_item, i)
if retval ~= nil then
list[i] = retval
end
return false
end
end
elseif contains(list, new_item, options) then
return false
end
local pos = options and options.pos
if pos then
insert(list, pos, new_item)
else
else
return key1 < key2
insert(list, new_item)
end
end
insert_if_not = export.insertIfNot
 
--[==[
Finds key for specified value in a given table. Roughly equivalent to reversing the key-value pairs in the table:
* {reversed_table = { [value1] = key1, [value2] = key2, ... }}
and then returning {reversed_table[value]}. Comparison is by value, using `deepEquals`.
 
Only reliable if there is just one key with the specified value. Otherwise, the function returns the first key found,
and the output is unpredictable.]==]
function export.keyFor(t, x)
for k, v in pairs(t) do
if deep_equals(v, x) then
return k
end
end
end
return nil
end
end


--[[
do
Returns a list of the keys in a table, sorted using either the default
local types
table.sort function or a custom keySort function.
local function get_types()
If there are only numerical keys, numKeys is probably more efficient.
types, get_types = invert{
]]
"number",
function export.keysToList(t, keySort, checked)
"boolean",
if not checked then
"string",
local check = _check('keysToList')
"table",
check(1, t, 'table')
"function",
check(2, keySort, 'function', true)
"thread",
"userdata"
}, nil
return types
end
end
local list = {}
local function less_than(key1, key2)
local index = 1
return key1 < key2
for key, _ in pairs(t) do
list[index] = key
index = index + 1
end
end
-- Place numbers before strings, otherwise sort using <.
-- The default sorting function used in export.keysToList if `keySort` is not given.
if not keySort then
local function default_compare(key1, key2)
keySort = defaultKeySort
local type1, type2 = type(key1), type(key2)
if type1 ~= type2 then
-- If the types are different, sort numbers first, functions last, and all other types alphabetically.
return (types or get_types())[type1] < types[type2]
-- `string_sort` fixes a bug in < which causes all codepoints above U+FFFF to be treated as equal.
elseif type1 == "string" then
return string_sort(key1, key2)
elseif type1 == "number" then
return key1 < key2
-- Attempt to compare tables, in case there's a metamethod.
elseif type1 == "table" then
local success, result = pcall(less_than, key1, key2)
if success then
return result
end
-- Sort true before false.
elseif type1 == "boolean" then
return key1
end
return false
end
 
--[==[
Returns a list of the keys in a table, sorted using either the default `table.sort` function or a custom `keySort` function.
 
If there are only numerical keys, `export.numKeys` is probably faster.]==]
function export.keysToList(t, keySort)
local list, i = {}, 0
for key in pairs(t) do
i = i + 1
list[i] = key
end
-- Use specified sort function, or otherwise `default_compare`.
sort(list, keySort or default_compare)
return list
end
end
keys_to_list = export.keysToList
table.sort(list, keySort)
return list
end
end


--[[
--[==[
Iterates through a table, with the keys sorted using the keysToList function.
Iterates through a table, with the keys sorted using the keysToList function.
If there are only numerical keys, sparseIpairs is probably more efficient.
 
]]
If there are only numerical keys, `export.sparseIpairs` is probably faster.]==]
function export.sortedPairs(t, keySort)
function export.sortedPairs(t, keySort)
local check = _check('keysToList')
local list, i = keys_to_list(t, keySort), 0
check(1, t, 'table')
return function(t)
check(2, keySort, 'function', true)
local list = export.keysToList(t, keySort, true)
local i = 0
return function()
i = i + 1
i = i + 1
local key = list[i]
local key = list[i]
if key ~= nil then
if key ~= nil then
return key, t[key]
return key, t[key]
else
return nil, nil
end
end
end, t
end
--[==[
Iterates through a table using `ipairs` in reverse.
`__ipairs` metamethods will be used, including those which return arbitrary (i.e. non-array) keys, but note that this function assumes that the first return value is a key which can be used to retrieve a value from the input table via a table lookup. As such, `__ipairs` metamethods for which this assumption is not true will not work correctly.
If the value `nil` is encountered early (e.g. because the table has been modified), the loop will terminate early.]==]
function export.reverseIpairs(t)
-- `__ipairs` metamethods can return arbitrary keys, so compile a list.
local keys, i = {}, 0
for k in ipairs(t) do
i = i + 1
keys[i] = k
end
return function(t)
if i == 0 then
return nil
end
local k = keys[i]
-- Retrieve `v` from the table. These aren't stored during the initial ipairs loop, so that they can be modified during the loop.
local v = t[k]
-- Return if not an early nil.
if v ~= nil then
i = i - 1
return k, v
end
end, t
end
local function getIteratorValues(i, j , step, t_len)
i, j = i and signed_index(t_len, i), j and signed_index(t_len, j)
if step == nil then
i, j = i or 1, j or t_len
return i, j, j < i and -1 or 1
elseif step == 0 or not is_integer(step) then
error("step must be a non-zero integer")
elseif step < 0 then
return i or t_len, j or 1, step
end
return i or 1, j or t_len, step
end
--[==[
Given an array `list` and function `func`, iterate through the array applying {func(r, k, v)}, and returning the result,
where `r` is the value calculated so far, `k` is an index, and `v` is the value at index `k`. For example,
{reduce(array, function(a, _, v) return a + v end)} will return the sum of `array`.
Optional arguments:
* `i`: start index; negative values count from the end of the array
* `j`: end index; negative values count from the end of the array
* `step`: step increment
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
backwards and by how much, based on these inputs (see examples below for default behaviours).
Examples:
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
# step=-1 results in backward iteration from the end to the start in steps of 1.
# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
# j=-3 results in forward iteration from the start to the 3rd last index.
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
function export.reduce(t, func, i, j, step)
i, j, step = getIteratorValues(i, j, step, table_len(t))
local ret = t[i]
for k = i + step, j, step do
ret = func(ret, k, t[k])
end
end
return ret
end
end


function export.reverseIpairs(list)
do
checkType('reverse_ipairs', 1, list, 'table')
local function replace(t, func, i, j, step, generate)
local t_len = table_len(t)
local i = #list + 1
-- Normalized i, j and step, based on the inputs.
return function()
local norm_i, norm_j, norm_step = getIteratorValues(i, j, step, t_len)
i = i - 1
if norm_step > 0 then
if list[i] ~= nil then
i, j, step = 1, t_len, 1
return i, list[i]
else
else
return nil, nil
i, j, step = t_len, 1, -1
end
end
-- "Signed" variables are multiplied by -1 if `step` is negative.
local t_new, signed_i, signed_j = generate and {} or t, norm_i * step, norm_j * step
for k = i, j, step do
-- Replace the values iff they're within the i to j range and `step` wouldn't skip the key.
-- Note: i > j if `step` is positive; i < j if `step` is negative. Otherwise, the range is empty.
local signed_k = k * step
if signed_k >= signed_i and signed_k <= signed_j and (k - norm_i) % norm_step == 0 then
t_new[k] = func(k, t[k])
-- Otherwise, add the existing value if `generate` is set.
elseif generate then
t_new[k] = t[k]
end
end
return t_new
end
--[==[
Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
`v` is the value at index `k`), replacing the relevant values with the result. For example,
{apply(array, function(_, v) return 2 * v end)} will double each member of the array.
Optional arguments:
* `i`: start index; negative values count from the end of the array
* `j`: end index; negative values count from the end of the array
* `step`: step increment
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
backwards and by how much, based on these inputs (see examples below for default behaviours).
Examples:
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
# step=-1 results in backward iteration from the end to the start in steps of 1.
# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
# j=-3 results in forward iteration from the start to the 3rd last index.
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
function export.apply(t, func, i, j, step)
return replace(t, func, i, j, step)
end
--[==[
Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
`v` is the value at index `k`), and return a shallow copy of the original array with the relevant values replaced. For example,
{generate(array, function(_, v) return 2 * v end)} will return a new array in which each value has been doubled.
Optional arguments:
* `i`: start index; negative values count from the end of the array
* `j`: end index; negative values count from the end of the array
* `step`: step increment
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
backwards and by how much, based on these inputs (see examples below for default behaviours).
Examples:
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
# step=-1 results in backward iteration from the end to the start in steps of 1.
# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
# j=-3 results in forward iteration from the start to the 3rd last index.
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
function export.generate(t, func, i, j, step)
return replace(t, func, i, j, step, true)
end
end
end
end


--[=[
--[==[
Joins an array with serial comma and serial conjunction, normally "and".
Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
An improvement on mw.text.listToText, which doesn't properly handle serial
`v` is the value at index `k`), and returning whether the function is true for all iterations.
commas.
 
Optional arguments:
Options:
* `i`: start index; negative values count from the end of the array
- conj
* `j`: end index; negative values count from the end of the array
Conjunction to use; defaults to "and".
* `step`: step increment
- italicizeConj
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
Italicize conjunction: for [[Module:Template:also]]
backwards and by how much, based on these inputs (see examples below for default behaviours).
- dontTag
 
Don't tag the serial comma and serial "and". For error messages, in
Examples:
which HTML cannot be used.
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
]=]
# step=-1 results in backward iteration from the end to the start in steps of 1.
function export.serialCommaJoin(seq, options)
# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
local check = _check("serialCommaJoin", "table")
# j=-3 results in forward iteration from the start to the 3rd last index.
check(1, seq)
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
check(2, options, true)
function export.all(t, func, i, j, step)
i, j, step = getIteratorValues(i, j, step, table_len(t))
local length = #seq
for k = i, j, step do
if not func(k, t[k]) then
if not options then
return false
options = {}
end
end
end
return true
local conj
end
if length > 1 then
 
conj = options.conj or "and"
--[==[
if options.italicizeConj then
Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
conj = "''" .. conj .. "''"
`v` is the value at index `k`), and returning whether the function is true for at least one iteration.
 
Optional arguments:
* `i`: start index; negative values count from the end of the array
* `j`: end index; negative values count from the end of the array
* `step`: step increment
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
backwards and by how much, based on these inputs (see examples below for default behaviours).
 
Examples:
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
# step=-1 results in backward iteration from the end to the start in steps of 1.
# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
# j=-3 results in forward iteration from the start to the 3rd last index.
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
function export.any(t, func, i, j, step)
i, j, step = getIteratorValues(i, j, step, table_len(t))
for k = i, j, step do
if not not (func(k, t[k])) then
return true
end
end
end
end
return false
end
 
--[==[
Joins an array with serial comma and serial conjunction, normally {"and"}. An improvement on {mw.text.listToText},
which doesn't properly handle serial commas.
 
Options:
* `conj`: Conjunction to use; defaults to {"and"}.
* `punc`: Punctuation to use; default to {","}.
* `dontTag`: Don't tag the serial comma and serial {"and"}. For error messages, in which HTML cannot be used.]==]
function export.serialCommaJoin(seq, options)
local length = table_len(seq)
if length == 0 then
if length == 0 then
return ""
return ""
elseif length == 1 then
elseif length == 1 then
return seq[1] -- nothing to join
return seq[1]
elseif length == 2 then
elseif length == 2 then
return seq[1] .. " " .. conj .. " " .. seq[2]
return seq[1] .. " " .. (options and options.conj or "and") .. " " .. seq[2]
end
 
local conj, punc, dont_tag
if options then
conj = options.conj or "and"
punc = options.punc or ","
dont_tag = options.dontTag
else
else
local comma = options.dontTag and "," or '<span class="serial-comma">,</span>'
conj, punc = "and", ","
conj = options.dontTag and ' ' .. conj .. " " or '<span class="serial-and"> ' .. conj .. '</span> '
end
return table.concat(seq, ", ", 1, length - 1) ..
comma .. conj .. seq[length]
local comma
if dont_tag then
comma = punc
conj = " " .. conj .. " "
else
comma = "<span class=\"serial-comma\">" .. punc .. "</span>"
conj = "<span class=\"serial-and\"> " .. conj .. "</span> "
end
return concat(seq, punc .. " ", 1, length - 1) .. comma .. conj .. seq[length]
end
 
--[==[
A function which works like `table.concat`, but respects any `__index` metamethod. This is useful for data loaded via `mw.loadData`.]==]
function export.concat(t, sep, i, j)
local list, k = {}, 0
while true do
k = k + 1
local v = t[k]
if v == nil then
return concat(list, sep, i, j)
end
list[k] = v
end
end
end
end


--[[
--[==[
Concatenates all values in the table that are indexed by a number, in order.
Concatenate all values in the table that are indexed by a number, in order.
sparseConcat{ a, nil, c, d }  =>  "acd"
* {sparseConcat{ a, nil, c, d }}  =>  {"acd"}
sparseConcat{ nil, b, c, d }  =>  "bcd"
* {sparseConcat{ nil, b, c, d }}  =>  {"bcd"}]==]
]]
function export.sparseConcat(t, sep, i, j)
function export.sparseConcat(t, sep, i, j)
local list = {}
local list, k = {}, 0
for _, v in sparse_ipairs(t) do
local list_i = 0
k = k + 1
for _, v in export.sparseIpairs(t) do
list[k] = v
list_i = list_i + 1
list[list_i] = v
end
end
return concat(list, sep, i, j)
return table.concat(list, sep, i, j)
end
end


--[[
--[==[
Values of numberic keys in array portion of table are reversed:
Values of numeric keys in array portion of table are reversed: { { "a", "b", "c" }} -> { { "c", "b", "a" }}]==]
{ "a", "b", "c" } -> { "c", "b", "a" }
--]]
function export.reverse(t)
function export.reverse(t)
checkType("reverse", 1, t, "table")
local list, t_len = {}, table_len(t)
for i = t_len, 1, -1 do
local new_t = {}
list[t_len - i + 1] = t[i]
local new_t_i = 1
for i = #t, 1, -1 do
new_t[new_t_i] = t[i]
new_t_i = new_t_i + 1
end
end
return new_t
return list
end
end
table_reverse = export.reverse


function export.reverseConcat(t, sep, i, j)
function export.reverseConcat(t, sep, i, j)
return table.concat(export.reverse(t), sep, i, j)
return concat(table_reverse(t), sep, i, j)
end
end


-- { "a", "b", "c" } -> { a = 1, b = 2, c = 3 }
--[==[
function export.invert(array)
Invert a list. For example, {invert({ "a", "b", "c" })} -> { { a = 1, b = 2, c = 3 }}]==]
checkType("invert", 1, array, "table")
function export.invert(list)
local map, i = {}, 0
local map = {}
while true do
for i, v in ipairs(array) do
i = i + 1
local v = list[i]
if v == nil then
return map
end
map[v] = i
map[v] = i
end
end
invert = export.invert
do
local function flatten(t, list, seen, n)
seen[t] = true
local i = 0
while true do
i = i + 1
local v = t[i]
if v == nil then
return n
elseif type(v) == "table" then
if seen[v] then
error("loop in input list")
end
n = flatten(v, list, seen, n)
else
n = n + 1
list[n] = v
end
end
end
end
return map
--[==[
Given a list, which may contain sublists, flatten it into a single list. For example, {flatten({ "a", { "b", "c" }, "d" })} ->
{ { "a", "b", "c", "d" }}]==]
function export.flatten(t)
local list = {}
flatten(t, list, {}, 0)
return list
end
end
end


--[[
--[==[
{ "a", "b", "c" } -> { ["a"] = true, ["b"] = true, ["c"] = true }
Convert `list` (a table with a list of values) into a set (a table where those values are keys instead). This is a useful
--]]
way to create a fast lookup table, since looking up a table key is much, much faster than iterating over the whole list
function export.listToSet(t)
to see if it contains a given value.
checkType("listToSet", 1, t, "table")
 
By default, each item is given the value true. If the optional parameter `value` is a function or functor, then it is called
local set = {}
as an iterator, with the list index as the first argument, the item as the second (which will be used as the key), plus any
for _, item in ipairs(t) do
additional arguments passed to {listToSet}; the returned value is used as the value for that list item. If `value` is anything
set[item] = true
else, then it is used as the fixed value for every item.]==]
function export.listToSet(list, value, ...)
local set, i, callable = {}, 0
if value == nil then
value = true
else
callable = is_callable(value)
end
while true do
i = i + 1
local item = list[i]
if item == nil then
return set
end
if callable then
set[item] = value(i, item, ...)
else
set[item] = value
end
end
end
return set
end
end
list_to_set = export.listToSet


--[[
--[==[
Returns true if all keys in the table are consecutive integers starting at 1.
Returns true if all keys in the table are consecutive integers starting at 1.]==]
--]]
function export.isArray(t)
function export.isArray(t)
checkType("isArray", 1, t, "table")
local i = 0
local i = 0
for _ in pairs(t) do
for _ in pairs(t) do
Line 674: Line 1,184:
end
end
return true
return true
end
--[==[
Returns true if the first list, taken as a set, is a subset of the second list, taken as  set.]==]
function export.isSubsetList(t1, t2)
t2 = list_to_set(t2)
local i = 0
while true do
i = i + 1
local v = t1[i]
if v == nil then
return true
elseif t2[v] == nil then
return false
end
end
end
--[==[
Returns true if the first map, taken as a set, is a subset of the second map, taken as  set.]==]
function export.isSubsetMap(t1, t2)
for k in pairs(t1) do
if t2[k] == nil then
return false
end
end
return true
end
--[==[
Add a list of aliases for a given key to a table. The aliases must be given as a table.]==]
function export.alias(t, k, aliases)
for _, alias in pairs(aliases) do
t[alias] = t[k]
end
end
end


return export
return export

Latest revision as of 19:50, 8 January 2025



--[[
------------------------------------------------------------------------------------
--                      table (formerly TableTools)                               --
--                                                                                --
-- This module includes a number of functions for dealing with Lua tables.        --
-- It is a meta-module, meant to be called from other Lua modules, and should     --
-- not be called directly from #invoke.                                           --
------------------------------------------------------------------------------------
--]]

local export = {}

local collation_module = "Module:collation"
local debug_track_module = "Module:debug/track"
local function_module = "Module:fun"
local math_module = "Module:math"

local table = table

local concat = table.concat
local contains -- defined as export.contains
local deep_copy -- defined as export.deepCopy
local deep_equals -- defined as export.deepEquals
local format = string.format
local getmetatable = getmetatable
local insert = table.insert
local insert_if_not -- defined as export.insertIfNot
local invert -- defined as export.invert
local ipairs = ipairs
local ipairs_default_iter = ipairs{export}
local keys_to_list -- defined as export.keysToList
local list_to_set -- defined as export.listToSet
local next = next
local num_keys -- defined as export.numKeys
local pairs = pairs
local pcall = pcall
local raw_pairs -- defined as export.rawPairs
local rawequal = rawequal
local rawget = rawget
local require = require
local select = select
local setmetatable = setmetatable
local signed_index -- defined as export.signedIndex
local sort = table.sort
local sparse_ipairs -- defined as export.sparseIpairs
local table_len -- defined as export.length
local table_reverse -- defined as export.reverse
local type = type

--[==[
Loaders for functions in other modules, which overwrite themselves with the target function when called. This ensures modules are only loaded when needed, retains the speed/convenience of locally-declared pre-loaded functions, and has no overhead after the first call, since the target functions are called directly in any subsequent calls.]==]
	local function debug_track(...)
		debug_track = require(debug_track_module)
		return debug_track(...)
	end
	
	local function is_callable(...)
		is_callable = require(function_module).is_callable
		return is_callable(...)
	end
	
	local function is_integer(...)
		is_integer = require(math_module).is_integer
		return is_integer(...)
	end
	
	local function is_positive_integer(...)
		is_positive_integer = require(math_module).is_positive_integer
		return is_positive_integer(...)
	end
	
	local function string_sort(...)
		string_sort = require(collation_module).string_sort
		return string_sort(...)
	end

--[==[
Returns a clone of an object. If the object is a table, the value returned is a new table, but all subtables and functions are shared. Metamethods are respected unless the `raw` flag is set, but the returned table will have no metatable of its own.]==]
function export.shallowCopy(orig, raw)
	if type(orig) ~= "table" then
		return orig
	end
	local copy, iter, state, init = {}
	if raw then
		iter, state = next, orig
	else
		iter, state, init = pairs(orig)
		-- Track instances of data loaded via `mw.loadData` being copied, which is very inefficient and usually unnecessary.
		-- `mw.loadData` sets the key "mw_loadData" to true in the metatable.
		local mt = getmetatable(orig)
		if mt and type(mt) == "table" and rawget(mt, "mw_loadData") == true then
			debug_track("table/shallowCopy/loaded data")
		end
	end
	for k, v in iter, state, init do
		copy[k] = v
	end
	return copy
end

function export.shallowcopy(orig, raw)
	return export.shallowCopy(orig, raw)
end

do
	local function make_copy(orig, seen, mt_flag, keep_loaded_data, tracked)
		if type(orig) ~= "table" then
			return orig
		end
		local memoized = seen[orig]
		if memoized ~= nil then
			return memoized
		end
		local mt, iter, state, init = getmetatable(orig)
		-- `mt` could be a non-table if `__metatable` has been used, but discard it in such cases.
		if not (mt and type(mt) == "table") then
			mt, iter, state, init = nil, next, orig, nil
		-- Data loaded via `mw.loadData`, which sets the key "mw_loadData" to true in the metatable.
		elseif rawget(mt, "mw_loadData") == true then
			if keep_loaded_data then
				seen[orig] = orig
				return orig
			-- Track instances of such data being copied, which is very inefficient and usually unnecessary.
			elseif not tracked then
				debug_track("table/deepCopy/loaded data")
				tracked = true
			end
			-- Discard the metatable, and use the `__pairs` metamethod.
			mt, iter, state, init = nil, pairs(orig)
		-- Otherwise, keep `mt`.
		else
			-- Track copied metatables to find any instances where it's really necessary, as it would be preferable for the default to be `pairs` instead of `next` (i.e. using __pairs if present, returning a table with no metatable).
			if not tracked then
				debug_track("table/deepCopy/copied metatable")
				tracked = true
			end
			iter, state, init = next, orig, nil
		end
		local copy = {}
		seen[orig] = copy
		for k, v in iter, state, init do
			copy[make_copy(k, seen, mt_flag, keep_loaded_data, tracked)] = make_copy(v, seen, mt_flag, keep_loaded_data, tracked)
		end
		if mt == nil or mt_flag == "none" then
			return copy
		elseif mt_flag ~= "keep" then
			mt = make_copy(mt, seen, mt_flag, keep_loaded_data, tracked)
		end
		return setmetatable(copy, mt)
	end

	--[==[
	Recursive deep copy function. Preserves copied identities of subtables.
	A more powerful version of {mw.clone}, with customizable options.
	* By default, metatables are copied, except for data loaded via {mw.loadData} (see below). If `metatableFlag` is set to "none", the copy will not have any metatables at all. Conversely, if `metatableFlag` is set to "keep", then the cloned table (and all its members) will have the exact same metatable as their original version.
	* If `keepLoadedData` is true, then any data loaded via {mw.loadData} will not be copied, and the original will be used instead. This is useful in iterative contexts where it is necessary to copy data being destructively modified, because objects loaded via mw.loadData are immutable.
	* Notes:
	*# Protected metatables will not be copied (i.e. those hidden behind a __metatable metamethod), as they are not
	   accessible by Lua's design. Instead, the output of the __metatable method will be used instead.
	*# When iterating over the table, the __pairs metamethod is ignored, since this can prevent the table from being properly cloned.
	*# Data loaded via mw.loadData is a special case in two ways: the metatable is stripped, because otherwise the cloned table throws errors when accessed; in addition, the __pairs metamethod is used, since otherwise the cloned table would be empty.]==]
	function export.deepCopy(orig, metatableFlag, keepLoadedData)
		return make_copy(orig, {}, metatableFlag, keepLoadedData)
	end
	deep_copy = export.deepCopy
end

--[==[
Given an array and a signed index, returns the true table index. If the signed index is negative, the array will be counted from the end, where {-1} is the highest index in the array; otherwise, the returned index will be the same. To aid optimization, the first argument may be a number representing the array length instead of the array itself; this is useful when the array length is already known, as it avoids recalculating it each time this function is called.]==]
function export.signedIndex(t, k)
	if not is_integer(k) then
		error("index must be an integer")
	end
	return k < 0 and (type(t) == "table" and table_len(t) or t) + k + 1 or k
end
signed_index = export.signedIndex

--[==[
Returns the highest positive integer index of a table or array that possibly has holes in it, or otherwise 0 if no positive integer keys are found. Note that this differs from `table.maxn`, which returns the highest positive numerical index, even if it is not an integer.]==]
function export.maxIndex(t)
	local max = 0
	for k in pairs(t) do
		if is_positive_integer(k) and k > max then
			max = k
		end
	end
	return max
end

--[==[
Append any number of lists together and returns the result. Compare the Lisp expression {(append list1 list2 ...)}.]==]
function export.append(...)
	local args, list, n = {...}, {}, 0
	for i = 1, select("#", ...) do
		local t, j = args[i], 0
		while true do
			j = j + 1
			local v = t[j]
			if v == nil then
				break
			end
			n = n + 1
			list[n] = v
		end
	end
	return list
end

--[==[
Extend an existing list by a new list, modifying the existing list in-place. Compare the Python expression
{list.extend(new_items)}.

`options` is an optional table of additional options to control the behavior of the operation. The following options are
recognized:
* `insertIfNot`: Use {export.insertIfNot()} instead of {table.insert()}, which ensures that duplicate items do not get
  inserted (at the cost of an O((M+N)*N) operation, where M = #list and N = #new_items).
* `key`: As in {insertIfNot()}. Ignored otherwise.
* `pos`: As in {insertIfNot()}. Ignored otherwise.]==]
function export.extend(t, new_items, options)
	local i, insert_if_not_option = 0, options and options.insertIfNot
	while true do
		i = i + 1
		local item = new_items[i]
		if item == nil then
			return
		elseif insert_if_not_option then
			insert_if_not(t, item, options)
		else
			insert(t, item)
		end
	end
end
export.extendList = export.extend

--[==[
Given a list, returns a new list consisting of the items between the start index `i` and end index `j` (inclusive). `i` defaults to `1`, and `j` defaults to the length of the input list.]==]
function export.slice(t, i, j)
	local t_len = table_len(t)
	i = i and signed_index(t_len, i) or 1
	local list, offset = {}, i - 1
	for key = i, j and signed_index(t_len, j) or t_len do
		list[key - offset] = t[key]
	end
	return list
end

do
	local pos_nan, neg_nan
	--[==[
	Remove any duplicate values from a list, ignoring non-positive-integer keys. The earliest value is kept, and all subsequent duplicate values are removed, but otherwise the list order is unchanged.]==]
	function export.removeDuplicates(t)
		local list, seen, i, n = {}, {}, 0, 0
		while true do
			i = i + 1
			local v = t[i]
			if v == nil then
				return list
			end
			local memo_key
			if v == v then
				memo_key = v
			-- NaN
			elseif format("%f", v) == "nan" then
				if not pos_nan then
					pos_nan = {}
				end
				memo_key = pos_nan
			-- -NaN
			else
				if not neg_nan then
					neg_nan = {}
				end
				memo_key = neg_nan
			end
			if not seen[memo_key] then
				n = n + 1
				list[n], seen[memo_key] = v, true
			end
		end
	end
end

--[==[
Given a table, return an array containing all positive integer keys, sorted in numerical order.]==]
function export.numKeys(t)
	local nums, i = {}, 0
	for k in pairs(t) do
		if is_positive_integer(k) then
			i = i + 1
			nums[i] = k
		end
	end
	sort(nums)
	return nums
end
num_keys = export.numKeys

--[==[
This takes a list with one or more nil values, and removes the nil values while preserving the order, so that the list can be safely traversed with ipairs.]==]
function export.compressSparseArray(t)
	local list, keys, i = {}, num_keys(t), 0
	while true do
		i = i + 1
		local k = keys[i]
		if k == nil then
			return list
		end
		list[i] = t[k]
	end
end

--[==[
An iterator which works like `pairs`, but ignores any `__pairs` metamethod.]==]
function export.rawPairs(t)
	return next, t, nil
end
raw_pairs = export.rawPairs

--[==[
An iterator which works like `ipairs`, but ignores any `__ipairs` metamethod.]==]
function export.rawIpairs(t)
	return ipairs_default_iter, t, 0
end

do
	local current
	--[==[
	An iterator which works like `pairs`, except that it also respects the `__index` metamethod. This works by iterating over the input table with `pairs`, followed by the table at its `__index` metamethod (if any). This is then repeated for that table's `__index` table and so on, with any repeated keys being skipped over, until there are no more tables, or a table repeats (so as to prevent an infinite loop). If `__index` is a function, however, then it is ignored, since there is no way to iterate over its return values.
	
	A `__pairs` metamethod will be respected for any given table instead of iterating over it directly, but these will be ignored if the `raw` flag is set.

	Note: this function can be used as a `__pairs` metamethod. In such cases, it does not call itself, since this would cause an infinite loop, so it treats the relevant table as having no `__pairs` metamethod. Other `__pairs` metamethods on subsequent tables will still be respected.]==]
	function export.indexPairs(t, raw)
		-- If there's no metatable, result is identical to `pairs`.
		-- To prevent infinite loops, act like `pairs` if `current` is set with `t`, which means this function is being used as a __pairs metamethod.
		if current and current[t] or getmetatable(t) == nil then
			return next, t, nil
		end
		
		-- `seen_k` memoizes keys, as they should never repeat; `seen_t` memoizes tables iterated over.
		local seen_k, seen_t, iter, state, k, v, success = {}, {[t] = true}
		
		return function()
			while true do
				if iter == nil then
					-- If `raw` is set, use `next`.
					if raw then
						iter, state, k = next, t, nil
					-- Otherwise, call `pairs`, setting `current` with `t` so that export.indexPairs knows to return `next` if it's being used as a metamethod, as this prevents infinite loops. `t` is then unset, so that `current` doesn't get polluted if the loop breaks early.
					else
						if not current then
							current = {}
						end
						current[t] = true
						-- Use `pcall`, so that `t` can always be unset from `current`.
						success, iter, state, k = pcall(pairs, t)
						current[t] = nil
						-- If there was an error, raise it.
						if not success then
							error(iter)
						end
					end
				end
				while true do
					-- It's possible for a `__pairs` metamethod to return additional values, but assume there aren't any, since this iterator specifically relates to table indexes.
					k, v = iter(state, k)
					if k == nil then
						break
					-- If a repeated key is found, skip and iterate again.
					elseif not seen_k[k] then
						seen_k[k] = true
						return k, v
					end
				end
				-- If there's an __index metamethod, iterate over it iff it's a table not already seen before.
				local mt = getmetatable(t)
				-- `mt` might not be a table if __metatable is used.
				if not mt or type(mt) ~= "table" then
					return nil
				end
				seen_t[t] = true
				t = rawget(mt, "__index")
				if not t or type(t) ~= "table" then
					return nil
				-- Throw error if it's been seen before.
				elseif seen_t[t] then
					error("loop in gettable")
				end
				iter = nil -- New `iter` will be generated on the next iteration of the while loop.
			end
		end
	end
end

do
	local function ipairs_func(t, i)
		i = i + 1
		local v = t[i]
		if v ~= nil then
			return i, v
		end
	end
	
	--[==[
	An iterator which works like `ipairs`, except that it also respects the `__index` metamethod. This works by looking up values in the table, iterating integers from key `1` until no value is found.]==]
	function export.indexIpairs(t)
		-- If there's no metatable, just use the default ipairs iterator.
		return getmetatable(t) == nil and ipairs_default_iter or ipairs_func, t, 0
	end
end

--[==[
An iterator which works like `indexIpairs`, but which only returns the value.]==]
function export.iterateList(t)
	local i = 0
	return function(t)
		i = i + 1
		return t[i]
	end, t
end

--[==[
This is an iterator for sparse arrays. It can be used like ipairs, but can handle nil values.]==]
function export.sparseIpairs(t)
	local keys, i = num_keys(t), 0
	return function(t)
		i = i + 1
		local k = keys[i]
		if k then
			return k, t[k]
		end
	end, t
end
sparse_ipairs = export.sparseIpairs

--[==[
This returns the size of a key/value pair table. If `raw` is set, then metamethods will be ignored, giving the true table size.

For arrays, it is faster to use `export.length`.]==]
function export.size(t, raw)
	local i, iter, state, init = 0
	if raw then
		iter, state, init = next, t, nil
	else
		iter, state, init = pairs(t)
	end
	for _ in iter, state, init do
		i = i + 1
	end
	return i
end

--[==[
This returns the length of a table, or the first integer key n counting from 1 such that t[n + 1] is nil. It is a more reliable form of the operator `#`, which can become unpredictable under certain circumstances due to the implementation of tables under the hood in Lua, and therefore should not be used when dealing with arbitrary tables. `#` also does not use metamethods, so will return the wrong value in cases where it is desirable to take these into account (e.g. data loaded via `mw.loadData`). If `raw` is set, then metamethods will be ignored, giving the true table length.

For arrays, this function is faster than `export.size`.]==]
function export.length(t, raw)
	local n = 0
	if raw then
		for i in ipairs_default_iter, t, 0 do
			n = i
		end
		return n
	end
	repeat
		n = n + 1
	until t[n] == nil
	return n - 1
end
table_len = export.length

do
	local function is_equivalent(a, b, seen, include_mt, pairs_func)
		-- Raw equality check.
		if rawequal(a, b) then
			return true
		-- If not equal, a and b can only be equivalent if they're both tables.
		elseif not (type(a) == "table" and type(b) == "table") then
			return false
		end
		-- If `a` and `b` have been compared before, return the memoized result. This will usually be true, since failures normally fail the whole check outright, but match failures can occur during the laborious check without this happening, so it could be false.
		local memo_a = seen[a]
		if memo_a then
			local result = memo_a[b]
			if result ~= nil then
				return result
			end
			-- To avoid recursive references causing infinite loops, assume the tables currently being compared are equivalent by memoizing them as true; this will be corrected to false if there's a match failure.
			memo_a[b] = true
		else
			memo_a = {[b] = true}
			seen[a] = memo_a
		end
		-- Don't bother checking `memo_b` for `a`, since if `a` and `b` had been compared before, then `b` would be in `memo_a`.
		local memo_b = seen[b]
		if memo_b then
			memo_b[a] = true
		else
			memo_b = {[a] = true}
			seen[b] = memo_b
		end
		-- If `include_mt` is set, check the metatables are equivalent.
		if include_mt and not is_equivalent(getmetatable(a), getmetatable(b), seen, true, pairs_func) then
			memo_a[b], memo_b[a] = false, false
			return false
		end
		-- Copy all key/values pairs in `b` to `remaining_b`, and count the size: this uses `pairs_func`, which will also be used to iterate over `a`, ensuring that `a` and `b` are iterated over in the same way. This is necessary to ensure that `export.deepEquals(a, b)` and `export.deepEquals(b, a)` always give the same result. Simply iterating over `a` while accessing keys in `b` for comparison would ignore any `__pairs` metamethod that `b` has, which could cause non-symmetrical outputs if `__pairs` returns more or less than the complete set of key/value pairs accessible via `__index`, so using `pairs_func` for both `a` and `b` prevents this.
		-- TODO: handle exotic `__pairs` methods which return the same key multiple times with different values.
		local remaining_b, size_b = {}, 0
		for k_b, v_b in pairs_func(b) do
			remaining_b[k_b], size_b = v_b, size_b + 1
		end
		-- Fast check: iterate over the keys in `a`, checking if an equivalent value exists at the same key in `remaining_b`. As matches are found, key/value pairs are removed from `remaining_b`. If any keys in `a` or `remaining_b` are tables, the fast check will only work if the exact same object exists as a key in the other table. Any others from `a` that don't match anything in `remaining_b` are added to `remaining_a`, while those in `remaining_b` that weren't found will still remain once the loop ends. `remaining_a` and `remaining_b` are then compared at the end with the laborious check.
		local size_a, remaining_a = 0
		for k, v_a in pairs_func(a) do
			local v_b = remaining_b[k]
			-- If `k` isn't in `remaining_b`, `a` and `b` can't be equivalent unless it's a table.
			if v_b == nil then
				if type(k) ~= "table" then
					memo_a[b], memo_b[a] = false, false
					return false
				-- Otherwise, add the `k`/`v_a` pair to `remaining_a` for the laborious check.
				elseif not remaining_a then
					remaining_a = {}
				end
				remaining_a[k], size_a = v_a, size_a + 1
			-- Otherwise, if `k` exists in `a` and `remaining_b`, `v_a` and `v_b` must be equivalent for there to be a match.
			elseif is_equivalent(v_a, v_b, seen, include_mt, pairs_func) then
				remaining_b[k], size_b = nil, size_b - 1
			else
				memo_a[b], memo_b[a] = false, false
				return false
			end
		end
		-- Must be the same number of remaining keys in each table.
		if size_a ~= size_b then
			memo_a[b], memo_b[a] = false, false
			return false
		-- If the size is 0, there's nothing left to check.
		elseif size_a == 0 then
			return true
		end
		-- Laborious check: since it's not possible to use table lookups, check each key/value pair in `remaining_a` against every key/value pair in `remaining_b` until a match is found, removing the matching key/value pair from `remaining_b` each time, to ensure one-to-one correspondence.
		for k_a, v_a in next, remaining_a do
			local success
			for k_b, v_b in next, remaining_b do
				-- Keys/value pairs must be equivalent in order to match.
				if (
					-- Check values first for speed, since they might not be tables.
					is_equivalent(v_a, v_b, seen, include_mt, pairs_func) and
					is_equivalent(k_a, k_b, seen, include_mt, pairs_func)
				) then
					-- Remove matched key from `remaining_b`, and break the inner loop.
					success, remaining_b[k_b] = true, nil
					break
				end
			end
			-- Fail if `remaining_b` runs out of keys, as the `k_a`/`v_a` pair still hasn't matched.
			if not success then
				memo_a[b], memo_b[a] = false, false
				return false
			end
		end
		-- If every key/value pair in `remaining_a` matched with one in `remaining_b`, `a` and `b` must be equivalent. Note that `remaining_b` will now be empty, since the laborious check only starts if `remaining_a` and `remaining_b` are the same size.
		return true
	end

	--[==[
	Recursively compare two values that may be tables, and returns true if all key-value pairs are structurally equivalent. Note that this handles arbitrary nesting of subtables (including recursive nesting) to any depth, for keys as well as values.

	If `include_mt` is true, then metatables are also compared. If `raw` is true, then metamethods are not used during the comparison.]==]
	function export.deepEquals(a, b, include_mt, raw)
		return is_equivalent(a, b, {}, include_mt, raw and raw_pairs or pairs)
	end
	deep_equals = export.deepEquals
end

do
	local function get_nested(t, k, ...)
		if t == nil then
			return nil
		elseif select("#", ...) ~= 0 then
			return get_nested(t[k], ...)
		end
		return t[k]
	end

	--[==[
	Given a table and an arbitrary number of keys, will successively access subtables using each key in turn, returning the value at the final key. For example, if {t} is { {[1] = {[2] = {[3] = "foo"}}}}, {export.getNested(t, 1, 2, 3)} will return {"foo"}.

	If no subtable exists for a given key value, returns nil, but will throw an error if a non-table is found at an intermediary key.]==]
	function export.getNested(t, ...)
		if t == nil or select("#", ...) == 0 then
			error("Must provide a table and at least one key.")
		end
		return get_nested(t, ...)
	end
end

do
	local function set_nested(t, v, k, ...)
		if select("#", ...) == 0 then
			t[k] = v
			return
		end
		local next_t = t[k]
		if next_t == nil then
			-- If there's no next table while setting nil, there's nothing more to do.
			if v == nil then
				return
			end
			next_t = {}
			t[k] = next_t
		end
		return set_nested(next_t, v, ...)
	end

	--[==[
	Given a table, value and an arbitrary number of keys, will successively access subtables using each key in turn, and sets the value at the final key. For example, if {t} is { {} }, {export.setNested(t, "foo", 1, 2, 3)} will modify {t} to { {[1] = {[2] = {[3] = "foo"} } } }.

	If no subtable exists for a given key value, one will be created, but the function will throw an error if a non-table value is found at an intermediary key.

	Note: the parameter order (table, value, keys) differs from functions like rawset, because the number of keys can be arbitrary. This is to avoid situations where an additional argument must be appended to arbitrary lists of variables, which can be awkward and error-prone: for example, when handling variable arguments ({{lua|...}}) or function return values.]==]
	function export.setNested(t, ...)
		if t == nil or select("#", ...) < 2 then
			error("Must provide a table and at least one key.")
		end
		return set_nested(t, ...)
	end
end

--[==[
Given a list and a value to be found, return true if the value is in the array portion of the list. Comparison is by value, using `deepEquals`.]==]
function export.contains(list, x, options)
	if options and options.key then
		x = options.key(x)
	end
	local i = 0
	while true do
		i = i + 1
		local v = list[i]
		if v == nil then
			return false
		elseif options and options.key then
			v = options.key(v)
		end
		if deep_equals(v, x) then
			return true
		end
	end
end
contains = export.contains

--[==[
Given a general table and a value to be found, return true if the value is in
either the array or hashmap portion of the table. Comparison is by value, using
`deepEquals`.]==]
function export.tableContains(t, x)
	for _, v in pairs(t) do
		if deep_equals(v, x) then
			return true
		end
	end
	return false
end

--[==[
Given a `list` and a `new_item` to be inserted, append the value to the end of the list if not already present
(or insert at an arbitrary position, if `options.pos` is given; see below). Comparison is by value, using {deepEquals}.

`options` is an optional table of additional options to control the behavior of the operation. The following options are
recognized:
* `pos`: Position at which insertion happens (i.e. before the existing item at position `pos`).
* `key`: Function of one argument to return a comparison key, as with {deepEquals}. The key function is applied to both
		 `item` and the existing item in `list` to compare against, and the comparison is done against the results.
		 This is useful when inserting a complex structure into an existing list while avoiding duplicates.
* `combine`: Function of three arguments (the existing item, the new item and the position, respectively) to combine an
			 existing item with `new_item`, when `new_item` is found in `list`. If unspecified, the existing item is
			 left alone.

Returns {false} if an entry is already found, or {true} if inserted.

For compatibility, `pos` can be specified directly as the third argument in place of `options`, but this is not
recommended for new code.

NOTE: This function is O(N) in the size of the existing list. If you use this function in a loop to insert several
items, you will get O(M*(M+N)) behavior, effectively O((M+N)^2). Thus it is not recommended to use this unless you are
sure the total number of items will be small. (An alternative for large lists is to insert all the items without
checking for duplicates, and use {removeDuplicates()} at the end.)]==]
function export.insertIfNot(list, new_item, options)
	if type(options) == "number" then
		options = {pos = options}
	end
	if options and options.combine then
		local new_key
		-- Don't use options.key and options.key(new_item) or new_item in case the key is legitimately false or nil.
		if options.key then
			new_key = options.key(new_item)
		else
			new_key = new_item
		end
		local i = 0
		while true do
			i = i + 1
			local item, key = list[i]
			if item == nil then
				break
			elseif options.key then
				key = options.key(item)
			else
				key = item
			end
			if deep_equals(key, new_key) then
				local retval = options.combine(item, new_item, i)
				if retval ~= nil then
					list[i] = retval
				end
				return false
			end
		end
	elseif contains(list, new_item, options) then
		return false
	end
	local pos = options and options.pos
	if pos then
		insert(list, pos, new_item)
	else
		insert(list, new_item)
	end
end
insert_if_not = export.insertIfNot

--[==[
Finds key for specified value in a given table. Roughly equivalent to reversing the key-value pairs in the table:
* {reversed_table = { [value1] = key1, [value2] = key2, ... }}
and then returning {reversed_table[value]}. Comparison is by value, using `deepEquals`.

Only reliable if there is just one key with the specified value. Otherwise, the function returns the first key found,
and the output is unpredictable.]==]
function export.keyFor(t, x)
	for k, v in pairs(t) do
		if deep_equals(v, x) then
			return k
		end
	end
	return nil
end

do
	local types
	local function get_types()
		types, get_types = invert{
			"number",
			"boolean",
			"string",
			"table",
			"function",
			"thread",
			"userdata"
		}, nil
		return types
	end
	
	local function less_than(key1, key2)
		return key1 < key2
	end
	
	-- The default sorting function used in export.keysToList if `keySort` is not given.
	local function default_compare(key1, key2)
		local type1, type2 = type(key1), type(key2)
		if type1 ~= type2 then
			-- If the types are different, sort numbers first, functions last, and all other types alphabetically.
			return (types or get_types())[type1] < types[type2]
		-- `string_sort` fixes a bug in < which causes all codepoints above U+FFFF to be treated as equal.
		elseif type1 == "string" then
			return string_sort(key1, key2)
		elseif type1 == "number" then
			return key1 < key2
		-- Attempt to compare tables, in case there's a metamethod.
		elseif type1 == "table" then
			local success, result = pcall(less_than, key1, key2)
			if success then
				return result
			end
		-- Sort true before false.
		elseif type1 == "boolean" then
			return key1
		end
		return false
	end

	--[==[
	Returns a list of the keys in a table, sorted using either the default `table.sort` function or a custom `keySort` function.

	If there are only numerical keys, `export.numKeys` is probably faster.]==]
	function export.keysToList(t, keySort)
		local list, i = {}, 0
		for key in pairs(t) do
			i = i + 1
			list[i] = key
		end
		-- Use specified sort function, or otherwise `default_compare`.
		sort(list, keySort or default_compare)
		return list
	end
	keys_to_list = export.keysToList
end

--[==[
Iterates through a table, with the keys sorted using the keysToList function.

If there are only numerical keys, `export.sparseIpairs` is probably faster.]==]
function export.sortedPairs(t, keySort)
	local list, i = keys_to_list(t, keySort), 0
	return function(t)
		i = i + 1
		local key = list[i]
		if key ~= nil then
			return key, t[key]
		end
	end, t
end

--[==[
Iterates through a table using `ipairs` in reverse.

`__ipairs` metamethods will be used, including those which return arbitrary (i.e. non-array) keys, but note that this function assumes that the first return value is a key which can be used to retrieve a value from the input table via a table lookup. As such, `__ipairs` metamethods for which this assumption is not true will not work correctly.

If the value `nil` is encountered early (e.g. because the table has been modified), the loop will terminate early.]==]
function export.reverseIpairs(t)
	-- `__ipairs` metamethods can return arbitrary keys, so compile a list.
	local keys, i = {}, 0
	for k in ipairs(t) do
		i = i + 1
		keys[i] = k
	end
	return function(t)
		if i == 0 then
			return nil
		end
		local k = keys[i]
		-- Retrieve `v` from the table. These aren't stored during the initial ipairs loop, so that they can be modified during the loop.
		local v = t[k]
		-- Return if not an early nil.
		if v ~= nil then
			i = i - 1
			return k, v
		end
	end, t
end

local function getIteratorValues(i, j , step, t_len)
	i, j = i and signed_index(t_len, i), j and signed_index(t_len, j)
	if step == nil then
		i, j = i or 1, j or t_len
		return i, j, j < i and -1 or 1
	elseif step == 0 or not is_integer(step) then
		error("step must be a non-zero integer")
	elseif step < 0 then
		return i or t_len, j or 1, step
	end
	return i or 1, j or t_len, step
end

--[==[
Given an array `list` and function `func`, iterate through the array applying {func(r, k, v)}, and returning the result,
where `r` is the value calculated so far, `k` is an index, and `v` is the value at index `k`. For example,
{reduce(array, function(a, _, v) return a + v end)} will return the sum of `array`.

Optional arguments:
* `i`: start index; negative values count from the end of the array
* `j`: end index; negative values count from the end of the array
* `step`: step increment
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
backwards and by how much, based on these inputs (see examples below for default behaviours).

Examples:
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
# step=-1 results in backward iteration from the end to the start in steps of 1.
# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
# j=-3 results in forward iteration from the start to the 3rd last index.
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
function export.reduce(t, func, i, j, step)
	i, j, step = getIteratorValues(i, j, step, table_len(t))
	local ret = t[i]
	for k = i + step, j, step do
		ret = func(ret, k, t[k])
	end
	return ret
end

do
	local function replace(t, func, i, j, step, generate)
		local t_len = table_len(t)
		-- Normalized i, j and step, based on the inputs.
		local norm_i, norm_j, norm_step = getIteratorValues(i, j, step, t_len)
		if norm_step > 0 then
			i, j, step = 1, t_len, 1
		else
			i, j, step = t_len, 1, -1
		end
		-- "Signed" variables are multiplied by -1 if `step` is negative.
		local t_new, signed_i, signed_j = generate and {} or t, norm_i * step, norm_j * step
		for k = i, j, step do
			-- Replace the values iff they're within the i to j range and `step` wouldn't skip the key.
			-- Note: i > j if `step` is positive; i < j if `step` is negative. Otherwise, the range is empty.
			local signed_k = k * step
			if signed_k >= signed_i and signed_k <= signed_j and (k - norm_i) % norm_step == 0 then
				t_new[k] = func(k, t[k])
			-- Otherwise, add the existing value if `generate` is set.
			elseif generate then
				t_new[k] = t[k]
			end
		end
		return t_new
	end
	
	--[==[
	Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
	`v` is the value at index `k`), replacing the relevant values with the result. For example,
	{apply(array, function(_, v) return 2 * v end)} will double each member of the array.

	Optional arguments:
	* `i`: start index; negative values count from the end of the array
	* `j`: end index; negative values count from the end of the array
	* `step`: step increment
	These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
	backwards and by how much, based on these inputs (see examples below for default behaviours).

	Examples:
	# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
	# step=-1 results in backward iteration from the end to the start in steps of 1.
	# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
	# j=-3 results in forward iteration from the start to the 3rd last index.
	# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
	function export.apply(t, func, i, j, step)
		return replace(t, func, i, j, step)
	end

	--[==[
	Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
	`v` is the value at index `k`), and return a shallow copy of the original array with the relevant values replaced. For example,
	{generate(array, function(_, v) return 2 * v end)} will return a new array in which each value has been doubled.

	Optional arguments:
	* `i`: start index; negative values count from the end of the array
	* `j`: end index; negative values count from the end of the array
	* `step`: step increment
	These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
	backwards and by how much, based on these inputs (see examples below for default behaviours).

	Examples:
	# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
	# step=-1 results in backward iteration from the end to the start in steps of 1.
	# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
	# j=-3 results in forward iteration from the start to the 3rd last index.
	# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
	function export.generate(t, func, i, j, step)
		return replace(t, func, i, j, step, true)
	end
end

--[==[
Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
`v` is the value at index `k`), and returning whether the function is true for all iterations.

Optional arguments:
* `i`: start index; negative values count from the end of the array
* `j`: end index; negative values count from the end of the array
* `step`: step increment
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
backwards and by how much, based on these inputs (see examples below for default behaviours).

Examples:
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
# step=-1 results in backward iteration from the end to the start in steps of 1.
# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
# j=-3 results in forward iteration from the start to the 3rd last index.
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
function export.all(t, func, i, j, step)
	i, j, step = getIteratorValues(i, j, step, table_len(t))
	for k = i, j, step do
		if not func(k, t[k]) then
			return false
		end
	end
	return true
end

--[==[
Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
`v` is the value at index `k`), and returning whether the function is true for at least one iteration.

Optional arguments:
* `i`: start index; negative values count from the end of the array
* `j`: end index; negative values count from the end of the array
* `step`: step increment
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
backwards and by how much, based on these inputs (see examples below for default behaviours).

Examples:
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
# step=-1 results in backward iteration from the end to the start in steps of 1.
# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
# j=-3 results in forward iteration from the start to the 3rd last index.
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
function export.any(t, func, i, j, step)
	i, j, step = getIteratorValues(i, j, step, table_len(t))
	for k = i, j, step do
		if not not (func(k, t[k])) then
			return true
		end
	end
	return false
end

--[==[
Joins an array with serial comma and serial conjunction, normally {"and"}. An improvement on {mw.text.listToText},
which doesn't properly handle serial commas.

Options:
* `conj`: Conjunction to use; defaults to {"and"}.
* `punc`: Punctuation to use; default to {","}.
* `dontTag`: Don't tag the serial comma and serial {"and"}. For error messages, in which HTML cannot be used.]==]
function export.serialCommaJoin(seq, options)
	local length = table_len(seq)
	if length == 0 then
		return ""
	elseif length == 1 then
		return seq[1]
	elseif length == 2 then
		return seq[1] .. " " .. (options and options.conj or "and") .. " " .. seq[2]
	end

	local conj, punc, dont_tag
	if options then
		conj = options.conj or "and"
		punc = options.punc or ","
		dont_tag = options.dontTag
	else
		conj, punc = "and", ","
	end
	
	local comma
	if dont_tag then
		comma = punc
		conj = " " .. conj .. " "
	else
		comma = "<span class=\"serial-comma\">" .. punc .. "</span>"
		conj = "<span class=\"serial-and\"> " .. conj .. "</span> "
	end
	
	return concat(seq, punc .. " ", 1, length - 1) .. comma .. conj .. seq[length]
end

--[==[
A function which works like `table.concat`, but respects any `__index` metamethod. This is useful for data loaded via `mw.loadData`.]==]
function export.concat(t, sep, i, j)
	local list, k = {}, 0
	while true do
		k = k + 1
		local v = t[k]
		if v == nil then
			return concat(list, sep, i, j)
		end
		list[k] = v
	end
end

--[==[
Concatenate all values in the table that are indexed by a number, in order.
* {sparseConcat{ a, nil, c, d }}  =>  {"acd"}
* {sparseConcat{ nil, b, c, d }}  =>  {"bcd"}]==]
function export.sparseConcat(t, sep, i, j)
	local list, k = {}, 0
	for _, v in sparse_ipairs(t) do
		k = k + 1
		list[k] = v
	end
	return concat(list, sep, i, j)
end

--[==[
Values of numeric keys in array portion of table are reversed: { { "a", "b", "c" }} -> { { "c", "b", "a" }}]==]
function export.reverse(t)
	local list, t_len = {}, table_len(t)
	for i = t_len, 1, -1 do
		list[t_len - i + 1] = t[i]
	end
	return list
end
table_reverse = export.reverse

function export.reverseConcat(t, sep, i, j)
	return concat(table_reverse(t), sep, i, j)
end

--[==[
Invert a list. For example, {invert({ "a", "b", "c" })} -> { { a = 1, b = 2, c = 3 }}]==]
function export.invert(list)
	local map, i = {}, 0
	while true do
		i = i + 1
		local v = list[i]
		if v == nil then
			return map
		end
		map[v] = i
	end
end
invert = export.invert

do
	local function flatten(t, list, seen, n)
		seen[t] = true
		local i = 0
		while true do
			i = i + 1
			local v = t[i]
			if v == nil then
				return n
			elseif type(v) == "table" then
				if seen[v] then
					error("loop in input list")
				end
				n = flatten(v, list, seen, n)
			else
				n = n + 1
				list[n] = v
			end
		end
	end
	
	--[==[
	Given a list, which may contain sublists, flatten it into a single list. For example, {flatten({ "a", { "b", "c" }, "d" })} ->
	{ { "a", "b", "c", "d" }}]==]
	function export.flatten(t)
		local list = {}
		flatten(t, list, {}, 0)
		return list
	end
end

--[==[
Convert `list` (a table with a list of values) into a set (a table where those values are keys instead). This is a useful
way to create a fast lookup table, since looking up a table key is much, much faster than iterating over the whole list
to see if it contains a given value.

By default, each item is given the value true. If the optional parameter `value` is a function or functor, then it is called
as an iterator, with the list index as the first argument, the item as the second (which will be used as the key), plus any
additional arguments passed to {listToSet}; the returned value is used as the value for that list item. If `value` is anything
else, then it is used as the fixed value for every item.]==]
function export.listToSet(list, value, ...)
	local set, i, callable = {}, 0
	if value == nil then
		value = true
	else
		callable = is_callable(value)
	end
	while true do
		i = i + 1
		local item = list[i]
		if item == nil then
			return set
		end
		if callable then
			set[item] = value(i, item, ...)
		else
			set[item] = value
		end
	end
end
list_to_set = export.listToSet

--[==[
Returns true if all keys in the table are consecutive integers starting at 1.]==]
function export.isArray(t)
	local i = 0
	for _ in pairs(t) do
		i = i + 1
		if t[i] == nil then
			return false
		end
	end
	return true
end

--[==[
Returns true if the first list, taken as a set, is a subset of the second list, taken as  set.]==]
function export.isSubsetList(t1, t2)
	t2 = list_to_set(t2)
	local i = 0
	while true do
		i = i + 1
		local v = t1[i]
		if v == nil then
			return true
		elseif t2[v] == nil then
			return false
		end
	end
end

--[==[
Returns true if the first map, taken as a set, is a subset of the second map, taken as  set.]==]
function export.isSubsetMap(t1, t2)
	for k in pairs(t1) do
		if t2[k] == nil then
			return false
		end
	end
	return true
end

--[==[
Add a list of aliases for a given key to a table. The aliases must be given as a table.]==]
function export.alias(t, k, aliases)
	for _, alias in pairs(aliases) do
		t[alias] = t[k]
	end
end

return export