From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,9e03b5de1f7825a5 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.glorb.com!transit.nntp.hccnet.nl!transit1.nntp.hccnet.nl!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!pe1.news.blueyonder.co.uk!blueyonder!fe3.news.blueyonder.co.uk.POSTED!53ab2750!not-for-mail From: Nick Roberts User-Agent: Mozilla Thunderbird 1.0.2 (Windows/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: discriminant type References: <1126644305.724460.155300@g47g2000cwa.googlegroups.com> In-Reply-To: <1126644305.724460.155300@g47g2000cwa.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Thu, 15 Sep 2005 15:08:24 GMT NNTP-Posting-Host: 82.43.35.196 X-Trace: fe3.news.blueyonder.co.uk 1126796904 82.43.35.196 (Thu, 15 Sep 2005 16:08:24 BST) NNTP-Posting-Date: Thu, 15 Sep 2005 16:08:24 BST Xref: g2news1.google.com comp.lang.ada:4714 Date: 2005-09-15T15:08:24+00:00 List-Id: 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