% SUDOKU_GENERATE   Will generate random sudoku table.
%   [ A, rating, solution ] = SUDOKU_GENERATE( d, desiredRating )
%   [ A, rating, solution ] = SUDOKU_GENERATE( d, desiredRating, verbose )
%
% Syntax:  [ A, rating, solution ] = SUDOKU_GENERATE( d, desiredRating, verbose )
%
% Inputs:
%    A - Generated sudoku table of dimensions (d*d)x(d*d) for integer d>1.
%    C - Influence index cache.
%
% Outputs:
%
% Example:
%    C = generate_influence_indices( d );
%    [ domains, domainSizes ] = sudoku_domains( A, C )
%
% Other m-files required: generate_influence_indices.m
% Subfunctions: none
% MAT-files required: none
%
% See also: GENERATE_INFLUENCE_INDICES
function [ domains, domainSizes ] = sudoku_domains( A, C )
d = sqrt(size(A,1));
r = d*d;                    % Range of values (also edge size of the board).
lr = r+1;                   % Range increased by one as marker for assigned.
t = r*r;
Error using sudoku_domains (line 23)
Not enough input arguments.

Generate data matrix (TF for whole domain). This matrix can be interpreted as original table, where for each possible value is created a page containing 1/0 based on whether such value can be assigned to that position in current configuration.

domains = ones(r,r,r);
for n = 1:r
    % All occurences of value n within original assignment.
    [idsRow, idsCol]= find(A == n);

    % Remove possibility from all influenced variables.
    for k = 1:length(idsRow)
        domains(idsRow(k), idsCol(k), :) = 0;

        row = idsRow(k);
        col = idsCol(k);

        rel = C(:, row, col);
        ser = domains(:, :, n);
        posG = rel(logical(ser(rel)))+(n-1)*t;

        domains(posG) = 0;

    end
end

% Domain sizes based on all available values, positions with assignment
% have domain size above max.
domainSizes = (A==0).*sum(domains,3) + (A>0)*lr;
end