filters.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 
------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand        --
-- 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>  <--------------


abstract class $FILTERS{ETP}

abstract class $FILTERS{ETP} is -- This abstraction models a container for which quantifiers are -- provided and for which sequences of elements satisfying some particular -- predicate may be retrieved. -- Version 1.0 Feb 01. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 26 Feb 01 kh Original when formally specifying containers count_if( predicate : ROUT{ETP} : BOOL ) : CARD ; -- This routine returns the number of elements of self which satisfy -- the given predicate. exists( predicate : ROUT{ETP} : BOOL ) : BOOL ; -- This predicate returns true if and only if an element of self -- which satisfies the predicate test exists. exists( predicate : ROUT{ETP} : BOOL, out res : ETP ) : BOOL ; -- This routine returns true if and only if self has at least one -- element that satisfies the predicate. The out argument 'res' is set to -- have the element value concerned if true is returned. not_exists( predicate : ROUT{ETP} : BOOL ) : BOOL ; -- This predicate returns true if and only if no element of self -- satisfies the given predicate. every( predicate : ROUT{ETP} : BOOL ) : BOOL ; -- This predicate returns true if and only if every element of self -- satisfies the given predicate. not_every( predicate : ROUT{ETP} :BOOL ) : BOOL ; -- This predicate returns true if at least one element of self fails -- the given predicate. unique! : ETP ; -- This iter yields in turn all of the elements of the given container -- which are unique. filter!( once predicate : ROUT{ETP} : BOOL ) : ETP ; -- This iter yields in turn all of the elements which satisfy the given -- predicate. not_filter!( once predicate : ROUT{ETP} : BOOL ) : ETP ; -- This iter yields in turn all of the elements which do NOT satisfy -- the given predicate. end ; -- $FILTERS{ETP}

partial class ELT_FILTERS {ETP, CTP < $ELT{ETP}}

partial class ELT_FILTERS {ETP, CTP < $ELT{ETP}} is -- This partial class contains a standard set of container -- filtering/testing iters for any class which specifies an elt! iterator. -- Version 1.0 Oct 98. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 6 Oct 98 kh Original from Sather 1.2 dist. private const Default_Array_Size : CARD := 5 ; count_if( predicate : ROUT{ETP} : BOOL ) : CARD pre void(self) or ~void(predicate) post (result <= size) is -- This routine returns the number of elements of self which satisfy -- the given predicate. res : CARD := 0 ; loop if predicate.call(elt!) then res := res + 1 end end ; return res end ; exists( predicate : ROUT{ETP} : BOOL ) : BOOL pre void(self) or ~void(predicate) post true is -- This predicate returns true if and only if some element of self -- satisfies test. loop if predicate.call(elt!) then return true end end ; return false end ; exists( predicate : ROUT{ETP} : BOOL, out res : ETP ) : BOOL pre ~void(self) and ~void(predicate) post predicate.call(res) is -- This routine returns true if and only if self has at least one -- element that satisfies the predicate. The out argument 'res' is set to -- have the element value concerned if true is returned. loop loc_res : ETP := elt! ; if predicate.call(loc_res) then res := loc_res ; return true end end ; return false end ; not_exists( predicate : ROUT{ETP} : BOOL ) : BOOL pre void(self) or ~void(predicate) post true is -- This predicate returns true if and only if no element of self -- satisfies the given predicate. loop if predicate.call(elt!) then return false end end ; return true end ; every( predicate : ROUT{ETP} : BOOL ) : BOOL pre void(self) or ~void(predicate) post true is -- This predicate returns true if and only if every element of self -- satisfies the given predicate. loop if ~predicate.call(elt!) then return false end end ; return true end ; not_every( predicate : ROUT{ETP} :BOOL ) : BOOL pre void(self) or ~void(predicate) post true is -- This predicate returns true if at least one element of self fails -- the given predicate. loop if ~predicate.call(elt!) then return true end end ; return false end ; unique! : ETP pre ~void(self) post true is -- This iter yields in turn all of the elements of the given container -- which are unique. already_seen : ARRAY{ETP} := ARRAY{ETP}::create(Default_Array_Size) ; num_seen : CARD := 0 ; arr_size : CARD := Default_Array_Size ; loop elem : ETP := elt! ; is_seen : BOOL := false ; loop index : CARD := num_seen.times! ; if elt_eq(already_seen[index],elem) then is_seen := true end end ; if ~is_seen then if arr_size <= num_seen then -- amortized doubling arr_size := 2 * arr_size ; already_seen := already_seen.resize(arr_size) end ; already_seen[num_seen] := elem ; num_seen := num_seen + 1 ; yield elem end end end ; filter!( once predicate : ROUT{ETP} : BOOL ) : ETP pre ~void(self) and ~void(predicate) post predicate.call(result) is -- This iter yields in turn all of the elements which satisfy the given -- predicate. loop elem : ETP := elt! ; if predicate.call(elem) then yield elem end end end ; not_filter!( once predicate : ROUT{ETP} : BOOL ) : ETP pre ~void(self) and ~void(predicate) post ~predicate.call(result) is -- This iter yields in turn all of the elements which do NOT satisfy -- the given predicate. loop elem : ETP := elt! ; if ~predicate.call(elem) then yield elem end end end ; end ; -- ELT_FILTERS{ETP,CTP}

class ELT_FILTERS{ETP}

class ELT_FILTERS{ETP} is -- This partial class contains a standard set of container iters for -- any container class of elements ETP which specifies an elt! iterator. -- Version 1.0 Oct 98. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 6 Oct 98 kh Original from Sather 1.2 dist. include ELT_FILTERS{ETP,$ELT{ETP}} ; end ; -- ELT_FILTERS{ETP}