note
	description: "[
		Finite sequences of some elements of type G.
		A valid index is 1..count.
		Array notation can be used, as well as iteration (across).
		Empty sequences can be created, or creation can be from an array. 
		Sequences have a first item (the head), a tail and last item.
		Infix notation for prepended_by where x is of generic type G:
			seq1 |< x
		Infix notation for appended_by where x is of generic type G:
			seq1 |> x
		For concatenation we use infix: seq1 |++| seq2
		For queries, to assert that the state is not changed, 
		the postcondition is
			Current ~ old Current.deep_twin
		Class also has an inefficient implementation:
	]"
	author: "JSO"
	date: "$Date$"
	revision: "$Revision$"

class interface
	SEQ [G -> attached ANY]

create 
	make_empty
			-- create empty list
		ensure
			is_empty: is_empty

	make_from_array (a: ARRAY [G])
			-- Create list from array a.
		ensure
				# Current = a.count
				across
					a.lower |..| a.upper as i
				all
					Current [i.item - a.lower + 1] ~ a [i.item]
				end

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

feature -- Access

	generating_type: TYPE [detachable SEQ [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: SEQ [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: SEQ [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 sequence as an array
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
				Result.lower = 1
				Result.upper = count
				across
					1 |..| count as pos
				all
					Current [pos.item] ~ Result [pos.item]
				end

	as_function: FUN [INTEGER_32, G]
			-- Return current sequence as a function
			-- with its domain being the set of positions
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			all_positions_included: # Result.domain = # Current
				across
					Result.domain as pos
				all
					valid_position (pos.item)
				end
				across
					Result.domain as pos
				all
					Current [pos.item] ~ Result [pos.item]
				end
	
feature -- Duplication

	copy (other: SEQ [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: SEQ [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: SEQ [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: SEQ [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: SEQ [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: SEQ [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 SEQ [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 -- 2-level copy

	twin2: like Current
	
feature -- Auxiliary functions for contracts

	valid_position (pos: INTEGER_32): BOOLEAN
			-- Is 'pos' a valid position in current sequence
		ensure
				Result = (1 <= pos and pos <= # Current)
	
feature -- Commands

	append (v: G)
			-- Add v to end.
		ensure
				Current ~ old Current.deep_twin.appended (v)

	concatenate (other: like Current)
			-- Append 'other' to the end of current sequence
		ensure
				Current ~ old Current.deep_twin.concatenated (other)

	insert (v: G; i: INTEGER_32)
			-- Insert 'v' as the i-th entry, if in index interval
			-- e.g., insert(1) on an empty sequence is forbidden.
		require
				valid_position (i)
		ensure
				Current ~ old Current.deep_twin.inserted (v, i)

	override (v: G; i: INTEGER_32)
			-- Replace i-th entry, if in index interval, by v.
		require
				valid_position (i)
		ensure
				Current ~ old Current.deep_twin.overriden (v, i)

	prepend (v: G)
			-- Add v to beginning.
		ensure
				Current ~ old Current.deep_twin.prepended (v)

	remove (i: INTEGER_32)
			-- Remove i-th entry, if in index interval
		require
				valid_position (i)
		ensure
				Current ~ old Current.deep_twin.removed (i)

	reverse
			-- Reverse the order of current sequence
		ensure
				Current ~ old Current.deep_twin.reversed

	subsequence (i, j: INTEGER_32)
			-- Cut down current seq so that segment i..j remains
		require
				valid_position (i)
				valid_position (j)
		ensure
				Current ~ old Current.deep_twin.subsequenced (i, j)
	
feature -- Constructors

	make_empty
			-- create empty list
		ensure
			is_empty: is_empty

	make_from_array (a: ARRAY [G])
			-- Create list from array a.
		ensure
				# Current = a.count
				across
					a.lower |..| a.upper as i
				all
					Current [i.item - a.lower + 1] ~ a [i.item]
				end
	
feature -- Equality

	is_equal (other: like Current): BOOLEAN
			-- Is current sequence 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 = (as_function ~ other.as_function)
	
feature -- Iteration

	new_cursor: ITERATION_CURSOR [G]
			-- A new cursor for iterating through current sequence
		ensure -- from ITERABLE
			result_attached: Result /= Void
	
feature -- Output

	debug_output: STRING_8
			-- Return string representation of current sequence for debugging
		ensure -- from DEBUG_OUTPUT
			result_not_void: Result /= Void

	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 string representation of current sequence.
		ensure -- from ANY
			out_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 -- Quantification

	comprehension alias "|" (exp: PREDICATE [PAIR [INTEGER_32, G]]): like Current
			-- Form elements of current sequence that satisfy exp
			-- as a new sequence
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			all_satisfying_items_exist: across
					Result as cur
				all
					Current.has (cur.item)
				end
			all_satisfying_exp: Result.hold_count (exp) = # Result
			consistent_satisfying_items: Current.hold_count (exp) = # Result

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

	appended alias "|->" (v: G): like Current
			-- Return a sequence representing current with 'v' added to end
		ensure
			current_seq_unchanged: Current ~ old Current.deep_twin
			count_incremented: # Result = # Current + 1
				Result.front ~ Current
				Result.last ~ v

	concatenated alias "|++|" (other: like Current): like Current
			-- Return a sequence representing current with 'other' appended
		ensure
			current_seq_unchanged: Current ~ old Current.deep_twin
			count_incremented: # Result = # Current + # other
				across
					1 |..| (# Current + # other) as pos
				all
					pos.item <= # Current implies Result [pos.item] ~ Current [pos.item] and pos.item > # Current implies Result [pos.item] ~ other [pos.item - # Current]
				end

	inserted (v: G; i: INTEGER_32): like Current
			-- Return a new sequence representing current with
			-- v inserted as the i-th entry, if in index interval
		require
				valid_position (i)
		ensure
			current_seq_unchanged: Current ~ old Current.deep_twin
			count_incremented: # Result = # Current + 1
				Result ~ subsequenced (1, i - 1).appended (v) |++| subsequenced (i, count)

	overriden (v: G; i: INTEGER_32): like Current
		require
				valid_position (i)
		ensure
			current_seq_unchanged: Current ~ old Current.deep_twin
			count_incremented: # Current = # Result
				across
					1 |..| count as pos
				all
					pos.item = i implies Result [pos.item] ~ v and pos.item /= i implies Result [pos.item] ~ Current [pos.item]
				end

	prepended alias "|<-" (v: G): like Current
			-- Return a sequence representing current with 'v' added to beginning
		ensure
			current_seq_unchanged: Current ~ old Current.deep_twin
			count_incremented: # Result = # Current + 1
				Result.first ~ v
				Result.tail ~ Current

	removed (i: INTEGER_32): like Current
			-- Return a new sequence representing current with
			-- i-th entry, if in index interval, removed
		require
				valid_position (i)
		ensure
			current_seq_unchanged: Current ~ old Current.deep_twin
			count_decremented: # Result = # Current - 1
				Result ~ subsequenced (1, i - 1) |++| subsequenced (i + 1, count)

	reversed: like Current
			-- Return an reversed version of current sequence
		ensure
			current_seq_unchanged: Current ~ old Current.deep_twin
			same_count: # Current = # Result
			reverse_property_1: Current ~ Result.reversed
			reverse_property_2: across
					1 |..| count as i
				all
					Current [i.item] ~ Result [count - i.item + 1]
				end

	slice (a_start, a_end, a_step: INTEGER_32): like Current
		require
				lower <= a_start and a_start <= a_end and a_end <= upper
				a_step >= 0

	subsequenced (i, j: INTEGER_32): like Current
			-- Segment of sequence i..j
		require
				valid_position (i)
				valid_position (j)
		ensure
			current_seq_unchanged: Current ~ old Current.deep_twin
				j >= i implies # Result = j - i + 1
				j < i implies # Result = 0
				across
					i |..| j as pos
				all
					Current [pos.item] ~ Result [pos.item - i + 1]
				end
	
feature -- Status Queries

	count alias "#": INTEGER_32
			-- Return the number of elements in current sequence
		ensure
				Result = as_array.count
				Result = # as_function

	first: G
			-- Return the 1st element in current sequence
		require
				not is_empty
		ensure
				Result ~ as_array [1]
				Result ~ as_function [1]

	front: like Current
			-- Return current sequence except the last element
		require
				not is_empty
		ensure
			case_of_empty_init: # Current = 1 implies Result.is_empty
			case_of_nonempty_init: # Current > 1 implies Result ~ subsequenced (1, count - 1)

	has (v: G): BOOLEAN
			-- Does 'v' exist in current sequence?
		ensure
				Result = across
					1 |..| count as i
				some
					Current [i.item] ~ v
				end

	is_contiguous_subseq_of (other: like Current): BOOLEAN
			-- Is current a contiguous subsequence of 'other'?
			-- e.g. contiguous subsequences of <2, 3, 8, 5, 2> are:
			-- <>, <2, 3> <3, 8, 5> etc.
		ensure
			case_current_empty: is_empty implies Result
			case_other_empty: other.is_empty implies Result = is_empty
			case_both_non_empty: not (is_empty or other.is_empty) implies Result = (across
					1 |..| # other as start_pos
				some
					across
						1 |..| # other as end_pos
					some
						end_pos.item >= start_pos.item and Current ~ other.subsequenced (start_pos.item, end_pos.item)
					end
				end)

	is_empty: BOOLEAN
			-- Is the current sequence empty?
		ensure
				Result = (# Current = 0)
				Result = as_array.is_empty
				Result = as_function.is_empty

	is_subsequence_of alias "|<:" (other: like Current): BOOLEAN
			-- Is current a contiguous subsequence of 'other'?
			-- subsequences are not necessarily contiguous
			-- e.g. subsequences of <2, 3, 8, 5, 2> are:
			-- <>, <3, 5> <3, 5, 2> etc.

	item alias "[]" (i: INTEGER_32): G assign override
			-- Element at i-th position
		require
				valid_position (i)
		ensure
				Result ~ as_function [i]
				Result ~ as_array [i]

	last: G
			-- Return the last element in current sequence
		require
				not is_empty
		ensure
				Result ~ as_array [count]
				Result ~ as_function [count]

	lower: INTEGER_32
			-- Starting position of current sequence
		ensure
				Result = 1

	tail: like Current
			-- Return current sequence except the first element
		require
				not is_empty
		ensure
			case_of_empty_tail: # Current = 1 implies Result.is_empty
			case_of_nonempty_tail: # Current > 1 implies Result ~ subsequenced (2, count)

	upper: INTEGER_32
			-- Ending position of current sequence
		ensure
				Result = # Current
	
invariant
	value_semantics: imp.object_comparison
		lower = 1
		imp.Lower = lower
		is_empty = (upper < lower)
	properties: not is_empty implies Current ~ tail.prepended (first) and Current ~ front.appended (last)

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

end -- class SEQ

Generated by ISE EiffelStudio