comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Help needed: Access type to functions in generic package
Date: 1999/09/13
Date: 1999-09-13T00:00:00+00:00	[thread overview]
Message-ID: <37dda46e@news1.prserv.net> (raw)
In-Reply-To: 7ri82a$45l$1@nnrp1.deja.com

In article <7ri82a$45l$1@nnrp1.deja.com> , zeiram@deathsdoor.com  wrote:

> By "dynamic implementation", I meant a definition of my maze with
> access types to the components of my maze. A bit "a la" sparse
> matrix. With that, the memory space is allocated only when a new
> corridor is created at run time.

You've seen the static implementation of the matrix in a previous post.
Now, here's an idea for a dynamic implementation:

generic

   type Item_Type is private;

package P is

   type T (X, Y, Z : Positive) is private;

   procedure Set_Item
     (O       : in out T;
      X, Y, Z : in     Positive;
      Item    : in     Item_Type);

   function Get_Item
     (O       : T;
      X, Y, Z : Positive) return Item_Type;


private

   type Node_Type;
   type Node_Access is access Node_Type;

   type Node_Type is
      limited record
         Index : Positive;
         Item  : Item_Type;
         Next  : Node_Access;
      end record;

   type Array_2D_Type is
      array (Positive range <>, Positive range <>) of Node_Access;

   type T (X, Y, Z : Positive) is
      record
         Array_2D : Array_2D_Type (1 .. X, 1 .. Y);
      end record;

end P;


This is a sort of sparse matrix.  You statically allocate an array for the
first 2 dimensions, but the 3rd dimension is dynamically allocated.

Here, I've implemented the 3rd dimension as a linked list.  To find an item,
you'll have to traverse the list until you find the Index you're interested
in.

This is space efficient, but grows more time inefficient as the number of
items in the column increases.

An alternative approach is to allocate the 3rd dimension as an array.
Fetching an item then requires only O(1) time, although it's not as space
efficient as the linked list (for large values of Z).

Is something like this what you had in mind?

(Je ne parle qu'un peu de francais.  Peut-etre vous pouvez demander les
autres francais sur le newsgroup fr.comp.lang.ada.)

Matt






      reply	other threads:[~1999-09-13  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-12  0:00 Help needed: Access type to functions in generic package Zeiram
1999-09-12  0:00 ` Matthew Heaney
1999-09-12  0:00   ` Zeiram
1999-09-12  0:00     ` Matthew Heaney
1999-09-13  0:00       ` zeiram
1999-09-13  0:00         ` Matthew Heaney [this message]
replies disabled

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