Automatic generation produced by ISE Eiffel

Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:
indexing description: "Trees where the children of each node are kept in an array" status: "See notice at end of class" names: tree representation: recursive, array access: cursor, membership contents: generic date: "$Date: 2001-11-16 20:32:23 +0000 (Fri, 16 Nov 2001) $" revision: "$Revision: 51435 $" class ARRAYED_TREE [G] create make feature -- Initialization make (n: INTEGER; v: G) is -- Create node with item v. -- Allocate space for n children. require valid_number_of_children: n >= 0 do al_make (n) replace (v) ensure node_item: item = v end make_filled (n: INTEGER) is -- Allocate list with n items. -- (n may be zero for empty list.) -- This list will be full. -- (from ARRAYED_LIST) require -- from ARRAYED_LIST valid_number_of_items: n >= 0 do child_index := 0 arity := n array_make (1, n) ensure -- from ARRAYED_LIST correct_position: child_before filled: al_full end make_from_array (a: ARRAY [ARRAYED_TREE [G]]) is -- Create list from array a. -- (from ARRAYED_LIST) require -- from ARRAY array_exists: a /= void do Precursor (a) lower := 1 arity := a.count upper := arity child_index := 0 end feature {NONE} -- Initialization al_make (n: INTEGER) is -- Allocate list with n items. -- (n may be zero for empty list.) -- (from ARRAYED_LIST) require -- from ARRAYED_LIST valid_number_of_items: n >= 0 do child_index := 0 arity := 0 array_make (1, n) ensure -- from ARRAYED_LIST correct_position: child_before end make_area (n: INTEGER) is -- Creates a special object for n entries. -- (from TO_SPECIAL) require -- from TO_SPECIAL non_negative_argument: n >= 0 do create area.make (n) ensure -- from TO_SPECIAL area_allocated: area /= void and then area.count = n end feature {ARRAYED_LIST} -- Initialization array_make (min_index, max_index: INTEGER) is -- Allocate array; set index interval to -- min_index .. max_index; set all values to default. -- (Make array empty if min_index = max_index + 1). -- (from ARRAY) require -- from ARRAY valid_bounds: min_index <= max_index + 1 do lower := min_index upper := max_index if min_index <= max_index then make_area (max_index - min_index + 1) else make_area (0) end ensure -- from ARRAY lower_set: lower = min_index upper_set: upper = max_index items_set: all_default end feature -- Access child_item: like item is -- Item in current child node -- (from TREE) require -- from TREE readable: child_readable do Result := child.item end child_cursor: CURSOR is -- Current cursor position -- (from ARRAYED_LIST) do create {ARRAYED_LIST_CURSOR} Result.make (child_index) end first_child: ARRAYED_TREE [G] is -- Item at first position -- (from ARRAYED_LIST) require -- from CHAIN not_empty: not is_leaf require -- from TREE is_not_leaf: not is_leaf do Result := area.item (0) end child_index: INTEGER -- Index of item, if valid. -- (from ARRAYED_LIST) index_of (v: like child; i: INTEGER): INTEGER is -- Index of i-th occurrence of item identical to v. -- (Reference or object equality, -- based on object_comparison.) -- 0 if none. -- (from CHAIN) require -- from LINEAR positive_occurrences: i > 0 local pos: CURSOR do pos := child_cursor Result := sequential_index_of (v, i) child_go_to (pos) ensure -- from LINEAR non_negative_result: Result >= 0 end item: G -- Content of cell. -- (from CELL) child: like first_child is -- Current item -- (from ARRAYED_LIST) require -- from TRAVERSABLE not_off: not child_off require -- from ACTIVE readable: readable_child require -- from TREE readable: readable_child require else -- from ARRAYED_LIST index_is_valid: valid_index (child_index) do Result := area.item (child_index - 1) end array_item (i: INTEGER): ARRAYED_TREE [G] is -- Entry at index i, if in index interval -- Was declared in ARRAY as synonym of @. -- (from ARRAY) require -- from TABLE valid_key: array_valid_index (k) do Result := area.item (i - lower) end last_child: like first_child is -- Item at last position -- (from ARRAYED_LIST) require -- from CHAIN not_empty: not is_leaf require -- from TREE is_not_leaf: not is_leaf do Result := area.item (arity - 1) end left_sibling: like parent is -- Left neighbor if any require -- from TREE is_not_root: not is_root do if position_in_parent > 1 then Result := parent.array_item (position_in_parent - 1) 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: ARRAYED_TREE [G]): INTEGER is -- Number of times v appears. -- (Reference or object equality, -- based on object_comparison.) -- (from LINEAR) 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: ARRAYED_TREE [G] -- Parent of current node right_sibling: like parent is -- Right neighbor if any require -- from TREE is_not_root: not is_root do if position_in_parent < parent.arity then Result := parent.array_item (position_in_parent + 1) end ensure -- from TREE is_sibling: Result /= void implies is_sibling (Result) left_is_current: (Result /= void) implies (Result.left_sibling = Current) end infix "@" (i: INTEGER): ARRAYED_TREE [G] is -- Entry at index i, if in index interval -- Was declared in ARRAY as synonym of item. -- (from ARRAY) require -- from TABLE valid_key: array_valid_index (k) do Result := area.item (i - lower) end feature {NONE} -- Access area: SPECIAL [ARRAYED_TREE [G]] -- Special data zone -- (from TO_SPECIAL) entry (i: INTEGER): ARRAYED_TREE [G] is -- Entry at index i, if in index interval -- (from ARRAY) require -- from ARRAY valid_key: array_valid_index (i) do Result := array_item (i) end al_has (v: ARRAYED_TREE [G]): BOOLEAN is -- Does v appear in array? -- (Reference or object equality, -- based on object_comparison.) -- (from ARRAY) local i: INTEGER upper_bound: INTEGER do upper_bound := upper if object_comparison then if v = void then i := upper_bound + 1 else from i := lower until i > upper_bound or else (array_item (i) /= void and then array_item (i).is_equal (v)) loop i := i + 1 end end else from i := lower until i > upper_bound or else (array_item (i) = v) loop i := i + 1 end end Result := not (i > upper_bound) ensure -- from CONTAINER not_found_in_empty: Result implies not is_leaf end sequential_has (v: like child): BOOLEAN is -- Does structure include an occurrence of v? -- (Reference or object equality, -- based on object_comparison.) -- (from LINEAR) 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 sequential_index_of (v: like child; i: INTEGER): INTEGER is -- 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 do if object_comparison and v /= void then from child_start pos := 1 until child_off or (occur = i) loop if child /= void and then v.is_equal (child) then occur := occur + 1 end child_forth pos := pos + 1 end else from child_start pos := 1 until child_off or (occur = i) loop if child = 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 sequential_search (v: like child) is -- 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 and v /= void then from until exhausted or else (child /= void and then v.is_equal (child)) loop child_forth end else from until exhausted or else v = child loop child_forth end end ensure -- from LINEAR object_found: (not exhausted and object_comparison) implies equal (v, child) item_found: (not exhausted and not object_comparison) implies v = child end feature -- Measurement child_capacity: INTEGER is -- Maximal number of children -- (from TREE) do Result := arity end count: INTEGER is -- Number of items -- (from TREE) do Result := subtree_count + 1 end arity: INTEGER -- Number of items. -- (from ARRAYED_LIST) index_set: INTEGER_INTERVAL is -- Range of acceptable indexes -- (from CHAIN) do create Result.make (1, arity) ensure -- from INDEXABLE not_void: Result /= void ensure then -- from CHAIN count_definition: Result.count = arity end occurrences (v: like child): INTEGER is -- 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) ensure -- from BAG non_negative_occurrences: Result >= 0 end occurrences (v: like child): INTEGER is -- 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) ensure -- from BAG non_negative_occurrences: Result >= 0 end feature {NONE} -- Measurement additional_space: INTEGER is -- Proposed number of additional items -- (from RESIZABLE) do Result := (capacity * growth_percentage // 100).max (minimal_increase) ensure -- from RESIZABLE at_least_one: Result >= 1 end array_count: INTEGER is -- Number of available indices -- (from ARRAY) do Result := upper - lower + 1 ensure then -- from ARRAY consistent_with_bounds: Result = upper - lower + 1 end Growth_percentage: INTEGER is 50 -- Percentage by which structure will grow automatically -- (from RESIZABLE) array_index_set: INTEGER_INTERVAL is -- Range of acceptable indexes -- (from ARRAY) do create Result.make (lower, upper) ensure -- from INDEXABLE not_void: Result /= void ensure then -- from ARRAY same_count: Result.count = arity same_bounds: ((Result.lower = lower) and (Result.upper = upper)) end lower: INTEGER -- Minimum index -- (from ARRAY) Minimal_increase: INTEGER is 5 -- Minimal number of additional items -- (from RESIZABLE) upper: INTEGER -- Maximum index -- (from ARRAY) feature -- Measurement capacity: INTEGER is -- Number of available indices -- Was declared in ARRAY as synonym of count. -- (from ARRAY) do Result := upper - lower + 1 ensure then -- from ARRAY consistent_with_bounds: Result = upper - lower + 1 end feature -- Comparison is_equal (other: like Current): BOOLEAN is -- 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.is_equal (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 feature -- Status report child_after: BOOLEAN is -- Is there no valid cursor position to the right of cursor? -- (from LIST) do Result := (child_index = arity + 1) end child_before: BOOLEAN is -- Is there no valid cursor position to the left of cursor? -- (from LIST) do Result := (child_index = 0) end changeable_comparison_criterion: BOOLEAN is -- May object_comparison be changed? -- (Answer: yes by default.) -- (from CONTAINER) do Result := True end child_isfirst: BOOLEAN is -- Is cursor under first child? -- (from TREE) 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 -- Is cursor under last child? -- (from TREE) 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 -- Is there a current child_item to be read? -- (from TREE) do Result := not child_off and then (child /= void) end child_writable: BOOLEAN is -- Is there a current child_item that may be modified? -- (from TREE) do Result := not child_off and then (child /= void) end exhausted: BOOLEAN is -- Has structure been completely explored? -- (from LINEAR) do Result := child_off ensure -- from LINEAR exhausted_when_off: child_off implies Result end Extendible: BOOLEAN is True -- May new items be added? -- (from DYNAMIC_TREE) Al_extendible: BOOLEAN is True -- May new items be added? (Answer: yes.) -- (from DYNAMIC_CHAIN) has (v: G): BOOLEAN is -- Does subtree include v? -- (Reference or object equality, -- based on object_comparison.) -- (from TREE) do if object_comparison then Result := (v /= void) and then (item /= void) and then (v.is_equal (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 -- Is structure empty of items? -- (from TREE) do Result := False end is_inserted (v: ARRAYED_TREE [G]): BOOLEAN is -- Has v been inserted at the end by the most recent put or -- extend? -- (from ARRAYED_LIST) do check put_constraint: (v /= last_child) implies not child_off end Result := (v = last_child) or else (v = child) end is_leaf: BOOLEAN is -- Are there no children? -- (from TREE) do Result := arity = 0 end is_root: BOOLEAN is -- Is there no parent? -- (from TREE) do Result := parent = void end is_sibling (other: like parent): BOOLEAN is -- 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 -- Is there no current item? -- (from CHAIN) do Result := (child_index = 0) or (child_index = arity + 1) end prunable: BOOLEAN is -- May items be removed? (Answer: yes.) -- (from ARRAYED_LIST) do Result := True end Readable: BOOLEAN is True -- (from TREE) readable_child: BOOLEAN is -- Is there a current item that may be read? -- (from SEQUENCE) do Result := not child_off end valid_cursor (p: CURSOR): BOOLEAN is -- Can the cursor be moved to position p? -- (from ARRAYED_LIST) local al_c: ARRAYED_LIST_CURSOR do al_c ?= p if al_c /= void then Result := valid_cursor_index (al_c.index) end end valid_cursor_index (i: INTEGER): BOOLEAN is -- Is i correctly bounded for cursor movement? -- (from TREE) 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): BOOLEAN is -- Is i a valid index? -- (from ARRAYED_LIST) do Result := (1 <= i) and (i <= arity) ensure then -- from INDEXABLE only_if_in_index_set: Result implies ((i >= index_set.lower) and (i <= index_set.upper)) ensure then -- from CHAIN valid_index_definition: Result = ((i >= 1) and (i <= arity)) end valid_index (i: INTEGER): BOOLEAN is -- Is i a valid index? -- (from ARRAYED_LIST) do Result := (1 <= i) and (i <= arity) ensure then -- from INDEXABLE only_if_in_index_set: Result implies ((i >= index_set.lower) and (i <= index_set.upper)) ensure then -- from CHAIN valid_index_definition: Result = ((i >= 1) and (i <= arity)) end Writable: BOOLEAN is True -- Is there a current item that may be modified? -- (from TREE) writable_child: BOOLEAN is -- Is there a current item that may be modified? -- (from SEQUENCE) do Result := not child_off end feature {NONE} -- Status report all_cleared: BOOLEAN is obsolete "Use `all_default' instead" -- Are all items set to default values? -- (from ARRAY) do Result := all_default end all_default: BOOLEAN is -- Are all items set to default values? -- (from ARRAY) do Result := area.all_default (upper - lower) ensure -- from ARRAY definition: Result = (arity = 0 or else ((array_item (upper) = void or else array_item (upper) = array_item (upper).default) and subarray (lower, upper - 1).all_default)) end al_empty: BOOLEAN is obsolete "ELKS 2000: Use `is_empty' instead" -- Is there no element? -- (from CONTAINER) do Result := is_leaf end al_full: BOOLEAN is -- Is structure filled to capacity? -- (from ARRAYED_LIST) do Result := (arity = capacity) end al_object_comparison: BOOLEAN -- Must search operations use equal rather than = -- for comparing references? (Default: no, use =.) -- (from CONTAINER) resizable: BOOLEAN is -- May capacity be changed? (Answer: yes.) -- (from RESIZABLE) do Result := True end same_items (other: like Current): BOOLEAN is -- Do other and Current have same items? -- (from ARRAY) require -- from ARRAY other_not_void: other /= void do if arity = other.arity then Result := area.same_items (other.area, upper - lower) end ensure -- from ARRAY definition: Result = ((arity = other.arity) and then (arity = 0 or else (array_item (upper) = other.array_item (other.upper) and subarray (lower, upper - 1).same_items (other.subarray (other.lower, other.upper - 1))))) end array_valid_index (i: INTEGER): BOOLEAN is -- Is i within the bounds of the array? -- (from ARRAY) do Result := (lower <= i) and then (i <= upper) ensure then -- from INDEXABLE only_if_in_index_set: Result implies ((i >= index_set.lower) and (i <= index_set.upper)) end valid_index_set: BOOLEAN is -- (from ARRAY) do Result := index_set.count = arity end feature -- Status setting compare_objects is -- Ensure that future search operations will use equal -- rather than = for comparing references. -- (from CONTAINER) require -- from CONTAINER changeable_comparison_criterion do object_comparison := True ensure -- from CONTAINER object_comparison end compare_references is -- Ensure that future search operations will use = -- rather than equal for comparing references. -- (from CONTAINER) require -- from CONTAINER changeable_comparison_criterion do object_comparison := False ensure -- from CONTAINER reference_comparison: not object_comparison end feature -- Cursor movement child_back is -- Move cursor one position backward. -- (from ARRAYED_LIST) require -- from TREE True require -- from BILINEAR not_before: not child_before do child_index := child_index - 1 end child_finish is -- Move cursor to last position if any. -- (from ARRAYED_LIST) do child_index := arity ensure then -- from CHAIN at_last: not is_leaf implies child_islast ensure then -- from TREE is_last_child: not is_leaf implies child_islast ensure then -- from ARRAYED_LIST before_when_empty: is_leaf implies child_before end child_forth is -- Move cursor one position forward. -- (from ARRAYED_LIST) require -- from TREE True require -- from LINEAR not_after: not child_after do child_index := child_index + 1 ensure then -- from LIST moved_forth: child_index = old child_index + 1 end child_go_i_th (i: INTEGER) is -- Move cursor to i-th position. -- (from ARRAYED_LIST) require -- from CHAIN valid_cursor_index: valid_cursor_index (i) require else -- from TREE valid_cursor_index: valid_cursor_index (i) do child_index := i ensure -- from CHAIN position_expected: child_index = i ensure then -- from TREE position: child_index = i end child_go_to (p: CURSOR) is -- Move cursor to position p. -- (from ARRAYED_LIST) require -- from TREE True require -- from CURSOR_STRUCTURE cursor_position_valid: valid_cursor (p) local al_c: ARRAYED_LIST_CURSOR do al_c ?= p check al_c /= void end child_index := al_c.index end move (i: INTEGER) is -- Move cursor i positions. -- (from ARRAYED_LIST) do child_index := child_index + i if (child_index > arity + 1) then child_index := arity + 1 elseif (child_index < 0) then child_index := 0 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) end search_child (v: like child) is -- 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 sequential_search (v) ensure -- from LINEAR object_found: (not exhausted and object_comparison) implies equal (v, child) item_found: (not exhausted and not object_comparison) implies v = child end child_start is -- Move cursor to first position if any. -- (from ARRAYED_LIST) do child_index := 1 ensure then -- from CHAIN at_first: not is_leaf implies child_isfirst ensure then -- from TREE is_first_child: not is_leaf implies child_isfirst ensure then -- from ARRAYED_LIST after_when_empty: is_leaf implies child_after end feature -- Element change append (s: SEQUENCE [ARRAYED_TREE [G]]) is -- Append a copy of s. -- (from SEQUENCE) require -- from SEQUENCE argument_not_void: s /= void local l: like s do if s = Current then l := deep_clone (s) else l := s end from l.start until l.exhausted loop al_extend (l.item) l.forth end ensure -- from SEQUENCE new_count: arity >= old arity end child_extend (v: like item) is -- Add v at end. -- Do not move child cursor. local n: like parent do n := new_cell (v) if object_comparison then n.compare_objects else n.compare_references end al_extend (n) end child_put (v: like item) is -- Replace current child item with v. -- Was declared in ARRAYED_TREE as synonym of child_replace. require -- from TREE child_writable: child_writable do if object_comparison then child.compare_objects else child.compare_references end child.replace (v) ensure -- from TREE item_inserted: child_item = v end child_put_left (v: like item) is -- Add v to the left of cursor position. -- Do not move child cursor. require -- from DYNAMIC_TREE not_child_before: not child_before local n: like parent do n := new_cell (v) if object_comparison then n.compare_objects else n.compare_references end al_put_left (n) end child_put_right (v: like item) is -- Add v to the right of cursor position. -- Do not move child cursor. require -- from DYNAMIC_TREE not_child_after: not child_after local n: like parent do n := new_cell (v) if object_comparison then n.compare_objects else n.compare_references end al_put_right (n) end child_replace (v: like item) is -- Replace current child item with v. -- Was declared in ARRAYED_TREE as synonym of child_put. require -- from TREE child_writable: child_writable do if object_comparison then child.compare_objects else child.compare_references end child.replace (v) ensure -- from TREE item_inserted: child_item = v end fill (other: TREE [G]) is -- 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 force (v: like child) is -- Add v to end. -- Do not move cursor. -- Was declared in ARRAYED_LIST as synonym of extend. -- (from ARRAYED_LIST) require -- from SEQUENCE extendible: al_extendible do arity := arity + 1 force_i_th (v, arity) ensure then -- from SEQUENCE new_count: arity = old arity + 1 item_inserted: has (v) end merge_tree_after (other: like first_child) is -- 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) al_merge_right (other) ensure -- from DYNAMIC_TREE other_is_leaf: other.is_leaf end merge_tree_before (other: like first_child) is -- 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) al_merge_left (other) ensure -- from DYNAMIC_TREE other_is_leaf: other.is_leaf end put_i_th (v: like array_item; i: INTEGER) is -- Replace i-th entry, if in index interval, by v. -- (from ARRAY) require -- from TABLE valid_key: array_valid_index (k) do area.put (v, i - lower) ensure then -- from INDEXABLE insertion_done: array_item (k) = v end put (v: like item) is -- 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 put_child (n: like parent) is -- Add n to the list of children. -- Do not move child cursor. require else -- from DYNAMIC_TREE non_void_argument: n /= void do if object_comparison then n.compare_objects else n.compare_references end al_extend (n) n.attach_to_parent (Current) end put_child_left (n: like parent) is -- 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 if object_comparison then n.compare_objects else n.compare_references end al_put_left (n) n.attach_to_parent (Current) end put_child_right (n: like parent) is -- Add n to the right of the cursor position. -- Do not move cursor. require -- from DYNAMIC_TREE not_child_after: not child_after non_void_argument: n /= void do if object_comparison then n.compare_objects else n.compare_references end al_put_right (n) n.attach_to_parent (Current) end put_front (v: like child) is -- Add v to the beginning. -- Do not move cursor. -- (from ARRAYED_LIST) do if is_leaf then al_extend (v) else insert (v, 1) end child_index := child_index + 1 ensure -- from DYNAMIC_CHAIN new_count: arity = old arity + 1 item_inserted: first_child = v end replace (v: like item) is -- 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 replace_child (n: like parent) is -- Make n the node's current child. require -- from TREE writable_child: writable_child was_root: n.is_root do if object_comparison then n.compare_objects else n.compare_references end al_replace (n) n.attach_to_parent (Current) ensure -- from TREE child_replaced: child = n ensure then child_replaced: n.parent = Current end sprout is -- Make current node a root. -- (from TREE) do if parent /= void then parent.prune (Current) end end feature {NONE} -- Element change enter (v: like array_item; i: INTEGER) is -- Replace i-th entry, if in index interval, by v. -- (from ARRAY) require -- from ARRAY valid_key: array_valid_index (i) do area.put (v, i - lower) end al_extend (v: like child) is -- Add v to end. -- Do not move cursor. -- Was declared in ARRAYED_LIST as synonym of force. -- (from ARRAYED_LIST) require -- from COLLECTION extendible: al_extendible do arity := arity + 1 force_i_th (v, arity) ensure -- from COLLECTION item_inserted: is_inserted (v) ensure then -- from BAG one_more_occurrence: occurrences (v) = old (occurrences (v)) + 1 end al_fill (other: CONTAINER [ARRAYED_TREE [G]]) is -- 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 al_extendible local lin_rep: LINEAR [ARRAYED_TREE [G]] do lin_rep := other.linear_representation from lin_rep.start until not al_extendible or else lin_rep.off loop al_extend (lin_rep.item) lin_rep.forth end end force_i_th (v: like array_item; i: INTEGER) is -- Assign item v to i-th entry. -- Always applicable: resize the array if i falls out of -- currently defined bounds; preserve existing items. -- (from ARRAY) do if i < lower then auto_resize (i, upper) elseif i > upper then auto_resize (lower, i) end put_i_th (v, i) ensure -- from ARRAY inserted: array_item (i) = v higher_count: arity >= old arity end al_merge_left (other: ARRAYED_LIST [ARRAYED_TREE [G]]) is -- Merge other into current structure before cursor. -- (from ARRAYED_LIST) require -- from DYNAMIC_CHAIN extendible: al_extendible not_before: not child_before other_exists: other /= void local old_index: INTEGER old_other_count: INTEGER do old_index := child_index old_other_count := other.count child_index := child_index - 1 al_merge_right (other) child_index := old_index + old_other_count ensure -- from DYNAMIC_CHAIN new_count: arity = old arity + old other.count new_index: child_index = old child_index + old other.count other_is_empty: other.is_empty ensure then -- from DYNAMIC_LIST other_empty: other.is_empty end al_merge_right (other: ARRAYED_LIST [ARRAYED_TREE [G]]) is -- Merge other into current structure after cursor. -- (from ARRAYED_LIST) require -- from DYNAMIC_CHAIN extendible: al_extendible not_after: not child_after other_exists: other /= void do if not other.is_empty then resize (1, arity + other.count) if child_index < arity then subcopy (Current, child_index + 1, arity, child_index + other.count + 1) end subcopy (other, 1, other.count, child_index + 1) arity := arity + other.count other.wipe_out end ensure -- from DYNAMIC_CHAIN new_count: arity = old arity + old other.count same_index: child_index = old child_index other_is_empty: other.is_empty ensure then -- from DYNAMIC_LIST other_empty: other.is_empty end al_put (v: like child) is -- Replace current item by v. -- (Synonym for replace) -- (from CHAIN) require -- from COLLECTION extendible: al_extendible do al_replace (v) ensure -- from COLLECTION item_inserted: is_inserted (v) ensure then -- from CHAIN same_count: arity = old arity end sequence_put (v: like child) is -- Add v to end. -- (from SEQUENCE) require -- from COLLECTION extendible: al_extendible do al_extend (v) ensure -- from COLLECTION item_inserted: is_inserted (v) ensure then -- from SEQUENCE new_count: arity = old arity + 1 end al_put_left (v: like child) is -- Add v to the left of current position. -- Do not move cursor. -- (from ARRAYED_LIST) require -- from DYNAMIC_CHAIN extendible: al_extendible not_before: not child_before do if child_after or is_leaf then al_extend (v) else insert (v, child_index) end child_index := child_index + 1 ensure -- from DYNAMIC_CHAIN new_count: arity = old arity + 1 new_index: child_index = old child_index + 1 end al_put_right (v: like child) is -- Add v to the right of current position. -- Do not move cursor. -- (from ARRAYED_LIST) require -- from DYNAMIC_CHAIN extendible: al_extendible not_after: not child_after do if child_index = arity then al_extend (v) else insert (v, child_index + 1) end ensure -- from DYNAMIC_CHAIN new_count: arity = old arity + 1 same_index: child_index = old child_index end al_replace (v: like first_child) is -- Replace current item by v. -- (from ARRAYED_LIST) require -- from ACTIVE writable: writable_child do put_i_th (v, child_index) ensure -- from ACTIVE item_replaced: child = v end set_area (other: like area) is -- Make other the new area -- (from TO_SPECIAL) do area := other end feature {ARRAYED_LIST} -- Element change subcopy (other: like Current; start_pos, end_pos, index_pos: INTEGER) is -- Copy items of other within bounds start_pos and end_pos -- to current array starting at index index_pos. -- (from ARRAY) require -- from ARRAY other_not_void: other /= void valid_start_pos: other.array_valid_index (start_pos) valid_end_pos: other.array_valid_index (end_pos) valid_bounds: (start_pos <= end_pos) or (start_pos = end_pos + 1) valid_index_pos: array_valid_index (index_pos) enough_space: (upper - index_pos) >= (end_pos - start_pos) local other_area: like area other_lower: INTEGER start0, end0, index0: INTEGER do other_area := other.area other_lower := other.lower start0 := start_pos - other_lower end0 := end_pos - other_lower index0 := index_pos - lower spsubcopy ($other_area, $area, start0, end0, index0) end feature -- Removal prune (v: like child) is -- Remove first occurrence of v, if any, -- after cursor position. -- Move cursor to right neighbor. -- (or after if no right neighbor or v does not occur) -- (from ARRAYED_LIST) require -- from COLLECTION prunable: prunable require -- from TREE is_child: n.parent = Current do if child_before then child_index := 1 end if object_comparison then if v /= void then from until child_after or else (child /= void and then v.is_equal (child)) loop child_forth end end else from until child_after or else child = v loop child_forth end end if not child_after then al_remove end ensure -- from TREE n_is_root: n.is_root end prune_all (v: like child) is -- Remove all occurrences of v. -- (Reference or object equality, -- based on object_comparison.) -- (from ARRAYED_LIST) require -- from COLLECTION prunable local i: INTEGER offset: INTEGER res: BOOLEAN obj_cmp: BOOLEAN default_val: like child do obj_cmp := object_comparison from i := 1 until i > arity loop if i <= arity - offset then if offset > 0 then put_i_th (array_item (i + offset), i) end if obj_cmp then res := equal (v, array_item (i)) else res := (v = array_item (i)) end if res then offset := offset + 1 else i := i + 1 end else put_i_th (default_val, i) i := i + 1 end end arity := arity - offset child_index := arity + 1 ensure -- from COLLECTION no_more_occurrences: not has (v) ensure then -- from DYNAMIC_CHAIN is_exhausted: exhausted ensure then -- from ARRAYED_LIST is_after: child_after end remove_child is -- Remove child at cursor position. -- Move cursor to the next sibling, or after if none. require -- from DYNAMIC_TREE child_not_off: not child_off do child.attach_to_parent (void) al_remove ensure -- from DYNAMIC_TREE new_arity: arity = old arity - 1 new_child_index: child_index = old child_index end remove_left_child is -- Remove item to the left of cursor position. -- Do not move cursor. require -- from DYNAMIC_TREE is_not_first: not child_isfirst do child_back remove_child ensure -- from DYNAMIC_TREE new_arity: arity = old arity - 1 new_child_index: child_index = old child_index - 1 end remove_right_child is -- Remove item to the right of cursor position. -- Do not move cursor. require -- from DYNAMIC_TREE is_not_last: not child_islast do child_forth remove_child child_back ensure -- from DYNAMIC_TREE new_arity: arity = old arity - 1 new_child_index: child_index = old child_index end wipe_out is -- Remove all items. -- (from ARRAYED_LIST) require -- from DYNAMIC_TREE True require -- from COLLECTION prunable do arity := 0 child_index := 0 discard_items ensure -- from COLLECTION wiped_out: is_leaf ensure then -- from DYNAMIC_LIST is_before: child_before end feature {NONE} -- Removal clear_all is -- Reset all items to default values. -- (from ARRAY) do area.clear_all ensure -- from ARRAY stable_lower: lower = old lower stable_upper: upper = old upper default_items: all_default end discard_items is -- Reset all items to default values with reallocation. -- (from ARRAY) do make_area (capacity) ensure -- from ARRAY default_items: all_default end al_remove is -- Remove current item. -- Move cursor to right neighbor -- (or after if no right neighbor) -- (from ARRAYED_LIST) require -- from ACTIVE prunable: prunable writable: writable_child local default_value: ARRAYED_TREE [G] do if child_index < arity then subcopy (Current, child_index + 1, arity, child_index) end arity := arity - 1 area.put (default_value, arity) ensure then -- from DYNAMIC_LIST after_when_empty: is_leaf implies child_after ensure then -- from ARRAYED_LIST index: child_index = old child_index end al_remove_left is -- Remove item to the left of cursor position. -- Do not move cursor. -- (from ARRAYED_LIST) require -- from DYNAMIC_CHAIN left_exists: child_index > 1 require else -- from DYNAMIC_LIST not_before: not child_before do child_index := child_index - 1 al_remove ensure -- from DYNAMIC_CHAIN new_count: arity = old arity - 1 new_index: child_index = old child_index - 1 end al_remove_right is -- Remove item to the right of cursor position -- Do not move cursor -- (from ARRAYED_LIST) require -- from DYNAMIC_CHAIN right_exists: child_index < arity do child_index := child_index + 1 al_remove child_index := child_index - 1 ensure -- from DYNAMIC_CHAIN new_count: arity = old arity - 1 same_index: child_index = old child_index end chain_wipe_out is -- Remove all items. -- (from DYNAMIC_CHAIN) require -- from COLLECTION prunable do from child_start until is_leaf loop al_remove end ensure -- from COLLECTION wiped_out: is_leaf end feature {NONE} -- Resizing automatic_grow is -- Change the capacity to accommodate at least -- Growth_percentage more items. -- (from RESIZABLE) do grow (capacity + additional_space) ensure -- from RESIZABLE increased_capacity: capacity >= old capacity + old capacity * growth_percentage // 100 end grow (i: INTEGER) is -- Change the capacity to at least i. -- (from ARRAY) do if i > capacity then resize (lower, upper + i - capacity) end ensure -- from RESIZABLE new_capacity: capacity >= i end resize (min_index, max_index: INTEGER) is -- Rearrange array so that it can accommodate -- indices down to min_index and up to max_index. -- Do not lose any previously entered item. -- (from ARRAY) require -- from ARRAY good_indices: min_index <= max_index local old_size, new_size, old_count: INTEGER new_lower, new_upper: INTEGER do if empty_area then new_lower := min_index new_upper := max_index else new_lower := min_index.min (lower) new_upper := max_index.max (upper) end new_size := new_upper - new_lower + 1 if not empty_area then old_size := area.count old_count := upper - lower + 1 end if empty_area then make_area (new_size) elseif new_size > old_size or new_lower < lower then area := arycpy ($area, new_size, lower - new_lower, old_count) end lower := new_lower upper := new_upper ensure -- from ARRAY no_low_lost: lower = min_index or else lower = old lower no_high_lost: upper = max_index or else upper = old upper end feature -- Transformation swap (i: INTEGER) is -- Exchange item at i-th position with item -- at cursor position. -- (from ARRAYED_LIST) require -- from CHAIN not_off: not child_off valid_index: array_valid_index (i) local old_item: like child do old_item := child al_replace (area.item (i - 1)) area.put (old_item, i - 1) ensure -- from CHAIN swapped_to_item: child = old array_item (i) swapped_from_item: array_item (i) = old child end feature -- Conversion binary_representation: BINARY_TREE [G] is -- Convert to binary tree representation: -- first child becomes left child, -- right sibling becomes right child. -- (from TREE) local current_sibling: BINARY_TREE [G] do create Result.make (item) if not is_leaf then Result.put_left_child (first_child.binary_representation) from child_start child_forth current_sibling := Result.left_child until child_after loop current_sibling.put_right_child (child.binary_representation) current_sibling := current_sibling.right_child 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]) is -- 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: BINARY_TREE [G] 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 child.fill_from_binary (current_node) current_node := current_node.right_child end end end linear_representation: LINEAR [G] is -- Representation as a linear structure -- (from TREE) local al: ARRAYED_LIST [G] do create al.make (count) al.start al.extend (item) fill_list (al) Result := al end feature {NONE} -- Conversion al_lin_rep: LINEAR [ARRAYED_TREE [G]] is -- Representation as a linear structure -- (from LINEAR) do Result := Current end to_c: ANY is -- Address of actual sequence of values, -- for passing to external (non-Eiffel) routines. -- (from ARRAY) do Result := area end feature -- Duplication copy (other: like Current) is -- Copy contents from other. require -- from ANY other_not_void: other /= void type_identity: same_type (other) local tmp_tree: like Current obj_comparison: BOOLEAN do obj_comparison := other.object_comparison create tmp_tree.make (other.arity, other.item) if not other.is_leaf then tree_copy (other, tmp_tree) end object_comparison := obj_comparison ensure -- from ANY is_equal: is_equal (other) ensure then -- from ARRAY equal_areas: area.is_equal (other.area) end duplicate (n: INTEGER): like Current is -- Copy of sub-tree beginning at cursor position and -- having min (n, arity - child_index + 1) -- children. require -- from TREE not_child_off: not child_off valid_sublist: n >= 0 local counter: INTEGER pos: CURSOR do from Result := new_node pos := child_cursor Result.child_start until child_after or else (counter = n) loop if child /= void then Result.replace_child (child.duplicate_all) end Result.child_forth child_forth counter := counter + 1 end child_go_to (pos) end feature {NONE} -- Duplication al_duplicate (n: INTEGER): like Current is -- Copy of sub-list beginning at current position -- and having min (n, count - index + 1) items. -- (from ARRAYED_LIST) require -- from CHAIN not_off_unless_after: child_off implies child_after valid_subchain: n >= 0 local end_pos: INTEGER do if child_after then create Result.al_make (0) else end_pos := arity.min (child_index + n - 1) create Result.make_filled (end_pos - child_index + 1) Result.subcopy (Current, child_index, end_pos, 1) end end subarray (start_pos, end_pos: INTEGER): like Current is -- Array made of items of current array within -- bounds start_pos and end_pos. -- (from ARRAY) require -- from ARRAY valid_start_pos: array_valid_index (start_pos) valid_end_pos: array_valid_index (end_pos) valid_bounds: (start_pos <= end_pos) or (start_pos = end_pos + 1) do create Result.array_make (start_pos, end_pos) Result.subcopy (Current, start_pos, end_pos, start_pos) ensure -- from ARRAY lower: Result.lower = start_pos upper: Result.upper = end_pos end feature {NONE} -- Inapplicable bag_put (v: ARRAYED_TREE [G]) is -- (from TABLE) require -- from COLLECTION extendible: al_extendible do ensure -- from COLLECTION item_inserted: is_inserted (v) end extend (v: G) is -- Add v as new child. do end new_chain: like Current is -- Unused -- (from ARRAYED_LIST) do ensure -- from DYNAMIC_CHAIN result_exists: Result /= void end feature {ARRAYED_TREE} -- Implementation attach_to_parent (n: like parent) is -- Make n parent of current node; do parent := n ensure -- from TREE new_parent: parent = n end duplicate_all: like Current is -- Copy of sub-tree including all children local pos: CURSOR do from Result := new_node pos := child_cursor Result.child_start child_start until child_off loop if child /= void then Result.replace_child (child.duplicate_all) end Result.child_forth child_forth end child_go_to (pos) end fill_subtree (other: TREE [G]) is -- Fill children with children of other local temp: like parent do from other.child_start child_start until child_after loop if other.child /= void then create temp.make (other.arity, other.child_item) temp.fill_subtree (other.child) end replace_child (temp) child_forth other.child_forth end end new_node: like Current is -- A newly created instance of the same type, -- with the same arity and node value. -- This feature may be redefined in descendants so as to -- produce an adequately allocated and initialized object. do create Result.make (arity, item) end feature {NONE} -- Implementation arycpy (old_area: POINTER; newsize, s, n: INTEGER): like area is -- New area of size newsize containing n items -- from oldarea. -- Old items are at position s in new area. -- (from ARRAY) external "C | %"eif_misc.h%"" end attach (other: like first_child) is -- Attach all children of other to current node. -- Put other in mode off. local c: like child do from other.child_start until other.child_off loop c := other.child other.child_forth c.attach_to_parent (Current) end end auto_resize (min_index, max_index: INTEGER) is -- Rearrange array so that it can accommodate -- indices down to min_index and up to max_index. -- Do not lose any previously entered item. -- If area must be extended, ensure that space for at least -- additional_space item is added. -- (from ARRAY) require -- from ARRAY valid_indices: min_index <= max_index local old_size, new_size: INTEGER new_lower, new_upper: INTEGER do if empty_area then new_lower := min_index new_upper := max_index else new_lower := min_index.min (lower) new_upper := max_index.max (upper) end new_size := new_upper - new_lower + 1 if not empty_area then old_size := area.count if new_size > old_size and new_size - old_size < additional_space then new_size := old_size + additional_space end end if empty_area then make_area (new_size) elseif new_size > old_size or new_lower < lower then area := arycpy ($area, new_size, lower - new_lower, capacity) end lower := new_lower upper := new_upper end child_remove is -- Remove item of current child -- (from TREE) do end empty_area: BOOLEAN is -- Is area empty? -- (from ARRAY) do Result := (area.count = 0) end insert (v: like child; pos: INTEGER) is -- Add v at pos, moving subsequent items -- to the right. -- (from ARRAYED_LIST) require -- from ARRAYED_LIST index_small_enough: pos <= arity index_large_enough: pos >= 1 do if arity + 1 > capacity then auto_resize (lower, arity + 1) end arity := arity + 1 subcopy (Current, pos, arity - 1, pos + 1) enter (v, pos) ensure -- from ARRAYED_LIST new_count: arity = old arity + 1 index_unchanged: child_index = old child_index insertion_done: array_item (pos) = v end new_cell (v: like item): like Current is -- New node with value v and no children. do create Result.make (0, v) Result.attach_to_parent (Current) end new_tree: like Current is -- A newly created instance of the same type. do create Result.make (0, item) ensure -- from DYNAMIC_TREE result_exists: Result /= void result_item: Result.item = item end position_in_parent: INTEGER is -- Position of current node in parent do if parent /= void then Result := parent.index_of (Current, 1) end end remove is -- Remove current item -- (from TREE) do end spsubcopy (source, target: POINTER; s, e, i: INTEGER) is -- Copy elements of source within bounds s -- and e to target starting at index i. -- (from ARRAY) external "C | %"eif_copy.h%"" end tree_copy (other, tmp_tree: like Current) is -- 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 p1, p2: like Current other_stack, tmp_stack: LINKED_STACK [like Current] idx_stack, orgidx_stack: LINKED_STACK [INTEGER] 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.child = void end p2.replace_child (clone (p1.child)) if other_stack.is_empty then p2.child.attach_to_parent (Current) end check comparison_mode_ok: p2.child.object_comparison = p1.child.object_comparison p1_consistent: p1.child.parent = p1 p2_consistent: p2.child.parent = p2 end if not p1.child.is_leaf then other_stack.put (p1) tmp_stack.put (p2) idx_stack.put (i + 1) p1 := p1.child p2 := p2.child orgidx_stack.put (p1.child_index) i := 0 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) 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 check tree_stacks_empty: other_stack.is_empty and tmp_stack.is_empty at_root: p1 = other and p2 = tmp_tree end standard_copy (tmp_tree) child_go_i_th (orgidx_stack.item) orgidx_stack.remove check index_stack_empty: orgidx_stack.is_empty end end tree_is_equal (t1, t2: like Current): BOOLEAN is -- 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 i: INTEGER p1, p2: like Current t1_stack, t2_stack: LINKED_STACK [like Current] idx_stack, orgidx1_stack, orgidx2_stack: LINKED_STACK [INTEGER] do if t1.is_leaf and t2.is_leaf then if t1.object_comparison then Result := equal (t1.item, t2.item) else Result := (t1.item = t2.item) end elseif t1.is_leaf xor t2.is_leaf then Result := False else create t1_stack.make create t2_stack.make create idx_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 i := 1 p1 := t1 p2 := t2 invariant same_count: t1_stack.count = t2_stack.count and t2_stack.count = idx_stack.count until not Result or else (i > p1.child_capacity and t1_stack.is_empty) loop check p1_not_void: p1 /= void p2_not_void: p2 /= void end p1.child_go_i_th (i) p2.child_go_i_th (i) if p1.child_readable and p2.child_readable and p1.child_capacity = p2.child_capacity then check p1_consistent: p1.child.parent = p1 p2_consistent: p2.child.parent = p2 end if t1.object_comparison then Result := equal (p1.item, p2.item) else Result := (p1.item = p2.item) end if not (p1.child.is_leaf or p2.child.is_leaf) then t1_stack.put (p1) t2_stack.put (p2) idx_stack.put (i + 1) p1 := p1.child p2 := p2.child orgidx1_stack.put (p1.child_index) orgidx2_stack.put (p2.child_index) i := 0 elseif p1.child.is_leaf xor p2.child.is_leaf then Result := False end elseif p1.child_capacity /= p2.child_capacity or else (p1.child_readable xor p2.child_readable) then Result := False end if i <= p1.child_capacity then i := i + 1 else from invariant same_count: t1_stack.count = t2_stack.count and t2_stack.count = idx_stack.count until t1_stack.is_empty or else i <= p1.child_capacity 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 i := idx_stack.item t1_stack.remove t2_stack.remove idx_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 end feature {TREE} -- Implementation fill_list (al: ARRAYED_LIST [G]) is -- Fill al with all the children's items. -- (from TREE) do from child_start until child_off loop if child /= void then al.extend (child_item) child.fill_list (al) end child_forth end end subtree_count: INTEGER is -- Number of items in children -- (from TREE) local pos: CURSOR do Result := arity from pos := child_cursor child_start until child_off loop if child /= void then Result := Result + child.subtree_count end child_forth end child_go_to (pos) end subtree_has (v: G): BOOLEAN is -- Do children include v? -- (Reference or object equality, -- based on object_comparison.) -- (from TREE) local cursor: CURSOR do cursor := child_cursor from child_start until child_off or else Result loop if child /= void then if object_comparison then Result := (v /= void) and then (child_item /= void) and then v.is_equal (child_item) else Result := v = child_item end end child_forth end from child_start until child_off or else Result loop if child /= void then Result := child.subtree_has (v) end child_forth end child_go_to (cursor) end feature -- Iteration do_all (action: PROCEDURE [ANY, TUPLE [ARRAYED_TREE [G]]]) is -- 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 t: TUPLE [ARRAYED_TREE [G]] do create t.make from child_start until child_after loop t.put (child, 1) action.call (t) child_forth end end do_if (action: PROCEDURE [ANY, TUPLE [ARRAYED_TREE [G]]]; test: FUNCTION [ANY, TUPLE [ARRAYED_TREE [G]], BOOLEAN]) is -- Apply action to every item that satisfies test. -- Semantics not guaranteed if action or test changes the structure; -- in such a case, apply iterator to clone of structure instead. -- (from LINEAR) require -- from TRAVERSABLE action_exists: action /= void test_exits: test /= void local t: TUPLE [ARRAYED_TREE [G]] do create t.make from child_start until child_after loop t.put (child, 1) if test.item (t) then action.call (t) end child_forth end end for_all (test: FUNCTION [ANY, TUPLE [ARRAYED_TREE [G]], BOOLEAN]): BOOLEAN is -- Is test true for all items? -- (from LINEAR) require -- from TRAVERSABLE test_exits: test /= void local cs: CURSOR_STRUCTURE [ARRAYED_TREE [G]] c: CURSOR t: TUPLE [ARRAYED_TREE [G]] do create t.make cs ?= Current if cs /= void then c := cs.cursor end from child_start Result := True until child_after or not Result loop t.put (child, 1) Result := test.item (t) child_forth end if cs /= void then cs.go_to (c) end end there_exists (test: FUNCTION [ANY, TUPLE [ARRAYED_TREE [G]], BOOLEAN]): BOOLEAN is -- Is test true for at least one item? -- (from LINEAR) require -- from TRAVERSABLE test_exits: test /= void local cs: CURSOR_STRUCTURE [ARRAYED_TREE [G]] c: CURSOR t: TUPLE [ARRAYED_TREE [G]] do create t.make cs ?= Current if cs /= void then c := cs.cursor end from child_start until child_after or Result loop t.put (child, 1) Result := test.item (t) child_forth end if cs /= void then cs.go_to (c) end end feature {NONE} -- Not Applicable set_child (n: like parent) is require -- from DYNAMIC_TREE non_void_argument: n /= void do end invariant -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) -- from DYNAMIC_TREE extendible_definition: extendible child_after_definition: child_after = (child_index = arity + 1) -- from TREE 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) child_consistency: child_readable implies child.parent = Current -- from ARRAYED_LIST prunable: prunable starts_from_one: lower = 1 empty_means_storage_empty: is_leaf implies all_default -- from ARRAY area_exists: area /= void consistent_size: capacity = upper - lower + 1 non_negative_count: arity >= 0 index_set_has_same_count: valid_index_set index_set_has_same_bounds: ((index_set.lower = lower) and (index_set.upper = lower + arity - 1)) -- from RESIZABLE increase_by_at_least_one: minimal_increase >= 1 -- from BOUNDED valid_count: arity <= capacity full_definition: al_full = (arity = capacity) -- from FINITE empty_definition: is_leaf = (arity = 0) non_negative_count: arity >= 0 -- from INDEXABLE index_set_not_void: index_set /= void -- 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 = array_item (child_index)) index_set_has_same_count: index_set.count = arity -- from ACTIVE writable_constraint: writable_child implies readable_child empty_constraint: is_leaf implies (not readable_child) and (not writable_child) -- 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 DYNAMIC_CHAIN extendible: al_extendible indexing library: "[ EiffelBase: Library of reusable components for Eiffel. ]" status: "[ Copyright 1986-2001 Interactive Software Engineering (ISE). For ISE customers the original versions are an ISE product covered by the ISE Eiffel license and support agreements. ]" license: "[ EiffelBase may now be used by anyone as FREE SOFTWARE to develop any product, public-domain or commercial, without payment to ISE, under the terms of the ISE Free Eiffel Library License (IFELL) at http://eiffel.com/products/base/license.html. ]" source: "[ Interactive Software Engineering Inc. ISE Building 360 Storke Road, Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Electronic mail <info@eiffel.com> Customer support http://support.eiffel.com ]" info: "[ For latest info see award-winning pages: http://eiffel.com ]" end -- class ARRAYED_TREE
Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:

-- Generated by ISE Eiffel --
For more details: www.eiffel.com