INDEX CLUSTER FEATURES SHORT FRAMES NO FRAMES

indexing

description: "Sequences of values, all of the same type or of a conforming one, accessible through integer indices in a contiguous interval"
project: "Eiffel Kernel Library"
revision: "$Revision: 1.1 $"
copyright: "Copyright (C) 1996-2005 Object Tools Group"
license: "http://visual-eiffel.com/license"
cluster: kernel
class ARRAY [G]
inherit

ANY
redefine
copy,
is_equal
end
creation

make,
make_from_array
feature {NONE}-- Creation

make (min_index, max_index: INTEGER)
-- Allocate array to hold values for indexes in
-- `min_index' .. `max_index'.
-- Set all values to default.
-- (Make array empty if `maxindex' = `minindex' - 1).
require
valid_bounds: min_index <= max_index + 1
ensure
lower_set: lower = min_index
upper_set: upper = max_index
items_set:
feature -- Initialization

make_from_array (a: ARRAY [G])
-- Initialize from the items of `a'.
require
a_not_void: a /= void
ensure
initialized: same_array (a)
feature -- Measurement

count: INTEGER
feature -- Basic specifiers

item (i: INTEGER): G
-- Item at index `i'.
require
valid_index: valid_index (i)

lower: INTEGER

upper: INTEGER
-- Maximum index
feature -- Access

infix "@" (i: INTEGER): G
-- Item at index `i'.
require
valid_index: valid_index (i)
ensure
definition: Result = item (i)

array: ARRAY [G]
-- New ARRAY having the same items as `Current'
ensure
array_not_void: Result /= void
same_count: Result.count = count
same_indexes: Result.lower = lower
first_item: count > 0 implies Result.item (lower) = item (lower)
recurse: count > 0 implies Result.subarray (lower + 1, upper).is_equal (subarray (lower + 1, upper).array)
feature -- Comparison

is_equal (other: like Current): BOOLEAN
-- Do both arrays have the same `lower', `upper', and items?
-- (Redefined from GENERAL.)
ensure
definition: Result = (same_type (other) and then (lower = other.lower) and then same_items (other))

same_array (other: ARRAY [G]): BOOLEAN
-- Do `Current' and `other' have the same items
-- with the same indices?
require
other_not_void: other /= void
ensure
definition: Result = array.is_equal (other.array)
feature -- Status report

all_default: BOOLEAN
-- Do all items have their type's default value?
ensure
definition: Result = (count = 0 or else ((item (upper) = void or else item (upper) = item (upper).default) and subarray (lower, upper - 1).all_default))

is_empty: BOOLEAN
-- Is array empty?
ensure
definition: Result = (count = 0)

same_items (other: like Current): BOOLEAN
-- Do both arrays have the same items?
require
other_not_void: other /= void
ensure
definition: Result = ((count = other.count) and then (count = 0 or else (item (upper) = other.item (other.upper) and subarray (lower, upper - 1).same_items (other.subarray (other.lower, other.upper - 1)))))

valid_index (i: INTEGER): BOOLEAN
-- Is `i' within bounds?
ensure
definition: Result = ((lower <= i) and (i <= upper))
feature -- Element change

clear_all
-- Set every item to its default value.
ensure
stable_upper: upper = upper
stable_lower: lower = lower
default_items:

force (v: G; i: INTEGER)
-- Make `v' the item at index `i', enlarging the
-- array if necessary.
ensure
new_lower: lower = i.min ( lower )
new_upper: upper = i.max ( upper )
new_item: item (i) = v
between_lower_and_i: subarray ( lower , ((i - 1).max ( lower - 1)).min ( upper )).same_items ( subarray (lower, ((i - 1).max (lower - 1)).min (upper)) )
between_i_and_upper: subarray (((i + 1).min ( upper + 1)).max ( lower ), upper ).same_items ( subarray (((i + 1).min (upper + 1)).max (lower), upper) )

put (v: G; i: INTEGER)
-- Make 'v' the item at index `i'.
require
valid_index: valid_index (i)
ensure
stable_lower: lower = lower
stable_upper: upper = upper
new_item: item (i) = v
stable_before_i: subarray (lower, i - 1).is_equal ( subarray (lower, i - 1) )
stable_after_i: subarray (i + 1, upper).is_equal ( subarray (i + 1, upper) )
feature -- Resizing

resize (min_index, max_index: INTEGER)
-- Resize to bounds `min_index' and `max_index'.
-- Do not lose any item whose index is in both
-- `lower..upper' and `min_index..max_index'.
require
valid_bounds: min_index <= max_index + 1
ensure
new_lower: lower = min_index
new_upper: upper = max_index
stable_in_intersection: max_index < lower or min_index > upper or else subarray (min_index.max ( lower ).min (max_index + 1), max_index.min ( upper ).max (min_index - 1)).is_equal ( subarray (min_index.max (lower).min (upper + 1), max_index.min (upper).max (lower - 1)) )
feature -- Duplication

copy (other: like Current)
-- Reinitialize by copying all the items of `other'.
-- (Also used by `clone'.)
-- (Redefined from GENERAL.)

subarray (min, max: INTEGER): ARRAY [G]
-- New array consisting of items at indexes
-- in `min .. max'
require
min_large_enough: lower <= min
max_small_enough: max <= upper
valid_bounds: min <= max + 1
ensure
subarray_not_void: Result /= void
lower_set: Result.lower = min
upper_set: Result.upper = max
items_set: Result.count > 0 implies (Result.item (max) = item (max) and Result.subarray (min, max - 1).is_equal (subarray (min, max - 1)))
feature -- ELKS 1995 compatibility

entry (i: INTEGER): G
obsolete "ELKS 1995 compatibility: use 'item (i)' instead"
-- Entry at index 'i', if in index interval.
-- (Synonym for 'item' and 'infix "@"')
require
good_key: valid_index (i)

enter (v: G; i: INTEGER)
obsolete "ELKS 1995 compatibility: use 'put (i)' instead"
-- Replace 'i-th' entry, if in index interval, by 'v'.
-- (Synonym for 'put')
require
good_key: valid_index (i)
ensure
inserted: item (i) = v

to_c: POINTER
-- Address of actual sequence of values,
-- for passing to external (non-Eiffel) routines
feature -- Visual Eiffel 3.2 compatibility

append (other: ARRAY [G])
-- Append all items of 'other', if not void, at end
ensure
higher_count: (other = void implies count = count ) and then (other /= void implies count >= count )

from_c (ptr: POINTER; cnt: INTEGER)
-- Creates an array from its external representation
require
non_void_ptr: ptr /= default_pointer
valid_cnt: cnt >= 0
ensure
new_lower: lower = 1
new_count: count = cnt
feature -- Eiffel/S 1.3 compatibility

to_external: POINTER
obsolete "Eiffel/S 1.3 compatibility: use 'to_c' instead"
-- Pointer to actual storage area

insert (element: G; index: INTEGER)
-- Insert 'element' after position 'index'.
-- Element at position 'upper' will be lost!
-- Note: Insertion is AFTER 'index'!
require
inside_bounds: index >= lower - 1 and then index < upper

move (lower_index, upper_index, distance: INTEGER)
-- Move Range 'lower_index' .. 'upper_index' by 'distance'
-- positions. Neg. distance moves towards lower indices.
-- Free places get default values
require
non_empty_range: lower_index <= upper_index
inside_bounds_before: lower <= lower_index and then upper_index <= upper
inside_bounds_after: lower <= lower_index + distance and then upper_index + distance <= upper

remove (index: INTEGER)
-- Remove element at position 'index'.
-- Elements after 'index' will move
-- one position left (=towards lower indices).
-- Element at position 'upper' is set to default value.
-- Note: Position 'upper' will be set to
-- appropriate default value
require
inside_bounds: index >= lower and then index <= upper

size: INTEGER
obsolete "Eiffel/S 1.3 compatibility: use 'count' instead"
-- Length of index interval

reindex (new_lower: INTEGER)
-- Change index interval so that
-- it starts at 'new_lower'.
-- No elements will be lost
ensure
bounds_adjusted: lower = new_lower and then upper = new_lower + count - 1

wipe_out
obsolete "Eiffel/S 1.3 compatibility: use 'resize (lower, lower - 1)' instead"
-- Empty the array, discard all items

empty: BOOLEAN
obsolete "Eiffel/S 1.3 compatibility: use 'is_empty' instead"
-- Is array empty?

all_cleared: BOOLEAN
obsolete "Eiffel/S 1.3 compatibility: use 'all_default' instead"
-- Are all items set to default values?
invariant

valid_bounds: lower <= upper + 1
consistent_with_bounds: count = upper - lower + 1
end -- class ARRAY

INDEX CLUSTER FEATURES SHORT FRAMES NO FRAMES