note
	description: "SET data structure for both specification and implementation."
	author: "JSO and JW"

class interface
	SET [G -> attached ANY]

create 
	make_empty
			-- Initialize an empty set.
		ensure
				is_empty

	make_from_array (a: ARRAY [G])
		ensure
			all_items_are_intended: across
					Current as it
				all
					a.has (it.item)
				end
			all_intended_items_added: across
					a as it
				all
					Current.has (it.item)
				end

	make_one (a_singleton: G)
			-- make a singleton set
		ensure
				count = 1
				has (a_singleton)

convert
	make_one ({ARRAY [G]}),
	make_one: {ARRAY [G]}

feature -- Access

	generating_type: TYPE [detachable SET [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: SET [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: SET [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 -- Duplication

	copy (other: SET [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: SET [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: SET [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: SET [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: SET [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: SET [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 SET [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 for implementation

	difference (other: like Current)
			-- Subtract the current set by 'other'.
		ensure
			definition: old Current.deep_twin ~ Current |\/| (old Current.deep_twin |/\| other)

	extend (g: G)
			-- Extend the current set by 'g'.
		ensure
			case_of_no_extension: (old Current.deep_twin).has (g) implies old Current.deep_twin ~ Current
			case_of_extension: not (old Current.deep_twin).has (g) implies old Current.deep_twin ~ Current - g

	intersect (other: like Current)
			-- Intersect current set with 'other'.
		ensure
				Current |<: old Current.deep_twin
				Current |<: other
			definition: hold_count (agent in_both_set (?, old Current.deep_twin, other)) = count

	subtract (g: G)
			-- Subtract the current set by 'g'.
		ensure
			case_of_subtraction: (old Current.deep_twin).has (g) implies old Current.deep_twin ~ Current + g
			case_of_no_subtraction: not (old Current.deep_twin).has (g) implies old Current.deep_twin ~ Current

	union (other: like Current)
			-- Extend the current set by 'other'.
		ensure
				old Current.deep_twin |<: Current
				other |<: Current
			definition: hold_count (agent in_either_set (?, old Current.deep_twin, other)) = count
	
feature -- Constructor

	make_empty
			-- Initialize an empty set.
		ensure
				is_empty

	make_from_array (a: ARRAY [G])
		ensure
			all_items_are_intended: across
					Current as it
				all
					a.has (it.item)
				end
			all_intended_items_added: across
					a as it
				all
					Current.has (it.item)
				end

	make_one (a_singleton: G)
			-- make a singleton set
		ensure
				count = 1
				has (a_singleton)
	
feature -- Conversion to array.
--	as_array: ARRAY[G]
--		do
--			create Result.make_empty
--			across
--				Current as c
--			loop
--				Result.force (c.item, Result.upper + 1)
--			end
----			Result.compare_objects
--		ensure
--			Current ~ create {SET[G]}.make_from_array (Result)
--			-- why does this fail in {REL}.t8
--		end

	as_array: ARRAY [G]
	
feature -- Debug output

	debug_output: STRING_8
			-- String that should be displayed in debugger to represent Current.
		ensure -- from DEBUG_OUTPUT
			result_not_void: Result /= Void

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
		ensure -- from ANY
			out_not_void: Result /= Void
	
feature -- Equality

	is_equal (other: like Current): BOOLEAN
			-- Is curren set equal to 'other'?
		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 then other |<: Current)
	
feature -- Iteration

	new_cursor: ITERATION_CURSOR [G]
			-- Fresh cursor associated with current structure
		ensure -- from ITERABLE
			result_attached: Result /= Void
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void

	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 [G]): like Current
			-- Largest subset of the current set whose elements satisfy exp
			-- e.g. for set: MSL_SET[STRING],
			-- (set | agent {STRING}.has_substring ("xy")) = {g:set | g.has_substring("xy")}
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			is_subset: Result |<: Current
			all_satisfying_exp: Result.hold_count (exp) = # Result
			consistent_satisfying_items: Current.hold_count (exp) = # Result

	hold_count (exp: PREDICATE [G]): INTEGER_32
			-- How many items satisfying exp are in Current?
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			maximum_result: Result <= count
	
feature -- Queries for specification.

	count alias "#": INTEGER_32
			-- Return the cardinality of the set.
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			definition: Result = hold_count (agent g_to_true)

	differenced alias "|\" (other: like Current): like Current
			-- Return a new set representing the difference between Current and 'other'.
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			definition: Current ~ Result |\/| (Current |/\| other)

	extended alias "+" (g: G): like Current
			-- Return a new set representing the addtion of current and 'g'.
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			case_of_no_extension: has (g) implies Current ~ Result
			case_of_extension: not has (g) implies Current ~ Result - g

	has (g: G): BOOLEAN
			-- Does the set contain 'g'?
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			definition_1: Result = (hold_count (agent eq (?, g)) = 1)
			definition_2: Result = across
					Current as it
				some
					it.item ~ g
				end

	intersected alias "|/\|" (other: like Current): like Current
			-- Return a new set representing the intersection of current and 'other'.
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			items_from_both_set: across
					Result as it
				all
					Current.has (it.item) and other.has (it.item)
				end

	is_empty: BOOLEAN
			-- Is the set empty?
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
				Result = (# Current = 0)

	is_subset_of alias "|<:" (other: like Current): BOOLEAN
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
				Result = across
					Current as cur
				all
					other.has (cur.item)
				end

	subtracted alias "-" (g: G): like Current
			-- Return a new set representing the difference between Current and 'other'.
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			case_of_subtraction: has (g) implies Current ~ Result + g
			case_of_no_subtraction: not has (g) implies Current ~ Result

	unioned alias "|\/|" (other: like Current): like Current
			-- Return a new set representing the union of current and 'other'.
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			items_from_either_set: across
					Result as it
				all
					Current.has (it.item) or other.has (it.item)
				end
	
feature -- Query and command for arbitrary set members

	choose_item
			-- Choose an arbitrary element of the
			-- set and store it in item
		require
				not is_empty
		ensure
				has (item)
				chosen

	chosen: BOOLEAN

	item: G
			-- Return an arbitrary member from the set.
		require
				not is_empty
				chosen
		ensure
				has (Result)

	remove_item
			-- Remove an arbitrary member (i.e., 'item') from the set.
		require
				not is_empty
				chosen
		ensure
				Current ~ old Current.deep_twin - old item
				not chosen
	
invariant
		imp.object_comparison
	no_duplicates: across
			imp.Lower |..| imp.upper as i
		all
			across
				imp.Lower |..| imp.upper as j
			all
				imp [i.item] ~ imp [j.item] implies i.item = j.item
			end
		end

		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

end -- class SET

Generated by ISE EiffelStudio