Module:table: Difference between revisions

(Created page with "--[[ ------------------------------------------------------------------------------------ -- table (formerly TableTools) --...")
 
m (1 revision imported)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
--[[
local export = {}
------------------------------------------------------------------------------------
--                      table (formerly TableTools)                              --
--                                                                                --
-- This module inclcudes 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.                                          --
------------------------------------------------------------------------------------
--]]


--[[
--[==[ intro:
Inserting new values into a table using a local "index" variable, which is
This module provides functions for dealing with Lua tables. All of them, except for two helper functions, take a table
incremented each time, is faster than using "table.insert(t, x)" or
as their first argument.
"t[#t + 1] = x". See the talk page.
]]


local libraryUtil = require('libraryUtil')
Some functions are available as methods in the arrays created by [[Module:array]].


local export = {}
Functions by what they do:
* Create a new table:
** `shallowCopy`, `deepCopy`, `removeDuplicates`, `numKeys`, `compressSparseArray`, `keysToList`, `reverse`, `invert`, `listToSet`
* Create an array:
** `removeDuplicates`, `numKeys`, `compressSparseArray`, `keysToList`, `reverse`
* Return information about the table:
** `size`, `length`, `contains`, `isArray`, `deepEquals`
* Treat the table as an array (that is, operate on the values in the array portion of the table: values indexed by
  consecutive integers starting at {1}):
** `removeDuplicates`, `length`, `contains`, `serialCommaJoin`, `reverseIpairs`, `reverse`, `invert`, `listToSet`, `isArray`
* Treat a table as a sparse array (that is, operate on values indexed by non-consecutive integers):
** `numKeys`, `maxIndex`, `compressSparseArray`, `sparseConcat`, `sparseIpairs`
* Generate an iterator:
** `sparseIpairs`, `sortedPairs`, `reverseIpairs`
* Other functions:
** `sparseConcat`, `serialCommaJoin`, `reverseConcat`
 
The original version was a copy of {{w|Module:TableTools}} on Wikipedia via [[c:Module:TableTools|Module:TableTools]] on
Commons, but in the course of time this module has been almost completely rewritten, with many new functions added. The
main legacy of this is the use of camelCase for function names rather than snake_case, as is normal in the English
Wiktionary.
]==]


-- Define often-used variables and functions.
local load_module = "Module:load"
local floor = math.floor
local math_module = "Module:math"
local infinity = math.huge
local checkType = libraryUtil.checkType
local checkTypeMulti = libraryUtil.checkTypeMulti


local function _check(funcName, expectType)
local table = table
if type(expectType) == "string" then
return function(argIndex, arg, nilOk)
checkType(funcName, argIndex, arg, expectType, nilOk)
end
else
return function(argIndex, arg, expectType, nilOk)
if type(expectType) == "table" then
checkTypeMulti(funcName, argIndex, arg, expectType, nilOk)
else
checkType(funcName, argIndex, arg, expectType, nilOk)
end
end
end
end


--[[
local concat = table.concat
------------------------------------------------------------------------------------
local dump = mw.dumpObject
-- isPositiveInteger
local ipairs = ipairs
--
local ipairs_default_iter = ipairs{export}
-- This function returns true if the given value is a positive integer, and false
local next = next
-- if not. Although it doesn't operate on tables, it is included here as it is
local pairs = pairs
-- useful for determining whether a given table key is in the array part or the
local require = require
-- hash part of a table.
local select = select
------------------------------------------------------------------------------------
local signed_index -- defined as export.signedIndex
--]]
local table_len -- defined as export.length
function export.isPositiveInteger(v)
local type = type
return type(v) == 'number' and v >= 1 and floor(v) == v and v < infinity
end


--[[
--[==[
------------------------------------------------------------------------------------
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.]==]
-- isNan
local function is_integer(...)
--
is_integer = require(math_module).is_integer
-- This function returns true if the given number is a NaN value, and false
return is_integer(...)
-- if not. Although it doesn't operate on tables, it is included here as it is
-- useful for determining whether a value can be a valid table key. Lua will
-- generate an error if a NaN is used as a table key.
------------------------------------------------------------------------------------
--]]
function export.isNan(v)
if type(v) == 'number' and tostring(v) == '-nan' then
return true
else
return false
end
end
end


--[[
local function safe_require(...)
------------------------------------------------------------------------------------
safe_require = require(load_module).safe_require
-- shallowClone
return safe_require(...)
--
-- This returns a clone of a table. The value returned is a new table, but all
-- 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
return ret
end
end


--[[
--[==[
Shallow copy
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)
function export.shallowcopy(orig)
if not is_integer(k) then
local orig_type = type(orig)
error("index must be an integer")
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
end
return copy
return k < 0 and (type(t) == "table" and table_len(t) or t) + k + 1 or k
end
end
signed_index = export.signedIndex


--[[
--[==[
Recursive deep copy function
An iterator which works like `pairs`, but ignores any `__pairs` metamethod.]==]
Equivalent to mw.clone?
function export.rawPairs(t)
]]
return next, t, nil
local function deepcopy(orig, includeMetatable, already_seen)
-- Stores copies of tables indexed by the original table.
already_seen = already_seen or {}
local copy = already_seen[orig]
if copy ~= nil then
return copy
end
if type(orig) == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[deepcopy(orig_key, includeMetatable, already_seen)] = deepcopy(orig_value, includeMetatable, already_seen)
end
already_seen[orig] = copy
if includeMetatable then
local mt = getmetatable(orig)
if mt ~= nil then
local mt_copy = deepcopy(mt, includeMetatable, already_seen)
setmetatable(copy, mt_copy)
end
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
end


function export.deepcopy(orig, noMetatable, already_seen)
--[==[
checkType("deepcopy", 3, already_seen, "table", true)
An iterator which works like `ipairs`, but ignores any `__ipairs` metamethod.]==]
function export.rawIpairs(t)
return deepcopy(orig, not noMetatable, already_seen)
return ipairs_default_iter, t, 0
end
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.
-- removeDuplicates
--
-- This removes duplicate values from an array. Non-positive-integer keys are
-- ignored. The earliest value is kept, and all subsequent duplicate values are
-- removed, but otherwise the array order is unchanged.
------------------------------------------------------------------------------------
--]]
function export.removeDuplicates(t)
checkType('removeDuplicates', 1, t, 'table')
local isNan = export.isNan
local ret, exists = {}, {}
local index = 1
for _, v in ipairs(t) do
if isNan(v) then
-- NaNs can't be table keys, and they are also unique, so we don't need to check existence.
ret[index] = v
index = index + 1
else
if not exists[v] then
ret[index] = v
index = index + 1
exists[v] = true
end
end
end
return ret
end


--[[
For arrays, this function is faster than `export.size`.]==]
------------------------------------------------------------------------------------
function export.length(t, raw)
-- numKeys
local n = 0
--
if raw then
-- This takes a table and returns an array containing the numbers of any numerical
for i in ipairs_default_iter, t, 0 do
-- keys that have non-nil values, sorted in numerical order.
n = i
------------------------------------------------------------------------------------
--]]
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
return n
end
end
table.sort(nums)
repeat
return nums
n = n + 1
until t[n] == nil
return n - 1
end
end
table_len = export.length


function export.maxIndex(t)
local function getIteratorValues(i, j , step, t_len)
checkType('maxIndex', 1, t, 'table')
i, j = i and signed_index(t_len, i), j and signed_index(t_len, j)
local positiveIntegerKeys = export.numKeys(t)
if step == nil then
if positiveIntegerKeys[1] then
i, j = i or 1, j or t_len
return math.max(unpack(positiveIntegerKeys))
return i, j, j < i and -1 or 1
else
elseif step == 0 or not is_integer(step) then
return 0 -- ???
error("step must be a non-zero integer")
elseif step < 0 then
return i or t_len, j or 1, step
end
end
return i or 1, j or t_len, step
end
end


--[[
--[==[
------------------------------------------------------------------------------------
Given an array `list` and function `func`, iterate through the array applying {func(r, k, v)}, and returning the result,
-- affixNums
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`.
-- This takes a table and returns an array containing the numbers of keys with the
-- specified prefix and suffix.
-- affixNums({a1 = 'foo', a3 = 'bar', a6 = 'baz'}, "a")
-- ↓
-- {1, 3, 6}.
------------------------------------------------------------------------------------
--]]
function export.affixNums(t, prefix, suffix)
local check = _check('affixNums')
check(1, t, 'table')
check(2, prefix, 'string', true)
check(3, suffix, 'string', true)
local function cleanPattern(s)
-- Cleans a pattern so that the magic characters ()%.[]*+-?^$ are interpreted literally.
s = s:gsub('([%(%)%%%.%[%]%*%+%-%?%^%$])', '%%%1')
return s
end
prefix = prefix or ''
suffix = suffix or ''
prefix = cleanPattern(prefix)
suffix = cleanPattern(suffix)
local pattern = '^' .. prefix .. '([1-9]%d*)' .. suffix .. '$'
local nums = {}
local index = 1
for k, _ in pairs(t) do
if type(k) == 'string' then
local num = mw.ustring.match(k, pattern)
if num then
nums[index] = tonumber(num)
index = index + 1
end
end
end
table.sort(nums)
return nums
end


--[[
Optional arguments:
------------------------------------------------------------------------------------
* `i`: start index; negative values count from the end of the array
-- numData
* `j`: end index; negative values count from the end of the array
--
* `step`: step increment
-- Given a table with keys like ("foo1", "bar1", "foo2", "baz2"), returns a table
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
-- of subtables in the format
backwards and by how much, based on these inputs (see examples below for default behaviours).
-- { [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
if compress then
local other = ret.other
ret = export.compressSparseArray(ret)
ret.other = other
end
return ret
end


--[[
Examples:
------------------------------------------------------------------------------------
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
-- compressSparseArray
# 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).
-- This takes an array with one or more nil values, and removes the nil values
# j=-3 results in forward iteration from the start to the 3rd last index.
-- while preserving the order, so that the array can be safely traversed with
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
-- ipairs.
function export.reduce(t, func, i, j, step)
------------------------------------------------------------------------------------
i, j, step = getIteratorValues(i, j, step, table_len(t))
--]]
local ret = t[i]
function export.compressSparseArray(t)
for k = i + step, j, step do
checkType('compressSparseArray', 1, t, 'table')
ret = func(ret, k, t[k])
local ret = {}
local index = 1
local nums = export.numKeys(t)
for _, num in ipairs(nums) do
ret[index] = t[num]
index = index + 1
end
end
return ret
return ret
end
end


--[[
do
------------------------------------------------------------------------------------
local function replace(t, func, i, j, step, generate)
-- sparseIpairs
local t_len = table_len(t)
--
-- Normalized i, j and step, based on the inputs.
-- This is an iterator for sparse arrays. It can be used like ipairs, but can
local norm_i, norm_j, norm_step = getIteratorValues(i, j, step, t_len)
-- handle nil values.
if norm_step > 0 then
------------------------------------------------------------------------------------
i, j, step = 1, t_len, 1
--]]
function export.sparseIpairs(t)
checkType('sparseIpairs', 1, t, 'table')
local nums = export.numKeys(t)
local i = 0
return function()
i = i + 1
local key = nums[i]
if key then
return key, t[key]
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
end
end


--[[
--[==[
------------------------------------------------------------------------------------
Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
-- size
`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.
-- This returns the size of a key/value pair table. It will also work on arrays,
-- but for arrays it is more efficient to use the # operator.
------------------------------------------------------------------------------------
--]]
function export.size(t)
checkType('size', 1, t, 'table')
local i = 0
for _ in pairs(t) do
i = i + 1
end
return i
end


--[[
Optional arguments:
-- This returns the length of a table, or the first integer key n counting from
* `i`: start index; negative values count from the end of the array
-- 1 such that t[n + 1] is nil. It is similar to the operator #, but may return
* `j`: end index; negative values count from the end of the array
-- a different value when there are gaps in the array portion of the table.
* `step`: step increment
-- Intended to be used on data loaded with mw.loadData. For other tables, use #.
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).
function export.length(t)
local i = 0
repeat
i = i + 1
until t[i] == nil
return i - 1
end


--[[
Examples:
Takes table and a value to be found.
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
If the value is in the array portion of the table, return true.
# step=-1 results in backward iteration from the end to the start in steps of 1.
If the value is in the hashmap or not in the table, return false.
# 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.
function export.contains(list, x)
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
for _, v in ipairs(list) do
function export.apply(t, func, i, j, step)
if v == x then return true end
return replace(t, func, i, j, step, false)
end
end
return false
end


--[[
--[==[
Recursively compare two values that may be tables, including tables with
Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
nested tables as values. Return true if both values are structurally equal.
`v` is the value at index `k`), and return a shallow copy of the original array with the relevant values replaced. For example,
Note that this handles arbitary levels of nesting. If all tables are known
{generate(array, function(_, v) return 2 * v end)} will return a new array in which each value has been doubled.
to be lists (with only integral keys), use export.deepEqualsList, which will
 
be more efficient.
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).


NOTE: This is *NOT* smart enough to properly handle cycles; in such a case, it
Examples:
will get into an infinite loop.
# 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.deepEquals(x, y)
# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
    if type(x) == "table" and type(y) == "table" then
# j=-3 results in forward iteration from the start to the 3rd last index.
    -- Two tables are the same if they have the same number of elements
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
    -- and all keys that are present in one of the tables compare equal
function export.generate(t, func, i, j, step)
    -- to the corresponding keys in the other table, using structural
return replace(t, func, i, j, step, true)
    -- comparison.
end
    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


--[[
--[==[
Recursively compare two values that may be lists (i.e. tables with integral
Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
keys), including lists with nested lists as values. Return true if both values
`v` is the value at index `k`), and returning whether the function is true for all iterations.
are structurally equal. Note that this handles arbitary levels of nesting.
Results are undefined if tables with non-integral keys are present anywhere in
either structure; if that may be the case, use export.deepEquals, which will
handle such tables correctly but be less efficient on lists than
export.deepEqualsList.


NOTE: This is *NOT* smart enough to properly handle cycles; in such a case, it
Optional arguments:
will get into an infinite loop.
* `i`: start index; negative values count from the end of the array
]]
* `j`: end index; negative values count from the end of the array
function export.deepEqualsList(x, y)
* `step`: step increment
    if type(x) == "table" and type(y) == "table" then
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
        if #x ~= #y then
backwards and by how much, based on these inputs (see examples below for default behaviours).
            return false
        end
        for key, value in ipairs(x) do
            if not export.deepEqualsList(value, y[key]) then
                return false
            end
        end
        return true
    end
    return x == y
end


--[[
Examples:
Finds key for specified value in a given table.
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
Roughly equivalent to reversing the key-value pairs in the table –
# step=-1 results in backward iteration from the end to the start in steps of 1.
reversed_table = { [value1] = key1, [value2] = key2, ... }
# i=7, j=3 results in backward iteration from indices 7 to 3 in steps of 1 (i.e. step=-1).
– and then returning reversed_table[valueToFind].
# 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.]==]
The value can only be a string or a number
function export.all(t, func, i, j, step)
(not nil, a boolean, a table, or a function).
i, j, step = getIteratorValues(i, j, step, table_len(t))
for k = i, j, step do
Only reliable if there is just one key with the specified value.
if not func(k, t[k]) then
Otherwise, the function returns the first key found,
return false
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 true
return nil
end
end


--[[
--[==[
The default sorting function used in export.keysToList if no keySort
Given an array `list` and function `func`, iterate through the array applying {func(k, v)} (where `k` is an index, and
is defined.
`v` is the value at index `k`), and returning whether the function is true for at least one iteration.
]]
local function defaultKeySort(key1, key2)
-- "number" < "string", so numbers will be sorted before strings.
local type1, type2 = type(key1), type(key2)
if type1 ~= type2 then
return type1 < type2
else
return key1 < key2
end
end


--[[
Optional arguments:
Returns a list of the keys in a table, sorted using either the default
* `i`: start index; negative values count from the end of the array
table.sort function or a custom keySort function.
* `j`: end index; negative values count from the end of the array
If there are only numerical keys, numKeys is probably more efficient.
* `step`: step increment
]]
These must be non-zero integers. The function will determine where to iterate from, whether to iterate forwards or
function export.keysToList(t, keySort, checked)
backwards and by how much, based on these inputs (see examples below for default behaviours).
if not checked then
local check = _check('keysToList')
check(1, t, 'table')
check(2, keySort, 'function', true)
end
local list = {}
local index = 1
for key, _ in pairs(t) do
list[index] = key
index = index + 1
end
-- Place numbers before strings, otherwise sort using <.
if not keySort then
keySort = defaultKeySort
end
table.sort(list, keySort)
return list
end


--[[
Examples:
Iterates through a table, with the keys sorted using the keysToList function.
# No values for i, j or step results in forward iteration from the start to the end in steps of 1 (the default).
If there are only numerical keys, sparseIpairs is probably more efficient.
# 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).
function export.sortedPairs(t, keySort)
# j=-3 results in forward iteration from the start to the 3rd last index.
local check = _check('keysToList')
# j=-3, step=-1 results in backward iteration from the end to the 3rd last index.]==]
check(1, t, 'table')
function export.any(t, func, i, j, step)
check(2, keySort, 'function', true)
i, j, step = getIteratorValues(i, j, step, table_len(t))
for k = i, j, step do
local list = export.keysToList(t, keySort, true)
if not not (func(k, t[k])) then
return true
local i = 0
return function()
i = i + 1
local key = list[i]
if key ~= nil then
return key, t[key]
else
return nil, nil
end
end
end
end
return false
end
end


function export.reverseIpairs(list)
--[==[
checkType('reverse_ipairs', 1, list, 'table')
Joins an array with serial comma and serial conjunction, normally {"and"}. An improvement on {mw.text.listToText},
which doesn't properly handle serial commas.
local i = #list + 1
return function()
i = i - 1
if list[i] ~= nil then
return i, list[i]
else
return nil, nil
end
end
end


--[=[
Options:
Joins an array with serial comma and serial conjunction, normally "and".
* `conj`: Conjunction to use; defaults to {"and"}.
An improvement on mw.text.listToText, which doesn't properly handle serial
* `punc`: Punctuation to use; default to {","}.
commas.
* `dontTag`: Don't tag the serial comma and serial {"and"}. For error messages, in which HTML cannot be used.
* `dump`: Each item will be serialized with {mw.dumpObject}. For warnings and error messages.]==]
Options:
- conj
Conjunction to use; defaults to "and".
- italicizeConj
Italicize conjunction: for [[Module:Template:also]]
- dontTag
Don't tag the serial comma and serial "and". For error messages, in
which HTML cannot be used.
]=]
function export.serialCommaJoin(seq, options)
function export.serialCommaJoin(seq, options)
local check = _check("serialCommaJoin", "table")
-- If the `dump` option is set, determine the table length as part of the
check(1, seq)
-- dump loop, instead of calling `table_len` separately.
check(2, options, true)
local length
if options and options.dump then
local length = #seq
local i, item = 1, seq[1]
if item ~= nil then
if not options then
local dumped = {}
options = {}
repeat
end
dumped[i] = dump(item)
i = i + 1
local conj
item = seq[i]
if length > 1 then
until item == nil
conj = options.conj or "and"
seq = dumped
if options.italicizeConj then
conj = "''" .. conj .. "''"
end
end
length = i - 1
else
length = table_len(seq)
end
end
 
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
end
 
local conj = options and options.conj
if conj == nil then
conj = "and"
end
 
if length == 2 then
return seq[1] .. " " .. conj .. " " .. seq[2]
return seq[1] .. " " .. conj .. " " .. seq[2]
else
local comma = options.dontTag and "," or '<span class="serial-comma">,</span>'
conj = options.dontTag and ' ' .. conj .. " " or '<span class="serial-and"> ' .. conj .. '</span> '
return table.concat(seq, ", ", 1, length - 1) ..
comma .. conj .. seq[length]
end
end
end


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


--[[
local comma
Values of numberic keys in array portion of table are reversed:
if dont_tag then
{ "a", "b", "c" } -> { "c", "b", "a" }
comma = "" -- since by default the serial comma doesn't display, when we can't tag we shouldn't display it.
--]]
conj = " " .. conj .. " "
function export.reverse(t)
else
checkType("reverse", 1, t, "table")
comma = "<span class=\"serial-comma\">" .. punc .. "</span>"
conj = "<span class=\"serial-and\"> " .. conj .. "</span> "
local new_t = {}
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
end


function export.reverseConcat(t, sep, i, j)
return concat(seq, punc .. " ", 1, length - 1) .. comma .. conj .. seq[length]
return table.concat(export.reverse(t), sep, i, j)
end
end


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


--[[
--[==[
{ "a", "b", "c" } -> { ["a"] = true, ["b"] = true, ["c"] = true }
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)
function export.listToSet(t)
for _, alias in pairs(aliases) do
checkType("listToSet", 1, t, "table")
t[alias] = t[k]
local set = {}
for _, item in ipairs(t) do
set[item] = true
end
end
return set
end
end


--[[
local mt = {}
Returns true if all keys in the table are consecutive integers starting at 1.
 
--]]
function mt:__index(k)
function export.isArray(t)
local submodule = safe_require("Module:table/" .. k)
checkType("isArray", 1, t, "table")
self[k] = submodule
return submodule
local i = 0
for _ in pairs(t) do
i = i + 1
if t[i] == nil then
return false
end
end
return true
end
end


return export
return setmetatable(export, mt)

Latest revision as of 17:47, 4 November 2025



local export = {}

--[==[ intro:
This module provides functions for dealing with Lua tables. All of them, except for two helper functions, take a table
as their first argument.

Some functions are available as methods in the arrays created by [[Module:array]].

Functions by what they do:
* Create a new table:
** `shallowCopy`, `deepCopy`, `removeDuplicates`, `numKeys`, `compressSparseArray`, `keysToList`, `reverse`, `invert`, `listToSet`
* Create an array:
** `removeDuplicates`, `numKeys`, `compressSparseArray`, `keysToList`, `reverse`
* Return information about the table:
** `size`, `length`, `contains`, `isArray`, `deepEquals`
* Treat the table as an array (that is, operate on the values in the array portion of the table: values indexed by
  consecutive integers starting at {1}):
** `removeDuplicates`, `length`, `contains`, `serialCommaJoin`, `reverseIpairs`, `reverse`, `invert`, `listToSet`, `isArray`
* Treat a table as a sparse array (that is, operate on values indexed by non-consecutive integers):
** `numKeys`, `maxIndex`, `compressSparseArray`, `sparseConcat`, `sparseIpairs`
* Generate an iterator:
** `sparseIpairs`, `sortedPairs`, `reverseIpairs`
* Other functions:
** `sparseConcat`, `serialCommaJoin`, `reverseConcat`

The original version was a copy of {{w|Module:TableTools}} on Wikipedia via [[c:Module:TableTools|Module:TableTools]] on
Commons, but in the course of time this module has been almost completely rewritten, with many new functions added. The
main legacy of this is the use of camelCase for function names rather than snake_case, as is normal in the English
Wiktionary.
]==]

local load_module = "Module:load"
local math_module = "Module:math"

local table = table

local concat = table.concat
local dump = mw.dumpObject
local ipairs = ipairs
local ipairs_default_iter = ipairs{export}
local next = next
local pairs = pairs
local require = require
local select = select
local signed_index -- defined as export.signedIndex
local table_len -- defined as export.length
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 is_integer(...)
	is_integer = require(math_module).is_integer
	return is_integer(...)
end

local function safe_require(...)
	safe_require = require(load_module).safe_require
	return safe_require(...)
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

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

--[==[
An iterator which works like `ipairs`, but ignores any `__ipairs` metamethod.]==]
function export.rawIpairs(t)
	return ipairs_default_iter, t, 0
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

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, false)
	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.
* `dump`: Each item will be serialized with {mw.dumpObject}. For warnings and error messages.]==]
function export.serialCommaJoin(seq, options)
	-- If the `dump` option is set, determine the table length as part of the
	-- dump loop, instead of calling `table_len` separately.
	local length
	if options and options.dump then
		local i, item = 1, seq[1]
		if item ~= nil then
			local dumped = {}
			repeat
				dumped[i] = dump(item)
				i = i + 1
				item = seq[i]
			until item == nil
			seq = dumped
		end
		length = i - 1
	else
		length = table_len(seq)
	end

	if length == 0 then
		return ""
	elseif length == 1 then
		return seq[1]
	end

	local conj = options and options.conj
	if conj == nil then
		conj = "and"
	end

	if length == 2 then
		return seq[1] .. " " .. conj .. " " .. seq[2]
	end

	local punc, dont_tag
	if options then
		punc = options.punc
		if punc == nil then
			punc = ","
		end
		dont_tag = options.dontTag
	else
		punc = ","
	end

	local comma
	if dont_tag then
		comma = "" -- since by default the serial comma doesn't display, when we can't tag we shouldn't display it.
		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

--[==[
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

local mt = {}

function mt:__index(k)
	local submodule = safe_require("Module:table/" .. k)
	self[k] = submodule
	return submodule
end

return setmetatable(export, mt)