function T = factortemplate(varargin)
% FACTORTEMPLATE Template for a repeated/shared factor
%
% T = FACTORTEMPLATE(L) where L is a variable cardinality vector
%
% T = FACTORTEMPLATE Creates an empty/default object
%
% T = FACTORTEMPLATE(A)  where A is a factortemplate object will
% return a copy of A
  
% Fields
%  card
%  id
    
% Jerod Weinman
% jerod@acm.org
    
% $Id: factortemplate.m 1 2009-05-07 13:41:00Z weinman $
 
% Jerod Weinman
% jerod@acm.org
% Copyright 2008, 2009, 2011
 
%    This file is part of MatLibFactorGraph.
%
%    MatLibFactorGraph is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    MatLibFactorGraph is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with MatLibFactorGraph.  If not, see <http://www.gnu.org/licenses/>.
    
    
  % NOTE: Matlab, being an interpreted sort of language requires you
  % to implement your own == (eq) operator for objects. Thus, we are
  % forced to come up with a hack for object reference equality. Our
  % hack is to use the "now" function to come up with some sort of
  % unique id. It's certainly not guaranteed to be portable across
  % systems, but the probability of conflicts is exceedingly small.
	
  if nargin == 0
	
	T.card = [];
	T.id = now;
	
	T = class(T,'factortemplate');
	
  elseif nargin==1 && isa(varargin{1},'factortemplate')
	
	T = varargin{1};
	
  else
	
	L = varargin{1};
	
	if ~isnumeric(L) || ndims(L)>2
	  error('L must be a numeric vector');
	end;
	
	if size(L,2)==1
	  L = L';
	elseif size(L,1)~=1
	  % Neither dimension is size 1
	  error('D must be a vector');
	end;
	
	T.card = L; % row vector
	T.id = now;
	
	T = class(T,'factortemplate');
  end;
  
  
