Automatic generation produced by ISE Eiffel

Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:
indexing description: "[ Trees where each node has a fixed number of children (The number of children is arbitrary but cannot be changed once the node has been created ]" names: fixed_tree, tree, fixed_list 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 FIXED_TREE [G] create make feature -- Initialization make (n: INTEGER; v: G) is -- Create node with n void children and item v. require valid_number_of_children: n >= 0 do fl_make_filled (n) replace (v) ensure node_item: item = v node_arity: arity = n end feature {NONE} -- Initialization fl_make (n: INTEGER) is -- Create an empty list. -- (from FIXED_LIST) do array_make (1, n) ensure -- from FIXED_LIST is_before: child_before new_count: arity = 0 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 fl_make_filled (n: INTEGER) is -- Create a list with n void elements. -- (from FIXED_LIST) do array_make (1, n) arity := n ensure -- from FIXED_LIST is_before: child_before new_count: arity = n end make_from_array (a: ARRAY [FIXED_TREE [G]]) is -- Initialize from the items of a. -- (Useful in proper descendants of class ARRAY, -- to initialize an array-like object from a manifest array.) -- (from ARRAY) require -- from ARRAY array_exists: a /= void do area := a.area lower := a.lower upper := a.upper end feature {FIXED_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 of active child require -- from TREE readable: child_readable do Result := child.item end child_cursor: CURSOR is -- Current cursor position -- (from FIXED_LIST) do create {ARRAYED_LIST_CURSOR} Result.make (child_index) end first_child: like parent is -- Leftmost child require -- from CHAIN not_empty: not is_leaf require -- from TREE is_not_leaf: not is_leaf do Result := array_item (1) end child_index: INTEGER -- Current position in the list -- (from FIXED_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: FIXED_TREE [G] is -- Current item -- (from FIXED_LIST) require -- from TRAVERSABLE not_off: not child_off require -- from ACTIVE readable: fl_readable require -- from TREE readable: readable_child do Result := area.item (child_index - 1) end last_child: like first_child is -- Item at last position -- (from FIXED_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: FIXED_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: FIXED_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): FIXED_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: valid_index (k) do Result := area.item (i - lower) end feature {NONE} -- Access entry (i: INTEGER): FIXED_TREE [G] is -- Entry at index i, if in index interval -- (from ARRAY) require -- from ARRAY valid_key: valid_index (i) do Result := array_item (i) 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 fl_has (v: FIXED_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_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 {FIXED_TREE} -- Access array_item (i: INTEGER): FIXED_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: valid_index (k) do Result := area.item (i - lower) end feature -- Access area: SPECIAL [FIXED_TREE [G]] -- Special data zone -- (from TO_SPECIAL) 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 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 -- (from FIXED_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: FIXED_TREE [G]): INTEGER is -- Number of times v appears in structure -- (from ARRAY) local i: INTEGER do if object_comparison then if v /= void then from i := lower until i > upper loop if array_item (i) /= void and then v.is_equal (array_item (i)) then Result := Result + 1 end i := i + 1 end end else from i := lower until i > upper loop if array_item (i) = v then Result := Result + 1 end i := i + 1 end end 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 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 -- 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 fl_changeable_object_criterion: BOOLEAN is -- May object_comparison be changed? -- (Answer: yes by default.) -- (from CONTAINER) do Result := True end child_contractable: BOOLEAN is -- May items be removed? do Result := not child_off ensure Result = not child_off 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 Full: BOOLEAN is True -- Is tree full? 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: FIXED_TREE [G]): BOOLEAN is -- Has v been inserted by the most recent insertion? -- (By default, the value returned is equivalent to calling -- `has (v)'. However, descendants might be able to provide more -- efficient implementations.) -- (from COLLECTION) do Result := has (v) end is_leaf: BOOLEAN is -- 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) fl_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? -- (from FIXED_LIST) do Result := not is_leaf end Readable: BOOLEAN is True -- (from TREE) readable_child: BOOLEAN is -- Is there a current child to be read? -- (from TREE) do Result := not child_off end Resizable: BOOLEAN is False -- May capacity be changed? (Answer: no.) -- (from FIXED) valid_cursor (p: CURSOR): BOOLEAN is -- Is p a valid cursor? -- (from FIXED_LIST) local fl_c: ARRAYED_LIST_CURSOR do fl_c ?= p if fl_c /= void then Result := valid_cursor_index (fl_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 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)) 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 child that may be modified? -- (from TREE) 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 fl_empty: BOOLEAN is obsolete "ELKS 2000: Use `is_empty' instead" -- Is there no element? -- (from CONTAINER) do Result := is_leaf end fl_extendible: BOOLEAN is -- May new items be added? -- (from FIXED_LIST) do Result := (arity < capacity) end array_extendible: BOOLEAN is -- May items be added? -- (Answer: no, although array may be resized.) -- (from ARRAY) do Result := False end fl_full: BOOLEAN is -- Is the list full? -- (from FIXED_LIST) do Result := arity = capacity end fl_readable: BOOLEAN is -- Is there a current item that may be read? -- (from SEQUENCE) do Result := not child_off 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 valid_index_set: BOOLEAN is -- (from ARRAY) do Result := index_set.count = arity end fl_writable: BOOLEAN is -- Is there a current item that may be modified? -- (from SEQUENCE) do Result := not child_off 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 to previous position, if any. -- (from FIXED_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. -- (from FIXED_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 end child_forth is -- Move cursor to next position, if any. -- (from FIXED_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 FIXED_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 element remembered in p. -- (from FIXED_LIST) require -- from TREE True require -- from CURSOR_STRUCTURE cursor_position_valid: valid_cursor (p) local fl_c: ARRAYED_LIST_CURSOR do fl_c ?= p check fl_c /= void end child_index := fl_c.index end move (i: INTEGER) is -- Move cursor i positions. -- (from FIXED_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. -- (from FIXED_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 end feature -- Element change append (s: SEQUENCE [FIXED_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 extend (l.item) l.forth end ensure -- from SEQUENCE new_count: arity >= old arity end child_put (v: like item) is -- Replace current child item with v -- Was declared in FIXED_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_replace (v: like item) is -- Replace current child item with v -- Was declared in FIXED_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 extend (v: like child) is -- Add v to end. -- Move index to the current item. -- (from FIXED_LIST) require -- from COLLECTION extendible: fl_extendible do arity := arity + 1 child_index := arity 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 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 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_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: valid_index (k) do area.put (v, i - lower) ensure then -- from INDEXABLE insertion_done: array_item (k) = v end put_child (n: like parent) is -- Make n the node's child. -- Was declared in FIXED_TREE as synonym of replace_child. do if object_comparison then n.compare_objects else n.compare_references end fl_replace (n) n.attach_to_parent (Current) ensure then child_replaced: n.parent = Current end put_left (v: like item) is -- Add v to the left of current node. require is_not_root: not is_root has_left_sibling: left_sibling /= void do parent.child_go_i_th (position_in_parent - 1) parent.child_replace (v) ensure item_put: left_sibling.item = v end put_left_sibling (other: like parent) is -- Make other the left sibling of current node. require is_not_root: not is_root has_left_sibling: left_sibling /= void do parent.child_go_i_th (position_in_parent - 1) parent.replace_child (other) ensure left_sibling_replaced: left_sibling = other end put_right (v: like item) is -- Add v to the right of current node. require is_not_root: not is_root has_right_sibling: right_sibling /= void do parent.child_go_i_th (position_in_parent + 1) parent.child_replace (v) ensure item_put: right_sibling.item = v end put_right_sibling (other: like parent) is -- Make other the right sibling of current node. require is_not_root: not is_root has_right_sibling: right_sibling /= void do parent.child_go_i_th (position_in_parent + 1) parent.replace_child (other) ensure right_sibling_replaced: right_sibling = other 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 child. -- Was declared in FIXED_TREE as synonym of put_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 fl_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: valid_index (i) do area.put (v, i - lower) end fl_fill (other: CONTAINER [FIXED_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 fl_extendible local lin_rep: LINEAR [FIXED_TREE [G]] do lin_rep := other.linear_representation from lin_rep.start until not fl_extendible or else lin_rep.off loop 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 fl_put (v: like first_child) is -- Replace current item by v. -- (Synonym for replace) -- (from FIXED_LIST) require -- from COLLECTION extendible: fl_extendible require else -- from FIXED_LIST True do fl_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: fl_extendible do extend (v) ensure -- from COLLECTION item_inserted: is_inserted (v) ensure then -- from SEQUENCE new_count: arity = old arity + 1 end fl_remove is -- Remove current item. -- Move cursor to right neighbor -- (or after if no right neighbor) -- (from FIXED_LIST) require -- from ACTIVE prunable: prunable writable: fl_writable local i, j: INTEGER default_value: FIXED_TREE [G] do if not child_off then from i := child_index - 1 until i >= arity - 1 loop j := i + 1 area.put (area.item (j), i) i := j end put_i_th (default_value, arity) arity := arity - 1 end end fl_replace (v: like first_child) is -- Replace current item by v. -- (from FIXED_LIST) require -- from ACTIVE writable: fl_writable 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 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.valid_index (start_pos) valid_end_pos: other.valid_index (end_pos) valid_bounds: (start_pos <= end_pos) or (start_pos = end_pos + 1) valid_index_pos: 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 remove_child is -- Remove active child. do fl_replace (void) ensure then child_removed: child = void end wipe_out is obsolete "Not applicable since not `prunable'. Use `discard_items' instead." -- Make array empty. -- (from ARRAY) require -- from COLLECTION prunable do discard_items ensure -- from COLLECTION wiped_out: is_leaf 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 prune_all (v: FIXED_TREE [G]) is -- Remove all occurrences of v. -- (Reference or object equality, -- based on object_comparison.) -- (from COLLECTION) require -- from COLLECTION prunable do from until not has (v) loop prune (v) end ensure -- from COLLECTION no_more_occurrences: not has (v) 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 FIXED_LIST) require -- from CHAIN not_off: not child_off valid_index: valid_index (i) local old_item: like child do old_item := child fl_replace (array_item (i)) put_i_th (old_item, i) 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 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 fl_lin_rep: LINEAR [FIXED_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 do create tmp_tree.make (other.arity, other.item) if not other.is_leaf then tree_copy (other, tmp_tree) end 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 CHAIN not_off_unless_after: child_off implies child_after valid_subchain: n >= 0 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 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: valid_index (start_pos) valid_end_pos: 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: FIXED_TREE [G]) is -- (from TABLE) require -- from COLLECTION extendible: fl_extendible do ensure -- from COLLECTION item_inserted: is_inserted (v) end feature -- Inapplicable prune (v: FIXED_TREE [G]) is -- Remove first occurrence of v if any. -- (Precondition is False.) -- (from ARRAY) require -- from COLLECTION prunable: prunable require -- from TREE is_child: n.parent = Current do ensure -- from TREE n_is_root: n.is_root end feature {FIXED_TREE} -- Implementation attach_to_parent (n: like parent) is -- Make n parent of current node -- and set position_in_parent. do parent := n position_in_parent := n.child_index 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 -- Instance of class `like Current'. -- New allocated node of arity arity -- and node value item 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 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 Extendible: BOOLEAN is False -- May new items be added? force (v: like child) is -- Not used since extend is not always applicable. -- (from FIXED_LIST) require -- from SEQUENCE extendible: fl_extendible do ensure then -- from SEQUENCE new_count: arity = old arity + 1 item_inserted: has (v) end position_in_parent: INTEGER -- Position of current node in parent 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 [FIXED_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 [FIXED_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 [FIXED_TREE [G]]]; test: FUNCTION [ANY, TUPLE [FIXED_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 [FIXED_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 [FIXED_TREE [G]], BOOLEAN]): BOOLEAN is -- Is test true for all items? -- (from LINEAR) require -- from TRAVERSABLE test_exits: test /= void local cs: CURSOR_STRUCTURE [FIXED_TREE [G]] c: CURSOR t: TUPLE [FIXED_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 [FIXED_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 [FIXED_TREE [G]] c: CURSOR t: TUPLE [FIXED_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 invariant -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) -- 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 FIXED_LIST empty_means_storage_empty: is_leaf implies all_default -- 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: fl_writable implies fl_readable empty_constraint: is_leaf implies (not fl_readable) and (not fl_writable) -- from INDEXABLE index_set_not_void: index_set /= void -- 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) non_negative_count: arity >= 0 -- 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: fl_full = (arity = capacity) -- from FIXED not_resizable: not resizable 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 FIXED_TREE
Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:

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