note description: "Trees implemented using a linked list representation" library: "Free implementation of ELKS library" legal: "See notice at end of class." status: "See notice at end of class." names: linked_tree, tree, linked_list representation: recursive, linked access: cursor, membership contents: generic date: "$Date: 2018-12-15 18:06:16 +0000 (Sat, 15 Dec 2018) $" revision: "$Revision: 102608 $" class LINKED_TREE [G] create make feature {NONE} -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) -- (from ANY) do end make (v: like item) -- Create single node with item v. do put (v) ll_make ensure is_root is_leaf end ll_make -- Create an empty list. -- (from LINKED_LIST) do child_before := True ensure -- from LINKED_LIST is_before: child_before end make_from_iterable (other: ITERABLE [G]) -- Create a linked list with all items obtained from other. -- (from LINKED_LIST) do ll_make across other as o loop child_extend (o.item) end end feature -- Access at alias "@" (i: INTEGER_32): like child_item assign put_i_th -- Item at i-th position -- Was declared in CHAIN as synonym of i_th. -- (from CHAIN) require -- from TABLE valid_key: valid_index (i) local pos: CURSOR do pos := child_cursor child_go_i_th (i) Result := child_item child_go_to (pos) end child_cursor: LINKED_TREE_CURSOR [G] -- Current cursor position require -- from CURSOR_STRUCTURE True require -- from TREE True do create Result.make (child, child_after, child_before) ensure -- from CURSOR_STRUCTURE cursor_not_void: Result /= Void end first: like child_item -- Item at first position -- (from LINKED_LIST) require -- from CHAIN not_empty: not is_leaf do check attached first_child as f then Result := f.item end end first_child: like parent -- Leftmost child generating_type: TYPE [detachable LINKED_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 sequential_has (v: like child_item): BOOLEAN -- Does structure include an occurrence of v? -- (Reference or object equality, -- based on object_comparison.) -- (from LINEAR) require -- from CONTAINER True do child_start if not child_off then search_child (v) end Result := not exhausted ensure -- from CONTAINER not_found_in_empty: Result implies not is_leaf end ll_has (v: like child_item): BOOLEAN -- Does chain include v? -- (Reference or object equality, -- based on object_comparison.) -- (from CHAIN) local pos: CURSOR do pos := child_cursor Result := sequential_has (v) child_go_to (pos) end i_th alias "[]" (i: INTEGER_32): like child_item assign put_i_th -- Item at i-th position -- Was declared in CHAIN as synonym of at. -- (from CHAIN) require -- from TABLE valid_key: valid_index (i) require -- from READABLE_INDEXABLE valid_index: valid_index (i) local pos: CURSOR do pos := child_cursor child_go_i_th (i) Result := child_item child_go_to (pos) end child_index: INTEGER_32 -- Index of current position -- (from LINKED_LIST) require -- from LINEAR True require -- from TREE True local l_active, l_active_iterator: like child do if child_after then Result := arity + 1 elseif not child_before then from Result := 1 l_active := child l_active_iterator := first_child until l_active_iterator = l_active or else l_active_iterator = Void loop l_active_iterator := l_active_iterator.right Result := Result + 1 end end ensure -- from TREE valid_index: Result >= 0 and Result <= arity + 1 end sequential_index_of (v: like child_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 child_start pos := 1 until exhausted or (occur = i) loop if child_item ~ v then occur := occur + 1 end child_forth pos := pos + 1 end else from child_start pos := 1 until exhausted or (occur = i) loop if child_item = v then occur := occur + 1 end child_forth pos := pos + 1 end end if occur = i then Result := pos - 1 end ensure -- from LINEAR non_negative_result: Result >= 0 end index_of (v: like child_item; i: INTEGER_32): INTEGER_32 -- Index of i-th occurrence of item identical to v. -- (Reference or object equality, -- based on object_comparison.) -- 0 if none. -- (from CHAIN) local pos: CURSOR do pos := child_cursor Result := sequential_index_of (v, i) child_go_to (pos) end child_item: G -- Current item -- (from LINKED_LIST) require -- from ACTIVE readable: child_readable require -- from TRAVERSABLE not_off: not child_off require -- from TREE readable: child_readable do check attached child as a then Result := a.item end end item: G -- Content of cell. -- (from CELL) item_for_iteration: G -- Item at current position -- (from LINEAR) require -- from LINEAR not_off: not child_off do Result := child_item end last: like child_item -- Item at last position -- (from LINKED_LIST) require -- from CHAIN not_empty: not is_leaf do check attached last_child as l then Result := l.item end end left_sibling: like parent -- Left neighbor (if any) require -- from TREE is_not_root: not is_root local p: like parent do p := parent if p /= Void then from Result := p.first_child until Result = Void or else Result.right_sibling = Current loop Result := Result.right_sibling end end ensure -- from TREE is_sibling: Result /= Void implies is_sibling (Result) right_is_current: (Result /= Void) implies (Result.right_sibling = Current) end sequential_occurrences (v: like child_item): INTEGER_32 -- Number of times v appears. -- (Reference or object equality, -- based on object_comparison.) -- (from LINEAR) require -- from BAG True do from child_start search_child (v) until exhausted loop Result := Result + 1 child_forth search_child (v) end ensure -- from BAG non_negative_occurrences: Result >= 0 end parent: detachable like new_cell -- Parent of current node right_sibling: detachable LINKED_TREE [G] -- Right neighbor -- (from LINKABLE) feature -- Measurement child_capacity: INTEGER_32 -- Maximal number of children -- (from TREE) do Result := arity end count: INTEGER_32 -- Number of items -- (from TREE) do Result := subtree_count + 1 end arity: INTEGER_32 -- Number of items -- (from LINKED_LIST) index_set: INTEGER_INTERVAL obsolete "Use `lower' and `upper' instead. [2017-05-31]" -- Range of acceptable indexes. -- (from READABLE_INDEXABLE) do create Result.make (Lower, arity) ensure -- from READABLE_INDEXABLE not_void: Result /= Void empty_if_not_in_order: (Lower > arity) implies Result.is_empty same_lower_if_not_empty: (Lower <= arity) implies Result.lower = Lower same_upper_if_not_empty: (Lower <= arity) implies Result.upper = arity end Lower: INTEGER_32 = 1 -- Minimum index. -- (from CHAIN) occurrences (v: like child_item): INTEGER_32 -- Number of times v appears. -- (Reference or object equality, -- based on object_comparison.) -- (from CHAIN) local pos: CURSOR do pos := child_cursor Result := sequential_occurrences (v) child_go_to (pos) end feature {NONE} -- Measurement 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 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: LINKED_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: LINKED_TREE [G]): BOOLEAN -- Does other contain the same elements? -- (Reference or object equality, -- based on object_comparison.) -- (from TREE) require -- from ANY other_not_void: other /= Void do if Current = other then Result := True else Result := (is_leaf = other.is_leaf) and (object_comparison = other.object_comparison) and (child_capacity = other.child_capacity) if Result and not is_leaf then Result := tree_is_equal (Current, other) end end ensure -- from ANY symmetric: Result implies other ~ Current consistent: standard_is_equal (other) implies Result ensure then -- from LIST indices_unchanged: child_index = old child_index and other.child_index = old other.child_index true_implies_same_size: Result implies arity = other.arity end node_is_equal (other: LINKED_TREE [G]): BOOLEAN -- Is other equal to Current? -- (from TREE) require -- from TREE other_not_void: other /= Void do if object_comparison then Result := item ~ other.item else Result := item = other.item end 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: LINKED_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 child_after: BOOLEAN -- Is there no valid cursor position to the right of cursor? -- (from LINKED_LIST) child_before: BOOLEAN -- Is there no valid cursor position to the left of cursor? -- (from LINKED_LIST) changeable_comparison_criterion: BOOLEAN -- May object_comparison be changed? -- (Answer: yes by default.) -- (from CONTAINER) do Result := True end child_isfirst: BOOLEAN -- Is cursor under first child? -- (from TREE) require -- from CHAIN True do Result := not is_leaf and child_index = 1 ensure -- from CHAIN valid_position: Result implies not is_leaf ensure -- from TREE not_is_leaf: Result implies not is_leaf end child_islast: BOOLEAN -- Is cursor under last child? -- (from TREE) require -- from CHAIN True do Result := not is_leaf and child_index = child_capacity ensure -- from CHAIN valid_position: Result implies not is_leaf ensure -- from TREE not_is_leaf: Result implies not is_leaf end child_readable: BOOLEAN -- Is there a current child_item to be read? -- (from TREE) require -- from ACTIVE True do Result := not child_off and then (child /= Void) end child_writable: BOOLEAN -- Is there a current child_item that may be modified? -- (from TREE) require -- from ACTIVE True do Result := not child_off and then (child /= Void) 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 ll_empty: BOOLEAN obsolete "ELKS 2000: Use `is_empty' instead. [2017-05-31]" -- Is there no element? -- (from CONTAINER) do Result := is_leaf end exhausted: BOOLEAN -- Has structure been completely explored? -- (from LINEAR) do Result := child_off ensure -- from LINEAR exhausted_when_off: child_off implies Result end Extendible: BOOLEAN = True -- May new items be added? -- (from DYNAMIC_TREE) child_extendible: BOOLEAN -- May new items be added? (Answer: yes.) -- (from DYNAMIC_CHAIN) require -- from COLLECTION True do Result := True end Ll_full: BOOLEAN = False -- Is structured filled to capacity? (Answer: no.) -- (from LINKED_LIST) has (v: G): BOOLEAN -- Does subtree include v? -- (Reference or object equality, -- based on object_comparison.) -- (from TREE) do if object_comparison then Result := v ~ item or else subtree_has (v) else Result := v = item or else subtree_has (v) end ensure -- from CONTAINER not_found_in_empty: Result implies not is_leaf end is_empty: BOOLEAN -- Is structure empty of items? -- (from TREE) do Result := False end is_inserted (v: G): BOOLEAN -- Has v been inserted at the end by the most recent put or -- extend? -- (from LINKED_LIST) local l: like last_child do l := last_child if l /= Void then check put_constraint: (v /= l.item) implies not child_off end Result := (v = l.item) or else (v = child_item) end end is_leaf: BOOLEAN -- Are there no children? -- (from TREE) do Result := arity = 0 end is_root: BOOLEAN -- Is there no parent? -- (from TREE) do Result := parent = Void end is_sibling (other: attached like parent): BOOLEAN -- Are current node and other siblings? -- (from TREE) require -- from TREE other_exists: other /= Void do Result := not is_root and other.parent = parent ensure -- from TREE not_root: Result implies not is_root other_not_root: Result implies not other.is_root same_parent: Result = not is_root and other.parent = parent end object_comparison: BOOLEAN -- Must search operations use equal rather than = -- for comparing references? (Default: no, use =.) -- (from CONTAINER) child_off: BOOLEAN -- Is there no current item? -- (from LINKED_LIST) require -- from TRAVERSABLE True require -- from TREE True do Result := child_after or child_before end prunable: BOOLEAN -- May items be removed? (Answer: yes.) -- (from DYNAMIC_TABLE) require -- from COLLECTION True do Result := True end Readable: BOOLEAN = True -- (from TREE) readable_child: BOOLEAN -- Is there a current child to be read? -- (from TREE) do Result := not child_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? -- (from LINKED_LIST) local temp, sought: like first_child do if attached {like child_cursor} p as ll_c then from temp := first_child sought := ll_c.active Result := ll_c.after or else ll_c.before until Result or else temp = Void loop Result := temp = sought temp := temp.right end end end valid_cursor_index (i: INTEGER_32): BOOLEAN -- Is i correctly bounded for cursor movement? -- (from TREE) require -- from CHAIN True do Result := (i >= 0) and (i <= child_capacity + 1) ensure -- from CHAIN valid_cursor_index_definition: Result = ((i >= 0) and (i <= arity + 1)) ensure -- from TREE valid_cursor_index_definition: Result = (i >= 0) and (i <= child_capacity + 1) end valid_index (i: INTEGER_32): BOOLEAN -- Is i within allowable bounds? -- (from CHAIN) require -- from READABLE_INDEXABLE True require -- from TABLE True do Result := (i >= 1) and (i <= arity) ensure -- from READABLE_INDEXABLE only_if_in_index_set: Result implies (Lower <= i and i <= arity) ensure then -- from CHAIN valid_index_definition: Result = ((i >= 1) and (i <= arity)) end Writable: BOOLEAN = True -- Is there a current item that may be modified? -- (from TREE) writable_child: BOOLEAN -- Is there a current child that may be modified? -- (from TREE) do Result := not child_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 child_back -- Move to previous item. -- (from LINKED_LIST) require -- from BILINEAR not_before: not child_before require -- from TREE True do if is_leaf then child_before := True child_after := False elseif child_after then child_after := False elseif child_isfirst then child_before := True else child := previous end end child_finish -- Move cursor to last position. -- (Go before if empty) -- (from LINKED_LIST) require -- from LINEAR True require -- from TREE True local p: detachable like new_cell do from p := child until p = Void loop child := p p := p.right end child_after := False child_before := child = Void ensure then -- from CHAIN at_last: not is_leaf implies child_islast ensure then -- from LINKED_LIST empty_convention: is_leaf implies child_before end child_forth -- Move cursor to next position. -- (from LINKED_LIST) require -- from LINEAR not_after: not child_after require -- from TREE True local a: like child do if child_before then child_before := False if is_leaf then child_after := True end else a := child if a /= Void and then a.right /= Void then child := a.right else child_after := True end end ensure then -- from LIST moved_forth: child_index = old child_index + 1 end child_go_i_th (i: INTEGER_32) -- Move cursor to i-th position. -- (from LINKED_LIST) require -- from CHAIN valid_cursor_index: valid_cursor_index (i) require -- from TREE True do if i = 0 then child_before := True child_after := False child := first_child elseif i = arity + 1 then child_before := False child_after := True child := last_child else move (i - child_index) end ensure -- from CHAIN position_expected: child_index = i ensure then -- from TREE position: child_index = i end child_go_to (p: CURSOR) -- Move cursor to position p. -- (from LINKED_LIST) require -- from CURSOR_STRUCTURE cursor_position_valid: valid_cursor (p) require -- from TREE True do if attached {like child_cursor} p as ll_c then child_after := ll_c.after child_before := ll_c.before if child_before then child := first_child elseif child_after then child := last_child else child := ll_c.active end else check correct_cursor_type: False end end end move (i: INTEGER_32) -- Move cursor i positions. The cursor -- may end up off if the offset is too big. -- (from LINKED_LIST) require -- from CHAIN True local counter, new_index: INTEGER_32 p: like first_child do if i > 0 then if child_before then child_before := False counter := 1 end from p := child until (counter = i) or else (p = Void) loop child := p p := p.right counter := counter + 1 end if p = Void then child_after := True else child := p end elseif i < 0 then new_index := child_index + i child_before := True child_after := False child := first_child if new_index > 0 then move (new_index) end end ensure -- from CHAIN too_far_right: (old child_index + i > arity) implies exhausted too_far_left: (old child_index + i < 1) implies exhausted expected_index: (not exhausted) implies (child_index = old child_index + i) ensure then -- from LINKED_LIST moved_if_inbounds: ((old child_index + i) >= 0 and (old child_index + i) <= (arity + 1)) implies child_index = (old child_index + i) before_set: (old child_index + i) <= 0 implies child_before after_set: (old child_index + i) >= (arity + 1) implies child_after end search_child (v: like child_item) -- Move to first position (at or after current -- position) where item and v are equal. -- If structure does not include v ensure that -- exhausted will be true. -- (Reference or object equality, -- based on object_comparison.) -- (from BILINEAR) do if child_before and not is_leaf then child_forth end Precursor (v) ensure -- from LINEAR object_found: (not exhausted and object_comparison) implies v ~ child_item item_found: (not exhausted and not object_comparison) implies v = child_item end child_start -- Move cursor to first position. -- (from LINKED_LIST) require -- from TRAVERSABLE True require -- from TREE True do if first_child /= Void then child := first_child child_after := False else child_after := True end child_before := False ensure then -- from CHAIN at_first: not is_leaf implies child_isfirst ensure then -- from LINKED_LIST empty_convention: is_leaf implies child_after end feature {RECURSIVE_CURSOR_TREE}{RECURSIVE_CURSOR_TREE} -- Element change set_child (n: like parent) -- Set the child of parent to n. require -- from DYNAMIC_TREE non_void_argument: n /= Void do child := n ensure then child_set: child = n end feature -- Element change append (s: SEQUENCE [G]) -- Append a copy of s. -- (from CHAIN) require -- from SEQUENCE argument_not_void: s /= Void local l: SEQUENCE [G] l_cursor: CURSOR do l := s if s = Current then l := twin end from l_cursor := child_cursor; l.start until l.exhausted loop child_extend (l.item) child_finish; l.forth end child_go_to (l_cursor) ensure -- from SEQUENCE new_count: arity >= old arity end extend (v: like item) -- Add v as new child. -- (from DYNAMIC_TREE) do child_extend (v) end child_extend (v: like child_item) -- Add v to end. -- Do not move cursor. -- (from LINKED_LIST) require -- from COLLECTION extendible: child_extendible require -- from DYNAMIC_TREE True local p: like first_child l: like last_child do p := new_cell (v) if is_leaf then first_child := p child := p else l := last_child if l /= Void then l.put_right (p) if child_after then child := p end end end arity := arity + 1 ensure -- from COLLECTION item_inserted: is_inserted (v) end fill (other: TREE [G]) obsolete "Fill the tree explicitly. [2018-11-30]" -- Fill with as many items of other as possible. -- The representations of other and current node -- need not be the same. -- (from TREE) do replace (other.item) fill_subtree (other) end ll_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 CHAIN) require -- from COLLECTION other_not_void: other /= Void extendible: child_extendible local lin_rep: LINEAR [G] l_cursor: CURSOR do lin_rep := other.linear_representation from l_cursor := child_cursor; lin_rep.start until not child_extendible or else lin_rep.off loop child_extend (lin_rep.item) child_finish; lin_rep.forth end child_go_to (l_cursor) end force (v: like child_item) -- Add v to end. -- (from SEQUENCE) require -- from SEQUENCE extendible: child_extendible do child_extend (v) ensure then -- from SEQUENCE new_count: arity = old arity + 1 item_inserted: has (v) end ll_merge_left (other: LINKED_TREE [G]) -- Merge other into current structure before cursor -- position. Do not move cursor. Empty other. -- (from LINKED_LIST) require -- from DYNAMIC_CHAIN extendible: child_extendible not_before: not child_before other_exists: other /= Void not_current: other /= Current local other_first_element: like first_child other_count: INTEGER_32 p: like first_child do if attached other.last_child as other_last_element then other_first_element := other.first_child other_count := other.arity; other.wipe_out check other_first_element /= Void end if is_leaf then first_child := other_first_element child := first_child elseif child_isfirst then other_last_element.l_put_right (first_child) first_child := other_first_element else p := previous if p /= Void then p.put_right (other_first_element) end; other_last_element.l_put_right (child) end arity := arity + other_count end ensure -- from DYNAMIC_CHAIN new_count: arity = old arity + old other.arity new_index: child_index = old child_index + old other.arity other_is_empty: other.is_leaf end ll_merge_right (other: LINKED_TREE [G]) -- Merge other into current structure after cursor -- position. Do not move cursor. Empty other. -- (from LINKED_LIST) require -- from DYNAMIC_CHAIN extendible: child_extendible not_after: not child_after other_exists: other /= Void not_current: other /= Current local other_first_element: like first_child other_count: INTEGER_32 a: like child do if attached other.last_child as other_last_element then other_first_element := other.first_child other_count := other.arity; other.wipe_out check other_first_element /= Void end a := child if a = Void then first_child := other_first_element child := first_child else if not child_islast then other_last_element.l_put_right (a.right) end; a.put_right (other_first_element) end arity := arity + other_count end ensure -- from DYNAMIC_CHAIN new_count: arity = old arity + old other.arity same_index: child_index = old child_index other_is_empty: other.is_leaf end merge_tree_after (other: like new_cell) -- Merge children of other into current structure -- after cursor position. Do not move cursor. -- Make other a leaf. require -- from DYNAMIC_TREE not_child_off: not child_off other_exists: other /= Void do attach (other) ll_merge_right (other) ensure -- from DYNAMIC_TREE other_is_leaf: other.is_leaf end merge_tree_before (other: like new_cell) -- Merge children of other into current structure -- before cursor position. Do not move cursor. -- Make other a leaf. require -- from DYNAMIC_TREE not_child_off: not child_off other_exists: other /= Void do attach (other) ll_merge_left (other) ensure -- from DYNAMIC_TREE other_is_leaf: other.is_leaf end sequence_put (v: like child_item) -- Add v to end. -- (from SEQUENCE) require -- from COLLECTION extendible: child_extendible do child_extend (v) ensure -- from COLLECTION item_inserted: is_inserted (v) ensure then -- from SEQUENCE new_count: arity = old arity + 1 end put (v: like item) -- Make v the cell's item. -- Was declared in CELL as synonym of replace. -- (from CELL) require -- from TREE is_writable: Writable do item := v ensure -- from TREE item_inserted: item = v ensure -- from CELL item_inserted: item = v end child_put (v: like child_item) -- Replace current item by v. -- (Synonym for replace) -- (from CHAIN) require -- from TREE child_writable: child_writable require -- from CHAIN writeable: child_writable replaceable: replaceable do child_replace (v) ensure -- from TREE item_inserted: child_item = v ensure -- from CHAIN same_count: arity = old arity is_inserted: is_inserted (v) end put_child (n: like new_cell) -- Add n to the list of children. -- Do not move child cursor. require -- from TREE non_void_argument: n /= Void local c: like last_child do if object_comparison then n.compare_objects else n.compare_references end if is_leaf then first_child := n child := n else c := last_child if c /= Void then c.l_put_right (n) end if child_after then child := n end end; n.attach_to_parent (Current) arity := arity + 1 end put_child_left (n: like new_cell) -- Add n to the left of cursor position. -- Do not move cursor. require -- from DYNAMIC_TREE not_child_before: not child_before non_void_argument: n /= Void do child_back put_child_right (n) child_forth child_forth end put_child_right (n: like new_cell) -- Add n to the right of cursor position. -- Do not move cursor. require -- from DYNAMIC_TREE not_child_after: not child_after non_void_argument: n /= Void local c: like child do if object_comparison then n.compare_objects else n.compare_references end if child_before then n.l_put_right (first_child) first_child := n else c := child if c /= Void then n.l_put_right (c.right_sibling); c.l_put_right (n) end end; n.attach_to_parent (Current) arity := arity + 1 end put_front (v: like child_item) -- Add v to beginning. -- Do not move cursor. -- (from LINKED_LIST) require -- from DYNAMIC_CHAIN extendible: child_extendible local p: like new_cell do p := new_cell (v); p.put_right (first_child) first_child := p if child_before or is_leaf then child := p end arity := arity + 1 ensure -- from DYNAMIC_CHAIN new_count: arity = old arity + 1 item_inserted: first = v end put_i_th (v: like child_item; i: INTEGER_32) -- Put v at i-th position. -- (from CHAIN) require -- from TABLE valid_key: valid_index (i) require -- from TABLE valid_key: valid_index (i) local pos: CURSOR do pos := child_cursor child_go_i_th (i) child_replace (v) child_go_to (pos) ensure -- from TABLE inserted: i_th (i) = v end child_put_left (v: like child_item) -- Add v to the left of cursor position. -- Do not move cursor. -- (from LINKED_LIST) require -- from DYNAMIC_CHAIN extendible: child_extendible not_before: not child_before require -- from DYNAMIC_TREE not_child_before: not child_before local p: like new_cell a: like child do a := child if a = Void then put_front (v) elseif child_after then child_back child_put_right (v) move (2) else p := new_cell (a.item); p.put_right (a.right); a.put (v); a.put_right (p) child := p arity := arity + 1 end ensure -- from DYNAMIC_CHAIN new_count: arity = old arity + 1 new_index: child_index = old child_index + 1 ensure then -- from LINKED_LIST previous_exists: previous /= Void item_inserted: attached previous as q and then q.item = v end child_put_right (v: like child_item) -- Add v to the right of cursor position. -- Do not move cursor. -- (from LINKED_LIST) require -- from DYNAMIC_CHAIN extendible: child_extendible not_after: not child_after require -- from DYNAMIC_TREE not_child_after: not child_after local p: like new_cell a: like child do p := new_cell (v) check is_leaf implies child_before end if child_before then p.put_right (first_child) first_child := p child := p else a := child if a /= Void then p.put_right (a.right); a.put_right (p) end end arity := arity + 1 ensure -- from DYNAMIC_CHAIN new_count: arity = old arity + 1 same_index: child_index = old child_index ensure then -- from LINKED_LIST next_exists: next /= Void item_inserted: not old child_before implies (attached next as n and then n.item = v) item_inserted_before: old child_before implies (attached child as c and then c.item = v) end replace (v: like item) -- Make v the cell's item. -- Was declared in CELL as synonym of put. -- (from CELL) require -- from TREE is_writable: Writable do item := v ensure -- from TREE item_inserted: item = v ensure -- from CELL item_inserted: item = v end child_replace (v: like child_item) -- Replace current item by v. -- (from LINKED_LIST) require -- from ACTIVE writable: child_writable replaceable: replaceable require -- from TREE child_writable: child_writable local a: like child do a := child if a /= Void then a.put (v) end ensure -- from ACTIVE item_replaced: child_item = v ensure -- from TREE item_inserted: child_item = v end replace_child (n: like new_cell) -- Replace current child by n. require -- from TREE writable_child: writable_child do put_child_right (n) remove_child ensure -- from TREE child_replaced: child = n end sprout -- Make current node a root. -- (from TREE) local p: like parent do p := parent if p /= Void then p.prune (Current) end end feature -- Removal prune (n: like new_cell) -- Prune n from children. require -- from TREE is_child: n.parent = Current local l_child: like first_child left_child: like first_child c: like first_child do from l_child := first_child until l_child = Void or l_child = n loop left_child := l_child l_child := l_child.right_sibling end if l_child /= Void then if left_child /= Void then left_child.l_put_right (l_child.right_sibling) if child = n then child := left_child end else c := first_child if c /= Void then first_child := c.right_sibling end if n = child then child := first_child end end arity := arity - 1 if is_leaf and not child_before then child_after := True end; n.attach_to_parent (Void); n.forget_right end ensure -- from TREE n_is_root: n.is_root end ll_prune (v: like child_item) -- Remove first occurrence of v, if any, -- after cursor position. -- If found, move cursor to right neighbor; -- if not, make structure exhausted. -- (from DYNAMIC_CHAIN) require -- from COLLECTION prunable: prunable do search_child (v) if not exhausted then remove_child end end prune_all (v: like child_item) -- Remove all occurrences of v. -- (Reference or object equality, -- based on object_comparison.) -- Leave structure exhausted. -- (from DYNAMIC_CHAIN) require -- from COLLECTION prunable: prunable do from child_start search_child (v) until exhausted loop remove_child search_child (v) end ensure -- from COLLECTION no_more_occurrences: not has (v) ensure then -- from DYNAMIC_CHAIN is_exhausted: exhausted end remove_child -- Remove current item. -- Move cursor to right neighbor -- (or after if no right neighbor). -- (from LINKED_LIST) require -- from ACTIVE prunable: prunable writable: child_writable require -- from DYNAMIC_TREE child_not_off: not child_off local succ: like first_child removed: like child a: like child p: like previous do removed := child if removed /= Void then if child_isfirst then first_child := removed.right; removed.forget_right child := first_child if arity = 1 then check no_active: child = Void end child_after := True end elseif child_islast then child := previous a := child if a /= Void then a.forget_right end child_after := True else succ := removed.right p := previous if p /= Void then p.put_right (succ) end; removed.forget_right child := succ end arity := arity - 1 cleanup_after_remove (removed) end ensure then -- from DYNAMIC_LIST after_when_empty: is_leaf implies child_after ensure -- from DYNAMIC_TREE new_arity: arity = old arity - 1 new_child_index: child_index = old child_index end remove_i_th (i: INTEGER_32) -- Remove item at index i. -- Move cursor to next neighbor (or after if no next neighbor) if it is at i-th position. -- Do not change cursor otherwise. -- (from DYNAMIC_CHAIN) require -- from DYNAMIC_TABLE prunable: prunable valid_key: valid_index (i) local new_index: like child_index do new_index := child_index if new_index > i then new_index := new_index - 1 end child_go_i_th (i) remove_child child_go_i_th (new_index) ensure then -- from DYNAMIC_CHAIN new_count: arity = old arity - 1 same_index_if_below: child_index <= i implies child_index = old child_index new_index_if_above: child_index > i implies child_index = old child_index - 1 same_leading_items: across old twin as c all c.target_index < i implies c.item = i_th (c.target_index) end same_trailing_items: across old twin as c all c.target_index > i implies c.item = i_th (c.target_index - 1) end end remove_left_child -- Remove item to the left of cursor position. -- Do not move cursor. -- (from LINKED_LIST) require -- from DYNAMIC_CHAIN left_exists: child_index > 1 require -- from DYNAMIC_TREE is_not_first: not child_isfirst do move (-2) remove_right_child child_forth ensure -- from DYNAMIC_CHAIN new_count: arity = old arity - 1 new_index: child_index = old child_index - 1 ensure -- from DYNAMIC_TREE new_arity: arity = old arity - 1 new_child_index: child_index = old child_index - 1 end remove_right_child -- Remove item to the right of cursor position. -- Do not move cursor. -- (from LINKED_LIST) require -- from DYNAMIC_CHAIN right_exists: child_index < arity require -- from DYNAMIC_TREE is_not_last: not child_islast local removed: like first_child f: like first_child a: like child succ: like child do if child_before then f := first_child if f /= Void then removed := f first_child := f.right a := child if a /= Void then a.forget_right end child := first_child end else a := child if a /= Void then succ := a.right if succ /= Void then removed := succ; a.put_right (succ.right); succ.forget_right end end end arity := arity - 1 cleanup_after_remove (removed) ensure -- from DYNAMIC_CHAIN new_count: arity = old arity - 1 same_index: child_index = old child_index ensure -- from DYNAMIC_TREE new_arity: arity = old arity - 1 new_child_index: child_index = old child_index end wipe_out -- Remove all items. -- (from LINKED_LIST) require -- from TREE True do internal_wipe_out ensure then -- from DYNAMIC_LIST is_before: child_before ensure -- from TREE is_leaf: is_leaf end chain_wipe_out -- Remove all items. -- (from DYNAMIC_CHAIN) require -- from COLLECTION prunable: prunable do from child_start until is_leaf loop remove_child end ensure -- from COLLECTION wiped_out: is_leaf end feature -- Transformation swap (i: INTEGER_32) -- Exchange item at i-th position with item -- at cursor position. -- (from CHAIN) require -- from CHAIN not_off: not child_off valid_index: valid_index (i) local old_item, new_item: like child_item pos: CURSOR do pos := child_cursor old_item := child_item child_go_i_th (i) new_item := child_item child_replace (old_item) child_go_to (pos) child_replace (new_item) ensure -- from CHAIN swapped_to_item: child_item = old i_th (i) swapped_from_item: i_th (i) = old child_item end feature -- Conversion binary_representation: BINARY_TREE [G] -- Convert to binary tree representation: -- first child becomes left child, -- right sibling becomes right child. -- (from TREE) local current_sibling: detachable BINARY_TREE [G] c: like first_child do create Result.make (item) if not is_leaf then c := first_child if c /= Void then Result.put_left_child (c.binary_representation) end from child_start child_forth current_sibling := Result.left_child until child_after loop if current_sibling /= Void then c := child if c /= Void then current_sibling.put_right_child (c.binary_representation) end current_sibling := current_sibling.right_child end child_forth end end ensure -- from TREE result_is_root: Result.is_root result_has_no_right_child: not Result.has_right end fill_from_binary (b: BINARY_TREE [G]) -- Fill from a binary tree representation. -- Left child becomes first child. -- Right child becomes right sibling. -- Any right child of b is ignored. -- (from DYNAMIC_TREE) local current_node: detachable BINARY_TREE [G] c: like child do replace (b.item) wipe_out if b.has_left then from current_node := b.left_child until current_node = Void loop child_put_right (current_node.item) child_forth c := child if c /= Void then c.fill_from_binary (current_node) end current_node := current_node.right_child end end end linear_representation: LINEAR [G] -- Representation as a linear structure -- (from TREE) require -- from CONTAINER True local al: ARRAYED_LIST [G] do create al.make (count); al.start; al.extend (item) fill_list (al) Result := al end feature -- Duplication 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: LINKED_TREE [G]) -- Copy contents from other. -- (from TREE) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) local i: INTEGER_32 old_idx: INTEGER_32 tmp_tree: LINKED_TREE [G] c: like child do tmp_tree := clone_node (other) if not other.is_leaf then tree_copy (other, tmp_tree) end standard_copy (tmp_tree) old_idx := child_index from i := 1 until i > child_capacity loop child_go_i_th (i) c := child if c /= Void then c.attach_to_parent (Current) end i := i + 1 end child_go_i_th (old_idx) 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: LINKED_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: LINKED_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 duplicate (n: INTEGER_32): LINKED_TREE [G] obsolete "Create and initialize a new tree explicitly. [2018-11-30]" -- Copy of sub-tree beginning at cursor position and -- having min (n, arity - child_index + 1) -- children -- (from DYNAMIC_TREE) require -- from TREE not_child_off: not child_off valid_sublist: n >= 0 local pos: CURSOR counter: INTEGER_32 c: like child do from Result := new_tree pos := child_cursor until child_after or else (counter = n) loop c := child if c /= Void then Result.put_child (c.duplicate_all) end child_forth counter := counter + 1 end child_go_to (pos) end ll_duplicate (n: INTEGER_32): LINKED_TREE [G] obsolete "[ Create a new container explicitly using `make_from_iterable` if available. Otherwise, replace a call to the feature with code that creates and initializes container. [2018-11-30] ]" -- Copy of sub-chain beginning at current position -- and having min (n, from_here) items, -- where from_here is the number of items -- at or to the right of current position. -- (from DYNAMIC_CHAIN) require -- from CHAIN not_off_unless_after: child_off implies child_after valid_subchain: n >= 0 local pos: CURSOR counter: INTEGER_32 do from Result := new_tree if object_comparison then Result.compare_objects end pos := child_cursor until (counter = n) or else exhausted loop Result.child_extend (child_item) child_forth counter := counter + 1 end child_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: LINKED_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: LINKED_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 frozen twin: LINKED_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 LINKED_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 LINKED_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 forget_left -- Forget all left siblings. do end feature {LINKED_TREE}{LINKED_LIST} -- Implementation new_cell (v: like item): like Current -- New cell containing v do create Result.make (v) if object_comparison then Result.compare_objects end; Result.attach_to_parent (Current) ensure -- from LINKED_LIST result_exists: Result /= Void end feature {LINKED_TREE}{LINKED_LIST}{DYNAMIC_CHAIN}{DYNAMIC_TREE} -- Implementation new_tree: like Current obsolete "Use explicit creation instead. See also explanations for `duplicate`. [2018-11-30]" -- 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. require -- from DYNAMIC_CHAIN True require -- from DYNAMIC_TREE True do create Result.make (item) ensure -- from DYNAMIC_CHAIN result_exists: Result /= Void ensure -- from DYNAMIC_TREE result_exists: Result /= Void result_item: Result.item = item end feature {LINKED_TREE}{TREE} -- Implementation clone_node (n: like Current): like Current -- Clone node n. require -- from TREE not_void: n /= Void do create Result.make (n.item); Result.copy_node (n) ensure -- from TREE result_is_root: Result.is_root result_is_leaf: Result.is_leaf end feature {LINKED_TREE} -- Implementation copy_node (n: like Current) -- Copy content of n except tree data into Current. require -- from TREE is_root: is_root is_leaf: is_leaf not_void: n /= Void do standard_copy (n) arity := 0 child := Void child_after := False child_before := True first_child := Void parent := Void right_sibling := Void ensure -- from TREE object_comparison_copied: object_comparison = n.object_comparison same_arity: arity = old arity same_item: item = old item result_is_root: is_root result_is_leaf: is_leaf end feature {NONE} -- Implementation attach (other: like new_cell) -- Attach all children of other to current node. local cursor: CURSOR c: like child do cursor := other.child_cursor from other.child_start until other.child_off loop c := other.child if c /= Void then c.attach_to_parent (Current) end; other.child_forth end; other.child_go_to (cursor) end child_remove -- Remove item of current child -- (from TREE) do end frozen internal_wipe_out -- Remove all items. -- (from LINKED_LIST) require -- from LINKED_LIST prunable do child := Void first_child := Void child_before := True child_after := False arity := 0 ensure -- from LINKED_LIST wiped_out: is_leaf is_before: child_before end remove -- Remove current item -- (from TREE) do end tree_copy (other, tmp_tree: LINKED_TREE [G]) -- Generic implementation of copy. other is copied onto -- Current. tmp_tree is used as temporary storage during -- copying. Since it cannot be created locally because of the -- generic implementation, it has to be passed in. -- (from TREE) require -- from TREE other_not_empty: other /= Void and then not other.is_leaf other_not_leaf: not other.is_leaf tmp_tree_exists: tmp_tree /= Void same_rule: object_comparison = other.object_comparison local i: INTEGER_32 p1, p2, node: LINKED_TREE [G] c1: like child other_stack, tmp_stack: LINKED_STACK [LINKED_TREE [G]] idx_stack, orgidx_stack: LINKED_STACK [INTEGER_32] do create other_stack.make create tmp_stack.make create idx_stack.make create orgidx_stack.make if other.object_comparison then tmp_tree.compare_objects end; orgidx_stack.put (other.child_index) from i := 1 p1 := other p2 := tmp_tree invariant same_count: other_stack.count = tmp_stack.count and tmp_stack.count = idx_stack.count until i > p1.child_capacity and other_stack.is_empty loop p1.child_go_i_th (i); p2.child_go_i_th (i) if p1.child_readable then check source_tree_not_void: p1 /= Void target_tree_not_void: p2 /= Void source_child_not_void: p1.child /= Void target_child_void: p2.readable_child implies p2.child = Void end c1 := p1.child if c1 = Void then check source_child_not_void: p1.child /= Void end else node := clone_node (c1) check not_the_same: node /= p1.child end; p2.put_child (node) check node_is_child: node = p2.child comparison_mode_ok: node.object_comparison = c1.object_comparison p1_consistent: c1.parent = p1 p2_consistent: node.parent = p2 end if not c1.is_leaf then other_stack.put (p1); tmp_stack.put (p2); idx_stack.put (i + 1) p1 := c1 p2 := node; orgidx_stack.put (p1.child_index) i := 0 end end end if i <= p1.child_capacity then i := i + 1 else from invariant same_count: other_stack.count = tmp_stack.count and tmp_stack.count = idx_stack.count until other_stack.is_empty or else i <= p1.child_capacity loop p1.child_go_i_th (orgidx_stack.item); p2.child_go_i_th (orgidx_stack.item) check child_indices_equal: p1.child_index = p2.child_index end p1 := other_stack.item p2 := tmp_stack.item check p1_not_void: p1 /= Void p2_not_void: p2 /= Void end i := idx_stack.item; other_stack.remove; tmp_stack.remove; idx_stack.remove; orgidx_stack.remove end end end; other.child_go_i_th (orgidx_stack.item); tmp_tree.child_go_i_th (orgidx_stack.item); orgidx_stack.remove check tree_stacks_empty: other_stack.is_empty and tmp_stack.is_empty at_root: p1 = other and p2 = tmp_tree copy_correct: other ~ tmp_tree index_stack_empty: orgidx_stack.is_empty end end tree_is_equal (t1, t2: LINKED_TREE [G]): BOOLEAN -- Are t1 and t2 recursively equal? -- (from TREE) require -- from TREE trees_exist: t1 /= Void and t2 /= Void trees_not_empty: not t1.is_leaf and not t2.is_leaf same_rule: t1.object_comparison = t2.object_comparison local p1, p2: LINKED_TREE [G] c1, c2: like child t1_stack, t2_stack: LINKED_STACK [LINKED_TREE [G]] orgidx1_stack, orgidx2_stack: LINKED_STACK [INTEGER_32] l_current_cursor, l_other_cursor: like child_cursor do l_current_cursor := t1.child_cursor l_other_cursor := t2.child_cursor if t1.is_leaf and t2.is_leaf then Result := t1.item ~ t2.item elseif t1.is_leaf xor t2.is_leaf then Result := False else create t1_stack.make create t2_stack.make create orgidx1_stack.make create orgidx2_stack.make; orgidx1_stack.put (t1.child_index); orgidx2_stack.put (t2.child_index) from Result := True p1 := t1 p2 := t2; p1.child_start; p2.child_start invariant same_count: t1_stack.count = t2_stack.count until not Result or else p1.child_after and t1_stack.is_empty loop check p1_not_void: p1 /= Void p2_not_void: p2 /= Void end if p1.child_readable and p2.child_readable and p1.child_capacity = p2.child_capacity then Result := p1.node_is_equal (p2) c1 := p1.child c2 := p2.child if c1 = Void or else c2 = Void then check False end else if not (c1.is_leaf or c2.is_leaf) then t1_stack.put (p1); t2_stack.put (p2) p1 := c1 p2 := c2 Result := p1.node_is_equal (p2); orgidx1_stack.put (p1.child_index); orgidx2_stack.put (p2.child_index); p1.child_start; p2.child_start elseif c1.is_leaf xor c2.is_leaf then Result := False else Result := c1.node_is_equal (c2) end end elseif p1.child_capacity /= p2.child_capacity or else (p1.child_readable xor p2.child_readable) then Result := False end if not p1.child_after then p1.child_forth; p2.child_forth else from invariant same_count: t1_stack.count = t2_stack.count until t1_stack.is_empty or else not p1.child_after loop p1 := t1_stack.item p2 := t2_stack.item; p1.child_forth; p2.child_forth; t1_stack.remove; t2_stack.remove; orgidx1_stack.remove; orgidx2_stack.remove end end end if not Result then from invariant same_count: t1_stack.count = t2_stack.count and orgidx1_stack.count = orgidx2_stack.count until orgidx1_stack.count = 1 loop p1.child_go_i_th (orgidx1_stack.item); p2.child_go_i_th (orgidx2_stack.item) p1 := t1_stack.item p2 := t2_stack.item check p1_not_void: p1 /= Void p2_not_void: p2 /= Void end; t1_stack.remove; t2_stack.remove; orgidx1_stack.remove; orgidx2_stack.remove end check tree_stacks_empty: t1_stack.is_empty and t2_stack.is_empty at_root: p1 = t1 and p2 = t2 p1_not_void: p1 /= Void p2_not_void: p2 /= Void end; p1.child_go_i_th (orgidx1_stack.item); p2.child_go_i_th (orgidx2_stack.item); orgidx1_stack.remove; orgidx2_stack.remove check index_stacks_empty: orgidx1_stack.is_empty and orgidx2_stack.is_empty end end end; t1.child_go_to (l_current_cursor); t2.child_go_to (l_other_cursor) end feature {DYNAMIC_TREE} -- Implementation duplicate_all: LINKED_TREE [G] obsolete "Create and initialize a new tree explicitly. [2018-11-30]" -- Copy of sub-tree including all children -- (from DYNAMIC_TREE) local pos: CURSOR c: like child do from Result := new_tree pos := child_cursor child_start until child_off loop c := child if c /= Void then Result.put_child (c.duplicate_all) end; Result.child_forth child_forth end child_go_to (pos) end fill_subtree (other: TREE [G]) obsolete "Fill subtree explicitly. [2018-11-30]" -- Fill children with children of other. -- (from DYNAMIC_TREE) local c: like child o: detachable TREE [G] do from other.child_start until other.child_off loop child_extend (other.item); other.child_forth end from child_start; other.child_start until child_off loop c := child o := other.child if c /= Void and then o /= Void then c.fill_subtree (o) end; other.child_forth child_forth end end feature {TREE} -- Implementation attach_to_parent (n: like parent) -- Make n parent of current node. -- (from TREE) do parent := n ensure -- from TREE new_parent: parent = n end fill_list (al: ARRAYED_LIST [G]) -- Fill al with all the children's items. -- (from TREE) local c: like child do from child_start until child_off loop c := child if c /= Void then al.extend (child_item); c.fill_list (al) end child_forth end end subtree_count: INTEGER_32 -- Number of items in children -- (from TREE) local pos: CURSOR c: like child do Result := arity from pos := child_cursor child_start until child_off loop c := child if c /= Void then Result := Result + c.subtree_count end child_forth end child_go_to (pos) end subtree_has (v: G): BOOLEAN -- Do children include v? -- (Reference or object equality, -- based on object_comparison.) -- (from TREE) local cursor: CURSOR c: like child do cursor := child_cursor from child_start until child_off or else Result loop if child /= Void then if object_comparison then Result := v ~ child_item else Result := v = child_item end end child_forth end from child_start until child_off or else Result loop c := child if c /= Void then Result := c.subtree_has (v) end child_forth end child_go_to (cursor) end feature {LINKED_TREE}{CELL, CHAIN} -- Implementation l_put_right (other: detachable LINKED_TREE [G]) -- Put other to the right of current cell. -- (from LINKABLE) do right_sibling := other ensure -- from LINKABLE chained: right_sibling = other end feature -- Implementation child: like first_child -- Element at cursor position -- (from LINKED_LIST) forget_right -- Remove right link. -- (from LINKABLE) require -- from TREE True do right_sibling := Void ensure -- from LINKABLE not_chained: right_sibling = Void end last_child: like first_child -- Tail of list -- (from LINKED_LIST) require -- from TREE is_not_leaf: not is_leaf local p: like first_child do from p := child until p = Void loop Result := p p := p.right end end feature {LINKED_LIST} -- Implementation cleanup_after_remove (v: like first_child) -- Clean-up a just removed cell. -- (from LINKED_LIST) require -- from LINKED_LIST non_void_cell: v /= Void do end next: like first_child -- Element right of cursor -- (from LINKED_LIST) local a: like child do if child_before then Result := child else a := child if a /= Void then Result := a.right end end end previous: like first_child -- Element left of cursor -- (from LINKED_LIST) local p: like first_child do if child_after then Result := child elseif not (child_isfirst or child_before) then from p := first_child until p = Void or else p.right = child loop p := p.right end Result := p end end feature -- Access: Cursor ll_new_cursor: LINKED_LIST_ITERATION_CURSOR [G] -- (from LINKED_LIST) require -- from ITERABLE True do create Result.make (Current); Result.start ensure -- from ITERABLE result_attached: Result /= Void 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 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 child_start until child_after loop action.call ([child_item]) child_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 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 child_start until child_after loop if test.item ([child_item]) then action.call ([child_item]) end child_forth end if cs /= Void and c /= Void then cs.go_to (c) end end 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 child_start Result := True until child_after or not Result loop Result := test.item ([child_item]) child_forth end if cs /= Void and c /= Void then cs.go_to (c) end ensure then -- from LINEAR empty: is_leaf implies Result end new_cursor: LINKED_TREE_ITERATION_CURSOR [G] -- Fresh cursor associated with current structure do create Result.make (Current) end 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 child_start until child_after or Result loop Result := test.item ([child_item]) child_forth end if cs /= Void and c /= Void then cs.go_to (c) end 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 {NONE} -- 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 no_void_child: readable_child = child_readable -- from DYNAMIC_TREE extendible_definition: Extendible child_after_definition: child_after = (child_index = arity + 1) -- from TREE tree_consistency: child_readable implies (attached child as c and then c.parent = Current) leaf_definition: is_leaf = (arity = 0) child_off_definition: child_off = child_before or child_after child_before_definition: child_before = (child_index = 0) child_isfirst_definition: child_isfirst = (not is_leaf and child_index = 1) child_islast_definition: child_islast = (not is_leaf and child_index = child_capacity) child_after_definition: child_after = (child_index >= child_capacity + 1) -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) -- from LINKED_LIST prunable: prunable empty_constraint: is_leaf implies ((first_child = Void) and (child = Void)) not_void_unless_empty: (child = Void) implies is_leaf before_constraint: child_before implies (child = first_child) after_constraint: child_after implies (child = last_child) -- from LIST before_definition: child_before = (child_index = 0) after_definition: child_after = (child_index = arity + 1) -- from CHAIN non_negative_index: child_index >= 0 index_small_enough: child_index <= arity + 1 off_definition: child_off = ((child_index = 0) or (child_index = arity + 1)) isfirst_definition: child_isfirst = ((not is_leaf) and (child_index = 1)) islast_definition: child_islast = ((not is_leaf) and (child_index = arity)) item_corresponds_to_index: (not child_off) implies (child_item = i_th (child_index)) -- from ACTIVE writable_constraint: child_writable implies child_readable empty_constraint: is_leaf implies (not child_readable) and (not child_writable) -- from READABLE_INDEXABLE consistent_boundaries: Lower <= arity or else Lower = arity + 1 -- from BILINEAR not_both: not (child_after and child_before) before_constraint: child_before implies child_off -- from LINEAR after_constraint: child_after implies child_off -- from TRAVERSABLE empty_constraint: is_leaf implies child_off -- from FINITE empty_definition: is_leaf = (arity = 0) 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 LINKED_TREE
Generated by ISE EiffelStudio