note description: "Collections with membership multilicity" author: "JSO, Jackie Wang" date: "$Date$" revision: "$Revision$" class BAG [G -> attached ANY] create make_empty, make_from_array, make_from_tuple_array feature {NONE} -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) -- (from ANY) do end feature -- Access generating_type: TYPE [detachable BAG [G]] -- Type of current object -- (type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generating_type_not_void: Result /= Void end generator: STRING_8 -- Name of current object's generating class -- (base class of the type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generator_not_void: Result /= Void generator_not_empty: not Result.is_empty end 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) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_deep_equal (b) end 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) end 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) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_equal (b) end 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)) end 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 external "built_in" 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) end 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) do if a = Void then Result := b = Void else Result := b /= Void and then a.standard_is_equal (b) end 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)) end 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 external "built_in" ensure -- from ANY same_type: Result implies same_type (other) symmetric: Result implies other.standard_is_equal (Current) end 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 external "built_in" end 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 external "built_in" ensure -- from ANY definition: Result = (conforms_to (other) and other.conforms_to (Current)) end feature -- Conversion as_array: ARRAY [G] -- Return current bag as an array of items. local i: INTEGER_32 do create Result.make_empty across imp as map loop from i := 1 until i > map.item.second loop Result.force (map.item.first, Result.upper + 1) i := i + 1 end end ensure current_bag_unchanged: Current ~ old Current.deep_twin same_count: Result.count = # Current end as_function: FUN [G, INTEGER_32] -- Return current bag as a function from items -- to number of occurrences. local pair: PAIR [G, INTEGER_32] do create Result.make_empty across imp as cur loop create pair.make (cur.item.first, cur.item.second) Result.extend (pair) end 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 end as_set: SET [G] -- Return the domain of curret bag do create Result.make_empty across imp as cur loop Result.extend (cur.item.first) end ensure Result ~ as_function.domain end feature -- Duplication frozen clone (other: detachable ANY): like other obsolete "Use `twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- equal to other -- -- For non-void other, clone calls copy; -- to change copying/cloning semantics, redefine copy. -- (from ANY) do if other /= Void then Result := other.twin end ensure -- from ANY instance_free: class equal: Result ~ other end 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) external "built_in" ensure -- from ANY is_equal: Current ~ other end frozen deep_clone (other: detachable ANY): like other obsolete "Use `deep_twin' instead. [2017-05-31]" -- Void if other is void: otherwise, new object structure -- recursively duplicated from the one attached to other -- (from ANY) do if other /= Void then Result := other.deep_twin end ensure -- from ANY instance_free: class deep_equal: deep_equal (other, Result) end 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 do copy (other.deep_twin) ensure -- from ANY deep_equal: deep_equal (Current, other) end frozen deep_twin: BAG [G] -- New object structure recursively duplicated from Current. -- (from ANY) external "built_in" ensure -- from ANY deep_twin_not_void: Result /= Void deep_equal: deep_equal (Current, Result) end frozen standard_clone (other: detachable ANY): like other obsolete "Use `standard_twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) do if other /= Void then Result := other.standard_twin end ensure -- from ANY instance_free: class equal: standard_equal (Result, other) end 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) external "built_in" ensure -- from ANY is_standard_equal: standard_is_equal (other) end frozen standard_twin: BAG [G] -- New object field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) external "built_in" ensure -- from ANY standard_twin_not_void: Result /= Void equal: standard_equal (Result, Current) end frozen twin: BAG [G] -- New object equal to Current -- twin calls copy; to change copying/twinning semantics, redefine copy. -- (from ANY) external "built_in" ensure -- from ANY twin_not_void: Result /= Void is_equal: Result ~ Current end feature -- Basic operations frozen as_attached: attached BAG [G] obsolete "Remove calls to this feature. [2017-05-31]" -- Attached version of Current. -- (Can be used during transitional period to convert -- non-void-safe classes to void-safe ones.) -- (from ANY) do Result := Current end frozen default: detachable BAG [G] -- Default value of object's type -- (from ANY) do end frozen default_pointer: POINTER -- Default value of type POINTER -- (Avoid the need to write p.default for -- some p of type POINTER.) -- (from ANY) do ensure -- from ANY instance_free: class end default_rescue -- Process exception for routines with no Rescue clause. -- (Default: do nothing.) -- (from ANY) do end frozen do_nothing -- Execute a null action. -- (from ANY) do ensure -- from ANY instance_free: class end feature {BAG} -- Implementation imp: FUN [G, INTEGER_32] set_imp (new_imp: like imp) do imp := new_imp ensure imp = new_imp end feature -- Commands difference (other: like Current) -- Subtract 'other' from current bag. require other |<: Current do across other.imp as cur loop subtract (cur.item.first, cur.item.second) end ensure Current ~ old Current.deep_twin.differenced (other) end extend (g: G; i: INTEGER_32) -- Add 'g' of quantity 'i' into current bag. local new_count: INTEGER_32 do if imp.domain.has (g) then new_count := imp [g] + i else new_count := i end imp.override (create {FUN [G, INTEGER_32]}.make_from_array (<<[g, new_count]>>)) ensure Current ~ old Current.deep_twin.extended (g, i) end extend_by (g: G) -- Add 'g' of quantity 1 into current bag. do extend (g, 1) ensure Current ~ old Current.deep_twin.extended_by (g) end override (i: INTEGER_32; g: G) -- Change quantity of 'g' to 'i' in current bag do imp.override (create {FUN [G, INTEGER_32]}.make_from_array (<<[g, i]>>)) ensure Current ~ old Current.deep_twin.overriden (i, g) end subtract (g: G; i: INTEGER_32) -- Subtract 'g' of quantity 'i' from current bag. require Current [g] >= i local old_count: INTEGER_32 do old_count := imp [g] if old_count = i then imp.subtract (create {PAIR [G, INTEGER_32]}.make (g, old_count)) else imp.override (create {FUN [G, INTEGER_32]}.make_from_array (<<[g, old_count - i]>>)) end ensure Current ~ old Current.deep_twin.subtracted (g, i) end subtract_by (g: G) -- Subtract 'g' of quantity 1 from current bag. require Current [g] >= 1 do subtract (g, 1) ensure Current ~ old Current.deep_twin.subtracted_by (g) end union (other: like Current) -- Add 'other' into current bag. do across other.imp as cur loop extend (cur.item.first, cur.item.second) end ensure Current ~ old Current.deep_twin.unioned (other) end feature -- Constructor make_empty do create imp.make_empty end make_from_array (a: ARRAY [G]) do make_empty across a as it loop extend_by (it.item) end ensure # Current = a.count end make_from_tuple_array (a: ARRAY [TUPLE [g: G; i: INTEGER_32]]) require across a as cur all cur.item.i > 0 end local new_count: INTEGER_32 do make_empty across a as t loop new_count := Current [t.item.g] + t.item.i imp.override (create {FUN [G, INTEGER_32]}.make_from_array (<<[t.item.g, new_count]>>)) end 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 do Result := # Current = # other and then imp ~ other.imp 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) end feature -- Iteration cursor new_cursor: ITERATION_CURSOR [PAIR [G, INTEGER_32]] -- Fresh cursor associated with current structure do Result := imp.new_cursor ensure -- from ITERABLE result_attached: Result /= Void end feature -- Output debug_output: STRING_8 -- Return a string representation of current bag -- for debugging require -- from DEBUG_OUTPUT True do Result := out ensure -- from DEBUG_OUTPUT result_not_void: Result /= Void ensure then Result ~ out end Io: STD_FILES -- Handle to standard file setup -- (from ANY) once create Result Result.set_output_default ensure -- from ANY instance_free: class io_not_void: Result /= Void end out: STRING_8 -- Return a string representation of current bag require -- from ANY True do Result := imp.out ensure -- from ANY out_not_void: Result /= Void ensure then Result ~ as_function.out end print (o: detachable ANY) -- Write terse external representation of o -- on standard output. -- (from ANY) do if o /= Void then Io.put_string (o.out) end ensure -- from ANY instance_free: class end frozen tagged_out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) external "built_in" ensure -- from ANY tagged_out_not_void: Result /= Void end feature -- Platform Operating_environment: OPERATING_ENVIRONMENT -- Objects available from the operating system -- (from ANY) once create Result ensure -- from ANY instance_free: class operating_environment_not_void: Result /= Void end feature -- Quantifiers comprehension alias "|" (exp: PREDICATE [PAIR [G, INTEGER_32]]): like Current -- Largest subset of the current fun whose elements satisfy exp local fun: FUN [G, INTEGER_32] do fun := as_function.comprehension (exp) create Result.make_empty Result.set_imp (fun) 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 end hold_count (exp: PREDICATE [PAIR [G, INTEGER_32]]): INTEGER_32 -- How many items satisfying exp are in Current? do Result := as_function.hold_count (exp) ensure current_bag_unchanged: Current ~ old Current.deep_twin maximum_result: 0 <= Result and Result <= count 0 <= Result and Result <= # as_function.domain end feature {NONE} -- Retrieval frozen internal_correct_mismatch -- Called from runtime to perform a proper dynamic dispatch on correct_mismatch -- from MISMATCH_CORRECTOR. -- (from ANY) local l_msg: STRING_8 l_exc: EXCEPTIONS do if attached {MISMATCH_CORRECTOR} Current as l_corrector then l_corrector.correct_mismatch else create l_msg.make_from_string ("Mismatch: ") create l_exc l_msg.append (generating_type.name) l_exc.raise_retrieval_exception (l_msg) end end feature -- Specification Queries differenced alias "|\" (other: like Current): like Current -- Return a representation of current bag with -- 'other' subtracted. require other |<: Current do Result := Current.deep_twin Result.difference (other) 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 end extended (g: G; i: INTEGER_32): like Current -- Return a representation of current bag with -- 'g' of quantity 'i' added. do Result := Current.deep_twin Result.extend (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 end extended_by alias "+" (g: G): like Current -- Return a representation of current bag with -- 'g' of quantity 1 added. do Result := Current.deep_twin Result.extend_by (g) 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 end overriden (i: INTEGER_32; g: G): like Current -- Return a representation of current bag with -- quantity of 'g' to 'i' do Result := Current.deep_twin Result.override (i, g) 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 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 do Result := Current.deep_twin Result.subtract (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 end subtracted_by alias "-" (g: G): like Current -- Return a representation of current bag with -- 'g' of quantity 1 subtracted. require Current [g] >= 1 do Result := Current.deep_twin Result.subtract_by (g) 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 end unioned alias "|\/|" (other: like Current): like Current -- Return a representation of current bag with -- 'other' added. do Result := Current.deep_twin Result.union (other) 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 end feature -- Status queries count alias "#": INTEGER_32 do Result := 0 across imp as map loop Result := Result + map.item.second end ensure Result = as_array.count end has (g: G): BOOLEAN do Result := imp.domain.has (g) ensure Result = as_array.has (g) Result = as_function.domain.has (g) Result = (Current [g] > 0) end is_empty: BOOLEAN do Result := # Current = 0 ensure Result = (# Current = 0) end is_subbag_of alias "|<:" (other: like Current): BOOLEAN local fun_cur: ITERATION_CURSOR [PAIR [G, INTEGER_32]] item: G occ: INTEGER_32 do Result := True from fun_cur := imp.new_cursor until not Result or else fun_cur.after loop item := fun_cur.item.first occ := fun_cur.item.second if other [item] < occ then Result := False end fun_cur.forth end ensure Result = across as_function as cur all cur.item.second <= other [cur.item.first] end end occurrences alias "[]" (g: G): INTEGER_32 assign override -- Number of 'g' in current bag. do if imp.domain.has (g) then Result := imp [g] else Result := 0 end 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] end 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