Differenze tra le versioni di "Modulo:Wikilib/tables"

Jump to navigation Jump to search
Inserite funzioni flip, keys, noDuplicates e nonIntPairs
(Bugfix in deepSearch: potevano essere ritornati nil non in ultima posizione *** sovrascritto il testo esistente ***)
(Inserite funzioni flip, keys, noDuplicates e nonIntPairs)
 
local t = {}
 
-- Stateless iterator sulle chiavi non intere
local nextNonInt = function(tab, key)
local nextKey, nextValue = key
repeat
nextKey, nextValue = next(tab, nextKey)
until type(nextKey) ~= 'number'
or math.floor(nextKey) ~= nextKey
return nextKey, nextValue
end
 
local next_non_int = nextNonInt
 
-- Stateless iterator da usare nei generic for loops
table.nonIntPairs = function(tab)
return nextNonInt, tab
end
 
table.non_int_pairs = table.nonIntPairs
t.nonIntPairs, t.non_int_pairs =
table.nonIntPairs, table.nonIntPairs
 
--[[
 
Ritorna true se a è lessicograficamente
maggiore di b, false altrimenti
 
--]]
local minor = function(a, b)
if not b then
return true
end
--[[
Necessario affinché roba come 1 e
'01' siano considerati uguali
--]]
local nA, nB = tonumber(a), tonumber(b)
if nA and nB then
return nA < nB
end
 
return tostring(a) < tostring(b)
end
 
--[[
 
--]]
 
table.search = function(tab, value)
for k, v in pairs(tab) do
end
end
-- No explicit return, thus nil
end
 
 
--]]
 
table.deepSearch = function(tab, value)
for k, v in pairs(tab) do
end
end
-- No explicit return, thus nil
end
 
 
--]]
 
table.getn = function(self, count)
count = count or 'all'
 
--]]
 
table.tableKeysAlias = function(tab, source, dest)
for destGroup, sourceKey in ipairs(source) do
 
--]]
 
table.map = function(tab, funct)
local dest = {}
 
--]]
 
table.equal = function(tab1, tab2)
 
 
--]]
 
table.merge = function(tab1, tab2, inPlace)
 
 
--]]
 
table.filter = function(tab, cond)
local dest = {}
 
t.filter = table.filter
 
--[[
 
Ritorna una table con chiavi e valori
invertiti rispetto a quella passata.
 
--]]
table.flip = function(tab)
local flipped = {}
for key, value in pairs(tab) do
flipped[value] = key
end
return flipped
end
 
t.flip = table.flip
 
--[[
 
Ritorna una table con gli stessi elementi di
quella passata, ma senza duplicati.
 
Le chiavi degli elementi mantenuti sono le
prime in ordine lessicografico rispetto alle
chiavi dei grupppi di elementi uguali, con
la sola eccezione di quelle intere che sono
rese contigue.
 
--]]
table.noDuplicates = function(tab)
local check, n = {}, 1
 
--[[
Il doppio ciclo si rende necessario per
mantenere l'ordine delle chiavi numeriche.
La variabile n serve per non avere buchi.
Non si usa minor poiché le chiavi vengono
già tornate in ordine crescente da ipairs.
--]]
for key, value in ipairs(tab) do
if not check[value] then
check[value] = n
n = n + 1
end
end
for key, value in table.nonIntPairs(tab) do
if minor(key, check[value]) then
check[value] = key
end
end
return table.flip(check)
end
 
table.no_duplicates, table.unique =
table.noDuplicates, table.noDuplicates
t.noDuplicates, t.no_duplicates, t.unique =
table.noDuplicates, table.noDuplicates, table.noDuplicates
 
--[[
 
Ritorna una table con soli indici numerici
i cui elementi sono le chiavi della table
passata, in ordine non specificato
 
--]]
table.keys = function(tab)
local keys = {}
for key, v in pairs(tab) do
table.insert(keys, key)
end
return keys
end
 
t.keys = table.keys
 
return t

Menu di navigazione