module(..., package.seeall);

--#############################################################################
-- returns true if tableTwo contains all elements of tableOne
function contains(tableOne, tableTwo)
    for _, x in pairs(tableOne) do
        local found = false
        for _, y in pairs(tableTwo) do
            if x == y then
                found = true
                break
            end
        end
        if not found then
            return false
        end
    end
    return true
end
--==============================================================================
function copy(oldTable) 
    local newTable = {}
    for _, x in ipairs(oldTable) do
        table.insert(newTable, x) 
    end
    return newTable
end
--==============================================================================
-- adds all elements in source table to res table
function append(res, source)
    if source then
        for _, x in ipairs(source) do
            table.insert(res, x)
        end
    end
end
--==============================================================================
function isEmpty(tableOne) 
    return not next(tableOne)
end
--==============================================================================
-- returns some sort of cartesian product of a table of tables and a table of 
-- values
--
-- example: original = {{"Dimitry"}, {"Anatoli"}} values = {"Karpov", "Kasparov"} 
-- {
--  {"Dimitry", "Karpov"},
--  {"Dimitry", "Kasparov"},
--  {"Anatoli", "Karpov"},
--  {"Anatoli", "Kasparov"},
-- }
function cartesianProduct(original, values) 

    local product = {}
    if #original == 0 then
        for _, value in pairs(values) do
            table.insert(product, {value})
        end
        return product
    end

    if #values == 0 then
        return copy(original)
    end
 
    for _, row in pairs(original) do
        for _, value in pairs(values) do
            local newRow = copy(row)
            table.insert(newRow, value)
            table.insert(product, newRow)
        end
    end
    return product
end

--#############################################################################
function getKeys(tableOne)
    local result = {}
    for k in pairs(tableOne or {}) do
        table.insert(result, k)
    end
    return result
end

--#############################################################################
function getDictSize(tableDict)
    local count = 0
    for _, _ in pairs(tableDict) do
        count = count + 1
    end
    return count
end

