Automatic generation produced by ISE Eiffel
indexing description: "[ Sequential, one-way linked lists that call an action sequence when an item is removed or added. ]" status: "See notice at end of class" keywords: "event, action, linked, list" date: "$Date: 2001/11/16 20:34:10 $" revision: "$Revision: 1.1.1.1 $" class ACTIVE_LIST [G] create default_create feature {NONE} -- Initialization default_create is -- Initialize do Precursor {INTERACTIVE_LIST} create add_actions create remove_actions end feature -- Initialization make is -- Create an empty list. -- (from LINKED_LIST) do before := True ensure -- from LINKED_LIST is_before: before end feature -- Access add_actions: ACTION_SEQUENCE [TUPLE [G]] -- Actions performed when an item has just been added. cursor: CURSOR is -- Current cursor position -- (from LINKED_LIST) do create {LINKED_LIST_CURSOR [G]} Result.make (active, after, before) end first: like item is -- Item at first position -- (from LINKED_LIST) require -- from CHAIN not_empty: not is_empty do Result := first_element.item end has (v: like item): BOOLEAN is -- Does chain include v? -- (Reference or object equality, -- based on object_comparison.) -- (from CHAIN) local pos: CURSOR do pos := cursor Result := sequential_has (v) go_to (pos) ensure -- from CONTAINER not_found_in_empty: Result implies not is_empty end i_th (i: INTEGER): like item is -- Item at i-th position -- Was declared in CHAIN as synonym of @. -- (from CHAIN) require -- from TABLE valid_key: valid_index (k) local pos: CURSOR do pos := cursor go_i_th (i) Result := item go_to (pos) end index: INTEGER is -- Index of current position -- (from LINKED_LIST) local p: LINKED_LIST_CURSOR [G] do if after then Result := count + 1 elseif not before then p ?= cursor; check p /= void end; from start Result := 1 until p.active = active loop forth Result := Result + 1 end; go_to (p) end end index_of (v: like item; 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 := cursor Result := sequential_index_of (v, i) go_to (pos) ensure -- from LINEAR non_negative_result: Result >= 0 end item: G is -- Current item -- (from LINKED_LIST) require -- from TRAVERSABLE not_off: not off require -- from ACTIVE readable: readable do Result := active.item end last: like item is -- Item at last position -- (from LINKED_LIST) require -- from CHAIN not_empty: not is_empty do Result := last_element.item end sequential_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 forth search (v) end ensure -- from BAG non_negative_occurrences: Result >= 0 end remove_actions: ACTION_SEQUENCE [TUPLE [G]] -- Actions performed when an item has just been removed. infix "@" (i: INTEGER): like item is -- Item at i-th position -- Was declared in CHAIN as synonym of i_th. -- (from CHAIN) require -- from TABLE valid_key: valid_index (k) local pos: CURSOR do pos := cursor go_i_th (i) Result := item go_to (pos) end feature {NONE} -- Access sequential_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 sequential_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 forth pos := pos + 1 end else from start pos := 1 until off or (occur = i) loop if item = v then occur := occur + 1 end 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 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 forth end else from until exhausted or else v = item loop 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 feature -- Measurement count: INTEGER -- Number of items -- (from LINKED_LIST) index_set: INTEGER_INTERVAL is -- Range of acceptable indexes -- (from CHAIN) do create Result.make (1, count) ensure -- from INDEXABLE not_void: Result /= void ensure then -- from CHAIN count_definition: Result.count = count end occurrences (v: like item): INTEGER is -- Number of times v appears. -- (Reference or object equality, -- based on object_comparison.) -- (from CHAIN) local pos: CURSOR do pos := cursor Result := sequential_occurrences (v) go_to (pos) ensure -- from BAG non_negative_occurrences: Result >= 0 end occurrences (v: like item): INTEGER is -- Number of times v appears. -- (Reference or object equality, -- based on object_comparison.) -- (from CHAIN) local pos: CURSOR do pos := cursor Result := sequential_occurrences (v) go_to (pos) ensure -- from BAG non_negative_occurrences: Result >= 0 end feature -- Comparison is_equal (other: like Current): BOOLEAN is -- Does other contain the same elements? -- (from LIST) require -- from ANY other_not_void: other /= void local c1, c2: CURSOR do if Current = other then Result := True else Result := (is_empty = other.is_empty) and (object_comparison = other.object_comparison) if Result and not is_empty then c1 ?= cursor c2 ?= other.cursor check cursors_exist: c1 /= void and c2 /= void end from start other.start until after or not Result loop if object_comparison then Result := equal (item, other.item) else Result := (item = other.item) end forth other.forth end go_to (c1) other.go_to (c2) elseif is_empty and other.is_empty and object_comparison = other.object_comparison then Result := True 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: index = old index and other.index = old other.index true_implies_same_size: Result implies count = other.count end feature -- Status report after: BOOLEAN -- Is there no valid cursor position to the right of cursor? -- (from LINKED_LIST) before: BOOLEAN -- Is there no valid cursor position to the left of cursor? -- (from LINKED_LIST) 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 True -- May new items be added? (Answer: yes.) -- (from DYNAMIC_CHAIN) Full: BOOLEAN is False -- Is structured filled to capacity? (Answer: no.) -- (from LINKED_LIST) is_empty: BOOLEAN is -- Is structure empty? -- (from FINITE) do Result := (count = 0) end is_inserted (v: G): BOOLEAN is -- Has v been inserted at the end by the most recent put or -- extend? -- (from LINKED_LIST) do check put_constraint: (v /= last_element.item) implies not off end Result := (v = last_element.item) or else (v = item) end isfirst: BOOLEAN is -- Is cursor at first position? -- (from LINKED_LIST) do Result := not after and not before and (active = first_element) ensure -- from CHAIN valid_position: Result implies not is_empty end islast: BOOLEAN is -- Is cursor at last position? -- (from LINKED_LIST) do Result := not after and not before and (active /= void) and then (active.right = void) ensure -- from CHAIN valid_position: Result implies not is_empty 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? -- (from LINKED_LIST) do Result := after or before end prunable: BOOLEAN is -- May items be removed? (Answer: yes.) -- (from DYNAMIC_CHAIN) do Result := True end readable: BOOLEAN is -- Is there a current item that may be read? -- (from LINKED_LIST) do Result := not off end valid_cursor (p: CURSOR): BOOLEAN is -- Can the cursor be moved to position p? -- (from LINKED_LIST) local ll_c: LINKED_LIST_CURSOR [G] temp, sought: like first_element do ll_c ?= p if ll_c /= void then from temp := first_element sought := ll_c.active Result := ll_c.after or else ll_c.before until Result or else temp = void loop Result := (temp = sought) temp := temp.right end end end valid_cursor_index (i: INTEGER): BOOLEAN is -- Is i correctly bounded for cursor movement? -- (from CHAIN) do Result := (i >= 0) and (i <= count + 1) ensure -- from CHAIN valid_cursor_index_definition: Result = ((i >= 0) and (i <= count + 1)) end valid_index (i: INTEGER): BOOLEAN is -- Is i within allowable bounds? -- (from CHAIN) do Result := (i >= 1) and (i <= count) 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 <= count)) end writable: BOOLEAN is -- Is there a current item that may be modified? -- (from SEQUENCE) do Result := not 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 back is -- Move to previous item. -- (from LINKED_LIST) require -- from BILINEAR not_before: not before do if is_empty then before := True after := False elseif after then after := False elseif isfirst then before := True else active := previous end end finish is -- Move cursor to last position. -- (Go before if empty) -- (from LINKED_LIST) local p: like first_element do if not is_empty then from p := active until p.right = void loop p := p.right end active := p after := False before := False else before := True after := False end ensure then -- from CHAIN at_last: not is_empty implies islast ensure then -- from LINKED_LIST empty_convention: is_empty implies before end forth is -- Move cursor to next position. -- (from LINKED_LIST) require -- from LINEAR not_after: not after local old_active: like first_element do if before then before := False if is_empty then after := True end else old_active := active active := active.right if active = void then active := old_active after := True end end ensure then -- from LIST moved_forth: index = old index + 1 end go_i_th (i: INTEGER) is -- Move cursor to i-th position. -- (from LINKED_LIST) require -- from CHAIN valid_cursor_index: valid_cursor_index (i) do if i = 0 then before := True after := False active := first_element elseif i = count + 1 then before := False; after := True; active := last_element else move (i - index) end ensure -- from CHAIN position_expected: index = i end go_to (p: CURSOR) is -- Move cursor to position p. -- (from LINKED_LIST) require -- from CURSOR_STRUCTURE cursor_position_valid: valid_cursor (p) local ll_c: LINKED_LIST_CURSOR [G] do ll_c ?= p check ll_c /= void end after := ll_c.after before := ll_c.before if before then active := first_element elseif after then active := last_element else active := ll_c.active end end move (i: INTEGER) is -- Move cursor i positions. The cursor -- may end up off if the offset is too big. -- (from LINKED_LIST) local counter, new_index: INTEGER p: like first_element do if i > 0 then if before then before := False counter := 1 end from p := active until (counter = i) or else (p = void) loop active := p p := p.right counter := counter + 1 end if p = void then after := True else active := p end elseif i < 0 then new_index := index + i; before := True; after := False; active := first_element; if (new_index > 0) then move (new_index) end end ensure -- from CHAIN too_far_right: (old index + i > count) implies exhausted too_far_left: (old index + i < 1) implies exhausted expected_index: (not exhausted) implies (index = old index + i) ensure then -- from LINKED_LIST moved_if_inbounds: ((old index + i) >= 0 and (old index + i) <= (count + 1)) implies index = (old index + i) before_set: (old index + i) <= 0 implies before after_set: (old index + i) >= (count + 1) implies after end search (v: like item) 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 before and not is_empty then forth end sequential_search (v) 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 start is -- Move cursor to first position. -- (from