function minidx = tjsr_getlivevertex(type, cellflag)
% idx = tjsr_getlivevertex(type, cellflag)
% This function belongs to tjsr!
% returns linear indices of vertices which are not inside the polytope and whose children are not all computed yet
% 
% Input:
%   type        the type struct from tjsr
%   cellflag    returns cell array of indices for each tree
%
% Written by tommsch, 2018

normidx=cell(1,type.counter.numordering);
minidx=cell(1,type.counter.numordering);
statusidx=cell(1,type.counter.numordering);
minlevel=inf;
for i=1:type.counter.numordering
    normidx{i} = type.cyclictree.norm{i}>1-type.opt.epslinprog;
    %get all indices where status==0 (no children yet)
    statusidx{i} = type.cyclictree.status{i}==0; 
    val=min(type.cyclictree.level{i}(statusidx{i} & normidx{i}));
    if(isempty(val)); val=inf; end;
    minlevel=min(minlevel,val);
end
if(isempty(minlevel)); minlevel=1; end;
vprintf('Minlevel: %i\n', minlevel,'imp',[3 type.opt.verbose])
for i=1:type.counter.numordering
    minidx{i} = normidx{i} & type.cyclictree.level{i}>=minlevel;
end
if(nargin==1 || ~cellflag)
    minidx=[minidx{:}];
end

end

function idx = tjsr_getlivevertex_old(type, cellflag) %#ok<*DEFNU>
% idx = tjsr_getlivevertex(type, cellflag)
% This function belongs to tjsr!
% returns linear indices of vertices which are not inside the polytope and whose children are not all computed yet
% 
% Input:
%   type        the type struct from tjsr
%   cellflag    returns cell array of indices for each tree
%
% Written by tommsch, 2018

idx=cell(1,type.counter.numordering);
for i=1:type.counter.numordering
    normidx = type.cyclictree.norm{i}>1-type.opt.epslinprog;
    nanidx = isnan(type.cyclictree.norm{i});
    
    %get all indices where status==0 and norm is computed 
    statusidx = type.cyclictree.status{i}==0; 
    
    %get all indices where status==0 but norm is not yet computed - we need to get norm from its parent
    val = type.cyclictree.parent{i}(nanidx);
    parentidx=false(size(normidx));
    parentidx(val)=true;
    
    idx{i} = normidx & (parentidx | statusidx);
end
if(nargin==1 || ~cellflag)
    idx=[idx{:}];
end

end

function dummy; end %#ok<DEFNU> %Generates an error, if the 'end' of a function is missing. 