comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: containers and garbage collections.
Date: Sat, 13 Sep 2003 03:33:56 GMT
Date: 2003-09-13T03:33:56+00:00	[thread overview]
Message-ID: <u3cf15pjr.fsf@earthlink.net> (raw)
In-Reply-To: 2458809.CYNMQ0Kujp@linux1.krischik.com

Martin Krischik <krischik@users.sourceforge.net> writes:

> Without garbage collection access types are pain in the bud. And when the
> container library only stores "type element is private;" than one need to
> use access types as element.

This statement is not quite correct.  It all depends on your level of
abstraction.  For example, a container for indefinite types can be
declared this way:

generic

   type Element_Type (<>) is private;

package Queues is

   type Queue_Type is limited private;

   procedure Insert
     (Queue : in out Queue_Type;
      Item  : in     Element_Type);

   function Top (Queue : Queue_Type) return Element_Type;

   procedure Pop (Queue : in out Queue_Type);

private

   type Element_Access is access Element_Type;

   type Node_Type;
   type Node_Access is access Node_Type;

   type Node_Type is record
      Element : Element_Access;
      Next    : Node_Access;
   end record;

   type Queue_Type is limited record
      Top : Node_Access;
   end record;

end Queues;


Now Insert can be implemented like this:

   procedure Insert
     (Queue : in out Queue_Type;
      Item  : in     Element_Type) is

      Node : constant Node_Access :=
         new Node_Type'(Element => new Element_Type'(Item),
                        Next    => null);
   begin
      ...
   end;

To extract an item, you just do this:

   procedure Op (Q : in out QT) is
      E : ET := Top (Q);
   begin
      Pop (Q);
   end;

From the point of view of a user of the queue, the queue contains
elements, of indefinite type Element_Type.  Clearly, inside the queue,
access types are used, but that is an implementation detail hidden from
client.


> I have proven that both options are possible
> 
> I have implemented a garbage collected storrage pool for GNAT.
> 
> I also have created containers for "type Element (<>) is abstract tagged
> private;" and I belive "type Element is array (Index range <>) of Item;" is
> possible as well (Enabling collection of Strings). Both can't be done with
> C++ templates - so we should not take the all mighty STL as an example.

Both of these containers are possible using the single declaration:

generic
   type Element_Type (<>) is private;
package GP is ...;

You can instantiate GP using either of the types in your example.  It's
not clear why you made two separate containers, one for tagged types
vs. another for array types, as a single container seems adequate.





  parent reply	other threads:[~2003-09-13  3:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-12 15:32 containers and garbage collections Martin Krischik
2003-09-12 16:41 ` Stephen Leake
2003-09-13  9:37   ` Martin Krischik
2003-09-13  3:33 ` Matthew Heaney [this message]
2003-09-13  9:45   ` Martin Krischik
2003-09-13 17:10     ` Matthew Heaney
2003-09-15 13:47     ` Dmitry A. Kazakov
2003-09-15 23:11       ` Matthew Heaney
2003-09-16 17:59         ` Martin Krischik
replies disabled

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