note description: "Cursor trees with a recursive structure" library: "Free implementation of ELKS library" 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: 2018-12-15 18:06:16 +0000 (Sat, 15 Dec 2018) $" revision: "$Revision: 102608 $" deferred class RECURSIVE_CURSOR_TREE [G] feature -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) -- (from ANY) do end 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 local pos: CURSOR do pos := cursor down (i) Result := item go_to (pos) end cursor: RECURSIVE_TREE_CURSOR [G] -- Current cursor position do create Result.make (active, active_parent, after, before, below) ensure -- from CURSOR_STRUCTURE cursor_not_void: Result /= Void end generating_type: TYPE [detachable RECURSIVE_CURSOR_TREE [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 has (v: G): BOOLEAN -- Does structure include an occurrence of v? -- (Reference or object equality, -- based on object_comparison.) -- (from CURSOR_TREE) local pos: CURSOR do pos := cursor Result := linear_has (v) go_to (pos) end linear_has (v: like item): BOOLEAN -- Does structure include an occurrence of v? -- (Reference or object equality, -- based on object_comparison.) -- (from LINEAR) do start if not off then search (v) end Result := not exhausted ensure -- from CONTAINER not_found_in_empty: Result implies not is_empty end 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 local occur, pos: INTEGER_32 do if object_comparison and v /= Void then from start pos := 1 until exhausted or (occur = i) loop if item ~ v then occur := occur + 1 end preorder_forth pos := pos + 1 end else from start pos := 1 until exhausted or (occur = i) loop if item = v then occur := occur + 1 end preorder_forth pos := pos + 1 end end if occur = i then Result := pos - 1 end ensure -- from LINEAR non_negative_result: Result >= 0 end item: G -- Item at cursor position require -- from TRAVERSABLE not_off: not off require -- from ACTIVE readable: readable local a: like active do a := active check a_attached: a /= Void end Result := a.item end item_for_iteration: G -- Item at current position -- (from LINEAR) require -- from LINEAR not_off: not off do Result := item end new_cursor: ITERATION_CURSOR [G] -- Fresh cursor associated with current structure -- (from ITERABLE) deferred ensure -- from ITERABLE result_attached: Result /= Void end occurrences (v: G): INTEGER_32 -- Number of times v appears in structure -- (Reference or object equality, -- based on object_comparison.) -- (from CURSOR_TREE) local pos: CURSOR do pos := cursor Result := linear_occurrences (v) go_to (pos) end linear_occurrences (v: like item): INTEGER_32 -- Number of times v appears. -- (Reference or object equality, -- based on object_comparison.) -- (from LINEAR) do from start search (v) until exhausted loop Result := Result + 1 preorder_forth search (v) end end parent_item: G -- Item in parent. -- (from CURSOR_TREE) require -- from CURSOR_TREE not_on_root: not is_root local pos: CURSOR do pos := cursor up Result := item go_to (pos) end 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) do if object_comparison then from until exhausted or else v ~ item loop preorder_forth end else from until exhausted or else v = item loop preorder_forth end end ensure -- from LINEAR object_found: (not exhausted and object_comparison) implies v ~ item item_found: (not exhausted and not object_comparison) implies v = item end 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 local a: like active do a := active if a /= Void then Result := a.arity end end breadth: INTEGER_32 -- Breadth of current level -- (from CURSOR_TREE) local l: INTEGER_32 pos: CURSOR do l := level if l > 0 then pos := cursor go_above Result := breadth_of_level_from_active (l + 1) go_to (pos) end end count: INTEGER_32 -- Number of items in the tree local pos: like cursor do pos := cursor from start until off loop Result := Result + 1 preorder_forth end go_to (pos) corresponding_child end depth: INTEGER_32 -- Depth of the tree -- (from CURSOR_TREE) local pos: CURSOR do if not is_empty then pos := cursor go_above Result := depth_from_active - 1 go_to (pos) end end estimated_count_of (other: ITERABLE [G]): INTEGER_32 -- Estimated number of elements in other. -- (from CONTAINER) do if attached {FINITE [G]} other as f then Result := f.count elseif attached {READABLE_INDEXABLE [G]} other as r then Result := r.upper - r.lower + 1 end ensure -- from CONTAINER instance_free: class non_negative_result: Result >= 0 end level: INTEGER_32 -- Level of current node in tree -- (Root is on level 1) -- (from CURSOR_TREE) local pos: CURSOR do from pos := cursor until above loop Result := Result + 1 up end go_to (pos) 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 alias "≡≡≡" (other: RECURSIVE_CURSOR_TREE [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 is_equal (other: RECURSIVE_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 external "built_in" ensure -- from ANY symmetric: Result implies other ~ Current consistent: standard_is_equal (other) implies Result 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 alias "≜" (other: RECURSIVE_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 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 above: BOOLEAN -- Is there no valid cursor position above cursor? do if not below then Result := active_parent = Void end end 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) do Result := True end 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 empty: BOOLEAN obsolete "ELKS 2000: Use `is_empty' instead. [2017-05-31]" -- Is there no element? -- (from CONTAINER) do Result := is_empty end exhausted: BOOLEAN -- Has structure been completely explored? -- (from LINEAR) do Result := off ensure -- from LINEAR exhausted_when_off: off implies Result end extendible: BOOLEAN -- May new items be added on current level? require -- from COLLECTION True do Result := (not above) and (not is_root) end is_empty: BOOLEAN -- Is the tree empty? local a: like above_node do a := above_node if a /= Void then Result := a.arity = 0 end end 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) do Result := has (v) end is_leaf: BOOLEAN -- Is cursor on a leaf? -- (from CURSOR_TREE) do if not off then Result := arity = 0 end end is_root: BOOLEAN -- Is cursor on tree root? do if not off then Result := active_parent = above_node end end isfirst: BOOLEAN -- Is cursor on first sibling? local a: like active_parent do a := active_parent if not off and then a /= Void then Result := a.child_isfirst end end islast: BOOLEAN -- Is cursor on last sibling? local a: like active_parent do a := active_parent if not off and then a /= Void then Result := a.child_islast end end 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) do Result := after or before or below or above end linear_off: BOOLEAN -- Is there no current item? -- (from LINEAR) do Result := is_empty or after end prunable: BOOLEAN -- May items be removed? -- (from COLLECTION) deferred end readable: BOOLEAN -- Is there a current item that may be read? -- (from CURSOR_TREE) do Result := not off end replaceable: BOOLEAN -- Can current item be replaced? -- (from ACTIVE) do Result := True 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 valid_cursor (p: CURSOR): BOOLEAN -- Can the cursor be moved to position p? local pos: like cursor do if attached {like cursor} p as temp then if temp.active = above_node or temp.before or temp.after or temp.below then Result := True else pos := cursor from start until active = temp.active or else off loop preorder_forth end Result := not off go_to (pos) end end end 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) do Result := i >= 0 and i <= (arity + 1) end writable: BOOLEAN -- Is there a current item that may be modified? -- (from CURSOR_TREE) do Result := not off end 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 do object_comparison := True ensure -- from CONTAINER object_comparison end 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 do object_comparison := False ensure -- from CONTAINER reference_comparison: not object_comparison end feature -- Cursor movement back -- Move cursor one position backward. do if below then after := False before := True elseif after then after := False elseif isfirst then before := True elseif attached active_parent as a then a.child_back check attached a.child as c then active := c end end end 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 local l: INTEGER_32 do l := level level_forth if above and (l < depth) then start_on_level (l + 1) end end 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) local a: like active do if i = 0 then if arity = 0 then below := True else a := active active_parent := a; a.child_go_i_th (1) check attached a.child as c then active := c end end before := True elseif above or else i <= arity then a := active active_parent := a; a.child_go_i_th (i) check attached a.child as c then active := c end else if arity = 0 then below := True else a := active active_parent := a; a.child_go_i_th (arity) check attached a.child as c then active := c end end after := True end ensure then -- from CURSOR_TREE gone_before: (i = 0) implies before end forth -- Move cursor one position forward. do if below then before := False after := True elseif before then before := False elseif islast then after := True elseif attached active_parent as a then a.child_forth check attached a.child as c then active := c end end end 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 do up down (arity) end go_to (p: CURSOR) -- Move cursor to position p. require -- from CURSOR_STRUCTURE cursor_position_valid: valid_cursor (p) do if attached {like cursor} p as temp then unchecked_go (temp) else check False end end end level_back -- Move cursor to previous position of current level. -- (from CURSOR_TREE) do if not isfirst then back elseif not above then from up level_back until above or else not is_leaf loop level_back end if not above then down (arity) end end end level_forth -- Move cursor to next position of current level. -- (from CURSOR_TREE) do if not above and then not islast then forth elseif not above then from up level_forth until above or else not is_leaf loop level_forth end if not above then down (1) end end end 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 do if islast then up else forth from until is_leaf loop down (1) end end end postorder_start -- Move cursor to first position in postorder. -- Leave cursor off if tree is empty. -- (from CURSOR_TREE) do from go_above until arity = 0 loop down (1) end end 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 do if is_leaf then from until not islast loop up end if not above then forth end else down (1) end end start -- Move cursor to root. -- Leave cursor off if is_empty. -- (from CURSOR_TREE) require -- from TRAVERSABLE True do go_above if not is_empty then down (1) end ensure then -- from CURSOR_TREE on_root_unless_empty: not is_empty implies is_root end 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 do go_above start_on_level_from_active (l + 1) ensure -- from CURSOR_TREE level_expected: level = l is_first: isfirst end 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 do if below then below := False else check attached active_parent as a then active := a active_parent := a.parent end corresponding_child end after := False before := False 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) end 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 do go_above if not other.is_empty then other.start down (0) put_right (other.item) forth fill_from_active (other) end end 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 local lin_rep: LINEAR [G] do lin_rep := other.linear_representation from lin_rep.start until not extendible or else lin_rep.off loop extend (lin_rep.item); lin_rep.forth end end 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 do if not other.is_leaf then from other.down (1) down (0) until other.after loop put_right (other.item) forth fill_from_active (other); other.forth end; other.up up end end 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 do back merge_right (other) end 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 local pos: CURSOR do if not other.is_empty then pos := other.cursor; other.start put_right (other.item) forth if not other.is_leaf then down (0); other.down (0) from until other.islast loop other.forth merge_right (other.subtree) end up end; other.go_to (pos) end end put (v: G) -- Put v at cursor position. -- (Synonym for replace) -- (from CURSOR_TREE) require -- from COLLECTION extendible: extendible do replace (v) ensure -- from COLLECTION item_inserted: is_inserted (v) end 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 do back put_right (v) forth forth end 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 deferred end replace (v: G) -- Replace current item by v. require -- from ACTIVE writable: writable replaceable: replaceable local a: like active do a := active if a /= Void then a.replace (v) end ensure -- from ACTIVE item_replaced: item = v end 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 do from until not has (v) loop prune (v) end ensure -- from COLLECTION no_more_occurrences: not has (v) end remove -- Remove node at cursor position -- (and consequently the corresponding -- subtree). Cursor moved up one level. require -- from ACTIVE prunable: prunable writable: writable do corresponding_child check attached active_parent as a then active := a active_parent := a.parent; a.remove_child; a.child_back end ensure then not_off_unless_empty: is_empty or else not off end wipe_out -- Remove all items. require -- from COLLECTION prunable: prunable local a: like above_node do if not is_empty then a := above_node if a /= Void then a.child_start; a.remove_child end end active := above_node active_parent := Void after := False before := False below := False ensure -- from COLLECTION wiped_out: is_empty ensure then cursor_above: above end feature -- Conversion linear_representation: LINEAR [G] -- Representation as a linear structure -- (from LINEAR) require -- from CONTAINER True do Result := Current end feature -- Duplication child_tree (i: INTEGER_32): RECURSIVE_CURSOR_TREE [G] -- 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 local pos: CURSOR do pos := cursor down (i) Result := subtree go_to (pos) end 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: RECURSIVE_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) 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: RECURSIVE_CURSOR_TREE [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: RECURSIVE_CURSOR_TREE [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 parent_tree: RECURSIVE_CURSOR_TREE [G] -- Subtree rooted at parent -- (from CURSOR_TREE) require -- from CURSOR_TREE not_on_root: not is_root not_off: not off local pos: CURSOR do pos := cursor up Result := subtree go_to (pos) 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: RECURSIVE_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) external "built_in" ensure -- from ANY is_standard_equal: standard_is_equal (other) end frozen standard_twin: RECURSIVE_CURSOR_TREE [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 subtree: RECURSIVE_CURSOR_TREE [G] -- Subtree rooted at current node -- (from CURSOR_TREE) require -- from CURSOR_TREE not_off: not off do Result := new_tree; Result.go_above; Result.down (0); Result.put_right (item); Result.forth; Result.fill_from_active (Current) end frozen twin: RECURSIVE_CURSOR_TREE [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 RECURSIVE_CURSOR_TREE [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 RECURSIVE_CURSOR_TREE [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 -- Inapplicable prune (v: G) -- Remove item v. -- (from CURSOR_TREE) require -- from COLLECTION prunable: prunable do end feature {NONE} -- Implementation above_node: like active -- Node above root; physical root of tree active: DYNAMIC_TREE [G] -- Current node active_parent: detachable like active -- Parent of current node breadth_of_level_from_active (a_level: INTEGER_32): INTEGER_32 -- Breadth of level level of subtree starting at current node -- (from CURSOR_TREE) do if (a_level = 2) or else is_leaf then Result := arity else from down (1) until after loop Result := Result + breadth_of_level_from_active (a_level - 1) forth end up end end corresponding_child -- Make active the current child of active_parent. require active_exists: active /= Void local a: like active_parent do a := active_parent if a /= Void then a.set_child (active) end end depth_from_active: INTEGER_32 -- Depth of subtree starting at active -- (from CURSOR_TREE) do if not off and then arity = 0 then Result := 1 else from down (1) until after loop Result := Result.max (depth_from_active + 1) forth end up end end start_on_level_from_active (l: INTEGER_32) -- Move the cursor to the first position -- of the l-th level counting from active. -- (from CURSOR_TREE) require -- from CURSOR_TREE deep_enough: depth_from_active >= l do from down (1) until depth_from_active >= l - 1 loop forth end if l > 2 then start_on_level_from_active (l - 1) end end unchecked_go (p: like cursor) -- Make an attempt to move cursor -- to position p, without checking -- whether p is a valid cursor position -- or not. do active_parent := p.active_parent check attached p.active as a then active := a end corresponding_child after := p.after before := p.before below := p.below end feature {CURSOR_TREE} -- Implementation go_above -- Move the cursor above the tree -- (from CURSOR_TREE) do from until above loop up end end new_tree: RECURSIVE_CURSOR_TREE [G] -- A newly created instance of the same type. -- This feature may be redefined in descendants so as to -- produce an adequately allocated and initialized object. -- (from CURSOR_TREE) deferred ensure -- from CURSOR_TREE result_exists: Result /= Void result_is_empty: Result.is_empty end 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 local pos: CURSOR do pos := cursor go_last_child put_right (v) go_to (pos) if below then below := False before := False after := False down (0) end ensure -- from COLLECTION item_inserted: is_inserted (v) end 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 do linear_representation.do_all (action) end 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 local c: detachable CURSOR cs: detachable CURSOR_STRUCTURE [G] do if attached {CURSOR_STRUCTURE [G]} Current as acs then cs := acs c := acs.cursor end from start until after loop action.call ([item]) preorder_forth end if cs /= Void and c /= Void then cs.go_to (c) end end 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 do linear_representation.do_if (action, test) end 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 local c: detachable CURSOR cs: detachable CURSOR_STRUCTURE [G] do if attached {CURSOR_STRUCTURE [G]} Current as acs then cs := acs c := acs.cursor end from start until after loop if test.item ([item]) then action.call ([item]) end preorder_forth end if cs /= Void and c /= Void then cs.go_to (c) end end 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 local c: detachable CURSOR cs: detachable CURSOR_STRUCTURE [G] do if attached {CURSOR_STRUCTURE [G]} Current as acs then cs := acs c := acs.cursor end from start Result := True until after or not Result loop Result := test.item ([item]) preorder_forth end if cs /= Void and c /= Void then cs.go_to (c) end ensure then -- from LINEAR empty: is_empty implies Result end for_all (test: FUNCTION [G, BOOLEAN]): BOOLEAN -- Is test true for all items? -- (from TRAVERSABLE) require -- from TRAVERSABLE test_exists: test /= Void do Result := linear_representation.for_all (test) end there_exists (test: FUNCTION [G, BOOLEAN]): BOOLEAN -- Is test true for at least one item? -- (from TRAVERSABLE) require -- from TRAVERSABLE test_exists: test /= Void do Result := linear_representation.there_exists (test) end 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 local c: detachable CURSOR cs: detachable CURSOR_STRUCTURE [G] do if attached {CURSOR_STRUCTURE [G]} Current as acs then cs := acs c := acs.cursor end from start until after or Result loop Result := test.item ([item]) preorder_forth end if cs /= Void and c /= Void then cs.go_to (c) end end feature -- Not applicable index: INTEGER_32 -- Index of current position -- (from CURSOR_TREE) do end feature -- Output 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 -- New string containing terse printable representation -- of current object -- (from ANY) do Result := tagged_out ensure -- from ANY out_not_void: Result /= Void end print (o: detachable ANY) -- Write terse external representation of o -- on standard output. -- (from ANY) local s: READABLE_STRING_8 do if attached o then s := o.out if attached {READABLE_STRING_32} s as s32 then Io.put_string_32 (s32) elseif attached {READABLE_STRING_8} s as s8 then Io.put_string (s8) else Io.put_string_32 (s.as_string_32) end 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 -- 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_32 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: ".as_string_32) create l_exc; l_msg.append (generating_type.name_32); l_exc.raise_retrieval_exception (l_msg) end end invariant coherency: not above implies (attached active_parent as a and then a.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 note copyright: "Copyright (c) 1984-2018, 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 RECURSIVE_CURSOR_TREE
Generated by ISE EiffelStudio