comp.lang.ada
 help / color / mirror / Atom feed
From: Stapler <spam.magnet@yahoo.com>
Subject: Int_Stack rewritten.
Date: Mon, 02 Dec 2002 18:37:27 GMT
Date: 2002-12-02T18:37:27+00:00	[thread overview]
Message-ID: <HLNG9.4194$pN3.466@sccrnsc03> (raw)

As per the suggestions, I've rewritten my simple Int_Stack package. Hope
it looks better.

package int_stack is
	
	pragma Pure(int_stack);
	
	Stack_Overflow : Exception;
	Stack_Underflow : Exception;
	
	-- As far as I can tell these procedures are squared away.
	-- 12/02/02
	type Int_Stack(Size : Natural) is limited private;
	procedure Push(The_Stack : in out Int_Stack; X : in Integer);
	procedure Pop(The_Stack : in out Int_Stack; X : out Integer);
	
	-- These functions and procedures are new and untested.
	function Is_Empty(The_Stack : in Int_Stack) return Boolean;
	function Is_Full(The_Stack : in Int_Stack) return Boolean;
	function Length_Of(The_Stack : in Int_Stack) return Natural;
	function Sum_Of(The_Stack : in Int_Stack) return Integer;
	-- I will add more functions and procedures as soon as I'm sure
	-- this basic package isn't hosed.
	
private
	
	type Integer_Array is array(Positive range <>) of Integer;

	type Int_Stack(Size : Natural) is 
	limited record
		Elements : Integer_array(1..Size);
		Top : Natural := 1;
	end record;
	
	
end int_stack;


package body int_stack is
	
	procedure Push(The_Stack : in out Int_Stack; X : in Integer) is
		
	begin
		if The_Stack.Top > The_Stack.Size then
			raise Stack_Overflow;
		end if;
		
		The_Stack.Elements(The_Stack.Top) := X;
		
		if The_Stack.Top < The_Stack.Size then
			The_Stack.Top := The_Stack.Top + 1;
		end if;
	
	end Push;
	
	procedure Pop(The_Stack : in out Int_Stack; X : out Integer) is
		
	begin
		If The_Stack.Top < 1 then
			raise Stack_Underflow;
		end if;
		
		X := The_Stack.Elements(The_Stack.Top);
		
		if The_Stack.Top > 1 then
			The_Stack.Top := The_Stack.Top - 1;
		end if;
		
		
	end Pop;
	
	function Is_Empty(The_Stack : in Int_stack) return Boolean is
	
	begin
	
		if The_Stack.Top = The_Stack.Elements'First then
			return True;
		else
			return False;
			
		end if;
		
	end Is_Empty;
	
	function Is_Full(The_Stack : in Int_Stack) return Boolean is
	
	begin
	
		if The_Stack.Top = The_Stack.Elements'Last then
			
			return True;
		else 
			return False;
		
		end if;
		
	end Is_Full;
	
	function Length_Of(The_Stack : in Int_Stack) return Natural is
	
		Length : Natural;
	
	begin
		-- This entire function seems somewhat rendundant
		-- considering the "Length" attribute. But it's
		-- being included as suggested
		
		Length := The_Stack.Elements'Length;
		
		return Length;
		
	end Length_Of;		

	function Sum_Of(The_Stack : in Int_Stack) return Integer is
		
		The_Sum : Integer := 0;
	
	begin
	
		for J in 1..The_Stack.Top loop
		
			The_Sum := The_Sum + The_Stack.Elements(J);
			
		end loop;
		
		return The_Sum;
	
	end Sum_Of;

end int_stack;

I hope this one looks better.

So, have I improved somewhat? Heh.

Thanks for your patience, and try not to laugh to hard. Heh.


Stapler



             reply	other threads:[~2002-12-02 18:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-02 18:37 Stapler [this message]
2002-12-02 20:34 ` Int_Stack rewritten Björn Lundin
2002-12-02 20:55 ` Jeffrey Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox