Module:TableTools: Difference between revisions
m
1 revision imported
en>MSGJ (updates/fixes requested by User:Uzume) |
Vivaporius (talk | contribs) m (1 revision imported) |
||
Line 1:
--[[
------------------------------------------------------------------------------------
-- 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 libraryUtil = require('libraryUtil')
Line 15 ⟶ 17:
local infinity = math.huge
local checkType = libraryUtil.checkType
--[[
------------------------------------------------------------------------------------
-- isPositiveInteger
Line 25 ⟶ 27:
-- hash part of a table.
------------------------------------------------------------------------------------
--]]
function p.isPositiveInteger(v)
return true
else
return false
end
end
--[[
------------------------------------------------------------------------------------
-- isNan
--
-- This function returns true if the given number is a NaN value, and false
-- 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 p.isNan(v)
return true
else
return false
end
end
--[[
------------------------------------------------------------------------------------
-- shallowClone
Line 48 ⟶ 62:
-- table will have no metatable of its own.
------------------------------------------------------------------------------------
--]]
function p.shallowClone(t)
local ret = {}
for k, v in pairs(t) do
Line 57 ⟶ 71:
end
--[[
------------------------------------------------------------------------------------
-- removeDuplicates
Line 64 ⟶ 79:
-- removed, but otherwise the array order is unchanged.
------------------------------------------------------------------------------------
--]]
function p.removeDuplicates(t)
checkType('removeDuplicates', 1, t, 'table')
local isNan = p.isNan
local ret, exists = {}, {}
for
if isNan(v) then
-- NaNs can't be table keys, and they are also unique, so we don't need to check existence.
Line 77 ⟶ 93:
exists[v] = true
end
end
end
return ret
end
--[[
------------------------------------------------------------------------------------
-- numKeys
Line 88 ⟶ 105:
-- keys that have non-nil values, sorted in numerical order.
------------------------------------------------------------------------------------
--]]
function p.numKeys(t)
checkType('numKeys', 1, t, 'table')
local isPositiveInteger = p.isPositiveInteger
local nums = {}
for k, v in pairs(t) do
if isPositiveInteger(k) then
nums[#nums + 1] = k
Line 101 ⟶ 119:
end
--[[
------------------------------------------------------------------------------------
-- affixNums
Line 106 ⟶ 125:
-- This takes a table and returns an array containing the numbers of keys with the
-- specified prefix and suffix. For example, for the table
-- {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", affixNums will
-- return {1, 3, 6}.
------------------------------------------------------------------------------------
--]]
function p.affixNums(t, prefix, suffix)
checkType('affixNums', 1, t, 'table')
Line 116 ⟶ 136:
local function cleanPattern(s)
-- Cleans a pattern so that the magic characters ()%.[]*+-?^$ are interpreted literally.
return s
end
Line 126 ⟶ 147:
local nums = {}
for k, v in pairs(t) do
if type(k) == 'string' then
local num = mw.ustring.match(k, pattern)
if num then
Line 138 ⟶ 159:
end
--[[
------------------------------------------------------------------------------------
-- numData
--
-- Given a table with keys like
-- of subtables in the format
-- { [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 p.numData(t, compress)
checkType('numData', 1, t, 'table')
Line 177 ⟶ 200:
end
--[[
------------------------------------------------------------------------------------
-- compressSparseArray
Line 184 ⟶ 208:
-- ipairs.
------------------------------------------------------------------------------------
--]]
function p.compressSparseArray(t)
checkType('compressSparseArray', 1, t, 'table')
Line 194 ⟶ 219:
end
--[[
------------------------------------------------------------------------------------
-- sparseIpairs
Line 200 ⟶ 226:
-- handle nil values.
------------------------------------------------------------------------------------
--]]
function p.sparseIpairs(t)
checkType('sparseIpairs', 1, t, 'table')
Line 216 ⟶ 243:
end
--[[
------------------------------------------------------------------------------------
-- size
Line 222 ⟶ 250:
-- but for arrays it is more efficient to use the # operator.
------------------------------------------------------------------------------------
--]]
function p.size(t)
checkType('size', 1, t, 'table')
local i = 0
for
i = i + 1
end
return i
end
|