comp.lang.ada
 help / color / mirror / Atom feed
* Unconstrained Array ??
@ 1999-08-10  0:00 Duncan Woodward
  1999-08-10  0:00 ` Stephen Leake
  1999-08-10  0:00 ` David C. Hoos, Sr.
  0 siblings, 2 replies; 3+ messages in thread
From: Duncan Woodward @ 1999-08-10  0:00 UTC (permalink / raw)


hi;
I want to create a package that handles actions upon
a multi dimensional unconstrained array.

It is likely that I've approached this problem from the
wrong direction, but this is where I'm at..

package array_Handler is

type Container_Matrix is array(POSITIVE range <>,
                                            POSITIVE range <>,
                                            POSITIVE range <>) of Boolean;

procedure1 Set_Container_Size(x,y,z:Integer);

                -- this procedure takes in the three axis values for
                -- the array index ranges.
                -- Creates instance of Container_Matrix using parameters
                -- x,y,z.

procedure2 ...etc
procedure3 ...etc

end array_Handler;

PROBLEM: How do I create an instance of type Container_Matrix
that is visible to all procedures in the package.

A single global declaration would be fine if the array sizes were know
in advance!

Any ideas please?

Duncan RD.







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

* Re: Unconstrained Array ??
  1999-08-10  0:00 Unconstrained Array ?? Duncan Woodward
  1999-08-10  0:00 ` Stephen Leake
@ 1999-08-10  0:00 ` David C. Hoos, Sr.
  1 sibling, 0 replies; 3+ messages in thread
From: David C. Hoos, Sr. @ 1999-08-10  0:00 UTC (permalink / raw)



Duncan Woodward <drdw@synthsoft.demon.co.uk> wrote in message
news:934306216.27590.0.nnrp-10.c2de38ef@news.demon.co.uk...
> hi;
> I want to create a package that handles actions upon
> a multi dimensional unconstrained array.
>
> It is likely that I've approached this problem from the
> wrong direction, but this is where I'm at..
>
> package array_Handler is
>
> type Container_Matrix is array(POSITIVE range <>,
>                                             POSITIVE range <>,
>                                             POSITIVE range <>) of Boolean;
>
> procedure1 Set_Container_Size(x,y,z:Integer);
>
>                 -- this procedure takes in the three axis values for
>                 -- the array index ranges.
>                 -- Creates instance of Container_Matrix using parameters
>                 -- x,y,z.
>
> procedure2 ...etc
> procedure3 ...etc
>
> end array_Handler;
>
> PROBLEM: How do I create an instance of type Container_Matrix
> that is visible to all procedures in the package.
>
> A single global declaration would be fine if the array sizes were know
> in advance!
>

If you're going to have multiple subprograms accessing the same object,
it sounds like a candidate for a protected type.

Why not something like the following example?

package array_Handler is

   type Matrix is array
     (Positive range <>, Positive range <>, Positive range <>)
     of Boolean;

   protected type Container_Matrix
     (X: Positive;
      Y: Positive;
      Z : Positive) is

      procedure Action_2;

      procedure Action_3;

   private

      The_Matrix : Matrix (1 .. X, 1 .. Y, 1 .. Z);

   end Container_Matrix;

end array_Handler;

with Ada.Text_IO;
package body array_Handler is

   ----------------------
   -- Container_Matrix --
   ----------------------

   protected body Container_Matrix is

      --------------
      -- Action_2 --
      --------------

      procedure Action_2 is
      begin
         Ada.Text_IO.Put_Line
           ("Action_2: " &
            "X range is" &
            Positive'Image (The_Matrix'First (1)) & " .." &
            Positive'Image (The_Matrix'Last (1)) & "; " &
            "Y range is" &
            Positive'Image (The_Matrix'First (2)) & " .." &
            Positive'Image (The_Matrix'Last (2)) & "; " &
            "Z range is" &
            Positive'Image (The_Matrix'First (3)) & " .." &
            Positive'Image (The_Matrix'Last (3)) & ".");
      end Action_2;


      --------------
      -- Action_3 --
      --------------

      procedure Action_3 is
      begin
         Ada.Text_IO.Put_Line
           ("Action_3: " &
            "X range is" &
            Positive'Image (The_Matrix'First (1)) & " .." &
            Positive'Image (The_Matrix'Last (1)) & "; " &
            "Y range is" &
            Positive'Image (The_Matrix'First (2)) & " .." &
            Positive'Image (The_Matrix'Last (2)) & "; " &
            "Z range is" &
            Positive'Image (The_Matrix'First (3)) & " .." &
            Positive'Image (The_Matrix'Last (3)) & ".");
      end Action_3;

   end Container_Matrix;

end array_Handler;

with Array_Handler;
procedure Test_Array_Handler is

   The_Array : Array_Handler.Container_Matrix (3, 4, 5);

begin

   The_Array.Action_2;

   The_Array.Action_3;

end Test_Array_Handler;







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

* Re: Unconstrained Array ??
  1999-08-10  0:00 Unconstrained Array ?? Duncan Woodward
@ 1999-08-10  0:00 ` Stephen Leake
  1999-08-10  0:00 ` David C. Hoos, Sr.
  1 sibling, 0 replies; 3+ messages in thread
From: Stephen Leake @ 1999-08-10  0:00 UTC (permalink / raw)


"Duncan Woodward" <drdw@synthsoft.demon.co.uk> writes:

> hi;
> I want to create a package that handles actions upon
> a multi dimensional unconstrained array.
> 
> It is likely that I've approached this problem from the
> wrong direction, but this is where I'm at..
> 
> package array_Handler is
> 
> type Container_Matrix is array(POSITIVE range <>,
>                                             POSITIVE range <>,
>                                             POSITIVE range <>) of Boolean;
> 
> procedure1 Set_Container_Size(x,y,z:Integer);
> 
>                 -- this procedure takes in the three axis values for
>                 -- the array index ranges.
>                 -- Creates instance of Container_Matrix using parameters
>                 -- x,y,z.
> 
> procedure2 ...etc
> procedure3 ...etc
> 
> end array_Handler;
> 
> PROBLEM: How do I create an instance of type Container_Matrix
> that is visible to all procedures in the package.
> 
> A single global declaration would be fine if the array sizes were know
> in advance!
> 
> Any ideas please?

You have two choices; declare an object that can change array size, or
use an access type.

The first approach:

package Duncan is

   Max_Index : constant := 100;
   -- 1000 gave STORAGE_ERROR with GNAT 3.11p Windows NT
   
   type Index_Type is range 1 .. Max_Index;
   type Container_Array is array
      (Index_Type range <>,
       Index_Type range <>,
       Index_Type range <>) of Boolean;

   type Container_Matrix  (Length, Width, Depth : Index_Type := 1) 
   is record
      Matrix : Container_Array (1 .. Length, 1 .. Width, 1 .. Depth);
   end record;

   Container : Container_Matrix;

   procedure Set_Container_Size (X, Y, Z : in Index_Type);

end Duncan;

package body Duncan is

   procedure Set_Container_Size (X, Y, Z : in Index_Type)
   is begin
      Container :=
         (Length => X,
          Width => Y,
          Depth => Z,
          Matrix => (others => (others => (others => False))));
   end Set_Container_Size;

end Duncan;

with Duncan;
procedure test_duncan
is
begin
   Duncan.Set_Container_Size (2, 3, 4);
   Duncan.Set_Container_Size (5, 6, 7);
end test_duncan;

Note the comment on Max_Index; the GNAT compiler allocates the maximum
size for Duncan.Container, so Max_Index can't be too large. Most
compilers take this approach, but some dynamically reallocate the
object, and won't have this restriction.

Using an access type is more flexible, but you should make it
Controlled to enforce garbage collection (not done here):

package Duncan1 is

   type Container_Array is array
      (Positive range <>,
       Positive range <>,
       Positive range <>) of Boolean;

   type Container_Matrix is access all Container_Array;

   Container : Container_Matrix;

   procedure Set_Container_Size (X, Y, Z : in Positive);

end Duncan1;

with Ada.Unchecked_Deallocation;
package body Duncan1 is

   procedure Free is new Ada.Unchecked_Deallocation (Container_Array, Container_Matrix);

   procedure Set_Container_Size (X, Y, Z : in Positive)
   is begin
      Free (Container);
      Container := new Container_Array'
         (1 .. X => (1 .. Y => (1 .. Z => False)));
   end Set_Container_Size;

end Duncan1;

with Duncan1;
procedure test_duncan1
is
begin
   Duncan1.Set_Container_Size (2, 3, 4);
   Duncan1.Set_Container_Size (5, 6, 7);
end test_duncan1;

In both approaches, you should probably make Container private. other
enhancements are also possible!

-- Stephe




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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-08-10  0:00 Unconstrained Array ?? Duncan Woodward
1999-08-10  0:00 ` Stephen Leake
1999-08-10  0:00 ` David C. Hoos, Sr.

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