comp.lang.ada
 help / color / mirror / Atom feed
* discriminant type
@ 2005-09-13 20:45 nicolas.b
  2005-09-13 20:49 ` Ludovic Brenta
  2005-09-15 15:08 ` Nick Roberts
  0 siblings, 2 replies; 5+ messages in thread
From: nicolas.b @ 2005-09-13 20:45 UTC (permalink / raw)


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.

Thanks,




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

* Re: discriminant type
  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
  1 sibling, 2 replies; 5+ messages in thread
From: Ludovic Brenta @ 2005-09-13 20:49 UTC (permalink / raw)


diff -u:
@@ -1,3 +1,3 @@
  type T_Matrix (Col_Size, Row_Size : Integer) is record
-   Vec : T_Vector (Col_Size * Row_Size);
+   Vec : T_Vector (1 .. Col_Size * Row_Size);
  end record;

-- 
Ludovic Brenta.



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

* Re: discriminant type
  2005-09-13 20:49 ` Ludovic Brenta
@ 2005-09-13 22:06   ` David Trudgett
  2005-09-14 20:08   ` Dr. Adrian Wrigley
  1 sibling, 0 replies; 5+ messages in thread
From: David Trudgett @ 2005-09-13 22:06 UTC (permalink / raw)


Ludovic Brenta <ludovic@ludovic-brenta.org> writes:

> diff -u:
> @@ -1,3 +1,3 @@
>   type T_Matrix (Col_Size, Row_Size : Integer) is record
> -   Vec : T_Vector (Col_Size * Row_Size);
> +   Vec : T_Vector (1 .. Col_Size * Row_Size);
>   end record;

You think that is impressive? In the old days, Ludovic used to program
using COPY CON: MYPROG.ZIP

:-)

Cheers,

David



-- 

David Trudgett
http://www.zeta.org.au/~wpower/

It's sociologically interesting, though scary, that you can be inside
an evil system and be somehow unaware of it.

    -- Actor Anthony Sher on growing up in apartheid-South Africa,
       interviewed by John Walsh, The Independent, 1 May, 2000




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

* Re: discriminant type
  2005-09-13 20:49 ` Ludovic Brenta
  2005-09-13 22:06   ` David Trudgett
@ 2005-09-14 20:08   ` Dr. Adrian Wrigley
  1 sibling, 0 replies; 5+ messages in thread
From: Dr. Adrian Wrigley @ 2005-09-14 20:08 UTC (permalink / raw)


On Tue, 13 Sep 2005 22:49:35 +0200, Ludovic Brenta wrote:

> diff -u:
> @@ -1,3 +1,3 @@
>   type T_Matrix (Col_Size, Row_Size : Integer) is record
> -   Vec : T_Vector (Col_Size * Row_Size);
> +   Vec : T_Vector (1 .. Col_Size * Row_Size);
>   end record;

I may have missed something here, but doesn't the discriminant
have to appear alone in a constraint?

gcc -c blob.adb
6:25: discriminant in constraint must appear alone
6:36: discriminant in constraint must appear alone
-- 
Adrian




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

* Re: discriminant type
  2005-09-13 20:45 discriminant type nicolas.b
  2005-09-13 20:49 ` Ludovic Brenta
@ 2005-09-15 15:08 ` Nick Roberts
  1 sibling, 0 replies; 5+ messages in thread
From: Nick Roberts @ 2005-09-15 15:08 UTC (permalink / raw)


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



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

end of thread, other threads:[~2005-09-15 15:08 UTC | newest]

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