comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Arrays in Ada 2012
Date: Tue, 6 Jun 2017 15:56:00 -0500
Date: 2017-06-06T15:56:00-05:00	[thread overview]
Message-ID: <oh74t0$1up$1@franka.jacob-sparre.dk> (raw)
In-Reply-To: e4066890-cc2c-496e-a6ce-f9a9ef9dc3f5@googlegroups.com

"AdaMagica" <christ-usch.grein@t-online.de> wrote in message 
news:e4066890-cc2c-496e-a6ce-f9a9ef9dc3f5@googlegroups.com...
> Am Dienstag, 6. Juni 2017 13:47:06 UTC+2 schrieb Anatoly Chernyshev:
>> Hello everyone,
>>
>> Consider the following nice Ada 2012 construct:
>>
>> --
>> procedure test is
>>    type a1 is array(1..35,1..35,1..35,1..35,1..35,1..35) of 
>> long_long_integer;
>>    ab:a1;
>>    cnt:long_long_integer:=0;
>> begin
>>   for e of ab loop
>>       cnt:=cnt+1;
>>       e:=cnt;
>>    end loop;
>> end test;
>> --
>>
>> Is there a way (e.g. some attribute) to find the  indices of the array 
>> for a current e within the cycle above?
>
> No, there are no attributes giving the current indices. You have to use 
> the nested loops form if you need the indices.

More than likely, the compiler generates code that doesn't even know the 
indices in question, as doing address math is much cheaper than redoing an 
address calculation on each loop. (This is an optimization called "strength 
reduction" that some compilers can do with the conventional loop form, but 
it's better to start out with the reduced code.)

That is, the loop will be generated as something like: [The following is 
pseudo code that's not all legal Ada code, but I hope you get the idea...]

    Current := AB(AB'First(1), ..., AB'First(6))'Address;
    Finish := AB(AB'Last(1), ..., AB'Last(6))'Address;
    while Current <= Finish loop
       cnt:=cnt+1;
       Current.all :=cnt;
       Current := Current + Long_Long_Integer'Size/Storage_Unit;
   end loop;

No indices anywhere, and having a way to provide them would prevent the 
optimization (or would be very complicated in the general case).

                                           Randy.



  reply	other threads:[~2017-06-06 20:56 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-06 11:47 Arrays in Ada 2012 Anatoly Chernyshev
2017-06-06 12:17 ` Dmitry A. Kazakov
2017-06-06 13:17   ` Simon Wright
2017-06-06 13:57     ` Dmitry A. Kazakov
2017-06-06 21:59       ` Simon Wright
2017-06-06 13:19 ` AdaMagica
2017-06-06 20:56   ` Randy Brukardt [this message]
2017-06-07  7:06     ` Dmitry A. Kazakov
2017-06-07 11:47       ` Anatoly Chernyshev
2017-06-07 12:15         ` Dmitry A. Kazakov
2017-06-08  0:55         ` Randy Brukardt
2017-06-08  0:51       ` Randy Brukardt
2017-06-08  7:07         ` Dmitry A. Kazakov
2017-06-09  3:23           ` Randy Brukardt
2017-06-18  2:14 ` Arrays in Ada 2020 Ivan Levashev
2017-06-18  3:00   ` Nasser M. Abbasi
2017-06-18 12:06     ` Robert Eachus
2017-06-18 20:15       ` Simon Wright
2017-06-20 13:33         ` Robert Eachus
2017-06-19  6:36       ` Ivan Levashev
2017-06-19 12:06         ` AdaMagica
2017-06-23  1:17           ` Randy Brukardt
2017-06-18 20:15     ` Pascal Obry
replies disabled

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