--Compares two tables, and produces a new table containing the differences

function module:MakeDeltaTable(oldTable, newTable)
    
    if (oldTable == nil) then
        return self:DeepCopy(newTable)
    end
    
    local deltaTable = {}
    local changes = 0
    for var, data in pairs(newTable) do
        if oldTable[var] == nil then
            deltaTable[var] = data
        else
            if type(newTable[var]) == "table" then
                --its a table, recurse
                local newtable, num = module:MakeDeltaTable(oldTable[var], newTable[var])
                if num > 0 then
                    changes = changes + 1
                    deltaTable[var] = newtable
                end
            else
                local a = newTable[var]
                local b = oldTable[var]
                if a ~= b then
                    changes = changes + 1
                    deltaTable[var] = a
                end
            end
        end
    end
    --Check for deletions
    for var, _ in pairs(oldTable) do
        if newTable[var] == nil then
            if deltaTable.__deletions == nil then
                deltaTable.__deletions = {}
            end
            table.insert(deltaTable.__deletions, var)
        end
    end

    return deltaTable, changes
end
