%% y = next_set (x, n) - Subset of {1..n} after x 
%%
%% Return the subset of {1..n} after x, when subsets are ordered first by
%% size, then by lexicographical order.
%%
%% INPUT:
%% x : vector or matrix representing a subset of {1..n}.
%% n : n upper bound for elements of x.
%%
%% OUTPUT:
%% y : vector or matrix representing the subset after x for the
%%     lexicographical order.
%%
%% Examples: next_set([],10) == 1; next_set(1:3,10) == [1,2,4]; 
%% next_set(8:10,10) == 1:4.
%%
%% See also : next_base()

%% Author : Etienne Grossmann <etienne@cs.uky.edu>

function x = next_set (x, n)

  x = create_set (x);
  
  if !(m = length (x))		% 1st set after empty set is {1}

    x = 1;

				% Grow set if x is the last (for
				% lexographical order) subset of size m.
  elseif all (x == [n - m + 1:n])

    if m<n
      x = 1:m + 1;		% Grow
    else
      x = [];			% Overflow behavior : go back to empty set.
    end

  else				% Next set in lex order

    i = 0;
    while x(m - i) == n - i
      i++;
    endwhile;
    x(m - i:m) = x(m - i) + [1:i + 1];

  endif;
endfunction

