Módulo:Tablas

De Biquipedia

La documentación para este módulo puede ser creada en Módulo:Tablas/doc

local z = {}

-- Codigo copiau de http://lua-users.org/wiki/SortedIteration
function __genOrderedIndex( t )
    local orderedIndex = {}
    for key in pairs(t) do
        table.insert( orderedIndex, key )
    end
    table.sort( orderedIndex )
    return orderedIndex
end

function orderedNext(t, state)
    -- Equivalent of the next function, but returns the keys in the alphabetic
    -- order. We use a temporary ordered key table that is stored in the
    -- table being iterated.

    local key = nil
    --print("orderedNext: state = "..tostring(state) )
    if state == nil then
        -- the first time, generate the index
        t.__orderedIndex = __genOrderedIndex( t )
        key = t.__orderedIndex[1]
    else
        -- fetch the next value
        for i = 1,table.getn(t.__orderedIndex) do
            if t.__orderedIndex[i] == state then
                key = t.__orderedIndex[i+1]
            end
        end
    end

    if key then
        return key, t[key]
    end

    -- no more value to return, cleanup
    t.__orderedIndex = nil
    return
end

function orderedPairs(t)
    -- Equivalent of the pairs() function on tables. Allows to iterate
    -- in order
    return orderedNext, t, nil
end

function z.tostringordered(tabla, identacion)
    identacion = identacion or '\n'
    local identacion2 = identacion .. '  '
    
    if not tabla then
    	return
    end
    
    local valors = {}
    local k2, v2
 
    for k,v in orderedPairs(tabla) do
    	if type(k) == 'string' then
    		k2 = '"' .. k .. '"'
    	else
    		k2 = k
    	end
        if type(v) == 'table' then
            v2 = z.tostringordered(v, identacion2)
        elseif type(v)=='string' then
        	v2 = '"' .. v .. '"'
        else
        	v2 = tostring(v)
        end
        
        table.insert(valors, '[' .. k2 .. '] = ' .. v2)
    end
 
    return '{' .. identacion2 .. (table.concat(valors, ', ' .. identacion2) or '') .. identacion .. '}'
end

function z.tostring(tabla, identacion)
    identacion = identacion or '\n'
    local identacion2 = identacion .. '  '
    
    if not tabla then
    	return
    end
    
    local valors = {}
    local k2, v2
 
    for k,v in pairs(tabla) do
    	if type(k) == 'string' then
    		k2 = '"' .. k .. '"'
    	else
    		k2 = k
    	end
        if type(v) == 'table' then
            v2 = z.tostring(v, identacion2)
        elseif type(v)=='string' then
        	v2 = '"' .. v .. '"'
        else
        	v2 = tostring(v)
        end
        
        table.insert(valors, '[' .. k2 .. '] = ' .. v2)
    end
 
    return '{' .. identacion2 .. (table.concat(valors, ', ' .. identacion2) or '') .. identacion .. '}'
end

function z.elemento(tabla, indice1, indice2, indice3, indice4, indice5, indice6, indice7)
	local resultau
	
	if not tabla or not indice1 then
		return
	end
	
	resultau = tabla[indice1]
	
	if not indice2 or not resultau then
		return resultau
	end
	
	resultau = resultau[indice2]

	if not indice3 or not resultau then
		return resultau
	end
	
	resultau = resultau[indice3]
	
	if not indice4 or not resultau then
		return resultau
	end
	
	resultau = resultau[indice4]
	
	if not indice5 or not resultau then
		return resultau
	end
	
	resultau = resultau[indice5]
	
	if not indice6 or not resultau then
		return resultau
	end
	
	resultau = resultau[indice6]
	
	if not indice7 or not resultau then
		return resultau
	end
	
	resultau = resultau[indice7]
	
	return resultau
end

function z.en(tabla, elemento)
    if not elemento then
        return
    end
    for k,v in pairs( tabla ) do
        if v == elemento then
            return k
        end
    end
end

function z.copiarElementosConValor(orichinal)
	local copia= {}
	
    for k,v in pairs(orichinal) do
        if v~='' then
            copia[k] = orichinal[k]
        end
    end	
    
    return copia
end

function z.insertar(tabla, elemento)
	if not elemento then
		return false
	end

	if not z.en(tabla, elemento) then
		table.insert(tabla, elemento)	
	end
	
	return true
end

function z.insertarElementosConValor(orichen, destin)
    for k,v in pairs(orichen) do
        if v~='' then
            table.insert(destin, v)
        end
    end	
    
    return copia
end

function z.sonIguals(tabla1, tabla2)
	if not tabla1 or not tabla2 then
		return false
	end
	
	if tabla1 == tabla2 then
		return true
	end
	
	for k,v in pairs(tabla1) do
		if tabla2[k] ~= v then
			return false
		end
	end

	for k,v in pairs(tabla2) do
		if not tabla1[k] then
			return false
		end
	end

	return true
end

function z.ordenarFuncion(tabla, funcion)
	local funcionInestable = funcion
	
	-- Adhibir a la tabla un campo con l'orden
	for i,n in ipairs(tabla) do tabla[i].__orden = i end
	
	table.sort(tabla, 
		function(a,b) 
		   if     funcionInestable(a, b) then return true -- a < b
		   elseif funcionInestable(b, a) then return false -- b < a
		   elseif a.__orden < b.__orden  then return true -- a = b y a apareixe antis que no b
		   else                               return false -- a = b y b apareixe antis que no a
		   end
		end
	)
    
    -- Eliminar d\'a tabla o campo con l'orden
	for i,n in ipairs(tabla) do tabla[i].__orden = nil end
end

function z.ordenar(tabla, criterio)
	if type(criterio) == 'table' then
		z.ordenarFuncion(tabla,
			function(a,b)
				local valorA, valorB
				for i,campo in ipairs(criterio) do
					valorA = a[campo]
					valorB = b[campo]
					if not valorA and not valorA then
						-- No fer cosa
					elseif not valorA and valorB then
						return true
					elseif valorA and not valorB then
						return false
					elseif valorA < valorB then
						return true
					elseif valorA > valorB then
						return false 
					end
				end	
		   		return false -- Todas as valors son iguals
			end
		)	
	else
		z.ordenarFuncion(tabla, criterio)
	end
end

function z.agrupar(tabla, clave, campo)
	local tabla2 = {}
	
	local v2
	
	for k,v in ipairs(tabla) do
		if not v[campo] then
			table.insert(tabla2, v)
			v2 = nil
		elseif v2 and v2[clave] == v[clave] then
			-- Agrupar
			table.insert(v2[campo], v[campo])
		else
			v2 = v
			v2[campo] = {v[campo]}	
			table.insert(tabla2, v2)
		end
	end
	
	return tabla2
end
 
return z