ሞድዩል:Tables
Appearance
Documentation for this module may be created at ሞድዩል:Tables/doc
local z = {}
-- Code copied from 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(board, indentation)
indentation = indentation or '\n'
local indentation2 = indentation .. ' '
if not board then
return
end
local values = {}
local k2, v2
for k,v in orderedPairs(board) do
if type(k) == 'string' then
k2 = '"' .. k .. '"'
else
k2 = k
end
if type(v) == 'table' then
v2 = z.tostringordered(v, indentation2)
elseif type(v)=='string' then
v2 = '"' .. v .. '"'
else
v2 = tostring(v)
end
table.insert(values, '[' .. k2 .. '] = ' .. v2)
end
return '{' .. indentation2 .. (table.concat(values, '፣ ' .. indentation2) or '') .. indentation .. '}'
end
function z.tostring(board, indentation)
indentation = indentation or '\n'
local indentation2 = indentation .. ' '
if not board then
return
end
local values = {}
local k2, v2
for k,v in pairs(board) do
if type(k) == 'string' then
k2 = '"' .. k .. '"'
else
k2 = k
end
if type(v) == 'table' then
v2 = z.tostring(v, indentation2)
elseif type(v)=='string' then
v2 = '"' .. v .. '"'
else
v2 = tostring(v)
end
table.insert(values, '[' .. k2 .. '] = ' .. v2)
end
return '{' .. indentation2 .. (table.concat(values, '፣ ' .. indentation2) or '') .. indentation .. '}'
end
function z.element(board, index1, index2, index3, index4, index5, index6, index7)
local result
if not board or not index1 then
return
end
result = board[index1]
if not index2 or not result then
return result
end
result = result[index2]
if not index3 or not result then
return result
end
result = result[index3]
if not index4 or not result then
return result
end
result = result[index4]
if not index5 or not result then
return result
end
result = result[index5]
if not index6 or not result then
return result
end
result = result[index6]
if not index7 or not result then
return result
end
result = result[index7]
return result
end
function z.on(board, element)
if not element then
return
end
for k,v in pairs( board ) do
if v == element then
return k
end
end
end
function z.copyElementsWithValue(original)
local copy= {}
for k,v in pairs(original) do
if v~='' then
copy[k] = original[k]
end
end
return copy
end
function z.put(board, element)
if not element then
return false
end
if not z.on(board, element) then
table.insert(board, element)
end
return true
end
function z.insertElementsWithValue(origin, destiny)
for k,v in pairs(origin) do
if v~='' then
table.insert(destiny, v)
end
end
return copy
end
function z.areEqual(board1, board2)
if not board1 or not board2 then
return false
end
if board1 == board2 then
return true
end
for k,v in pairs(board1) do
if board2[k] ~= v then
return false
end
end
for k,v in pairs(board2) do
if not board1[k] then
return false
end
end
return true
end
function z.orderFeature(board, feature)
local unstableFeature = feature
-- Add a field to the table with the order
for i,n in ipairs(table) do board[i].__order = i end
table.sort(table,
function(a,b)
if unstableFeature(a, b) then return true -- a < b
elseif unstableFeature(b, a) then return false -- b < a
elseif a.__order < b.__order then return true -- a = b and a appears before b
else return false -- a = b and b appears before a
end
end
)
-- Remove the field with the order from the table
for i,n in ipairs(table) do board[i].__order = nil end
end
function z.toOrder(board, criterion)
if type(criterion) == 'table' then
z.orderFeature(board,
function(a,b)
local valueA, valueB
for i,field in ipairs(criterion) do
valueA = a[field]
valueB = b[field]
if not valueA and not valueA then
-- Do nothing
elseif not valueA and valueB then
return true
elseif valueA and not valueB then
return false
elseif valueA < valueB then
return true
elseif valueA > valueB then
return false
end
end
return false -- All values are equal
end
)
else
z.orderFeature(board, criterion)
end
end
function z.toGroup(board, key, field)
local board2 = {}
local v2
for k,v in ipairs(table) do
if not v[field] then
table.insert(board2, v)
v2 = nil
elseif v2 and v2[key] == v[key] then
-- To group
table.insert(v2[field], v[field])
else
v2 = v
v2[field] = {v[field]}
table.insert(board2, v2)
end
end
return board2
end
return z