indexing
	description: "Contiguous integer intervals"
	status: "See notice at end of class"
	date: "$Date$"
	revision: "$Revision$"

class interface
	INTEGER_INTERVAL

create 

	make (min_index, max_index: INTEGER)
			-- Set up interval to have bounds min_index and
			-- max_index (empty if min_index > max_index)
		ensure
			lower_defined: lower_defined
			upper_defined: upper_defined
			set_if_non_empty: (min_index <= max_index) implies ((lower = min_index) and (upper = max_index))
			empty_if_not_in_order: (min_index > max_index) implies is_empty

feature -- Initialization

	adapt (other: INTEGER_INTERVAL)
			-- Reset to be the same interval as other.
		require
			other_not_void: other /= void
		ensure
			same_lower: lower = other.lower
			same_upper: upper = other.upper
			same_lower_defined: lower_defined = other.lower_defined
			same_upper_defined: upper_defined = other.upper_defined
	
feature -- Access

	has (v: INTEGER): BOOLEAN
			-- Does v appear in interval?
			-- Was declared in INTEGER_INTERVAL as synonym of valid_index.
		ensure -- from CONTAINER
			not_found_in_empty: Result implies not is_empty
		ensure then
			iff_within_bounds: Result = ((upper_defined implies v <= upper) and (lower_defined implies v >= lower))

	item (i: INTEGER): INTEGER
			-- Entry at index i, if in index interval
			-- Was declared in INTEGER_INTERVAL as synonym of @.
		require -- from TABLE
			valid_key: valid_index (k)

	lower: INTEGER
			-- Smallest value in interval
		require
			lower_defined: lower_defined

	lower_defined: BOOLEAN
			-- Is there a lower bound?

	upper: INTEGER
			-- Largest value in interval
		require
			upper_defined: upper_defined

	upper_defined: BOOLEAN
			-- Is there an upper bound?

	valid_index (v: INTEGER): BOOLEAN
			-- Does v appear in interval?
			-- Was declared in INTEGER_INTERVAL as synonym of has.
		ensure then -- from INDEXABLE
			only_if_in_index_set: Result implies ((i >= index_set.lower) and (i <= index_set.upper))
		ensure then
			iff_within_bounds: Result = ((upper_defined implies v <= upper) and (lower_defined implies v >= lower))

	infix "@" (i: INTEGER): INTEGER
			-- Entry at index i, if in index interval
			-- Was declared in INTEGER_INTERVAL as synonym of item.
		require -- from TABLE
			valid_key: valid_index (k)
	
feature -- Measurement

	additional_space: INTEGER
			-- Proposed number of additional items
			-- (from RESIZABLE)
		ensure -- from RESIZABLE
			at_least_one: Result >= 1

	capacity: INTEGER
			-- Maximum number of items in interval
			-- (here the same thing as count)

	count: INTEGER
			-- Number of items in interval
		ensure then
			definition: Result = upper - lower + 1

	Growth_percentage: INTEGER is 50
			-- Percentage by which structure will grow automatically
			-- (from RESIZABLE)

	index_set: INTEGER_INTERVAL
			-- Range of acceptable indexes
			-- (here: the interval itself)
		ensure -- from INDEXABLE
			not_void: Result /= void
		ensure then
			index_set_is_range: equal (Result, Current)

	Minimal_increase: INTEGER is 5
			-- Minimal number of additional items
			-- (from RESIZABLE)

	occurrences (v: INTEGER): INTEGER
			-- Number of times v appears in structure
		ensure -- from BAG
			non_negative_occurrences: Result >= 0
		ensure then
			one_iff_in_bounds: Result = 1 implies has (v)
			zero_otherwise: Result /= 1 implies Result = 0
	
feature -- Comparison

	is_equal (other: like Current): BOOLEAN
			-- Is array made of the same items as other?
		require -- from ANY
			other_not_void: other /= void
		ensure -- from ANY
			symmetric: Result implies other.is_equal (Current)
			consistent: standard_is_equal (other) implies Result
		ensure then
			iff_same_bounds: Result = ((lower_defined implies (other.lower_defined and lower = other.lower)) and (upper_defined implies (other.upper_defined and upper = other.upper)))
	
feature -- Status report

	all_cleared: BOOLEAN
			-- Are all items set to default values?
		ensure then
			iff_at_zero: Result = ((lower = 0) and (upper = 0))

	extendible: BOOLEAN
			-- May new items be added?
			-- Answer: yes

	full: BOOLEAN
			-- Is structure full?
			-- (from BOUNDED)

	is_empty: BOOLEAN
			-- Is structure empty?
			-- (from FINITE)

	is_inserted (v: INTEGER): BOOLEAN
			-- 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)

	object_comparison: BOOLEAN
			-- Must search operations use equal rather than =
			-- for comparing references? (Default: no, use =.)
			-- (from CONTAINER)

	prunable: BOOLEAN
			-- May individual items be removed?
			-- Answer: no

	resizable: BOOLEAN
			-- May capacity be changed? (Answer: yes.)
			-- (from RESIZABLE)
	
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 -- Element change

	extend (v: INTEGER)
			-- Make sure that interval goes all the way
			-- to v (up or down).
			-- Was declared in INTEGER_INTERVAL as synonym of put.
		require -- from COLLECTION
			extendible: extendible
		ensure -- from COLLECTION
			item_inserted: is_inserted (v)
		ensure then -- from SET
			in_set_already: old has (v) implies (count = old count)
			added_to_set: not old has (v) implies (count = old count + 1)
		ensure then -- from BAG
			one_more_occurrence: occurrences (v) = old (occurrences (v)) + 1
		ensure then
			extended_down: lower = (old lower).min (v)
			extended_up: upper = (old upper).max (v)

	fill (other: CONTAINER [INTEGER])
			-- 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

	put (v: INTEGER)
			-- Make sure that interval goes all the way
			-- to v (up or down).
			-- Was declared in INTEGER_INTERVAL as synonym of extend.
		require -- from COLLECTION
			extendible: extendible
		ensure -- from COLLECTION
			item_inserted: is_inserted (v)
		ensure then -- from SET
			in_set_already: old has (v) implies (count = old count)
			added_to_set: not old has (v) implies (count = old count + 1)
		ensure then
			extended_down: lower = (old lower).min (v)
			extended_up: upper = (old upper).max (v)
	
feature -- Removal

	changeable_comparison_criterion: BOOLEAN
			-- May object_comparison be changed?
			-- (Answer: only if set empty; otherwise insertions might
			-- introduce duplicates, destroying the set property.)
			-- (from SET)
		ensure then -- from SET
			only_on_empty: Result = is_empty

	prune_all (v: INTEGER)
			-- Remove all occurrences of v.
			-- (Reference or object equality,
			-- based on object_comparison.)
			-- (from COLLECTION)
		require -- from COLLECTION
			prunable
		ensure -- from COLLECTION
			no_more_occurrences: not has (v)

	wipe_out
			-- Remove all items.
		require -- from COLLECTION
			prunable
		ensure -- from COLLECTION
			wiped_out: is_empty
	
feature -- Resizing

	automatic_grow
			-- Change the capacity to accommodate at least
			-- Growth_percentage more items.
			-- (from RESIZABLE)
		ensure -- from RESIZABLE
			increased_capacity: capacity >= old capacity + old capacity * growth_percentage // 100

	grow (i: INTEGER)
			-- Ensure that capacity is at least i.
		ensure -- from RESIZABLE
			new_capacity: capacity >= i
		ensure then
			no_loss_from_bottom: lower <= old lower
			no_loss_from_top: upper >= old upper

	resize (min_index, max_index: INTEGER)
			-- Rearrange interval to go from at most
			-- min_index to at least max_index,
			-- encompassing previous bounds.

	resize_exactly (min_index, max_index: INTEGER)
			-- Rearrange interval to go from
			-- min_index to max_index.
	
feature -- Conversion

	as_array: ARRAY [INTEGER]
			-- Plain array containing interval's items
		require
			finite: upper_defined and lower_defined
		ensure
			same_lower: Result.lower = lower
			same_upper: Result.upper = upper

	linear_representation: LINEAR [INTEGER]
			-- Representation as a linear structure
	
feature -- Duplication

	copy (other: like Current)
			-- Reset to be the same interval as other.
		require -- from ANY
			other_not_void: other /= void
			type_identity: same_type (other)
		ensure -- from ANY
			is_equal: is_equal (other)
		ensure then
			same_lower: lower = other.lower
			same_upper: upper = other.upper
			same_lower_defined: lower_defined = other.lower_defined
			same_upper_defined: upper_defined = other.upper_defined

	subinterval (start_pos, end_pos: INTEGER): like Current
			-- Interval made of items of current array within
			-- bounds start_pos and end_pos.
	
feature -- Iteration

	exists (condition: FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]): BOOLEAN
			-- Does at least one of  interval's values
			-- satisfy condition?
		require
			finite: upper_defined and lower_defined
		ensure
			consistent_with_count: Result = (hold_count (condition) > 0)

	exists1 (condition: FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]): BOOLEAN
			-- Does exactly one of  interval's values
			-- satisfy condition?
		require
			finite: upper_defined and lower_defined
		ensure
			consistent_with_count: Result = (hold_count (condition) = 1)

	for_all (condition: FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]): BOOLEAN
			-- Do all interval's values satisfy condition?
		require
			finite: upper_defined and lower_defined
		ensure
			consistent_with_count: Result = (hold_count (condition) = count)

	hold_count (condition: FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]): INTEGER
			-- Number of  interval's values that
			-- satisfy condition
		require
			finite: upper_defined and lower_defined
		ensure
			non_negative: Result >= 0
	
invariant

	count_definition: upper_defined and lower_defined implies count = upper - lower + 1
	index_set_is_range: equal (index_set, Current)
	not_infinite: upper_defined and lower_defined
		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)
		-- from RESIZABLE
	increase_by_at_least_one: minimal_increase >= 1
		-- from BOUNDED
	valid_count: count <= capacity
	full_definition: full = (count = capacity)
		-- from FINITE
	empty_definition: is_empty = (count = 0)
	non_negative_count: count >= 0
		-- from INDEXABLE
	index_set_not_void: index_set /= void

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 INTEGER_INTERVAL