note description: "Collections with membership multilicity" author: "JSO, Jackie Wang" date: "$Date$" revision: "$Revision$" class interface BAG [G -> attached ANY] create make_empty make_from_array (a: ARRAY [G]) ensure # Current = a.count make_from_tuple_array (a: ARRAY [TUPLE [g: G; i: INTEGER_32]]) require across a as cur all cur.item.i > 0 end feature -- Access generating_type: TYPE [detachable BAG [G]] -- Type of current object -- (type of which it is a direct instance) -- (from ANY) ensure -- from ANY generating_type_not_void: Result /= Void generator: STRING_8 -- Name of current object's generating class -- (base class of the type of which it is a direct instance) -- (from ANY) ensure -- from ANY generator_not_void: Result /= Void generator_not_empty: not Result.is_empty feature -- Comparison frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void -- or attached to isomorphic object structures? -- (from ANY) ensure -- from ANY instance_free: class shallow_implies_deep: standard_equal (a, b) implies Result both_or_none_void: (a = Void) implies (Result = (b = Void)) same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b)) symmetric: Result implies deep_equal (b, a) frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached -- to objects considered equal? -- (from ANY) ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b)) frozen is_deep_equal (other: BAG [G]): BOOLEAN -- Are Current and other attached to isomorphic object structures? -- (from ANY) require -- from ANY other_not_void: other /= Void ensure -- from ANY shallow_implies_deep: standard_is_equal (other) implies Result same_type: Result implies same_type (other) symmetric: Result implies other.is_deep_equal (Current) frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached to -- field-by-field identical objects of the same type? -- Always uses default object comparison criterion. -- (from ANY) ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b)) frozen standard_is_equal (other: BAG [G]): BOOLEAN -- Is other attached to an object of the same type -- as current object, and field-by-field identical to it? -- (from ANY) require -- from ANY other_not_void: other /= Void ensure -- from ANY same_type: Result implies same_type (other) symmetric: Result implies other.standard_is_equal (Current) feature -- Status report conforms_to (other: ANY): BOOLEAN -- Does type of current object conform to type -- of other (as per Eiffel: The Language, chapter 13)? -- (from ANY) require -- from ANY other_not_void: other /= Void same_type (other: ANY): BOOLEAN -- Is type of current object identical to type of other? -- (from ANY) require -- from ANY other_not_void: other /= Void ensure -- from ANY definition: Result = (conforms_to (other) and other.conforms_to (Current)) feature -- Conversion as_array: ARRAY [G] -- Return current bag as an array of items. ensure current_bag_unchanged: Current ~ old Current.deep_twin same_count: Result.count = # Current as_function: FUN [G, INTEGER_32] -- Return current bag as a function from items -- to number of occurrences. ensure all_bag_items_covered: across Current as cur all Current [cur.item.first] = Result [cur.item.first] end only_bag_items_covered: across Result as cur all Current [cur.item.first] = Result [cur.item.first] end as_set: SET [G] -- Return the domain of curret bag ensure Result ~ as_function.domain feature -- Duplication copy (other: BAG [G]) -- Update current object using fields of object attached -- to other, so as to yield equal objects. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) ensure -- from ANY is_equal: Current ~ other frozen deep_copy (other: BAG [G]) -- Effect equivalent to that of: -- copy (other . deep_twin) -- (from ANY) require -- from ANY other_not_void: other /= Void ensure -- from ANY deep_equal: deep_equal (Current, other) frozen deep_twin: BAG [G] -- New object structure recursively duplicated from Current. -- (from ANY) ensure -- from ANY deep_twin_not_void: Result /= Void deep_equal: deep_equal (Current, Result) frozen standard_copy (other: BAG [G]) -- Copy every field of other onto corresponding field -- of current object. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) ensure -- from ANY is_standard_equal: standard_is_equal (other) frozen standard_twin: BAG [G] -- New object field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) ensure -- from ANY standard_twin_not_void: Result /= Void equal: standard_equal (Result, Current) frozen twin: BAG [G] -- New object equal to Current -- twin calls copy; to change copying/twinning semantics, redefine copy. -- (from ANY) ensure -- from ANY twin_not_void: Result /= Void is_equal: Result ~ Current feature -- Basic operations frozen default: detachable BAG [G] -- Default value of object's type -- (from ANY) frozen default_pointer: POINTER -- Default value of type POINTER -- (Avoid the need to write p.default for -- some p of type POINTER.) -- (from ANY) ensure -- from ANY instance_free: class default_rescue -- Process exception for routines with no Rescue clause. -- (Default: do nothing.) -- (from ANY) frozen do_nothing -- Execute a null action. -- (from ANY) ensure -- from ANY instance_free: class feature -- Commands difference (other: like Current) -- Subtract 'other' from current bag. require other |<: Current ensure Current ~ old Current.deep_twin.differenced (other) extend (g: G; i: INTEGER_32) -- Add 'g' of quantity 'i' into current bag. ensure Current ~ old Current.deep_twin.extended (g, i) extend_by (g: G) -- Add 'g' of quantity 1 into current bag. ensure Current ~ old Current.deep_twin.extended_by (g) override (i: INTEGER_32; g: G) -- Change quantity of 'g' to 'i' in current bag ensure Current ~ old Current.deep_twin.overriden (i, g) subtract (g: G; i: INTEGER_32) -- Subtract 'g' of quantity 'i' from current bag. require Current [g] >= i ensure Current ~ old Current.deep_twin.subtracted (g, i) subtract_by (g: G) -- Subtract 'g' of quantity 1 from current bag. require Current [g] >= 1 ensure Current ~ old Current.deep_twin.subtracted_by (g) union (other: like Current) -- Add 'other' into current bag. ensure Current ~ old Current.deep_twin.unioned (other) feature -- Constructor make_empty make_from_array (a: ARRAY [G]) ensure # Current = a.count make_from_tuple_array (a: ARRAY [TUPLE [g: G; i: INTEGER_32]]) require across a as cur all cur.item.i > 0 end feature -- Equality is_equal (other: like Current): BOOLEAN -- Is other attached to an object considered -- equal to current object? require -- from ANY other_not_void: other /= Void ensure -- from ANY symmetric: Result implies other ~ Current consistent: standard_is_equal (other) implies Result ensure then Result = (Current |<: other and other |<: Current) Result = (as_function ~ other.as_function) feature -- Iteration cursor new_cursor: ITERATION_CURSOR [PAIR [G, INTEGER_32]] -- Fresh cursor associated with current structure ensure -- from ITERABLE result_attached: Result /= Void feature -- Output debug_output: STRING_8 -- Return a string representation of current bag -- for debugging require -- from DEBUG_OUTPUT True ensure -- from DEBUG_OUTPUT result_not_void: Result /= Void ensure then Result ~ out Io: STD_FILES -- Handle to standard file setup -- (from ANY) ensure -- from ANY instance_free: class io_not_void: Result /= Void out: STRING_8 -- Return a string representation of current bag require -- from ANY True ensure -- from ANY out_not_void: Result /= Void ensure then Result ~ as_function.out print (o: detachable ANY) -- Write terse external representation of o -- on standard output. -- (from ANY) ensure -- from ANY instance_free: class frozen tagged_out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) ensure -- from ANY tagged_out_not_void: Result /= Void feature -- Platform Operating_environment: OPERATING_ENVIRONMENT -- Objects available from the operating system -- (from ANY) ensure -- from ANY instance_free: class operating_environment_not_void: Result /= Void feature -- Quantifiers comprehension alias "|" (exp: PREDICATE [PAIR [G, INTEGER_32]]): like Current -- Largest subset of the current fun whose elements satisfy exp ensure current_bag_unchanged: Current ~ old Current.deep_twin is_subset: Result |<: Current all_satisfying_exp: Result.hold_count (exp) = # Result.as_set consistent_satisfying_items: Current.hold_count (exp) = # Result.as_set hold_count (exp: PREDICATE [PAIR [G, INTEGER_32]]): INTEGER_32 -- How many items satisfying exp are in Current? ensure current_bag_unchanged: Current ~ old Current.deep_twin maximum_result: 0 <= Result and Result <= count 0 <= Result and Result <= # as_function.domain feature -- Specification Queries differenced alias "|\" (other: like Current): like Current -- Return a representation of current bag with -- 'other' subtracted. require other |<: Current ensure current_bag_unchanged: Current ~ old Current.deep_twin across Result as cur all (other.has (cur.item.first) implies Result [cur.item.first] = Current [cur.item.first] - other [cur.item.first]) and (not other.has (cur.item.first) implies Result [cur.item.first] = Current [cur.item.first]) end extended (g: G; i: INTEGER_32): like Current -- Return a representation of current bag with -- 'g' of quantity 'i' added. ensure current_bag_unchanged: Current ~ old Current.deep_twin across Result as cur all (cur.item.first ~ g implies Result [cur.item.first] = Current [cur.item.first] + i) and (cur.item.first /~ g implies Result [cur.item.first] = Current [cur.item.first]) end extended_by alias "+" (g: G): like Current -- Return a representation of current bag with -- 'g' of quantity 1 added. ensure current_bag_unchanged: Current ~ old Current.deep_twin across Result as cur all (cur.item.first ~ g implies Result [cur.item.first] = Current [cur.item.first] + 1) and (cur.item.first /~ g implies Result [cur.item.first] = Current [cur.item.first]) end overriden (i: INTEGER_32; g: G): like Current -- Return a representation of current bag with -- quantity of 'g' to 'i' ensure current_bag_unchanged: Current ~ old Current.deep_twin across Result as cur all (cur.item.first ~ g implies Result [cur.item.first] = i) and (cur.item.first /~ g implies Result [cur.item.first] = Current [cur.item.first]) end subtracted (g: G; i: INTEGER_32): like Current -- Return a representation of current bag with -- 'g' of quantity 'i' subtracted. require Current [g] >= i ensure current_bag_unchanged: Current ~ old Current.deep_twin across Result as cur all (cur.item.first ~ g implies Result [cur.item.first] = Current [cur.item.first] - i) and (cur.item.first /~ g implies Result [cur.item.first] = Current [cur.item.first]) end subtracted_by alias "-" (g: G): like Current -- Return a representation of current bag with -- 'g' of quantity 1 subtracted. require Current [g] >= 1 ensure current_bag_unchanged: Current ~ old Current.deep_twin across Result as cur all (cur.item.first ~ g implies Result [cur.item.first] = Current [cur.item.first] - 1) and (cur.item.first /~ g implies Result [cur.item.first] = Current [cur.item.first]) end unioned alias "|\/|" (other: like Current): like Current -- Return a representation of current bag with -- 'other' added. ensure current_bag_unchanged: Current ~ old Current.deep_twin across Result as cur all (other.has (cur.item.first) implies Result [cur.item.first] = Current [cur.item.first] + other [cur.item.first]) and (not other.has (cur.item.first) implies Result [cur.item.first] = Current [cur.item.first]) end feature -- Status queries count alias "#": INTEGER_32 ensure Result = as_array.count has (g: G): BOOLEAN ensure Result = as_array.has (g) Result = as_function.domain.has (g) Result = (Current [g] > 0) is_empty: BOOLEAN ensure Result = (# Current = 0) is_subbag_of alias "|<:" (other: like Current): BOOLEAN ensure Result = across as_function as cur all cur.item.second <= other [cur.item.first] end occurrences alias "[]" (g: G): INTEGER_32 assign override -- Number of 'g' in current bag. ensure non_negative_occurrences: Result >= 0 case_of_nonmember: not has (g) implies Result = 0 case_of_member: has (g) implies Result > 0 and Result = as_function [g] invariant bag_not_containing_zero_occurrence_item: across Current as pair all pair.item.second > 0 end -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) end -- class BAG
Generated by ISE EiffelStudio