% INDEX index into a 'ltpda_uoh' object array or matrix. This properly captures the history.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: INDEX into an 'ltpda_uoh' object array or matrix.
%              This properly captures the history.
%
% CALL:        b = index(a, i)
%              b = index(a, i, j)
%              b = a.index(plist('I', i))
%              b = a.index(plist('I', i, 'J', j))
%
% <a href="matlab:utils.helper.displayMethodInfo('ltpda_uoh', 'index')">Parameters Description</a>
%
% VERSION:     $Id: index.m,v 1.13 2011/04/08 08:56:30 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function varargout = index(varargin)

  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end

  if nargout == 0
    error('### index cannot be used as a modifier. Please give an output variable.');
  end

  import utils.const.*
  utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);

  % We can only effectively handle a single matrix of 'ltpda_uoh' objects
  af = 0;
  for j=1:nargin
    if isa(varargin{j}, 'ltpda_uoh')
      af = af + 1;
    end
  end
  if af > 1
    error('### Input a single matrix of ''ltpda_uoh'' objects for indexing.');
  end

  % Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end

  % Collect all 'ltpda_uoh' objects and plists
  [aobjs, obj_invars] = utils.helper.collect_objects(varargin(:), 'ltpda_uoh', in_names);
  [pl, pl_invars, rest] = utils.helper.collect_objects(varargin(:), 'plist', in_names);

  % Decide on a deep copy or a modify
  bobjs = copy(aobjs, nargout);

  % Combine plists
  pl = combine(pl, getDefaultPlist);

  % Get indices
  idxi  = find(pl, 'i');
  idxj  = find(pl, 'j');
  if isempty(idxi) || isempty(idxj)
    % go through the other inputs
    for j=1:numel(rest)
      if isnumeric(rest{j}) && isempty(idxi)
        idxi = rest{j};
      elseif isnumeric(rest{j}) && isempty(idxj)
        idxj = rest{j};
      end
    end
  end

  if isempty(idxi) && isempty(idxj)
    error('### Please provide an index of index pair.');
  end

  % Gather the input histories
  inhists = [];
  for j=1:numel(bobjs)
    inhists = [inhists bobjs(j).hist];
  end

  % Now index with either (i) or (i,j)
  if isempty(idxj)
    aout = bobjs(idxi);
    inhists = inhists(idxi);
  else
    aout = bobjs(idxi, idxj);
    inhists = inhists(sub2ind(size(bobjs), idxi, idxj));
  end

  % Make sure we store the indices
  pl = pl.pset('i', idxi);
  if ~isempty(idxj)
    pl = pl.pset('j', idxj);
  end

  % Name and add history to all outputs
  for j=1:numel(aout)
    if isempty(idxj)
%      aout(j).name = sprintf('(%s)(%d)', obj_invars{idxi(j)}, idxi(j));
      aout(j).addHistory(getInfo('None'), pl, obj_invars(idxi), inhists(j));
    else
%      aout(j).name = sprintf('%s', obj_invars{sub2ind(size(aobjs), idxi,idxj)});
      aout(j).addHistory(getInfo('None'), pl, obj_invars(sub2ind(size(aobjs), idxi,idxj)), inhists(j));
    end
  end

  % Set output
  varargout{1} = aout;
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'ltpda_uoh', 'ltpda', utils.const.categories.helper, '$Id: index.m,v 1.13 2011/04/08 08:56:30 hewitson Exp $', sets, pl);
  ii.setModifier(false);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function plout = getDefaultPlist()
  persistent pl;  
  if exist('pl', 'var')==0 || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function pl = buildplist()
  pl = plist('I',  [], 'J', []);
end

