note
	description: "Trees as active structures that may be traversed using a cursor"
	library: "Free implementation of ELKS library"
	legal: "See notice at end of class."
	status: "See notice at end of class."
	names: cursor_tree, tree
	access: cursor, membership
	contents: generic
	date: "$Date: 2017-03-23 19:18:26 +0000 (Thu, 23 Mar 2017) $"
	revision: "$Revision: 100033 $"

deferred class interface
	CURSOR_TREE [G]

feature -- 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
		require
			argument_within_bounds: i >= 1 and then i <= arity
			not_off: not off

	cursor: CURSOR
			-- Current cursor position
			-- (from CURSOR_STRUCTURE)
		ensure -- from CURSOR_STRUCTURE
			cursor_not_void: Result /= Void

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

	linear_has (v: like item): BOOLEAN
			-- Does structure include an occurrence of v?
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from LINEAR)
		ensure -- from CONTAINER
			not_found_in_empty: Result implies not is_empty

	has (v: G): BOOLEAN
			-- Does structure include an occurrence of v?
			-- (Reference or object equality,
			-- based on object_comparison.)

	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 current position
			-- (from TRAVERSABLE)
		require -- from TRAVERSABLE
			not_off: not off
		require -- from ACTIVE
			readable: readable

	item_for_iteration: G
			-- Item at current position
			-- (from LINEAR)
		require -- from LINEAR
			not_off: not off

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

	occurrences (v: G): INTEGER_32
			-- Number of times v appears in structure
			-- (Reference or object equality,
			-- based on object_comparison.)

	linear_occurrences (v: like item): INTEGER_32
			-- Number of times v appears.
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from LINEAR)

	parent_item: G
			-- Item in parent.
		require
			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 v ~ item
			item_found: (not exhausted and not object_comparison) implies v = item

	arity: INTEGER_32
			-- Number of successors of current element
			-- (from HIERARCHICAL)
		require -- from HIERARCHICAL
			not_off: not off
	
feature -- Measurement

	breadth: INTEGER_32
			-- Breadth of current level

	depth: INTEGER_32
			-- Depth of the tree

	estimated_count_of (other: ITERABLE [G]): INTEGER_32
			-- Estimated number of elements in other.
			-- (from CONTAINER)
		ensure -- from CONTAINER
			instance_free: class
			non_negative_result: Result >= 0

	level: INTEGER_32
			-- Level of current node in tree
			-- (Root is on level 1)
	
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 alias "≡≡≡" (other: CURSOR_TREE [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)

	is_equal (other: CURSOR_TREE [G]): 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 ~ Current
			consistent: standard_is_equal (other) implies Result

	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 alias "" (other: CURSOR_TREE [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

	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?

	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?

	is_empty: BOOLEAN
			-- Is there no element?
			-- (from CONTAINER)

	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?

	is_root: BOOLEAN
			-- Is cursor on 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)

	linear_off: BOOLEAN
			-- Is there no current item?
			-- (from LINEAR)

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

	readable: BOOLEAN
			-- Is there a current item that may be read?

	replaceable: BOOLEAN
			-- Can current item be replaced?
			-- (from ACTIVE)

	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?
			-- (from CURSOR_STRUCTURE)

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

	writable: BOOLEAN
			-- Is there a current item that may be modified?
	
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.
		require
			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
			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
			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
		require -- from  LINEAR
			True
		require else
			not_above: not above

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

	level_back
			-- Move cursor to previous position of current level.

	level_forth
			-- Move cursor to next position of current level.

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

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

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

	start
			-- Move cursor to root.
			-- Leave cursor off if is_empty.
		require -- from  TRAVERSABLE
			True
		ensure then
			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.
		require
			argument_within_bounds: l >= 1 and then depth >= l
		ensure
			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
			not_above: not above
		ensure then
			not_before: not before
			not_after: not after
			not_below: not below
			coherency: (not old off and above) = (old is_root)
	
feature -- Element change

	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)

	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.
		require
			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.
		require
			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.
		require
			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.
		require
			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)
		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.
		require
			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.
		require
			not_after: not after
			not_above: not above
			only_one_root: (level = 1) implies is_empty

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

	prune_all (v: G)
			-- Remove all occurrences of v.
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from COLLECTION)
		require -- from COLLECTION
			prunable: prunable
		ensure -- from COLLECTION
			no_more_occurrences: not has (v)

	remove
			-- Remove current item.
			-- (from ACTIVE)
		require -- from ACTIVE
			prunable: prunable
			writable: writable

	wipe_out
			-- Remove all items.
			-- (from COLLECTION)
		require -- from COLLECTION
			prunable: prunable
		ensure -- from COLLECTION
			wiped_out: is_empty
	
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
		require
			argument_within_bounds: i >= 1 and then i <= arity
			not_off: not off

	copy (other: CURSOR_TREE [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: CURSOR_TREE [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: CURSOR_TREE [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)

	parent_tree: like Current
			-- Subtree rooted at parent
		require
			not_on_root: not is_root
			not_off: not off

	frozen standard_copy (other: CURSOR_TREE [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: CURSOR_TREE [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)

	subtree: like Current
			-- Subtree rooted at current node
		require
			not_off: not off

	frozen twin: CURSOR_TREE [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 CURSOR_TREE [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 -- Inapplicable

	prune (v: G)
			-- Remove item v.
		require -- from COLLECTION
			prunable: prunable
	
feature -- Iteration

	do_all (action: PROCEDURE [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 [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 [G]; test: FUNCTION [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_exists: test /= Void

	linear_do_if (action: PROCEDURE [G]; test: FUNCTION [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_exists: test /= Void

	linear_for_all (test: FUNCTION [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_exists: test /= Void
		ensure then -- from LINEAR
			empty: is_empty implies Result

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

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

	linear_there_exists (test: FUNCTION [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_exists: test /= Void
	
feature -- Not applicable

	index: INTEGER_32
			-- Index of current position
	
feature -- Output

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

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		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 -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
	
invariant
	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

note
	copyright: "Copyright (c) 1984-2017, Eiffel Software and others"
	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
	source: "[
		Eiffel Software
		5949 Hollister Ave., 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 CURSOR_TREE

Generated by ISE EiffelStudio