This site contains older material on Eiffel. For the main Eiffel page, see http://www.eiffel.com.

EiffelBase class
(HTML page generated by ISE Eiffel 4.2)

Eiffel Class
indexing
	description: "Stacks with a bounded physical size, implemented by arrays";
	status: "See notice at end of class";
	names: dispenser, array;
	representation: array;
	access: fixed, lifo, membership;
	size: fixed;
	contents: generic;
	date: "$Date: 2007-03-30 11:10:11 -0800 (Fri, 30 Mar 2007) $";
	revision: "$Revision: 95354 $"

class BOUNDED_STACK [G]

inherit
	STACK [G]
		redefine
			replace, item, linear_representation
		end;
	BOUNDED [G]
		undefine
			consistent, copy, setup, is_equal
		end

creation
	make

feature -- Initialization

	make (n: INTEGER) is
			-- Create a stack for at most n items.
		require
			non_negative_argument: n >= 0
		do
			create fl.make (0, n)
		ensure
			stack_allocated: capacity = n;
			empty_stack: count = 0
		end;

feature -- Access

	item: G is
			-- Last item pushed (i.e. top)
		require
			not_empty: count > 0
		do
			Result := fl.item (count)
		end;

feature -- Measurement

	count: INTEGER;

	capacity: INTEGER is
		do
			Result := fl.count - 1
		end;

	occurrences (v: G): INTEGER is
		do
			if object_comparison then
				fl.compare_objects
			else
				fl.compare_references
			end;
			Result := fl.occurrences (v)
		end;

feature -- Element change

	extend (v: like item) is
			-- Push v on top.
			-- Was declared in BOUNDED_STACK as synonym of extend, force and put.
		do
			count := count + 1;
			fl.put (v, count)
		end;

	force (v: like item) is
			-- Push v on top.
			-- Was declared in BOUNDED_STACK as synonym of extend, force and put.
		do
			count := count + 1;
			fl.put (v, count)
		end;

	put (v: like item) is
			-- Push v on top.
			-- Was declared in BOUNDED_STACK as synonym of extend, force and put.
		do
			count := count + 1;
			fl.put (v, count)
		end;

	replace (v: like item) is
			-- Replace top item by v.
		do
			fl.put (v, count)
		end;

feature -- Access

	has (v: G): BOOLEAN is
			-- Does v appear in stack?
			-- (Reference or object equality,
			-- based on object_comparison.)
		do
			if object_comparison then
				fl.compare_objects
			else
				fl.compare_references
			end;
			Result := fl.has (v)
		end;

feature -- Removal

	remove is
			-- Remove top item.
		require
			not_empty: count /= 0
		local
			default_value: like item
		do
			fl.put (default_value, count);
			count := count - 1
		end;

	wipe_out is
			-- Remove all items.
		do
			fl.clear_all;
			count := 0
		end;

feature -- Status report

	extendible: BOOLEAN is
		do
			Result := notfull
		ensure
			Result = notfull
		end;

	Resizable: BOOLEAN is true;

	Prunable: BOOLEAN is true;

feature -- Conversion

	linear_representation: ARRAYED_LIST [G] is
			-- Representation as a linear structure
			-- (in the reverse order of original insertion)
		local
			i: INTEGER
		do
			from
				create Result.make (count);
				i := count
			until
				i < 0
			loop
				Result.extend (fl.item (i));
				i := i - 1
			end
		end;

feature {STACK} -- Implementation

	start is
			-- Move to first position.
			-- (No effect if empty)
		do
			if notempty then
				index := count
			end
		end;

	finish is
			-- Move to last position.
			-- (No effect if empty)
		do
			if notempty then
				index := 1
			end
		end;

	forth is
			-- Move to next position.
		do
			index := index - 1
		end;

	off: BOOLEAN is
			-- Is there no current item?
		do
			Result := (index < 1) or else (index > count)
		end;

	fl: ARRAY [G];
			-- Storage

	index: INTEGER;
			-- Current place in stack.

feature {NONE} -- Inapplicable

	prune (v: G) is
		do
		end;

invariant

	count_small_enough: count <= capacity;
	extendible_definition: extendible = notfull;

end -- class BOUNDED_STACK