indexing
	description: "Cursor trees with a recursive structure"
	legal: "See notice at end of class."
	status: "See notice at end of class."
	names: recursive_cursor_tree, cursor_tree, tree
	access: cursor, membership
	representation: recursive
	contents: generic
	date: "$Date: 2006-01-22 18:25:44 -0800 (Sun, 22 Jan 2006) $"
	revision: "$Revision: 56675 $"

deferred class interface
	RECURSIVE_CURSOR_TREE [G]

feature {ANY} -- Initialization

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)
			-- (from ANY)
	
feature -- Access

	child_item (i: INTEGER_32): G
			-- Item in `i'-th child
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			argument_within_bounds: i >= 1 and then i <= arity
			not_off: not off

	cursor: RECURSIVE_TREE_CURSOR [G]
			-- Current cursor position
		ensure -- from CURSOR_STRUCTURE
			cursor_not_void: Result /= Void

	generating_type: STRING_8
			-- Name of current object's generating type
			-- (type of which it is a direct instance)
			-- (from ANY)

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)

	has (v: G): BOOLEAN
			-- Does structure include an occurrence of `v'?
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from CURSOR_TREE)
		require -- from  CONTAINER
			True
		ensure -- from CONTAINER
			not_found_in_empty: Result implies not is_empty

	index_of (v: like item; i: INTEGER_32): INTEGER_32
			-- Index of `i'-th occurrence of `v'.
			-- 0 if none.
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from LINEAR)
		require -- from LINEAR
			positive_occurrences: i > 0
		ensure -- from LINEAR
			non_negative_result: Result >= 0

	item: G
			-- Item at cursor position
		require -- from TRAVERSABLE
			not_off: not off
		require -- from ACTIVE
			readable: readable

	occurrences (v: G): INTEGER_32
			-- (from CURSOR_TREE)
		require -- from  BAG
			True
		require -- from  LINEAR
			True
		ensure -- from BAG
			non_negative_occurrences: Result >= 0

	parent_item: G
			-- Item in parent.
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			not_on_root: not is_root

	search (v: like item)
			-- Move to first position (at or after current
			-- position) where item and `v' are equal.
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- If no such position ensure that exhausted will be true.
			-- (from LINEAR)
		ensure -- from LINEAR
			object_found: (not exhausted and object_comparison) implies equal (v, item)
			item_found: (not exhausted and not object_comparison) implies v = item
	
feature -- Measurement

	arity: INTEGER_32
			-- Number of children of active node.
			-- If cursor is above, 0 if tree is empty, 1 otherwise.
		require -- from HIERARCHICAL
			not_off: not off
		require else
			valid_cursor: not off or else above

	breadth: INTEGER_32
			-- Breadth of current level
			-- (from CURSOR_TREE)

	count: INTEGER_32
			-- Number of items in the tree

	depth: INTEGER_32
			-- Depth of the tree
			-- (from CURSOR_TREE)

	level: INTEGER_32
			-- Level of current node in tree
			-- (Root is on level 1)
			-- (from CURSOR_TREE)
	
feature -- Comparison

	frozen deep_equal (some: ANY; other: like arg #1): BOOLEAN
			-- Are `some' and `other' either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		ensure -- from ANY
			shallow_implies_deep: standard_equal (some, other) implies Result
			both_or_none_void: (some = Void) implies (Result = (other = Void))
			same_type: (Result and (some /= Void)) implies some.same_type (other)
			symmetric: Result implies deep_equal (other, some)

	frozen equal (some: ANY; other: like arg #1): BOOLEAN
			-- Are `some' and `other' either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		ensure -- from ANY
			definition: Result = (some = Void and other = Void) or else ((some /= Void and other /= Void) and then some.is_equal (other))

	is_equal (other: like Current): BOOLEAN
			-- Is `other' attached to an object considered
			-- equal to current object?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			symmetric: Result implies other.is_equal (Current)
			consistent: standard_is_equal (other) implies Result

	frozen standard_equal (some: ANY; other: like arg #1): BOOLEAN
			-- Are `some' and `other' 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
			definition: Result = (some = Void and other = Void) or else ((some /= Void and other /= Void) and then some.standard_is_equal (other))

	frozen standard_is_equal (other: like Current): 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

	above: BOOLEAN
			-- Is there no valid cursor position above cursor?

	after: BOOLEAN
			-- Is there no valid cursor position to the right of cursor?

	before: BOOLEAN
			-- Is there no valid cursor position to the left of cursor?

	below: BOOLEAN
			-- Is there no valid cursor position below cursor?
			-- (from CURSOR_TREE)

	changeable_comparison_criterion: BOOLEAN
			-- May object_comparison be changed?
			-- (Answer: yes by default.)
			-- (from CONTAINER)

	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

	exhausted: BOOLEAN
			-- Has structure been completely explored?
			-- (from LINEAR)
		ensure -- from LINEAR
			exhausted_when_off: off implies Result

	extendible: BOOLEAN
			-- May new items be added on current level?
		require -- from  COLLECTION
			True

	is_empty: BOOLEAN
			-- Is the tree empty?

	is_inserted (v: G): BOOLEAN
			-- Has `v' been inserted by the most recent insertion?
			-- (By default, the value returned is equivalent to calling 
			-- `has (v)'. However, descendants might be able to provide more
			-- efficient implementations.)
			-- (from COLLECTION)

	is_leaf: BOOLEAN
			-- Is cursor on a leaf?
			-- (from CURSOR_TREE)

	is_root: BOOLEAN
			-- Is cursor on tree root?

	isfirst: BOOLEAN
			-- Is cursor on first sibling?

	islast: BOOLEAN
			-- Is cursor on last sibling?

	object_comparison: BOOLEAN
			-- Must search operations use equal rather than `='
			-- for comparing references? (Default: no, use `='.)
			-- (from CONTAINER)

	off: BOOLEAN
			-- Is there no current item?
			-- (True if is_empty)
			-- (from CURSOR_TREE)
		require -- from  TRAVERSABLE
			True

	prunable: BOOLEAN
			-- May items be removed?
			-- (from COLLECTION)

	readable: BOOLEAN
			-- Is there a current item that may be read?
			-- (from CURSOR_TREE)

	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))

	valid_cursor (p: CURSOR): BOOLEAN
			-- Can the cursor be moved to position `p'?

	valid_cursor_index (i: INTEGER_32): BOOLEAN
			-- Can cursor be moved to `i'-th child?
			-- 0 is before and arity + 1 is after.
			-- (from CURSOR_TREE)

	writable: BOOLEAN
			-- Is there a current item that may be modified?
			-- (from CURSOR_TREE)
	
feature -- Status setting

	compare_objects
			-- Ensure that future search operations will use equal
			-- rather than `=' for comparing references.
			-- (from CONTAINER)
		require -- from CONTAINER
			changeable_comparison_criterion: changeable_comparison_criterion
		ensure -- from CONTAINER
			object_comparison

	compare_references
			-- Ensure that future search operations will use `='
			-- rather than equal for comparing references.
			-- (from CONTAINER)
		require -- from CONTAINER
			changeable_comparison_criterion: changeable_comparison_criterion
		ensure -- from CONTAINER
			reference_comparison: not object_comparison
	
feature -- Cursor movement

	back
			-- Move cursor one position backward.

	breadth_forth
			-- Move cursor to next position in breadth-first order.
			-- If the active node is the last in
			-- breadth-first order, the cursor ends up off.
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			not_off: not off

	down (i: INTEGER_32)
			-- Move cursor one level downward:
			-- to `i'-th child if there is one,
			-- or after if `i' = arity + 1,
			-- or before if `i' = 0.
		require -- from HIERARCHICAL
			not_off: not off
			argument_within_bounds: i >= 1 and i <= arity
		require else -- from CURSOR_TREE
			not_before: not before
			not_after: not after
			not_below: not below
			valid_cursor_index: (above and i = 0) or else valid_cursor_index (i)
		ensure then -- from CURSOR_TREE
			gone_before: (i = 0) implies before

	forth
			-- Move cursor one position forward.

	go_last_child
			-- Go to the last child of current parent.
			-- No effect if below
			-- (from CURSOR_TREE)
		require -- from  LINEAR
			True
		require else -- from CURSOR_TREE
			not_above: not above

	go_to (p: CURSOR)
			-- Move cursor to position `p'.
		require -- from CURSOR_STRUCTURE
			cursor_position_valid: valid_cursor (p)

	level_back
			-- Move cursor to previous position of current level.
			-- (from CURSOR_TREE)

	level_forth
			-- Move cursor to next position of current level.
			-- (from CURSOR_TREE)

	postorder_forth
			-- Move cursor to next position in postorder.
			-- If the active node is the last in
			-- postorder, the cursor ends up off.
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			not_off: not off

	postorder_start
			-- Move cursor to first position in postorder.
			-- Leave cursor off if tree is empty.
			-- (from CURSOR_TREE)

	preorder_forth
			-- Move cursor to next position in preorder.
			-- If the active node is the last in
			-- preorder, the cursor ends up off.
			-- (from CURSOR_TREE)
		require -- from LINEAR
			not_after: not after

	start
			-- Move cursor to root.
			-- Leave cursor off if is_empty.
			-- (from CURSOR_TREE)
		require -- from  TRAVERSABLE
			True
		ensure then -- from CURSOR_TREE
			on_root_unless_empty: not is_empty implies is_root

	start_on_level (l: INTEGER_32)
			-- Move the cursor to the first position
			-- of the `l'-th level counting from root.
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			argument_within_bounds: l >= 1 and then depth >= l
		ensure -- from CURSOR_TREE
			level_expected: level = l
			is_first: isfirst

	up
			-- Move cursor one level upward to parent,
			-- or above if is_root holds.
		require -- from HIERARCHICAL
			not_off: not off
		require else -- from CURSOR_TREE
			not_above: not above
		ensure then -- from CURSOR_TREE
			not_before: not before
			not_after: not after
			not_below: not below
			coherency: (not old off and above) = (old is_root)
	
feature -- Element change

	fill (other: CURSOR_TREE [G])
			-- Fill with as many items of `other'
			-- as possible.
			-- The representations of `other' and current structure
			-- need not be the same.
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			is_empty: is_empty

	container_fill (other: CONTAINER [G])
			-- Fill with as many items of `other' as possible.
			-- The representations of `other' and current structure
			-- need not be the same.
			-- (from COLLECTION)
		require -- from COLLECTION
			other_not_void: other /= Void
			extendible: extendible

	fill_from_active (other: CURSOR_TREE [G])
			-- Copy subtree of `other''s active node
			-- onto active node of current tree.
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			cursor_on_leaf: is_leaf

	merge_left (other: CURSOR_TREE [G])
			-- Merge the items of `other' into current structure to
			-- the left of cursor position.
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			other_exists: other /= Void
			not_before: not before
			not_above: not above
			only_one_root: (level = 1) implies is_empty

	merge_right (other: CURSOR_TREE [G])
			-- Merge the items of `other' into current structure to
			-- the right of cursor position.
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			other_exists: other /= Void
			not_after: not after
			not_above: not above
			only_one_root: (level = 1) implies is_empty

	put (v: G)
			-- Put `v' at cursor position.
			-- (Synonym for replace)
			-- (from CURSOR_TREE)
		require -- from COLLECTION
			extendible: extendible
		ensure -- from COLLECTION
			item_inserted: is_inserted (v)

	put_left (v: G)
			-- Add `v' to the left of cursor position.
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			not_before: not before
			not_above: not above
			only_one_root: (level = 1) implies is_empty

	put_right (v: G)
			-- Add `v' to the right of cursor position.
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			not_after: not after
			not_above: not above
			only_one_root: (level = 1) implies is_empty

	replace (v: G)
			-- Replace current item by `v'.
		require -- from ACTIVE
			writable: writable
		ensure -- from ACTIVE
			item_replaced: item = v
	
feature -- Removal

	remove
			-- Remove node at cursor position
			-- (and consequently the corresponding
			-- subtree). Cursor moved up one level.
		require -- from ACTIVE
			prunable: prunable
			writable: writable
		ensure then
			not_off_unless_empty: is_empty or else not off

	wipe_out
			-- Remove all items.
		require -- from COLLECTION
			prunable: prunable
		ensure -- from COLLECTION
			wiped_out: is_empty
		ensure then
			cursor_above: above
	
feature -- Conversion

	linear_representation: LINEAR [G]
			-- Representation as a linear structure
			-- (from LINEAR)
		require -- from  CONTAINER
			True
	
feature -- Duplication

	child_tree (i: INTEGER_32): like Current
			-- Subtree rooted at `i'-th child
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			argument_within_bounds: i >= 1 and then i <= arity
			not_off: not off

	copy (other: like Current)
			-- 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: is_equal (other)

	frozen deep_copy (other: like Current)
			-- 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: like Current
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		ensure -- from ANY
			deep_equal: deep_equal (Current, Result)

	parent_tree: like Current
			-- Subtree rooted at parent
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			not_on_root: not is_root
			not_off: not off

	frozen standard_copy (other: like Current)
			-- 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: like Current
			-- 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)

	subtree: like Current
			-- Subtree rooted at current node
			-- (from CURSOR_TREE)
		require -- from CURSOR_TREE
			not_off: not off

	frozen twin: like Current
			-- New object equal to `Current'
			-- twin calls copy; to change copying/twining semantics, redefine copy.
			-- (from ANY)
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result.is_equal (Current)
	
feature -- Basic operations

	frozen default: like Current
			-- 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)

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
	
feature -- Insert element

	extend (v: G)
			-- Add `v' after last child.
			-- Make `v' the `first_child' if below and place
			-- cursor before.
		require -- from COLLECTION
			extendible: extendible
		ensure -- from COLLECTION
			item_inserted: is_inserted (v)
		ensure then -- from BAG
			one_more_occurrence: occurrences (v) = old (occurrences (v)) + 1
	
feature -- Iteration

	do_all (action: PROCEDURE [ANY, TUPLE [G]])
			-- Apply `action' to every item.
			-- Semantics not guaranteed if `action' changes the structure;
			-- in such a case, apply iterator to clone of structure instead. 
			-- (from TRAVERSABLE)
		require -- from TRAVERSABLE
			action_exists: action /= Void

	linear_do_all (action: PROCEDURE [ANY, TUPLE [G]])
			-- Apply `action' to every item.
			-- Semantics not guaranteed if `action' changes the structure;
			-- in such a case, apply iterator to clone of structure instead.
			-- (from LINEAR)
		require -- from TRAVERSABLE
			action_exists: action /= Void

	do_if (action: PROCEDURE [ANY, TUPLE [G]]; test: FUNCTION [ANY, TUPLE [G], BOOLEAN])
			-- Apply `action' to every item that satisfies `test'.
			-- Semantics not guaranteed if `action' or `test' changes the structure;
			-- in such a case, apply iterator to clone of structure instead. 
			-- (from TRAVERSABLE)
		require -- from TRAVERSABLE
			action_exists: action /= Void
			test_exits: test /= Void

	linear_do_if (action: PROCEDURE [ANY, TUPLE [G]]; test: FUNCTION [ANY, TUPLE [G], BOOLEAN])
			-- Apply `action' to every item that satisfies `test'.
			-- Semantics not guaranteed if `action' or `test' changes the structure;
			-- in such a case, apply iterator to clone of structure instead.
			-- (from LINEAR)
		require -- from TRAVERSABLE
			action_exists: action /= Void
			test_exits: test /= Void

	linear_for_all (test: FUNCTION [ANY, TUPLE [G], BOOLEAN]): BOOLEAN
			-- Is `test' true for all items?
			-- Semantics not guaranteed if `test' changes the structure;
			-- in such a case, apply iterator to clone of structure instead.
			-- (from LINEAR)
		require -- from TRAVERSABLE
			test_exits: test /= Void
		ensure then -- from LINEAR
			empty: is_empty implies Result

	for_all (test: FUNCTION [ANY, TUPLE [G], BOOLEAN]): BOOLEAN
			-- Is `test' true for all items?
			-- (from TRAVERSABLE)
		require -- from TRAVERSABLE
			test_exits: test /= Void

	there_exists (test: FUNCTION [ANY, TUPLE [G], BOOLEAN]): BOOLEAN
			-- Is `test' true for at least one item?
			-- (from TRAVERSABLE)
		require -- from TRAVERSABLE
			test_exits: test /= Void

	linear_there_exists (test: FUNCTION [ANY, TUPLE [G], BOOLEAN]): BOOLEAN
			-- Is `test' true for at least one item?
			-- Semantics not guaranteed if `test' changes the structure;
			-- in such a case, apply iterator to clone of structure instead.
			-- (from LINEAR)
		require -- from TRAVERSABLE
			test_exits: test /= Void
	
feature -- Output

	io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- Was declared in ANY as synonym of tagged_out.
			-- (from ANY)

	print (some: ANY)
			-- Write terse external representation of `some'
			-- on standard output.
			-- (from ANY)

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- Was declared in ANY as synonym of out.
			-- (from ANY)
	
feature -- Platform

	operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
	
feature {ANY} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on `correct_mismatch'
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
	
invariant
	coherency: not above implies active_parent.child = active

		-- from CURSOR_TREE
	non_negative_depth: depth >= 0
	non_negative_breadth: breadth >= 0
	is_leaf_definition: not off implies is_leaf = (arity = 0)
	above_property: above implies (arity <= 1)
	on_tree: (isfirst or islast or is_leaf or is_root) implies not off
	off_definition: off = after or before or above or below
	below_constraint: below implies ((after or before) and not above)
	above_constraint: above implies not (before or after or below)
	after_constraint: after implies not (before or above)
	before_constaint: before implies not (after or above)
	empty_below_constraint: (is_empty and (after or before)) implies below

		-- from HIERARCHICAL
	non_negative_successor_count: arity >= 0

		-- from TRAVERSABLE
	empty_constraint: is_empty implies off

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

		-- from ACTIVE
	writable_constraint: writable implies readable
	empty_constraint: is_empty implies (not readable) and (not writable)

		-- from LINEAR
	after_constraint: after implies off

indexing
	library: "EiffelBase: Library of reusable components for Eiffel."
	copyright: "Copyright (c) 1984-2006, Eiffel Software and others"
	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
	source: "[
		Eiffel Software
		356 Storke Road, Goleta, CA 93117 USA
		Telephone 805-685-1006, Fax 805-685-6869
		Website http://www.eiffel.com
		Customer support http://support.eiffel.com
	]"

end -- class RECURSIVE_CURSOR_TREE