container.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
---------------------------> Sather 1.1 source file <--------------------------
-- container_alg.sa: Operations that work by inclusion on any container
-- Author: Benedict A. Gomes <gomes@tiramisu.ICSI.Berkeley.EDU>
-- Copyright (C) 1995, International Computer Science Institute
-- $Id: container.sa,v 1.3 1996/07/16 04:38:12 holger Exp $
--
-- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
-- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
-- LICENSE contained in the file: Sather/Doc/License of the
-- Sather distribution. The license is also available from ICSI,
-- 1947 Center St., Suite 600, Berkeley CA 94704, USA.
abstract class $CONTAINER{ETP} < $ELT{ETP}
abstract class $CONTAINER{ETP} < $ELT{ETP} is
-- The basic abstract container type
-- Does not have a create:SAME method, since this does not make sense
-- for arrays and other indexible types, where the create should
-- take SAME as an argument
-- Inherits: elt!, str
copy: SAME;
-- Return a copy of the current container
-- post result = self
size: INT;
-- Number of elements actually inserted
has(e: ETP): BOOL;
-- pre ~void(self)
-- True if the container contains the element "e"
end;
class CONTAINER_ALG{ETP,CTP<$CONTAINER{ETP}}
class CONTAINER_ALG{ETP,CTP<$CONTAINER{ETP}} is
filter!(c:CTP, once f:ROUT{ETP}:BOOL): ETP pre ~void(c) is
-- Yield all elements that satisfy the boolean predicate "f"
loop
e ::= c.elt!;
if f.call(e) then yield e end
end
end;
filter_not!(c:CTP, once f:ROUT{ETP}:BOOL): ETP pre ~void(c) is
-- Yield all elements that do not satisfy the boolean predicate "f"
loop e ::= c.elt!; if ~f.call(e) then yield e end end
end;
elt_if(c:CTP,test:ROUT{ETP}:BOOL,out res:ETP):BOOL is
-- Return true if the container has an element that satisfies the
-- predicate 'test'. The out argument 'res' is set to the
-- return value
loop r ::= c.elt!; if test.call(r) then res:=r; return true; end; end;
return false;
end;
count_if(c:CTP, test:ROUT{ETP}:BOOL):INT is
-- The number of elements which satisfy `test'.
r::=0;
loop if test.call(c.elt!) then r:=r+1 end end;
return r
end;
some(c:CTP, test:ROUT{ETP}:BOOL):BOOL is
-- True if some element of self satisfies `test'.
-- Self may be void.
loop if test.call(c.elt!) then return true end end;
return false
end;
every(c:CTP,test:ROUT{ETP}:BOOL):BOOL is
-- True if every element of self satisfies `test'.
-- Self may be void.
loop if ~test.call(c.elt!) then return false end end;
return true
end;
notany(c:CTP,test:ROUT{ETP}:BOOL):BOOL is
-- True if none of the elements of self satisfies `test'.
-- Self may be void.
loop if test.call(c.elt!) then return false end end;
return true
end;
notevery(c:CTP, test:ROUT{ETP}:BOOL):BOOL is
-- True if not every element of self satisfies `test'.
-- Self may be void.
loop if ~test.call(c.elt!) then return true end end;
return false
end;
end;