comp.lang.ada
 help / color / mirror / Atom feed
* Package instantiation and automatic de-allocation.
@ 2002-12-23  4:42 Stapler
  2002-12-23  5:09 ` James S. Rogers
  2002-12-24 21:57 ` Stapler
  0 siblings, 2 replies; 6+ messages in thread
From: Stapler @ 2002-12-23  4:42 UTC (permalink / raw)


Alright, I'm running into a bit of wall here.

I can get my package to work, but I cant get it to work the way I want it
to.

Basically, as I understand it, if I declare something such as

	package foo is new generic_stack(Size, Integer);

Then it wont be de-allocated when it goes out of scope(like at the end of
a declare block) like it should be. This is assuming I've declared it in
the declare section of the declare block. 

Further more I can't "use" generic_stack and then declare a variable such
as

	My_Stack : Stack(Size, Integer);

because language rules wont allow me to "use" a generic_package.

I've tried all sorts of things...
	with gen_stack;

		My_Stack : gen_stack.Stack(Size, Integer);

		My_Stack : new gen_stack.Stack(Size, Integer);

		My_Stack : Blah blah blah.


Heres the package spec, any pointers would be helpful.


generic
	Size : Positive;
	type Item is private;
	
package gen_stack is
	
	Stack_Overflow : exception;
	Stack_Underflow : exception;
	
	procedure Push( X : in Item);
	function Pop return Item;
	function Is_Full return Boolean; -- These functions simply export 
	function Is_Empty return Boolean;-- The Full, Empty fields of the stack.
	
private

	subtype Stack_range is positive range 1..Size;
	type Item_array is array(positive range 1..Size) of Item;
	
	type Stack is limited record
		Elements : Item_Array;
		Top : Stack_range;
		Full, Empty : Boolean;
	end record;
	
end gen_stack;


Basically I want to figure out how to instantiate this within a program
without having to dick with Unchecked_Deallocation. i.e. I want to be
able to assume that it will be de-allocated at then "end" of a declare
block, whether a procedure or an inline "declare" region.

Currently I have to go ...

	package My_Stack is new gen_stack(Size, Foo_Type);


Any pointers would be appreciated.

Stapler



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Package instantiation and automatic de-allocation.
  2002-12-23  4:42 Package instantiation and automatic de-allocation Stapler
@ 2002-12-23  5:09 ` James S. Rogers
  2002-12-23  6:22   ` tmoran
  2002-12-24 21:57 ` Stapler
  1 sibling, 1 reply; 6+ messages in thread
From: James S. Rogers @ 2002-12-23  5:09 UTC (permalink / raw)


"Stapler" <spam.magnet@yahoo.com> wrote in message
news:xuwN9.431253$P31.150740@rwcrnsc53...
> Alright, I'm running into a bit of wall here.
>
> I can get my package to work, but I cant get it to work the way I want it
> to.
>
> Basically, as I understand it, if I declare something such as
>
> package foo is new generic_stack(Size, Integer);
>
> Then it wont be de-allocated when it goes out of scope(like at the end of
> a declare block) like it should be. This is assuming I've declared it in
> the declare section of the declare block.
>
> Further more I can't "use" generic_stack and then declare a variable such
> as
>
> My_Stack : Stack(Size, Integer);
>
> because language rules wont allow me to "use" a generic_package.
>
> I've tried all sorts of things...

Your package below does not work because it lacks a public
declaration for your Stack type. Once you have a name for the
type you must pass an instance of that type to each function and
procedure. Ada does not have an impicit "this" variable like C++ or
Java.

> with gen_stack;
>
> My_Stack : gen_stack.Stack(Size, Integer);
>
> My_Stack : new gen_stack.Stack(Size, Integer);
>
> My_Stack : Blah blah blah.
>
>
> Heres the package spec, any pointers would be helpful.
>
>
> generic
> Size : Positive;
> type Item is private;
>
> package gen_stack is
>
   type Stack is private;

> Stack_Overflow : exception;
> Stack_Underflow : exception;
>
 procedure Push( X : in Item : Onto : in out Stack);
function Pop(From : Stack) return Item;
function Is_Full(Item : Stack) return Boolean; -- These functions simply
export
function Is_Empty(Item : Stack) return Boolean;-- The Full, Empty fields of
the stack.
>
> private
>
> subtype Stack_range is positive range 1..Size;
> type Item_array is array(positive range 1..Size) of Item;
>
> type Stack is limited record
> Elements : Item_Array;
> Top : Stack_range;
> Full, Empty : Boolean;
> end record;
>
> end gen_stack;
>
>
> Basically I want to figure out how to instantiate this within a program
> without having to dick with Unchecked_Deallocation. i.e. I want to be
> able to assume that it will be de-allocated at then "end" of a declare
> block, whether a procedure or an inline "declare" region.

In the example above an instantiation does not allocate an object.
Creating an instance of the generic package is separate from
creating an instance of a stack object from an instantiated package.

>
> Currently I have to go ...
>
> package My_Stack is new gen_stack(Size, Foo_Type);

This is correct. It instantiates the package with a size of Size and
a type of Foo_Type. Next you want to create an instance of
the stack from that package:

My_Foo_Stack : My_Stack.Stack;

This will be deallocated when the block it is declared in goes
out of scope.

Jim Rogers





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Package instantiation and automatic de-allocation.
  2002-12-23  5:09 ` James S. Rogers
@ 2002-12-23  6:22   ` tmoran
  0 siblings, 0 replies; 6+ messages in thread
From: tmoran @ 2002-12-23  6:22 UTC (permalink / raw)


> Your package below does not work because it lacks a public
> declaration for your Stack type. Once you have a name for the
> type you must pass an instance of that type to each function and
> procedure. Ada does not have an impicit "this" variable like C++ or
> Java.
  That's true if you want to allow multiple Stack objects, perhaps
arrays of Stacks, etc.  Sometimes you really only want one or very
few objects, so one/generic instantiation is good.  For instance,
say you have a space rocket with two similar radios, one of which
talks to Earth and the other to your Moon base.  You might then
   generic
     Hardware_Port : Natural;
   package Communications is ...
     procedure Send(...);
     procedure Receive(...);
 ...
 package Earth is new Communications(Port=>16#378#);
 package Moon is new Communications(Port=>16#278#);

Then you can say
  Earth.Send(...);
  Moon.Receive(...);
But if you had 4 moon bases, you would have to
  package Moon_1 is new Communications(...
  package Moon_2 is new Communications(...
  package Moon_3 is new Communications(...
  package Moon_4 is new Communications(...
you couldn't make an array Moon(1 .. 4).

> without having to dick with Unchecked_Deallocation.
If you never say "X := new ...", you never have to use
Unchecked_Deallocation.  If you declare an object or do a generic
instantiation in a declare, it will automagically be deallocated
when you leave that scope.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Package instantiation and automatic de-allocation.
  2002-12-23  4:42 Package instantiation and automatic de-allocation Stapler
  2002-12-23  5:09 ` James S. Rogers
@ 2002-12-24 21:57 ` Stapler
       [not found]   ` <j1cbua.fpc.ln@beastie.ix.netcom.com>
  1 sibling, 1 reply; 6+ messages in thread
From: Stapler @ 2002-12-24 21:57 UTC (permalink / raw)


Alright, I've made some progress.

All I want to do is declare a single instance of a generic stack within a
declarative block(whether a procedure or inlined declare block).

As a newbie, I probably overlooked some things.(As readers of my previous
posts have probably already guessed.) ;->

I'm basically posting this up in hopes of getting feedback. To the Gurus
among us, I'm posting under my own thread so as to allow your newsreader
to scan past my hopeless, banal, novice ramblings.

To those who find the stomach to look at it, thanks for not tossing your
cookies.

Heres what it looks like right now.

generic
	Size : Positive;
	type Item is private;
	
package gen_stack is
	
	Stack_Overflow : exception;
	Stack_Underflow : exception;
	
	procedure Push(X : in Item);
	procedure Pop(Y : out Item);
	function Is_Full return Boolean;
	function Is_Empty return Boolean;
	
private

	subtype Stack_range is natural range 1..Size;
	type Item_array is array(positive range 1..Size) of Item;
	
	-- Make this a limited record?
	type Stack is record
		Elements : Item_Array;
		Top : Stack_range;
		Full, Empty : Boolean;
	end record;
	
end gen_stack;
	
package body gen_stack is
	
	Local : Stack; -- Using this variable to access the
			   -- Stack in the Private part of the package.
			   -- Assuming it should be visible to all procedures
			   -- and functions in this package.	
			   -- Needs to be In/Out in procedure parameters?
	procedure Push(X : in Item) is separate;
	procedure Pop(Y : out Item) is separate;
	
	function Is_Full return Boolean is
	-- All this function does is exports the value
	-- in the full field of the record	
	
	begin
	
		return Local.Full; -- A Boolean value
		
	end Is_Full;
	
	function Is_Empty return Boolean is
	-- Same as Is_Full, except exports Empty instead
	
	begin
	
		return Local.Empty; -- A Boolean value.
		
	end Is_Empty;
	
end gen_stack;

separate (gen_stack)
procedure Pop(Y : out Item) is

begin
	-- If the user attempts to Pop an Item off an
	-- empty stack, then raise Stack_Underflow
	-- Check using the public Is_Empty function;
	if Local.Empty = True then
	
		raise Stack_Underflow;
		
	end if;
	
	-- It seems like it shouldn't be necessary to
	-- set the Empty/Full flags explicitly.
	Local.Empty := False;
	
	Y := Local.Elements(Local.Top);
	
	if (Local.Top - 1) = 0 then
	
		Local.Empty := True;
		
	else
		-- Assuming my logic is correct
		-- If the stack is already on it's last
		-- element, the code should not reach here.
		Local.Top := Local.Top - 1;
		
	end if;

end Pop;

separate (gen_stack)
procedure Push(X : in Item) is

	procedure Insert(X : in Item) is

	begin

		Local.Elements(Local.Top) := X;

	end Insert;

begin
	-- If the user failed to check and see if the Stack is
	-- full via the Is_Full function, and the Stack is full
	-- then raise Stack_Overflow
	if Local.Full = True then
		raise Stack_Overflow;

	end if;
	
	-- If the Code reaches here, then the Full flag wasnt
	-- set to True, but set it explicitly anyways for safety.
	-- Is this needed?
	Local.Full := False;

	-- If there is one remaining element then
	-- Set the Full flag to true and Insert 
	-- the Item into the remaining Element
	-- i.e. Top and Size should both be indexing
	-- the same element at the start of this
	-- "If" function.
	if Local.Top = Size then
		Local.Full := True;
		Insert(X);
	else if Local.Top < Size then
		Insert(X);
		Local.Top := Local.Top + 1;
	end if;
	end if;

end Push;



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Package instantiation and automatic de-allocation.
       [not found]   ` <j1cbua.fpc.ln@beastie.ix.netcom.com>
@ 2002-12-25 23:56     ` Stapler
       [not found]       ` <amndua.f24.ln@beastie.ix.netcom.com>
  0 siblings, 1 reply; 6+ messages in thread
From: Stapler @ 2002-12-25 23:56 UTC (permalink / raw)


Thanks for the pointers Dennis. The package is much simpler to use and
maintain now.

To be honest, I didn't think I could catch the Boolean result of an "="
expression and return it as the result of a function.

I've just about got a handle on Stacks. Do you suppose I should move on
to Trees or Queues next? Furthermore, do increase my understanding of the
Stack data structure, are there any particular Stack algorithms I should
read up on?

I've already covered Linked Lists, which are pretty simple.

Any advice would be appreciated.

Stapler



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Package instantiation and automatic de-allocation.
       [not found]       ` <amndua.f24.ln@beastie.ix.netcom.com>
@ 2003-01-13 15:43         ` John English
  0 siblings, 0 replies; 6+ messages in thread
From: John English @ 2003-01-13 15:43 UTC (permalink / raw)


Dennis Lee Bieber wrote:
> 
> Stapler fed this fish to the penguins on Wednesday 25 December 2002
> 03:56 pm:
> 
> > Thanks for the pointers Dennis. The package is much simpler to use and
> > maintain now.
> >
> It's not perfect -- I think almost every printed Ada textbook covers a
> generic stack...

Indeed -- see http://www.it.bton.ac.uk/staff/je/adacraft/ch13.htm
for another example.

-----------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.it.bton.ac.uk/staff/je
 Dept. of Computing        | ** NON-PROFIT CD FOR CS STUDENTS **
 University of Brighton    |    -- see http://burks.bton.ac.uk
-----------------------------------------------------------------



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-01-13 15:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-23  4:42 Package instantiation and automatic de-allocation Stapler
2002-12-23  5:09 ` James S. Rogers
2002-12-23  6:22   ` tmoran
2002-12-24 21:57 ` Stapler
     [not found]   ` <j1cbua.fpc.ln@beastie.ix.netcom.com>
2002-12-25 23:56     ` Stapler
     [not found]       ` <amndua.f24.ln@beastie.ix.netcom.com>
2003-01-13 15:43         ` John English

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