function [T] = addT(t1,t2,T)

%   splices a pair of times t1, t2 into an ordered list of similar
%   pairs defining non-overlapping segments
%   Usage: T = addT(t1,t2,T);
 
%   make sure t1,t2 are properly ordered
if(t1 > t2) 
  temp = t1;
  t1 = t2;
  t2 = temp;
end

if(isempty(T))
   T = [ t1 t2];
else
   %  if t1 falls in an already defined interval replace t1 with begining of 
   %   interval
   i1 = find((T(:,1) <=  t1)&(t1 <= T(:,2)));
   if(~isempty(i1))
      t1 = T(i1,1);
   end
   %  if t2 falls in an already defined interval replace t2 with end of 
   %   interval
   i2 = find((T(:,1) <=  t2)&(t2 <= T(:,2)));
   if(~isempty(i2))
      t2 = T(i2,2);
   end
   %  eliminate any pairs in list T that are completely covered by the
   %  interval (t1,t2)
   T = T(find(1-((t1 <= T(:,1))&(T(:,2) <= t2))),:);
   %  splice (t1,t2) in
   I = find(t1<T(:,1));
   if(isempty(I))
      T = [T; t1  t2];
   else
      i1 = min(I);
      T = [T(1:i1-1,:); t1 t2 ; T(i1:end,:)];
   end
end
