Modulo:TableTools

À prupositu di Wikipedia

La documentazione per questo modulo può essere creata in Modulo:TableTools/man

--[[
------------------------------------------------------------------------------------
--                               TableTools                                       --
--                                                                                --
-- Stu modulu include una serie di funzioni per trattà cù tavule Lua.		      --
-- Hè un meta-modulu, chì deve esse chjamatu da altri moduli Lua, è duverebbe     --
-- micca esse chjamatu direttamente da #invoke.                                           --
------------------------------------------------------------------------------------
--]]

local libraryUtil = require('libraryUtil')

local p = {}

-- Definisce variabili è funzioni spessu usati.
local floor = math.floor
local infinity = math.huge
local checkType = libraryUtil.checkType
local checkTypeMulti = libraryUtil.checkTypeMulti

--[[
------------------------------------------------------------------------------------
-- isPositiveInteger
--
-- Sta funzione rende vera se u valore datu hè un interu pusitivu, è falu
-- sè ùn. Ancu s'ellu ùn opera nantu à e tavule, hè inclusu quì cume
-- utile per determinà se una chjave tabella determinata hè in a parte di a matriu o
-- parte hash di una tavola.
------------------------------------------------------------------------------------
--]]
function p.isPositiveInteger(v)
	return type(v) == 'number' and v >= 1 and floor(v) == v and v < infinity
end

--[[
------------------------------------------------------------------------------------
-- isNan
--
-- Sta funzione rende vera se u numeru datu hè un valore NaN, è falsu
-- sè ùn. Ancu s'ellu ùn opera nantu à e tavule, hè inclusu quì cume
-- utile per determinà se un valore pò esse una chjave di tabella valida. Lua serà
-- generà un errore se un NaN hè utilizatu com'è chjave di tabella.
------------------------------------------------------------------------------------
--]]
function p.isNan(v)
	return type(v) == 'number' and tostring(v) == '-nan'
end

--[[
------------------------------------------------------------------------------------
-- shallowClone
--
-- Questu rende un clone di una tavola. U valore tornatu hè una nova tavula, ma tuttu
-- sottumessi è e funzioni sò spartuti. I metametoddi sò rispettati, ma u ritornu
-- a tavula ùn duverà micca metatable da ellu stessu.
------------------------------------------------------------------------------------
--]]
function p.shallowClone(t)
	local ret = {}
	for k, v in pairs(t) do
		ret[k] = v
	end
	return ret
end

--[[
------------------------------------------------------------------------------------
-- removeDuplicates
--
-- Questu elimina i valori duplicati da una matrice. E chjavi micca pusitivi-integer sò
-- ignuratu. U valore più anticu hè mantinutu, è tutti i valori duplicati sussegwenti sò
-- eliminatu, ma altrimenti l'ordine di array hè inutile.
------------------------------------------------------------------------------------
--]]
function p.removeDuplicates(t)
	checkType('removeDuplicates', 1, t, 'table')
	local isNan = p.isNan
	local ret, exists = {}, {}
	for i, v in ipairs(t) do
		if isNan(v) then
			-- NaNs ùn ponu esse chjave di tavula, è sò ancu unichi, per quessa ùn avemu micca bisognu di verificà l'esistenza.
			ret[#ret + 1] = v
		else
			if not exists[v] then
				ret[#ret + 1] = v
				exists[v] = true
			end
		end	
	end
	return ret
end			

--[[
------------------------------------------------------------------------------------
-- numKeys
--
-- Questa piglia una tavola è torna una matrice chì cuntene i numeri di qualsiasi  
-- chjave numeriche chì anu valori non nuli, ordenati in ordine numerica.
------------------------------------------------------------------------------------
--]]
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
		end
	end
	table.sort(nums)
	return nums
end

--[[
------------------------------------------------------------------------------------
-- affixNums
--
-- Questa piglia una tavola è rende un array chì cuntene i numeri di chjave cù u
-- prefissu specificatu è u suffissu. Per esempiu, per a tavula
-- {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')
	checkType('affixNums', 2, prefix, 'string', true)
	checkType('affixNums', 3, suffix, 'string', true)

	local function cleanPattern(s)
		-- Cleans a pattern so that the magic characters ()%.[]*+-?^$ are interpreted literally.
		return s:gsub('([%(%)%%%.%[%]%*%+%-%?%^%$])', '%%%1')
	end

	prefix = prefix or ''
	suffix = suffix or ''
	prefix = cleanPattern(prefix)
	suffix = cleanPattern(suffix)
	local pattern = '^' .. prefix .. '([1-9]%d*)' .. suffix .. '$'

	local nums = {}
	for k, v in pairs(t) do
		if type(k) == 'string' then			
			local num = mw.ustring.match(k, pattern)
			if num then
				nums[#nums + 1] = tonumber(num)
			end
		end
	end
	table.sort(nums)
	return nums
end

--[[
------------------------------------------------------------------------------------
-- numData
--
-- Da una tavola cù chjavi cume ("foo1", "bar1", "foo2", "baz2"), rende una 
-- tabella di sottutantuli in u formatu
-- { [1] = {foo = 'text', bar = 'text'}, [2] = {foo = 'text', baz = 'text'} }
-- I chjavi chì ùn finiscenu micca cù un interu hè guardatu in un sottumessu chjamatu "altru".
-- L'opzione di cumpressa comprime a tavola per chì pò esse iteratu cù ipairs
------------------------------------------------------------------------------------
--]]
function p.numData(t, compress)
	checkType('numData', 1, t, 'table')
	checkType('numData', 2, compress, 'boolean', true)
	local ret = {}
	for k, v in pairs(t) do
		local prefix, num = mw.ustring.match(tostring(k), '^([^0-9]*)([1-9][0-9]*)$')
		if num then
			num = tonumber(num)
			local subtable = ret[num] or {}
			if prefix == '' then
				-- I paràmetri pusitivi currispondenu à a catena bianca mette in principiu di u sottumessu invece.
				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 = p.compressSparseArray(ret)
		ret.other = other
	end
	return ret
end

--[[
------------------------------------------------------------------------------------
-- compressSparseArray
--
-- Questa porta una matrice cù unu o più valori nul, è elimina i valori nul 
-- mentre cunservanu l'ordine, per chì a matrice pò esse attraversata in 
-- sicurità cù ipairs.
------------------------------------------------------------------------------------
--]]
function p.compressSparseArray(t)
	checkType('compressSparseArray', 1, t, 'table')
	local ret = {}
	local nums = p.numKeys(t)
	for _, num in ipairs(nums) do
		ret[#ret + 1] = t[num]
	end
	return ret
end

--[[
------------------------------------------------------------------------------------
-- sparseIpairs
--
-- Questu hè un iteratore per arrays sparse. Pò esse usatu cum'è ipairs, 
-- ma pò manighjà i valori nuli.
------------------------------------------------------------------------------------
--]]
function p.sparseIpairs(t)
	checkType('sparseIpairs', 1, t, 'table')
	local nums = p.numKeys(t)
	local i = 0
	local lim = #nums
	return function ()
		i = i + 1
		if i <= lim then
			local key = nums[i]
			return key, t[key]
		else
			return nil, nil
		end
	end
end

--[[
------------------------------------------------------------------------------------
-- size
--
-- Questu rende a dimensione di una tavola di paru di chjave / valore. 
-- Funcionerà ancu nantu i matri, ma per i matri hè più efficiente per aduprà 
-- l'operatore #.
------------------------------------------------------------------------------------
--]]

function p.size(t)
	checkType('size', 1, t, 'table')
	local i = 0
	for k in pairs(t) do
		i = i + 1
	end
	return i
end


local function defaultKeySort(item1, item2)
	-- "number" < "string", so numbers will be sorted before strings.
	local type1, type2 = type(item1), type(item2)
	if type1 ~= type2 then
		return type1 < type2
	else -- This will fail with table, boolean, function.
		return item1 < item2
	end
end

--[[
	Ritorna una lista di e chjavi in una tabella, ordenata utilizendu sia 
	una funzione di paragone predeterminata sia una funzione keySort persunalizata.
]]
function p.keysToList(t, keySort, checked)
	if not checked then
		checkType('keysToList', 1, t, 'table')
		checkTypeMulti('keysToList', 2, keySort, { 'function', 'boolean', 'nil' })
	end
	
	local list = {}
	local index = 1
	for key, value in pairs(t) do
		list[index] = key
		index = index + 1
	end
	
	if keySort ~= false then
		keySort = type(keySort) == 'function' and keySort or defaultKeySort
		
		table.sort(list, keySort)
	end
	
	return list
end

--[[
	Iterate à traversu un tavulinu, cù e chjavi ordenate utilizendu a funzione 
	keysToList. Se ci sò solu chjavi numerichi, sparseIpairs hè probabilmente 
	più efficiente.
]]
function p.sortedPairs(t, keySort)
	checkType('sortedPairs', 1, t, 'table')
	checkType('sortedPairs', 2, keySort, 'function', true)
	
	local list = p.keysToList(t, keySort, 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

--[[
	Ritorna veru se tutti i chjavi in a tavula sò numeri integri consecutivi da 1.
--]]
function p.isArray(t)
	checkType("isArray", 1, t, "table")
	
	local i = 0
	for k, v in pairs(t) do
		i = i + 1
		if t[i] == nil then
			return false
		end
	end
	return true
end

-- { "a", "b", "c" } -> { a = 1, b = 2, c = 3 }
function p.invert(array)
	checkType("invert", 1, array, "table")
	
	local map = {}
	for i, v in ipairs(array) do
		map[v] = i
	end
	
	return map
end

--[[
	{ "a", "b", "c" } -> { ["a"] = true, ["b"] = true, ["c"] = true }
--]]
function p.listToSet(t)
	checkType("listToSet", 1, t, "table")
	
	local set = {}
	for _, item in ipairs(t) do
		set[item] = true
	end
	
	return set
end

--[[
	Funzione ricurrente di copia prufonda.
	Preserva identità di sottumessi.
	
]]
local function _deepCopy(orig, includeMetatable, already_seen)
	-- Mantene copie di tavule indexate da a tabella originale.
	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)
				already_seen[mt] = mt_copy
			end
		end
	else -- number, string, boolean, etc
		copy = orig
	end
	return copy
end

function p.deepCopy(orig, noMetatable, already_seen)
	checkType("deepCopy", 3, already_seen, "table", true)
	
	return _deepCopy(orig, not noMetatable, already_seen)
end

--[[
	Concatenate tutti i valori in a tavola chì sò indexati da un numeru, in ordine.
	sparseConcat{ a, nil, c, d }  =>  "acd"
	sparseConcat{ nil, b, c, d }  =>  "bcd"
]]
function p.sparseConcat(t, sep, i, j)
	local list = {}
	
	local list_i = 0
	for _, v in p.sparseIpairs(t) do
		list_i = list_i + 1
		list[list_i] = v
	end
	
	return table.concat(list, sep, i, j)
end

--[[
-- Trova a lunghezza di una matrice, o di una quasi-matrice cù chjavi cum'è 
-- "data1", "data2", ecc, utilizendu un algoritmu di ricerca esponenziale.
-- Hè simile à l'operatore #, ma pò rende un valore differenti quandu ci sò 
-- lacune in a porzione di u tabellu.
-- Intenditu esse adupratu nantu à i dati carricati cù mw.loadData. Per altri 
-- tavulini, aduprate #.
-- Nota: # frame.args in object frame sò sempre imposti à 0, indipendentemente da 
-- u numeru di parametri di mudellu senza nome, per quessa, utilizate sta 
-- funzione per frame.args.
--]]

function p.length(t, prefix)
	-- esigenza modulu in linea per chì [[Modulo:Exponential search]]
	-- chì hè solu bisognu da sta funzione
	-- ùn vene micca milioni di trasclusioni
	local expSearch = require("Modulo:Exponential search")
	checkType('length', 1, t, 'table')
	checkType('length', 2, prefix, 'string', true)
	return expSearch(function(i)
		local key
		if prefix then
			key = prefix .. tostring(i)
		else
			key = i
		end
		return t[key] ~= nil
	end) or 0
end
function p.inArray(arr, valueToFind)
	checkType("inArray", 1, arr, "table")
	
	-- se valueToFind hè nil, errore ?
	
	for _, v in ipairs(arr) do
		if v == valueToFind then
			return true
		end
	end
	
	return false
end

return p