------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 1995 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>  <--------------

abstract class $RO_BAG{ETP} < $CONTAINER{ETP}
-- An unordered container in which the elements need not be unique
-- 
-- Definition: A bag is an arbitrary unordered collection, with
-- extra accessing functionality
-- 
-- This is a read-only abstraction in which no modifying operations
-- are permitted.  For documentation see:
--    The overall organization of container classes
--    The organization and usage of bags and related classes
-- 
is

   add(e:ETP):$RO_BAG{ETP};
   -- Result is a new bag containing all the elements of self and 'e'
   
   delete(e:ETP):$RO_BAG{ETP};
   -- Result is a new bag containing all the elements of self except for
   -- an element equal to 'e', if one exists. If more than one element
   -- is equal to 'e', delete only one of them
   
   delete_all(e:ETP):$RO_BAG{ETP};
   -- Result is a new bag containing all the elements of self except for
   -- any elements equal to 'e'
         
   count(e:ETP):INT;
   -- Return the number of occurences of 'e' in self
   
   unique!:ETP;
   -- Yield the unique elements of self. Equivalent to self.as_set.elt!
   
   n_unique: INT;
   -- Returns the number of unique elements in the bag 
   -- 
   -- result = number of unique elements
   
   is_subset_of(arg: $RO_BAG{ETP}): BOOL;
   -- Returns true if 'self' is a subset of 'arg'. For elements that occur
   -- multiple times, the number of occurences of the element in 'arg'
   -- must be greater than or equal to the number of occurences in self
   -- 
   -- result=true iff for all e in self: count(e) <= arg.count(e)

   concat(arg:$ELT{ETP}): $RO_BAG{ETP};
   -- Returns a bag containing all the elements of self and 'arg'.
   -- For elements that occur multiple times, the result contains
   -- the sum of the number of occurences in self and 'arg'
   -- 
   -- result=bag of all e s.t. result.count(e)=self.count(e)+arg.count(e) > 0 
   
   union(arg:$RO_BAG{ETP}): $RO_BAG{ETP};
   -- Returns a bag containing the elements of 'self' and 'arg'. 
   -- For elements that occur multiple times, the result contains
   -- the maximum number of occurences in either self or 'arg'
   -- This definition permits the union of sets to be consistent
   -- with the union of bags.
   -- 
   -- result=bag of all e s.t. 
   --             result.count(e)= max(self.count(e),arg.count(e)) > 0
   
   intersection(arg:$RO_BAG{ETP}):$RO_BAG{ETP};
   -- Returns a bag containing the elements common to self and 'arg'
   -- For elements that occur multiple times, the result contains
   -- the minimum number of occurrences in either self or 'arg'
   -- 
   -- result=bag of all e s.t. 
   --              result.count(e)=min(self.count(e),arg.count(e)) > 0 

   is_empty:BOOL;
   -- Returns true if the size of the container = 0

   size: INT;
   -- Number of elements contained
   
   copy: SAME;
    -- Return a copy of the current container

   has(e: ETP): BOOL;
   -- True if the container contains the element "e"
   
   equals(c:$RO_BAG{ETP}):BOOL;
   -- Return true if both containers contain the same elements with
   -- the same number of repetitions, irrespective of the order of the
   -- elements
   
   as_array:ARRAY{ETP};
   -- Return the elements of the container in an array

   elt!:ETP;
   -- Yield all the elements of self. The order is not defined.
   
   str:STR;
   -- Yield a string version of self
   
end;
--------------------------------------------------------------------------
abstract class $VBAG{ETP} < $RO_BAG{ETP},$HASH 
-- An unordered container in which the elements are not unique.
-- Corresponds to a mathematical bag.
-- 
-- IMPORTANT: This is a value abstraction and is stricter than the
-- read-only abstraction. Subtypes must not support *any*
-- operations that modify self. The language cannot enforce this
-- restriction. 
-- 
-- For pointers to other documentation please see the class comment in
-- the read-only abstraction $RO_BAG{ETP}
-- 
is


   add(e:ETP):$VBAG{ETP};
   -- Result is a new bag containing all the elements of self and 'e'

   delete(e:ETP):$VBAG{ETP};
   -- Result is a new bag containing all the elements of self except for
   -- an element equal to 'e', if one exists. If more than one element
   -- is equal to 'e', delete only one of them
   
   delete_all(e:ETP):$VBAG{ETP};
   -- Result is a new bag containing all the elements of self except for
   -- any elements equal to 'e'
   
   concat(arg:$ELT{ETP}): $VBAG{ETP};
   -- Returns a bag containing all the elements of self and 'arg'.
   -- For elements that occur multiple times, the result contains
   -- the sum of the number of occurences in self and 'arg'
   -- 
   -- result=bag of all e s.t. result.count(e)=self.count(e)+arg.count(e) > 0 
   
   union(arg: $RO_BAG{ETP}): $VBAG{ETP};
   -- Returns a bag containing the elements of 'self' and 'arg'. 
   -- For elements that occur multiple times, the result contains
   -- the maximum number of occurences in either self or 'arg'
   -- This definition permits the union of sets to be consistent
   -- with the union of bags.
   -- 
   -- result=bag of all e s.t. 
   --             result.count(e)= max(self.count(e),arg.count(e)) > 0
   
   intersection(arg: $RO_BAG{ETP}):$VBAG{ETP};
   -- Returns a bag containing the elements common to self and 'arg'
   -- For elements that occur multiple times, the result contains
   -- the minimum number of occurrences in either self or 'arg'
   -- 
   -- result=bag of all e s.t. 
   --              result.count(e)=min(self.count(e),arg.count(e)) > 0 

   count(e:ETP):INT;
   -- Return the number of occurences of 'e' in self
   
   unique!:ETP;
   -- Yield the unique elements of self. Equivalent to self.as_set.elt!

   n_unique: INT;
   -- Returns the number of unique elements in the bag 
   -- 
   -- result = number of unique elements

   is_subset_of(arg: $RO_BAG{ETP}): BOOL;
   -- Returns true if 'self' is a subset of 'arg'. For elements that occur
   -- multiple times, the number of occurences of the element in 'arg'
   -- must be greater than or equal to the number of occurences in self
   -- 
   -- result=true iff for all e in self: count(e) <= arg.count(e)

   is_empty:BOOL;
   -- Returns true if the size of the container = 0

   size: INT;
   -- Number of elements contained
   
   copy: SAME;
    -- Return a copy of the current container

   has(e: ETP): BOOL;
   -- True if the container contains the element "e"

   equals(c:$RO_BAG{ETP}):BOOL;
   -- Return true if both containers contain the same elements with
   -- the same number of repetitions, irrespective of the order of the
   -- elements
   
   as_array:ARRAY{ETP};
   -- Return the elements of the container in an array

   elt!:ETP;
   -- Yield all the elements of self. The order is not defined.
   
   str:STR;
   -- Yield a string version of self
   
end;
--------------------------------------------------------------------------
abstract class $BAG{ETP} < $RO_BAG{ETP}, $VAR 
-- An unordered container in which the elements are not unique.  
-- 
-- This is a reference abstraction and supports operations that modify
-- self. Instances of subtypes may be viewed as variables with a value
-- of $VBAG{ETP}
-- 
-- For pointers to other documentation please see the class comment in
-- the read-only abstraction $RO_BAG
-- 
is

   as_value:$VBAG{ETP};
   -- Return the current value associated with self
   
   add(e:ETP);
   -- Add the element 'e' to self
   -- self <- initial(self).add(e)
    
   delete(e:ETP);
   -- Delete at most one occurance of 'e' from self
   -- self <- initial(self).delete(e)

   delete_all(e:ETP);
   -- Delete all occurences of 'e' from self
   -- self <- initial(self).delete(e)
   
   clear;
   -- Delete all elements of self. post result.size = 0
   
   to_concat(arg:$RO_BAG{ETP});
   -- Concatenate the elemetns of 'arg' to this bag
   -- self <- initial(self).add_bag(arg)
   
   to_union(arg: $RO_BAG{ETP});
   -- Turn this bag into the union of self and 'arg'
   -- self <- initial(self).union(arg)
   
   to_intersection(arg:$RO_BAG{ETP});
   -- Turn this bag into the intersection of self and 'arg'
   -- self <- initial(self).intersection(arg)
   
   add(e:ETP):$BAG{ETP};
   -- Result is a new bag containing all the elements of self and 'e'

   delete(e:ETP):$BAG{ETP};
   -- Result is a new bag containing all the elements of self except for
   -- an element equal to 'e', if one exists. If more than one element
   -- is equal to 'e', delete only one of them
   
   delete_all(e:ETP):$BAG{ETP};
   -- Result is a new bag containing all the elements of self except for
   -- any elements equal to 'e'

   count(e:ETP):INT;
   -- Return the number of occurences of 'e' in self
   
   unique!:ETP;
   -- Yield the unique elements of self. Equivalent to self.as_set.elt!

   n_unique: INT;
   -- Returns the number of unique elements in the bag 
   -- 
   -- result = number of unique elements

   is_subset_of(arg: $RO_BAG{ETP}): BOOL;
   -- Returns true if 'self' is a subset of 'arg'. For elements that occur
   -- multiple times, the number of occurences of the element in 'arg'
   -- must be greater than or equal to the number of occurences in self
   -- 
   -- result=true iff for all e in self: count(e) <= arg.count(e)

   concat(arg:$ELT{ETP}): $RO_BAG{ETP};
   -- Returns a bag containing all the elements of self and 'arg'.
   -- For elements that occur multiple times, the result contains
   -- the sum of the number of occurences in self and 'arg'
   -- 
   -- result=bag of all e s.t. result.count(e)=self.count(e)+arg.count(e) > 0
 
   union(arg: $RO_BAG{ETP}): $RO_BAG{ETP};
   -- Returns a bag containing the elements of 'self' and 'arg'. 
   -- For elements that occur multiple times, the result contains
   -- the maximum number of occurences in either self or 'arg'
   -- This definition permits the union of sets to be consistent
   -- with the union of bags.
   -- 
   -- result=bag of all e s.t. 
   --         result.count(e)=max(self.count(e),arg.count(e)) > 0
   
   intersection(arg: $RO_BAG{ETP}):$RO_BAG{ETP};
   -- Returns a bag containing the elements common to self and 'arg'
   -- For elements that occur multiple times, the result contains
   -- the minimum number of occurrences in either self or 'arg'
   -- 
   -- result=bag of all e s.t. 
   --          result.count(e)=min(self.count(e),arg.count(e)) > 0
   
   is_empty:BOOL;
   -- Returns true if the size of the container = 0

   size: INT;
   -- Number of elements contained
   
   copy: SAME;
    -- Return a copy of the current container

   has(e: ETP): BOOL;
   -- True if the container contains the element "e"
   
   equals(c:$RO_BAG{ETP}):BOOL;
   -- Return true if both containers contain the same elements with
   -- the same number of repetitions, irrespective of the order of the
   -- elements
   
   as_array:ARRAY{ETP};
   -- Return the elements of the container in an array

   elt!:ETP;
   -- Yield all the elements of self. The order is not defined.
   
   str:STR;
   -- Yield a string version of self

end; -- $BAG{ETP}
--------------------------------------------------------------------------
