comp.lang.ada
 help / color / mirror / Atom feed
* Re: Help needed: Access type to functions in generic package
  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
  0 siblings, 1 reply; 6+ messages in thread
From: Matthew Heaney @ 1999-09-12  0:00 UTC (permalink / raw)


In article <37DBA86F.3805AA69@deathsdoor.com> , Zeiram 
<zeiram@deathsdoor.com>  wrote:


> generic
>     Dim_X, Dim_Y, Dim_Z : Positive;
> package Maze is
>     type T_Cell is (Wall, Corridor, Pebble);
>     type T_Maze is array(1..Dim_X, 1..Dim_Y, 1..Dim_Z) of T_Cell;
> end Maze;

Why is this package generic?  Why don't you declare your array type this
way:

package Maze is

  type T_Cell is (Well, Corridor, Pebble);

  type T_Maze is
    array (Positive range <>, Positive range <>, Positive range <>)
    of T_Cell;

end Maze;


Instead of instantiating a generic to get a constrained array subtype, you
can just declare an array object, or declare an array subtype.  No generic
is needed.

  Maze : T_Maze (1 .. 10, 2 .. 11, 3 .. 12);

or

  subtype My_Maze is T_Maze (1 .. 10, 2 .. 11, 3 .. 12);

  Maze : My_Maze;




> generic
> package Maze.Display is
>     procedure Display_Maze(Maze : in T_Maze);
> end Maze.Display;

I package Maze is non-generic, then this child package doesn't need to be
generic either:

  package Maze.Display is

    procedure Display_Maze (Maze : in T_Maze);
    -- (sub)type T_Maze is now an unconstrained array

  end;



> with glut;
> with Win32.GL;
> with Maze.Display.Callback;
> package body Maze.Display is
>     package Callback is new Maze.Display.Callback;
>     use Callback;


Maze.Display.Callback doesn't need to be generic; see below.


>     procedure Display_Maze(Maze : in T_Maze) is
>       Menu : Integer;
>     begin
>       Menu := glutCreateMenu(Main_Menu'access);
>     end Display_Maze;
>
> end Maze.Display;
>
> package Maze.Display.Callback is
>     procedure Main_Menu(Value : Integer);
> end Maze.Display.Callback;


Again, this doesn't need to be generic either:

  package Maze.Display.Callback is

    procedure Main_Menu (...);

  end Maze.Display.Callback;


Try the changes I've suggested, and see if your problem goes away.





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

* Re: Help needed: Access type to functions in generic package
  1999-09-12  0:00   ` Zeiram
@ 1999-09-12  0:00     ` Matthew Heaney
  1999-09-13  0:00       ` zeiram
  0 siblings, 1 reply; 6+ messages in thread
From: Matthew Heaney @ 1999-09-12  0:00 UTC (permalink / raw)


In article <37DBFEBF.B74D1958@deathsdoor.com> , Zeiram 
<zeiram@deathsdoor.com>  wrote:

>> Why is this package generic?  Why don't you declare your array type this
>> way:
>>
>
> In fact, this was just a part of the definitions of my package.
> In reality, my type T_Maze is declared as private.

Then why not declare T_Maze as private, but with discriminants?

  package Maze is

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

  private

    type T_Maze_Array is
      array (Positive range <>, Positive range <>, Positive range <>) of
      T_Cell;

    type T_Maze (X, Y, Z : Positive) is
      record
        Items : T_Maze_Array (1 .. X, 1 .. Y, 1 .. Z);
      end record;

  end Maze;



> This is due
> to the fact that I also need to realize a dynamic implementation
> of my software (yes, this a project from my CS course :-) )

I don't know what you mean by "realize a dynamic implementation."

> It consequently seemed logical to create this package as a
> generic one.

I'm still not convinced you need a generic.


> If I decide to use a non private type and do only a static
> implementation, I know for sure your solution will work
> perfect. (It was, in fact, the way I ran my first tests)

I don't know what you mean by "static implementation."

Does the private type above meet your needs?  If not, why not?




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

* Help needed: Access type to functions in generic package
@ 1999-09-12  0:00 Zeiram
  1999-09-12  0:00 ` Matthew Heaney
  0 siblings, 1 reply; 6+ messages in thread
From: Zeiram @ 1999-09-12  0:00 UTC (permalink / raw)


Hello

I'm trying to use access types to functions in a generic package and
GNAT refuses to compile my package. Here's the complete description of
my
problem.

I'm trying to use OpenGL and the GLUT toolkit to display a 3D maze on
the screen. My approach was to create a package specifying a type
T_Maze,
which is generic on the length, height and depth of the maze.

Then, I wanted to create a child of this package for the procedure
displaying
my maze. I have to use access types on functions for defining my
callback functions
used for OpenGL. And, of course, my child package is also generic. My
callback
functions are defined in a child of the child package.

But when I try to compile this with GNAT for Win32, I get the following
error
message:
    access type must not be outside generic body

I tried to move all my callback functions in the body of my display
package, but
the same error message was there. Does anyone have an idea how I could
compile this package?

Here are part of the specs and bodies of my packages.

generic
    Dim_X, Dim_Y, Dim_Z : Positive;
package Maze is
    type T_Cell is (Wall, Corridor, Pebble);
    type T_Maze is array(1..Dim_X, 1..Dim_Y, 1..Dim_Z) of T_Cell;
end Maze;

generic
package Maze.Display is
    procedure Display_Maze(Maze : in T_Maze);
end Maze.Display;

with glut;
with Win32.GL;
with Maze.Display.Callback;
package body Maze.Display is
    package Callback is new Maze.Display.Callback;
    use Callback;

    procedure Display_Maze(Maze : in T_Maze) is
      Menu : Integer;
    begin
      Menu := glutCreateMenu(Main_Menu'access);
    end Display_Maze;

end Maze.Display;

package Maze.Display.Callback is
    procedure Main_Menu(Value : Integer);
end Maze.Display.Callback;

Thanks in advance for helping me.

Regards

    Zeiram





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

* Re: Help needed: Access type to functions in generic package
  1999-09-12  0:00 ` Matthew Heaney
@ 1999-09-12  0:00   ` Zeiram
  1999-09-12  0:00     ` Matthew Heaney
  0 siblings, 1 reply; 6+ messages in thread
From: Zeiram @ 1999-09-12  0:00 UTC (permalink / raw)




Matthew Heaney wrote :

> Why is this package generic?  Why don't you declare your array type this
> way:
>

In fact, this was just a part of the definitions of my package.
In reality, my type T_Maze is declared as private. This is due
to the fact that I also need to realize a dynamic implementation
of my software (yes, this a project from my CS course :-) )
It consequently seemed logical to create this package as a
generic one.

If I decide to use a non private type and do only a static
implementation, I know for sure your solution will work
perfect. (It was, in fact, the way I ran my first tests)

Thanks for your help.

Has anybody other ideas?

    Regards,
    Zeiram







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

* Re: Help needed: Access type to functions in generic package
  1999-09-12  0:00     ` Matthew Heaney
@ 1999-09-13  0:00       ` zeiram
  1999-09-13  0:00         ` Matthew Heaney
  0 siblings, 1 reply; 6+ messages in thread
From: zeiram @ 1999-09-13  0:00 UTC (permalink / raw)


In article <37dc1a3f@news1.prserv.net>,
  "Matthew Heaney" <matthew_heaney@acm.org> wrote:
> In article <37DBFEBF.B74D1958@deathsdoor.com> , Zeiram
> <zeiram@deathsdoor.com>  wrote:
>
> > This is due
> > to the fact that I also need to realize a dynamic implementation
> > of my software (yes, this a project from my CS course :-) )
>
> I don't know what you mean by "realize a dynamic implementation."
>
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.

Opposed to this is a "static implementation" where the needed memory
space is allocated at compile time, hence using a maximum memory.

I'm not sure these terms are correct in English, they are a direct
translation from the terms we use in French. Does these definitions
help you understand what I wanted with my code?

> Does the private type above meet your needs?  If not, why not?
>
It will meet my needs, but only because we reduced our needs.
Part of the definition of our project was to extend our
maze to a Nth dimension space, not just 3 dim... If we still
had to do it in a larger dimensional space, I think this solution
would be difficult to use.

Anyway, thanks a lot for your help.


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




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

* Re: Help needed: Access type to functions in generic package
  1999-09-13  0:00       ` zeiram
@ 1999-09-13  0:00         ` Matthew Heaney
  0 siblings, 0 replies; 6+ messages in thread
From: Matthew Heaney @ 1999-09-13  0:00 UTC (permalink / raw)


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






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

end of thread, other threads:[~1999-09-13  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox