comp.lang.ada
 help / color / mirror / Atom feed
From: cs.utexas.edu!ut-emx!hasan@uunet.uu.net  (David A. Hasan)
Subject: Re: loop indices
Date: 20 Apr 92 16:39:56 GMT	[thread overview]
Message-ID: <70628@ut-emx.uucp> (raw)

In a previous article, I asked for suggestions on how to
deal with loop indices and gave the following example.
>
>   DECLARE
>      TYPE Vector IS ARRAY( Positive RANGE <> ) OF Integer;
>      p1 : CONSTANT Positive := 21;
>      p2 : CONSTANT Positive := 51;
>      size : Natural := 5;
>
>      v1 : Vector( p1 .. (p1+size-1) );
>      v2 : Vector( p2 .. (p2+size-1) );
>   BEGIN
>      FOR i IN 1..size LOOP
>         v2(i) := v1(i);      -------------------(!)
>      END LOOP;
>   END;
>
This was an unfortunate example, for as numerous people 
suggested, slicing is the obvious solution.  Short of that,
there is some POSITIVE arithmetic which can be used to
"play" with the indices of one array while the loop index
is used to index into the other array.  Variations on this
approach were also suggested by several people.

Thank you all for your comments.

I guess in my zeal to simplify my problem, I posted an overly
simple example.  My real interest is in situations like the
following.  The important differences in this new example are
    1) the two array variables of interest are of different
       types,
    2) the type of the associated index are also different.

  DECLARE 
    TYPE Index_1 IS (a,b,c,d,e,f,g);
    TYPE Array_1 IS ARRAY(Index_Type1 RANGE <>) OF Integer;

    TYPE Index_2 IS (aa,bb,cc,dd,ee,ff,gg);
    TYPE Array_2 IS ARRAY(Index_Type2 RANGE <>) OF Integer;

    v1 : Array_1( a..e );
    v2 : Array_2( bb..ff );
    i1 : Index_1;
    i2 : Index_2;
  BEGIN
    i1 := v1'FIRST;
    i2 := v2'FIRST;
    FOR i IN 1..5 LOOP
      v1(i1) := v2(i2);
      IF i<5 THEN
        i1 := Index_1'SUCC( i1 );
        i2 := Index_2'SUCC( i2 );
      END IF;
    END LOOP;
  END;

In this case, slicing is not an option.  Neither is it
possible to perform arithmetic *directly* on the indices
of either <v1> or <v2> (or both).

Of course, using 'POS and 'VAL it is possible to come up
with a solution which does not require the IF...END IF;
in my example above.

I prefer the code above for the following reasons:
  1) the explicit use of 'SUCC makes it clear what's going
     on (there is no index arithmetic hiding my intent),
  2) the two array objects, <v1> and <v2>, are dealt with
     "symmetrically" (i.e., neither is given the distinguished
     position of defining the LOOP index type),
  3) the method applies equally to all ARRAY types with any
     sort of index type.

I have no inherent opposition to using 'POS and 'VAL.  I just
"prefer" the appearance of the code in this example.  So...

My first question is this:  does this code carry a performance
hit with it (relative to analogous code which works with
'POS and 'VAL)?

My second question is this:  is there some other approach to
solving this problem in a "readable" way?
-- 
 |   David A. Hasan
 |   hasan@emx.utexas.edu 

             reply	other threads:[~1992-04-20 16:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-04-20 16:39 David A. Hasan [this message]
  -- strict thread matches above, loose matches on Subject: below --
1992-04-22 21:02 loop indices Robert I. Eachus
1992-04-20 21:11 David A. Hasan
1992-04-20 14:00 Robert Firth
1992-04-17 20:00 Mark A Biggar
replies disabled

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