Automatic generation produced by ISE Eiffel

Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:
indexing description: "Trees as active structures that may be traversed using a cursor" status: "See notice at end of class" names: cursor_tree, tree access: cursor, membership contents: generic date: "$Date: 2001/11/16 20:34:16 $" revision: "$Revision: 1.1.1.1 $" deferred class CURSOR_TREE [G] feature -- Access child_item (i: INTEGER): G is -- Item in i-th child require argument_within_bounds: i >= 1 and then i <= arity not_off: not off local pos: CURSOR do pos := cursor down (i) Result := item go_to (pos) end cursor: CURSOR is -- Current cursor position -- (from CURSOR_STRUCTURE) deferred end has (v: G): BOOLEAN is -- Does structure include an occurrence of v? -- (Reference or object equality, -- based on object_comparison.) local pos: CURSOR do pos := cursor Result := linear_has (v) go_to (pos) ensure -- from CONTAINER not_found_in_empty: Result implies not is_empty end index_of (v: like item; 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 start pos := 1 until off or (occur = i) loop if item /= void and then v.is_equal (item) then occur := occur + 1 end preorder_forth pos := pos + 1 end else from start pos := 1 until off or (occur = i) loop if item = v then occur := occur + 1 end preorder_forth pos := pos + 1 end end if occur = i then Result := pos - 1 end ensure -- from LINEAR non_negative_result: Result >= 0 end item: G is -- Item at current position -- (from TRAVERSABLE) require -- from TRAVERSABLE not_off: not off require -- from ACTIVE readable: readable deferred end occurrences (v: G): INTEGER is local pos: CURSOR do pos := cursor Result := linear_occurrences (v) go_to (pos) ensure -- from BAG non_negative_occurrences: Result >= 0 end parent_item: G is -- Item in parent. require not_on_root: not is_root local pos: CURSOR do pos := cursor up Result := item go_to (pos) end search (v: like item) 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 (item /= void and then v.is_equal (item)) loop preorder_forth end else from until exhausted or else v = item loop preorder_forth end end ensure -- from LINEAR object_found: (not exhausted and object_comparison) implies equal (v, item) item_found: (not exhausted and not object_comparison) implies v = item end arity: INTEGER is -- Number of successors of current element -- (from HIERARCHICAL) require -- from HIERARCHICAL not_off: not off deferred end feature {NONE} -- Access linear_has (v: like item): BOOLEAN is -- Does structure include an occurrence of v? -- (Reference or object equality, -- based on object_comparison.) -- (from LINEAR) do start if not off then search (v) end Result := not exhausted ensure -- from CONTAINER not_found_in_empty: Result implies not is_empty end linear_occurrences (v: G): INTEGER is -- Number of times v appears. -- (Reference or object equality, -- based on object_comparison.) -- (from LINEAR) do from start search (v) until exhausted loop Result := Result + 1 preorder_forth search (v) end end feature -- Measurement breadth: INTEGER is -- Breadth of current level local l: INTEGER pos: CURSOR do l := level if l > 0 then pos := cursor go_above Result := breadth_of_level_from_active (l + 1) go_to (pos) end end depth: INTEGER is -- Depth of the tree local pos: CURSOR do if not is_empty then pos := cursor go_above Result := depth_from_active - 1 go_to (pos) end end level: INTEGER is -- Level of current node in tree -- (Root is on level 1) local pos: CURSOR do from pos := cursor until above loop Result := Result + 1 up end go_to (pos) end feature -- Status report above: BOOLEAN is -- Is there no valid cursor position above cursor? deferred end after: BOOLEAN is -- Is there no valid cursor position to the right of cursor? deferred end before: BOOLEAN is -- Is there no valid cursor position to the left of cursor? deferred end below: BOOLEAN -- Is there no valid cursor position below cursor? changeable_comparison_criterion: BOOLEAN is -- May object_comparison be changed? -- (Answer: yes by default.) -- (from CONTAINER) do Result := True end empty: BOOLEAN is obsolete "ELKS 2000: Use `is_empty' instead" -- Is there no element? -- (from CONTAINER) do Result := is_empty end exhausted: BOOLEAN is -- Has structure been completely explored? -- (from LINEAR) do Result := off ensure -- from LINEAR exhausted_when_off: off implies Result end extendible: BOOLEAN is -- May new items be added? do Result := not above and then (level = 1) implies is_empty end is_empty: BOOLEAN is -- Is there no element? -- (from CONTAINER) deferred end is_inserted (v: 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 -- Is cursor on a leaf? do if not off then Result := (arity = 0) end end is_root: BOOLEAN is -- Is cursor on root? deferred end isfirst: BOOLEAN is -- Is cursor on first sibling? deferred end islast: BOOLEAN is -- Is cursor on last sibling? deferred end object_comparison: BOOLEAN -- Must search operations use equal rather than = -- for comparing references? (Default: no, use =.) -- (from CONTAINER) off: BOOLEAN is -- Is there no current item? -- (True if is_empty) do Result := (after or before or below or above) end prunable: BOOLEAN is -- May items be removed? -- (from COLLECTION) deferred end readable: BOOLEAN is -- Is there a current item that may be read? do Result := not off end valid_cursor (p: CURSOR): BOOLEAN is -- Can the cursor be moved to position p? -- (from CURSOR_STRUCTURE) deferred end valid_cursor_index (i: INTEGER): BOOLEAN is -- Can cursor be moved to i-th child? -- 0 is before and arity + 1 is after. do Result := i >= 0 and i <= (arity + 1) end writable: BOOLEAN is -- Is there a current item that may be modified? do Result := not off end feature {NONE} -- Status report linear_off: BOOLEAN is -- Is there no current item? -- (from LINEAR) do Result := is_empty or after 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 back is -- Move cursor one position backward. deferred end breadth_forth is -- Move cursor to next position in breadth-first order. -- If the active node is the last in -- breadth-first order, the cursor ends up off. require not_off: not off local l: INTEGER do l := level level_forth if above and (l < depth) then start_on_level (l + 1) end end down (i: INTEGER) is -- Move cursor one level downward: -- to i-th child if there is one, -- or after if i = arity + 1, -- or before if i = 0. require -- from HIERARCHICAL not_off: not off argument_within_bounds: i >= 1 and i <= arity require else not_before: not before not_after: not after not_below: not below valid_cursor_index: (above and i = 0) or else valid_cursor_index (i) deferred ensure then gone_before: (i = 0) implies before end forth is -- Move cursor one position forward. deferred end go_last_child is -- Go to the last child of current parent. -- No effect if below require -- from LINEAR True require else not_above: not above do up down (arity) end go_to (p: CURSOR) is -- Move cursor to position p. -- (from CURSOR_STRUCTURE) require -- from CURSOR_STRUCTURE cursor_position_valid: valid_cursor (p) deferred end level_back is -- Move cursor to previous position of current level. do if not isfirst then back elseif not above then from up level_back until above or else not is_leaf loop level_back end; if not above then down (arity) end end end level_forth is -- Move cursor to next position of current level. do if not above and then not islast then forth elseif not above then from up level_forth until above or else not is_leaf loop level_forth end; if not above then down (1) end end end postorder_forth is -- Move cursor to next position in postorder. -- If the active node is the last in -- postorder, the cursor ends up off. require not_off: not off do if islast then up else forth from until is_leaf loop down (1) end end end postorder_start is -- Move cursor to first position in postorder. -- Leave cursor off if tree is empty. do from go_above until arity = 0 loop down (1) end end preorder_forth is -- Move cursor to next position in preorder. -- If the active node is the last in -- preorder, the cursor ends up off. require -- from LINEAR not_after: not after do if is_leaf then from until not islast loop up end if not above then forth end else down (1) end end start is -- Move cursor to root. -- Leave cursor off if is_empty. do go_above if not is_empty then down (1) end ensure then on_root_unless_empty: not is_empty implies is_root end start_on_level (l: INTEGER) is -- Move the cursor to the first position -- of the l-th level counting from root. require argument_within_bounds: l >= 1 and then depth >= l do go_above start_on_level_from_active (l + 1) ensure level_expected: level = l is_first: isfirst end up is -- Move cursor one level upward to parent, -- or above if is_root holds. require -- from HIERARCHICAL not_off: not off require else not_above: not above deferred ensure then not_before: not before not_after: not after not_below: not below coherency: (not old off and above) = (old is_root) end feature -- Element change extend (v: G) is -- Add v after last child. -- Make v the first_child if below and place -- cursor before. require -- from COLLECTION extendible: extendible local pos: CURSOR do pos := cursor go_last_child put_right (v) go_to (pos) if below then below := False down (0) end ensure -- from COLLECTION item_inserted: is_inserted (v) ensure then -- from BAG one_more_occurrence: occurrences (v) = old (occurrences (v)) + 1 end fill (other: CURSOR_TREE [G]) is -- Fill with as many items of other -- as possible. -- The representations of other and current structure -- need not be the same. require is_empty: is_empty do go_above if not other.is_empty then other.start down (0) put_right (other.item) forth fill_from_active (other) end end container_fill (other: CONTAINER [G]) 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 extendible local lin_rep: LINEAR [G] do lin_rep := other.linear_representation from lin_rep.start until not extendible or else lin_rep.off loop extend (lin_rep.item) lin_rep.forth end end fill_from_active (other: CURSOR_TREE [G]) is -- Copy subtree of other's active node -- onto active node of current tree. require cursor_on_leaf: is_leaf do if not other.is_leaf then from other.down (1) down (0) until other.after loop put_right (other.item) forth fill_from_active (other) other.forth end other.up up end end merge_left (other: CURSOR_TREE [G]) is -- Merge the items of other into current structure to -- the left of cursor position. require other_exists: other /= void not_before: not before not_above: not above only_one_root: (level = 1) implies is_empty do back merge_right (other) end merge_right (other: CURSOR_TREE [G]) is -- Merge the items of other into current structure to -- the right of cursor position. require other_exists: other /= void not_after: not after not_above: not above only_one_root: (level = 1) implies is_empty local pos: CURSOR do if not other.is_empty then pos := other.cursor other.start put_right (other.item) forth if not other.is_leaf then down (0) other.down (0) from until other.islast loop other.forth merge_right (other.subtree) end up end other.go_to (pos) end end put (v: G) is -- Put v at cursor position. -- (Synonym for replace) require -- from COLLECTION extendible: extendible do replace (v) ensure -- from COLLECTION item_inserted: is_inserted (v) end put_left (v: G) is -- Add v to the left of cursor position. require not_before: not before not_above: not above only_one_root: (level = 1) implies is_empty do back put_right (v) forth forth end put_right (v: G) is -- Add v to the right of cursor position. require not_after: not after not_above: not above only_one_root: (level = 1) implies is_empty deferred end replace (v: G) is -- Replace current item by v. -- (from ACTIVE) require -- from ACTIVE writable: writable deferred ensure -- from ACTIVE item_replaced: item = v end feature -- Removal remove is -- Remove current item. -- (from ACTIVE) require -- from ACTIVE prunable: prunable writable: writable deferred end wipe_out is -- Remove all items. -- (from COLLECTION) require -- from COLLECTION prunable deferred ensure -- from COLLECTION wiped_out: is_empty end feature {NONE} -- Removal prune_all (v: 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 -- Conversion linear_representation: LINEAR [G] is -- Representation as a linear structure -- (from LINEAR) do Result := Current end feature -- Duplication child_tree (i: INTEGER): like Current is -- Subtree rooted at i-th child require argument_within_bounds: i >= 1 and then i <= arity not_off: not off local pos: CURSOR do pos := cursor down (i) Result := subtree go_to (pos) end parent_tree: like Current is -- Subtree rooted at parent require not_on_root: not is_root not_off: not off local pos: CURSOR do pos := cursor up Result := subtree go_to (