comp.lang.ada
 help / color / mirror / Atom feed
* Array Syntax
@ 2008-02-29 15:19 ma740988
  2008-02-29 16:01 ` Ludovic Brenta
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: ma740988 @ 2008-02-29 15:19 UTC (permalink / raw)


Trying to garner a feel for Ada - while perusing texts - and I'm
having slight difficulties understanding array syntax.


   Max_Num : constant := 6;

   type Pixel_Data is
      record
         Num_Of_Values        : Int32;
         Is_Valid_Data           : Boolean;
      end record;

   type Pixel_Number is Int16 range 1 .. Max_Num ;
   type Pixel_Data_Array is array
     ( Pixel_Number range 1 .. Max_Num  )
     of Pixel_Data;

Pixel_Data_Array is an array 6 deep of composite type Pixel Data.  So
far so good?


Now given:

   Frames_In_Buffer : constant := 35 ;
   Buffer_Pixel_Data :
     array ( Int32 range 1 .. Frames_In_Buffer )
     of aliased Pixel_Data_Array ;

From the looks of this Buffer_Pixel_data is an array within an array.
On paper I'm having a hard time representing this in part becuase I'm
having a hard time understanding how a multi-dimensional array can
have two separate types.   IOW:  I'm envisioning in C/C++ land:

   int Buffer_Pixel_Data [ 35 ] [ 6 ]

Trouble is the 35 elements is of type Int32 and the 6 elements is of
type Pixel Data.  Then again, I'm sure I'm not deducing this right.

Any help appreciated in clearing up my confusion.  Thanks in advance.







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

* Re: Array Syntax
  2008-02-29 15:19 Array Syntax ma740988
@ 2008-02-29 16:01 ` Ludovic Brenta
  2008-02-29 17:16 ` Georg Bauhaus
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Ludovic Brenta @ 2008-02-29 16:01 UTC (permalink / raw)


No, what you have is an array of 35 arrays of 6 records. Each record
(of type Pixel_Data) has two components, Num_Of_Values and
Is_Valid_Data.

So:

 Buffer_Pixel_Data (12) (1).Is_Valid_Data := True;

is legal.

The index types for the outermost array and the 35 arrays within it
are different. This is good as it makes it more difficult to
inadvertently use the wrong index. For example:

for J in Buffer_Pixel_Data'Range loop
   for K in Buffer_Pixel_Range (J)'Range loop
       Buffer_Pixel_Data (K) (J) := -- ERROR
          (Num_Of_Values => 42, Is_Valid_Data => True);
   end loop;
end loop;

will give you a compile-time error because you inadvertently swapped J
and K. HTH

--
Ludovic Brenta.



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

* Re: Array Syntax
  2008-02-29 15:19 Array Syntax ma740988
  2008-02-29 16:01 ` Ludovic Brenta
@ 2008-02-29 17:16 ` Georg Bauhaus
  2008-02-29 17:48 ` Dmitry A. Kazakov
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Georg Bauhaus @ 2008-02-29 17:16 UTC (permalink / raw)


ma740988@gmail.com wrote:
> Trying to garner a feel for Ada - while perusing texts - and I'm
> having slight difficulties understanding array syntax.
> 
> 
>    Max_Num : constant := 6;
> 
>    type Pixel_Data is
>       record
>          Num_Of_Values        : Int32;
>          Is_Valid_Data           : Boolean;
>       end record;
> 
>    type Pixel_Number is Int16 range 1 .. Max_Num ;
>    type Pixel_Data_Array is array
>      ( Pixel_Number range 1 .. Max_Num  )
>      of Pixel_Data;
> 
> Pixel_Data_Array is an array 6 deep of composite type Pixel Data.  So
> far so good?

Yes, though either Pixel_Number should be a "new Int16"
or else a subtype of Int16. Below, you'll find an aggregate
object of your anonymous array Buffer_Pixel_Data.
The numbers in the leftmost column are index values of
this type's index.


   D1: constant Pixel_Data :=
        (Num_Of_Values => 42, Is_Valid_Data => true);

   D2: constant Pixel_Data :=
        (Num_Of_Values => -666, Is_Valid_Data => false);
begin
   Buffer_Pixel_Data :=
     -- the first component of Buffer_Pixel_Data
     -- is an array of six, assigned one by one:
     (1 => Pixel_Data_Array'(1 => (0, True),
                             2 => (-1, false),
                             3 => (0, False),
                             4 => D2, 5 => D1,
			     6 => Pixel_Data'(D2)),
      -- the components of the array are records of type Pixel_Data
      -- the last one, at index 6, emphasizes

      -- using range 1 .. Max_Num for collectively setting values
      2 | 4  => Pixel_Data_Array'(1 .. Max_Num => D2),

      -- explicitly mention the respective index type (as per Ludovic's
      -- remarks). The type of 6 is inferred by the compiler
      Int32'(6) => Pixel_Data_Array'(Pixel_Number'(1) .. 6 => D2),

      -- at index positions 3, 5, and 7 put an array with all
      -- components set to record value (1, True)
      3 | 5 | 7 => Pixel_Data_Array'(others => (1, True)),

      -- you can indicate the range of index values by not using
      -- single index values, but the 'Range of the index type,
      8 .. 9 => Pixel_Data_Array'(Pixel_Number'Range => D2),

      -- but it is sufficient to just name the index type
      10 .. Frames_in_Buffer => Pixel_Data_Array'(Pixel_Number => D1));

   -- the latter two work because of a coincidence:
   -- type Pixel_Number is new Int16 range 1 .. Max_Num ;
   -- and
   -- type Pixel_Data_Array is array
   -- ( Pixel_Number range 1 .. Max_Num  ) ... have the same range
constraint
end News6;



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

* Re: Array Syntax
  2008-02-29 15:19 Array Syntax ma740988
  2008-02-29 16:01 ` Ludovic Brenta
  2008-02-29 17:16 ` Georg Bauhaus
@ 2008-02-29 17:48 ` Dmitry A. Kazakov
  2008-02-29 17:50 ` Jeffrey R. Carter
  2008-03-04 16:18 ` ma740988
  4 siblings, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2008-02-29 17:48 UTC (permalink / raw)


On Fri, 29 Feb 2008 07:19:06 -0800 (PST), ma740988@gmail.com wrote:

> Trying to garner a feel for Ada - while perusing texts - and I'm
> having slight difficulties understanding array syntax.
> 
>    Max_Num : constant := 6;
> 
>    type Pixel_Data is
>       record
>          Num_Of_Values        : Int32;
>          Is_Valid_Data           : Boolean;
>       end record;
> 
>    type Pixel_Number is Int16 range 1 .. Max_Num ;
>    type Pixel_Data_Array is array
>      ( Pixel_Number range 1 .. Max_Num  )
>      of Pixel_Data;
> 
> Pixel_Data_Array is an array 6 deep of composite type Pixel Data.  So
> far so good?
> 
> Now given:
> 
>    Frames_In_Buffer : constant := 35 ;
>    Buffer_Pixel_Data :
>      array ( Int32 range 1 .. Frames_In_Buffer )
>      of aliased Pixel_Data_Array ;
> 
> From the looks of this Buffer_Pixel_data is an array within an array.

Buffer_Pixel_Data is an anonymous array of arrays.

> On paper I'm having a hard time representing this in part becuase I'm
> having a hard time understanding how a multi-dimensional array can
> have two separate types.

It is a single-dimensional array which elements are single-dimensional
arrays. When you index it you get a Pixel_Data_Array. When you index that
once more, you get Pixel_Data.

A two-dimensional array would be:

type Pixel_Number is range 1..Max_Num;
type Frame_Number is range 1..Frames_In_Buffer;
type Image is array (Frame_Number, Pixel_Number) of Pixel_Data;

That is indexed by one index, which is a tuple of Frame_Number and
Pixel_Number.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Array Syntax
  2008-02-29 15:19 Array Syntax ma740988
                   ` (2 preceding siblings ...)
  2008-02-29 17:48 ` Dmitry A. Kazakov
@ 2008-02-29 17:50 ` Jeffrey R. Carter
  2008-03-04 16:18 ` ma740988
  4 siblings, 0 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2008-02-29 17:50 UTC (permalink / raw)


ma740988@gmail.com wrote:
> 
>    Max_Num : constant := 6;
> 
>    type Pixel_Data is
>       record
>          Num_Of_Values        : Int32;
>          Is_Valid_Data           : Boolean;
>       end record;
> 
>    type Pixel_Number is Int16 range 1 .. Max_Num ;

This is not valid. You either want a subtype, a derived type ("new Int16"), or 
just a type ("is range").

>    type Pixel_Data_Array is array
>      ( Pixel_Number range 1 .. Max_Num  )
>      of Pixel_Data;

This can just be

type Pixel_Data_Array is array (Pixel_Number) of Pixel_Data;

Normally, unless you're doing something where you need to match the HW, you 
should avoid types such as those implied by Int16 and Int32. Array indices are 
not such a case, so you'd probably be better off with

type Pixel_Number is range 1 .. Max_Num;

and

type Frame_Number is range 1 .. Frames_In_Buffer;

-- 
Jeff Carter
"I like it when the support group complains that they have
insufficient data on mean time to repair bugs in Ada software."
Robert I. Eachus
91



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

* Re: Array Syntax
  2008-02-29 15:19 Array Syntax ma740988
                   ` (3 preceding siblings ...)
  2008-02-29 17:50 ` Jeffrey R. Carter
@ 2008-03-04 16:18 ` ma740988
  4 siblings, 0 replies; 6+ messages in thread
From: ma740988 @ 2008-03-04 16:18 UTC (permalink / raw)


You folks have been great thanks alot.  Now I just need to represent
this on paper.



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

end of thread, other threads:[~2008-03-04 16:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-29 15:19 Array Syntax ma740988
2008-02-29 16:01 ` Ludovic Brenta
2008-02-29 17:16 ` Georg Bauhaus
2008-02-29 17:48 ` Dmitry A. Kazakov
2008-02-29 17:50 ` Jeffrey R. Carter
2008-03-04 16:18 ` ma740988

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