indexing
description: "Lists implemented by resizable arrays"
names: sequence
representation: array
access: index, cursor, membership
size: fixed
contents: generic
class interface
ARRAYED_LIST [G]
create
make (n: INTEGER)
-- Allocate list with n items.
-- (n may be zero for empty list.)
require
valid_number_of_items: n >= 0
ensure
correct_position: before
make_filled (n: INTEGER)
-- Allocate list with n items.
-- (n may be zero for empty list.)
-- This list will be full.
require
valid_number_of_items: n >= 0
ensure
correct_position: before;
filled: full
feature -- Initialization
make (n: INTEGER)
-- Allocate list with n items.
-- (n may be zero for empty list.)
require
valid_number_of_items: n >= 0
ensure
correct_position: before
make_filled (n: INTEGER)
-- Allocate list with n items.
-- (n may be zero for empty list.)
-- This list will be full.
require
valid_number_of_items: n >= 0
ensure
correct_position: before;
filled: full
feature -- Access
cursor: CURSOR
-- Current cursor position
first: G
-- Item at first position
require -- from CHAIN
not_empty: not empty
has (v: like item): BOOLEAN
-- Does chain include v?
-- (Reference or object equality,
-- based on object_comparison.)
-- (from CHAIN)
ensure -- from CONTAINER
not_found_in_empty: Result implies not empty
index: INTEGER
-- Index of item, if valid.
index_of (v: like item; i: INTEGER): INTEGER
-- 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
ensure -- from LINEAR
non_negative_result: Result >= 0
frozen i_th (i: INTEGER): G
-- Entry at index i, if in index interval
-- Was declared in ARRAY as synonym of item, @ and entry.
-- (from ARRAY)
require -- from TABLE
valid_key: valid_index (k)
item: like first
-- Current item
require -- from ACTIVE
readable: readable
require -- from TRAVERSABLE
not_off: not off
require else
index_is_valid: valid_index (index)
last: like first
-- Item at last position
require -- from CHAIN
not_empty: not empty
occurrences (v: like item): INTEGER
-- Number of times v appears.
-- (Reference or object equality,
-- based on object_comparison.)
-- (from CHAIN)
ensure -- from BAG
non_negative_occurrences: Result >= 0
occurrences (v: like item): INTEGER
-- Number of times v appears.
-- (Reference or object equality,
-- based on object_comparison.)
-- (from CHAIN)
ensure -- from BAG
non_negative_occurrences: Result >= 0
sequential_occurrences (v: G): INTEGER
-- Number of times v appears.
-- (Reference or object equality,
-- based on object_comparison.)
-- (from LINEAR)
ensure -- from BAG
non_negative_occurrences: Result >= 0
frozen infix "@" (i: INTEGER): G
-- Entry at index i, if in index interval
-- Was declared in ARRAY as synonym of item, @ and entry.
-- (from ARRAY)
require -- from TABLE
valid_key: valid_index (k)
feature -- Measurement
count: INTEGER
-- Number of items.
feature {ANY} -- Measurement
capacity: INTEGER
-- Number of available indices
-- Was declared in ARRAY as synonym of count and capacity.
-- (from ARRAY)
feature -- Comparison
is_equal (other: like Current): BOOLEAN
--Is other equal to Current?
require -- from GENERAL
other_not_void: other /= void
ensure -- from GENERAL
symmetric: Result implies other.is_equal (Current);
consistent: standard_is_equal (other) implies Result
feature -- Status report
after: BOOLEAN
-- Is there no valid cursor position to the right of cursor?
-- (from LIST)
before: BOOLEAN
-- Is there no valid cursor position to the left of cursor?
-- (from LIST)
changeable_comparison_criterion: BOOLEAN
-- May object_comparison be changed?
-- (Answer: yes by default.)
-- (from CONTAINER)
empty: BOOLEAN
-- Is structure empty?
-- (from FINITE)
exhausted: BOOLEAN
-- Has structure been completely explored?
-- (from LINEAR)
ensure -- from LINEAR
exhausted_when_off: off implies Result
Extendible: BOOLEAN is true
-- May new items be added? (Answer: yes.)
-- (from DYNAMIC_CHAIN)
full: BOOLEAN
-- Is structure filled to capacity? (Answer: no.)
isfirst: BOOLEAN
-- Is cursor at first position?
-- (from CHAIN)
ensure -- from CHAIN
valid_position: Result implies not empty
islast: BOOLEAN
-- Is cursor at last position?
-- (from CHAIN)
ensure -- from CHAIN
valid_position: Result implies not empty
object_comparison: BOOLEAN
-- Must search operations use equal rather than =
-- for comparing references? (Default: no, use =.)
-- (from CONTAINER)
off: BOOLEAN
-- Is there no current item?
-- (from CHAIN)
prunable: BOOLEAN
-- May items be removed? (Answer: yes.)
readable: BOOLEAN
-- Is there a current item that may be read?
-- (from SEQUENCE)
valid_cursor (p: CURSOR): BOOLEAN
-- Can the cursor be moved to position p?
valid_cursor_index (i: INTEGER): BOOLEAN
-- Is i correctly bounded for cursor movement?
-- (from CHAIN)
ensure -- from CHAIN
valid_cursor_index_definition: Result = ((i >= 0) and (i <= count + 1))
valid_index (i: INTEGER): BOOLEAN
-- Is i a valid index?
ensure then -- from CHAIN
valid_index_definition: Result = ((i >= 1) and (i <= count))
writable: BOOLEAN
-- Is there a current item that may be modified?
-- (from SEQUENCE)
feature -- Status setting
compare_objects
-- Ensure that future search operations will use equal
-- rather than = for comparing references.
-- (from CONTAINER)
require -- from CONTAINER
changeable_comparison_criterion
ensure -- from CONTAINER
object_comparison
compare_references
-- Ensure that future search operations will use =
-- rather than equal for comparing references.
-- (from CONTAINER)
require -- from CONTAINER
changeable_comparison_criterion
ensure -- from CONTAINER
reference_comparison: not object_comparison
feature -- Cursor movement
back
-- Move cursor one position backward.
require -- from BILINEAR
not_before: not before
ensure then -- from BILINEAR
moved_back: index = old index - 1
finish
-- Move cursor to last position if any.
ensure then -- from CHAIN
at_last: not empty implies islast
ensure then
before_when_empty: empty implies before
forth
-- Move cursor one position forward.
require -- LIST
precursor: True
require -- from LINEAR
not_after: not after
ensure then -- from LIST
moved_forth: index = old index + 1
go_i_th (i: INTEGER)
-- Move cursor to i-th position.
require -- from CHAIN
valid_cursor_index: valid_cursor_index (i)
ensure -- from CHAIN
position_expected: index = i
go_to (p: CURSOR)
-- Move cursor to position p.
require -- from CURSOR_STRUCTURE
cursor_position_valid: valid_cursor (p)
move (i: INTEGER)
-- Move cursor i positions.
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)
search (v: like item)
-- Move to first position (at or after current
-- position) where item and v are equal.
-- If structure does not include v ensure that
-- exhausted will be true.
-- (Reference or object equality,
-- based on object_comparison.)
-- (from BILINEAR)
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
start
-- Move cursor to first position if any.
ensure then -- from CHAIN
at_first: not empty implies isfirst
ensure then
after_when_empty: empty implies after
feature -- Element change
append (s: SEQUENCE [G])
-- Append a copy of s.
-- (from SEQUENCE)
require -- from SEQUENCE
argument_not_void: s /= void
ensure -- from SEQUENCE
new_count: count >= old count
extend (v: like item)
-- Add v to end.
-- Do not move cursor.
-- Was declared in ARRAYED_LIST as synonym of force and extend.
require -- from COLLECTION
extendible: extendible
ensure -- from COLLECTION
item_inserted: has (v)
ensure then -- from BAG
one_more_occurrence: occurrences (v) = old (occurrences (v)) + 1
fill (other: CONTAINER [G])
-- Fill with as many items of other as possible.
-- The representations of other and current structure
-- need not be the same.
-- (from COLLECTION)
require -- from COLLECTION
other_not_void: other /= void;
extendible
force (v: like item)
-- Add v to end.
-- Do not move cursor.
-- Was declared in ARRAYED_LIST as synonym of force and extend.
require -- from SEQUENCE
extendible: extendible
ensure then -- from SEQUENCE
new_count: count = old count + 1;
item_inserted: has (v)
merge_left (other: ARRAYED_LIST [G])
require -- from DYNAMIC_CHAIN
extendible: extendible;
not_off: not before;
other_exists: other /= void
ensure -- from DYNAMIC_CHAIN
new_count: count = old count + old other.count;
new_index: index = old index + old other.count;
other_is_empty: other.empty
merge_right (other: ARRAYED_LIST [G])
require -- from DYNAMIC_CHAIN
extendible: extendible;
not_off: not after;
other_exists: other /= void
ensure -- from DYNAMIC_CHAIN
new_count: count = old count + old other.count;
same_index: index = old index;
other_is_empty: other.empty
put (v: like item)
-- Replace current item by v.
-- (Synonym for replace)
-- (from CHAIN)
require -- CHAIN
precursor: True
require -- from COLLECTION
extendible: extendible
ensure then -- from CHAIN
same_count: count = old count
ensure -- from COLLECTION
item_inserted: has (v)
ensure then -- from CHAIN
same_count: count = old count
frozen put_i_th (v: like i_th; i: INTEGER)
-- Replace i-th entry, if in index interval, by v.
-- Was declared in ARRAY as synonym of put and enter.
-- (from ARRAY)
require -- from TABLE
valid_key: valid_index (k)
ensure then -- from INDEXABLE
insertion_done: i_th (k) = v
put_front (v: like item)
-- Add v to the beginning.
-- Do not move cursor.
ensure -- from DYNAMIC_CHAIN
new_count: count = old count + 1;
item_inserted: first = v
put_left (v: like item)
-- Add v to the left of current position.
-- Do not move cursor.
require -- from DYNAMIC_CHAIN
extendible: extendible;
not_before: not before
ensure -- from DYNAMIC_CHAIN
new_count: count = old count + 1;
new_index: index = old index + 1
put_right (v: like item)
-- Add v to the right of current position.
-- Do not move cursor.
require -- from DYNAMIC_CHAIN
extendible: extendible;
not_after: not after
ensure -- from DYNAMIC_CHAIN
new_count: count = old count + 1;
same_index: index = old index
replace (v: like first)
-- Replace current item by v.
require -- from ACTIVE
writable: writable
ensure -- from ACTIVE
item_replaced: item = v
feature -- Removal
prune (v: like item)
-- Remove first occurrence of v, if any,
-- after cursor position.
-- Move cursor to right neighbor
-- (or after if no right neighbor or vdoes not occur)
require -- from COLLECTION
prunable: prunable
prune_all (v: like item)
-- Remove all occurrences of v.
-- (Reference or object equality,
-- based on object_comparison.)
-- Leave cursor after.
require -- from COLLECTION
prunable
ensure -- from COLLECTION
no_more_occurrences: not has (v)
ensure then -- from DYNAMIC_CHAIN
is_exhausted: exhausted
ensure then
is_after: after
remove
-- Remove current item.
-- Move cursor to right neighbor
-- (or after if no right neighbor)
require -- from ACTIVE
prunable: prunable;
writable: writable
ensure then -- from DYNAMIC_LIST
after_when_empty: empty implies after
remove_left
-- Remove item to the left of cursor position.
-- Do not move cursor.
require -- from DYNAMIC_CHAIN
left_exists: index > 1
require else -- from DYNAMIC_LIST
not_before: not before
ensure -- from DYNAMIC_CHAIN
new_count: count = old count - 1;
new_index: index = old index - 1
remove_right
-- Remove item to the right of cursor position
-- Do not move cursor
require -- from DYNAMIC_CHAIN
right_exists: index < count
ensure -- from DYNAMIC_CHAIN
new_count: count = old count - 1;
same_index: index = old index
wipe_out
-- Remove all items.
require -- from COLLECTION
prunable
ensure -- from COLLECTION
wiped_out: empty
ensure then -- from DYNAMIC_LIST
is_before: before
feature -- Transformation
swap (i: INTEGER)
-- Exchange item at i-th position with item
-- at cursor position.
require -- from CHAIN
not_off: not off;
valid_index: valid_index (i)
ensure -- from CHAIN
swapped_to_item: item = old i_th (i);
swapped_from_item: i_th (i) = old item
feature -- Conversion
linear_representation: LINEAR [G]
-- Representation as a linear structure
-- (from LINEAR)
feature -- Duplication
copy (other: like Current)
require -- from GENERAL
other_not_void: other /= void;
type_identity: same_type (other)
ensure -- from GENERAL
is_equal: is_equal (other)
duplicate (n: INTEGER): like Current
-- Copy of sub-list beginning at current position
-- and having min (n, count - index + 1) items.
require -- from CHAIN
not_off_unless_after: off implies after;
valid_subchain: n >= 0
setup (other: like Current)
-- Prepare current object so that other
-- can be easily copied into it.
-- It is not necessary to call setup
-- (since consistent is always true)
-- but it will make copying quicker.
ensure -- from GENERAL
consistent (other)
invariant
-- from GENERAL
reflexive_equality: standard_is_equal (Current);
reflexive_conformance: conforms_to (Current);
prunable: prunable;
starts_from_one: lower = 1;
-- from ARRAY
non_negative_count: count >= 0;
-- from BOUNDED
valid_count: count <= capacity;
full_definition: full = (count = capacity);
-- from FINITE
empty_definition: empty = (count = 0);
non_negative_count: count >= 0;
-- from LIST
before_definition: before = (index = 0);
after_definition: after = (index = count + 1);
-- from CHAIN
non_negative_index: index >= 0;
index_small_enough: index <= count + 1;
off_definition: off = ((index = 0) or (index = count + 1));
isfirst_definition: isfirst = ((not empty) and (index = 1));
islast_definition: islast = ((not empty) and (index = count));
item_corresponds_to_index: (not off) implies (item = i_th (index));
-- from ACTIVE
writable_constraint: writable implies readable;
empty_constraint: empty implies (not readable) and (not writable);
-- from BILINEAR
not_both: not (after and before);
empty_property: empty implies (after or before);
before_constraint: before implies off;
-- from LINEAR
after_constraint: after implies off;
-- from TRAVERSABLE
empty_constraint: empty implies off;
-- from DYNAMIC_CHAIN
extendible: extendible;
end -- class ARRAYED_LIST