comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Need help...
Date: 1998/01/11
Date: 1998-01-11T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680001101982236380001@news.ni.net> (raw)
In-Reply-To: 69bvak$14d@bgtnsc01.worldnet.att.net


In article <69bvak$14d@bgtnsc01.worldnet.att.net>, "Scott Barrish"
<TeufelHunde@worldnet.att.net> wrote:

Is there anway to instantiate a queue of stacks?  I know it might not have
>any value in real programming situations, but I just want to see if it can
>be done.  If you would like to help with this problem, I would be more than
>happy to e-mail my packages and code files.

Yes, you can.  Consider these parial declarations

generic
   type Queue_Item is private;
   with function "=" (L, R : Queue_Item) return Boolean is <>;
package Queues is

   type Root_Queue is tagged null record;
...
end Queue;

generic
   type Stack_Item is private;
   with function "=" (L, R : Stack_Item) return Boolean is <>;
package Stacks is 
   
   type Root_Stack is tagged null record;
...
end Stacks;

Assume an instantiation of stacks with on type Integer

with Stacks;
package Integer_Stacks is 
   new Stacks (Integer);


with Integer_Stacks, Stacks.Bounded_G;
package Integer_Stacks.Bounded is 
   new Integer_Stacks.Bounded_G;

(I think that's the syntax.)


Now you can instantiate your queue with your bounded integer stack:

with package Queues, Integer_Stacks.Bounded;
package Integer_Stack_Queues is 
   new Queues (Integer_Stacks.Bounded.Bounded_Stack);


with Integer_Stack_Queues, Queues.Bounded_G;
package Integer_Stack_Queues.Bounded is
   new Queues.Bounded_G;

(I think that's the syntax.)


Now you have a bounded queue of bounded stacks.

Be careful, though.  This is all by-copy.  If you get the front item
(stack) of the queue, you're getting a copy of what's in the queue.  As in

declare
   The_Queue : Integer_Stack_Queues.Bounded.Bounded_Queue;
   The_Stack : Integer_Stacks.Bounded.Bounded_Stack;
begin
   <populate queue>

   The_Stack := Front (The_Queue);

   Push (10, On => The_Stack);
end;

Be careful, because even though you pushed the item 10 on the integer
stack, it's only a copy of the stack at the front of the queue.  If you
were to get the front item of the queue again, that stack doesn't have the
value 10 on its top.

The question you need to ask is, do you want a copy, or do you want to get
a pointer to what's on the queue?  If the latter, then if you push an item
on the stack, it will show up on top of the stack at the front of the
queue.  To do this, you need to instantiate the queue with a pointer to
stack.  When you put a stack in the queue, you're really putting a
poointer-to-stack on the queue.  Something like

with Integer_Stacks;
package Integer_Stack_Types is

   type Integer_Stack_Class_Access_All is
      access all Integer_Stack.Root_Stack'Class;

end;

with Integer_Stack_Types, Queues;
package Integer_Stack_Queues is
   new Queues (Integer_Stack_Types.Integer_Stack_Class_Access_All);

with Integer_Stack_Queues, Queues.Bounded_G;
package Integer_Stack_Queues.Bounded is 
   new Integer_Stack_Queues.Bounded_G;

Now you have a bounded queue of pointers to integer stacks.  As written,
it's a pointer to any kind of stack in the class of integer stack types, so
it's actually a heterogeneous queue of stacks.  Pretty cool, huh?  This is
the kind of thing Chris Sparks was asking about a couple of days ago.

With this formulation, when you ask for the front item of the queue, you're
getting a pointer to the stack at the front.  So any changes you make to
the state of the stack will be reflected in the queue too.

If you have any more questions about this issue, feel free to email me
privately.

Hope that helps,
Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  reply	other threads:[~1998-01-11  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-01-12  0:00 Need help Scott Barrish
1998-01-11  0:00 ` Matthew Heaney [this message]
  -- strict thread matches above, loose matches on Subject: below --
1998-10-14  0:00 need help mostoc
1998-10-15  0:00 ` Pascal Obry
1997-10-26  0:00 Need Help sho
1997-11-01  0:00 ` Matthew Heaney
1996-11-19  0:00 Need help Antonio Casimiro Costa
1994-11-15  1:10 need help Basri Basri
replies disabled

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