container.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 $CONTAINER{ETP} < $ELT{ETP}, $FILTERS{ETP}, $COPY, $STR

abstract class $CONTAINER{ETP} < $ELT{ETP}, $FILTERS{ETP}, $COPY, $STR is -- This abstraction is the basic abstract container type. It does -- not have a create : SAME routine, since this does not make sense -- for arrays and other indexable types, where the create should take SAME -- as an argument. -- Version 1.3 Dec 2000. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 11 Apr 96 bg Original -- 13 Jan 97 kh Changed to CARD from INT -- 6 Nov 98 kh Revised from 1.2 dist -- 5 Dec 00 kh Simplified inheritance is_empty : BOOL ; -- This predicate returns true if and only if there are no elements when -- size is zero. contains( elem : ETP ) : BOOL ; -- This feature returns true if and only if the element with the value -- elem is contained. end ; -- $CONTAINER{ETP}

partial class CONTAINER{ETP}

partial class CONTAINER{ETP} is -- This partial class provides some of the basic functionality of -- a general container object. -- Version 1.0 Nov 98. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 6 Nov 98 kh Original from 1.2 distribution. include COMPARE{ETP} ; include ELT_FILTERS{ETP} ; include CONTAINER_STR{ETP} ; stub elt! : ETP ; -- This iter yields all of the elements of self in a container specified -- order. is_empty : BOOL is -- This generic predicate returns true if and only if there are no -- elements in self. loop discard : ETP := elt! ; -- if this quits then empty! -- Note only runs once! return false end ; return true end ; count( elem : ETP ) : CARD pre true -- may be void! post true -- and the count is correct! is -- Thie routine returns the count of occurrences of the given element -- in self. Note that for certain containers a more efficient method may -- be possible. res : CARD := 0 ; loop loc_elem : ETP := elt! ; if elt_eq(elem,loc_elem) then res := res + 1 end end ; return res end ; contains( elem : ETP ) : BOOL pre ~void(self) post true is -- This predicate returns true if and only if the container has an -- element which is element equal to elem. This is a renaming of has above. -- More efficient algorithms may be possible for some containers (see count -- below). return count(elem) > 0 end ; size : CARD pre true -- may be void! post true -- and the result is the correct count! is -- This routine returns the total number of elements in self, using -- a loop which is an inefficient method for some containers - where the -- appropriate class should define a more efficient routine. res : CARD := 0 ; loop elem : ETP := elt! ; res := res + 1 end ; return res end ; array : ARRAY{ETP} pre ~void(self) post (result.size = size) is -- This routine returns all of the elements of self as an array. res : ARRAY{ETP} := ARRAY{ETP}::create(size) ; loop res.set!(elt!) end ; return res end ; end ; -- CONTAINER