------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 199x by International Computer Science Institute            --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute  and/or modify it under the terms of the GNU Library General --
-- Public  License (LGPL)  as published  by the  Free  Software  Foundation; --
-- either version 2 of the license, or (at your option) any later version.   --
-- This  library  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 Doc/LGPL for more details.       --
-- The license text is also available from:  Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     --
-------------->  Please email comments to <bug-sather@gnu.org>  <--------------

class RO_MAP_ALG{ITP,TTP,MTP<$RO_MAP{ITP,TTP}} is
   
   test_if_target(c:MTP,test:ROUT{TTP}:BOOL,out ind:ITP,out target:TTP):BOOL is
      -- Return true if a target satisfies the prediate 'test'
      -- Arg "ind" is set to the index of the element satisfying "test"
      -- Arg "target" is set to the target satisfying "test"
      loop i ::= c.ind!; 
	 e ::= aget(r);
	 if test.call(e) then ind := i; target := e; return true end;  
      end; 
      return false;
   end;

   test_if_ind(c:MTP,test:ROUT{ITP}:BOOL,out ind:ITP,out target:TTP):BOOL is
      -- Return true if an index satisfies the prediate 'test'
      -- Arg "ind" is set to the index of the element satisfying "test"
      -- Arg "target" is set to the target satisfying "test"
      loop i ::= c.ind!; 
	 e ::= aget(r);
	 if test.call(i) then ind := i; target := e; return true end;  
      end; 
      return false;
   end;

end;
-------------------------------------------------------------------
class MAP_ALG{ITP,TTP,MTP<$MAP{ITP,TTP}} is
   
   replace_index(c:MTP,orig,replacement:ITP) is
      -- Replace any element with (orig,_) by (replacement,_) 
      loop e ::= elt!;
	 if ELT_EQ{ITP}::elt_eq(original,e.t1) then 
	    c.aset(replacement,e.t2); 
	 end; 
      end;
   end;

   replace_target(c:MTP,orig,replacement:TTP) is
      -- Replace any element with (_,orig) by (_,replacement) 
      loop  e::=c.elt!;
	 if ELT_EQ{TTP}::elt_eq(original,e.t2) then 
	    c.aset(e.t1,replacement); 
	 end; 
      end;
   end;

   replace_target_if_target(c:MTP,test:ROUT{TTP}:BOOL, replacement:TTP) is
      -- Replace any elements (i,t) such that test(t) is true
      -- by (i,replacement)
      loop e ::= c.elt!;
	 if test.call(e.t2) then c.aset(e.t1,replacement); end; 
      end;
   end;
   
   remap_targets(c:MTP,func:ROUT{TTP}:TTP) is
      -- Set each element (i,t) by (i,func(t))
      loop e ::= c.elt!;
	 c.aset(e.t1,func.call(e.t2));
      end;
   end;

end;
-------------------------------------------------------------------
