comp.lang.ada
 help / color / mirror / Atom feed
From: Nick Roberts <nick.roberts@acm.org>
Subject: Re: discriminant type
Date: Thu, 15 Sep 2005 15:08:24 GMT
Date: 2005-09-15T15:08:24+00:00	[thread overview]
Message-ID: <I7gWe.69513$2n6.13197@fe3.news.blueyonder.co.uk> (raw)
In-Reply-To: <1126644305.724460.155300@g47g2000cwa.googlegroups.com>

nicolas.b wrote:
> I want to create a Matrix from a one-D Vector :
> type T_Matrix (Col_Size, Row_Size : Integer) is record
>   Vec : T_Vector (Col_Size * Row_Size);
> end record;
> 
> There is a compilation error "Col_Size * Row_Size". Is there a solution
> to my problem ? It is important for me to use a vector one-D to
> construct T_Matrix.

I think you have to put a 'wrapper' interface around the vector type, to 
make it appear as a two-dimensional matrix. For example:

    generic
       type T_Vector is array (Positive range <>) of T;
       Height, Width: in Positive;
    package As_Matrix is
       function Element (Vector: in T_Vector;
                         Row, Column: in Positive) return T;
       procedure Set_Element (Vector: in out T_Vector;
                              Row, Column: in Positive;
                              Value: in T);
    end;


    package body As_Matrix is

       procedure Check_Indices (Row, Column: in Positive) is
       begin
          if Row > Height or Column > Width then
             raise Constraint_Error;
          end if;
       end;

       function Element (Vector: in T_Vector;
                         Row, Column: in Positive) return T is
       begin
          Check_Indices(Row,Column);
          return Vector( (Row-1)*Width + Column );
       end;

       procedure Set_Element (Vector: in out T_Vector;
                              Row, Column: in Positive;
                              Value: in T) is
       begin
          Check_Indices(Row,Column);
          Vector( (Row-1)*Width + Column ) := Value;
       end;

    end As_Matrix;

This implementation uses 'row-major' ordering. You may need to add 
further operations, for example to allow access by slice. You may wish 
to use pragma Inline to improve performance.

The advantage of this approach is that it allows a section of your 
program code to access objects of type T_Vector as a matrix in a way 
that is completely independent of the structure of T_Vector. T_Vector 
could later be changed to a two-dimensional array (or some other 
structure), and that section of code would not have to be changed (the 
implementation of As_Matrix would have to be modified appropriately).

Just a suggestion.

-- 
Nick Roberts



      parent reply	other threads:[~2005-09-15 15:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-13 20:45 discriminant type nicolas.b
2005-09-13 20:49 ` Ludovic Brenta
2005-09-13 22:06   ` David Trudgett
2005-09-14 20:08   ` Dr. Adrian Wrigley
2005-09-15 15:08 ` Nick Roberts [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