comp.lang.ada
 help / color / mirror / Atom feed
* How to access an array using two different indexing schemes
@ 2017-11-24 11:42 Jerry
  2017-11-24 12:33 ` Jeffrey R. Carter
                   ` (3 more replies)
  0 siblings, 4 replies; 48+ messages in thread
From: Jerry @ 2017-11-24 11:42 UTC (permalink / raw)


I want to access an array such as Real_Vector (built-in for Ada >= 2005) with two different indexing schemes. For example, I would access a Real_Vector indexed (0 .. 4) when thinking of it as a times series and the same data indexed (1 .. 5) when thinking of it as a vector. Another application would be a vector indexed (-128 .. 127) because it fits my problem domain, perhaps a spatial variable, but I need to index it as (0 .. 255) when thinking of doing a Fast Fourier Transform on it.

I could declare two arrays such as

x : Real_Vector(0 .. 4);
y : Real_Vector(1 .. 5); 

Ada then allows the assignments

y := x;
and
x := y;

but this has two problems. First, I have wasted memory and cycles by declaring y and copying x into it. Second, I would then have a kind of version control problem where every time I modified one, I have to copy it to the other.

I have the notion that I should be able to accomplish this using pointers but I can’t seem to crack how do to this. I’ve tried all sorts of things and am resigned to doing the above duplication if I have to. (My arrays can be _much_ larger so the overhead might be significant. They might also be two-dimensional, as in Real_Matrix or Complex_Matrix.)

I'm currently looking at System.Address_To_Access_Conversions but I thought I would query the list in the meantime.

Any ideas?

Thanks,
Jerry

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

* Re: How to access an array using two different indexing schemes
  2017-11-24 11:42 How to access an array using two different indexing schemes Jerry
@ 2017-11-24 12:33 ` Jeffrey R. Carter
  2017-11-24 15:52   ` AdaMagica
                     ` (2 more replies)
  2017-11-24 17:37 ` A. Cervetti
                   ` (2 subsequent siblings)
  3 siblings, 3 replies; 48+ messages in thread
From: Jeffrey R. Carter @ 2017-11-24 12:33 UTC (permalink / raw)


On 11/24/2017 12:42 PM, Jerry wrote:
> I want to access an array such as Real_Vector (built-in for Ada >= 2005) with two different indexing schemes. For example, I would access a Real_Vector indexed (0 .. 4) when thinking of it as a times series and the same data indexed (1 .. 5) when thinking of it as a vector. Another application would be a vector indexed (-128 .. 127) because it fits my problem domain, perhaps a spatial variable, but I need to index it as (0 .. 255) when thinking of doing a Fast Fourier Transform on it.

I can't think of any situation when I'd need to access the same component of an 
array using different indices, but I note that this compiles:

procedure Renaming is
    subtype T1 is String (1 .. 5);
    subtype T2 is String (2 .. T1'Length + 1);

    S1 : T1 := "Hello";
    S2 : T2 renames S1;
begin -- Renaming
    null;
end Renaming;

-- 
Jeff Carter
"I wave my private parts at your aunties."
Monty Python & the Holy Grail
13


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

* Re: How to access an array using two different indexing schemes
  2017-11-24 12:33 ` Jeffrey R. Carter
@ 2017-11-24 15:52   ` AdaMagica
  2017-11-24 22:25     ` Jerry
  2017-11-24 22:23   ` Jerry
  2017-11-25 15:39   ` Robin
  2 siblings, 1 reply; 48+ messages in thread
From: AdaMagica @ 2017-11-24 15:52 UTC (permalink / raw)


Am Freitag, 24. November 2017 13:33:25 UTC+1 schrieb Jeffrey R. Carter:
> On 11/24/2017 12:42 PM, Jerry wrote:
> > I want to access an array such as Real_Vector (built-in for Ada >= 2005) with two different indexing schemes. For example, I would access a Real_Vector indexed (0 .. 4) when thinking of it as a times series and the same data indexed (1 .. 5) when thinking of it as a vector. Another application would be a vector indexed (-128 .. 127) because it fits my problem domain, perhaps a spatial variable, but I need to index it as (0 .. 255) when thinking of doing a Fast Fourier Transform on it.
> 
> I can't think of any situation when I'd need to access the same component of an 
> array using different indices, but I note that this compiles:
> 
> procedure Renaming is
>     subtype T1 is String (1 .. 5);
>     subtype T2 is String (2 .. T1'Length + 1);
> 
>     S1 : T1 := "Hello";
>     S2 : T2 renames S1;
> begin -- Renaming
>     null;
> end Renaming;

  procedure Renaming is
    subtype T1 is String (1 .. 5);
    subtype T2 is String (1 .. 0);   -- !
    S1 : T1 := "Hello";
    S2 : T2 renames S1;
  begin -- Renaming
    Put (S1'First'Image); Put (S1 (S1'First));
    Put (S2'First'Image); Put (S2 (S2'First));
    Put (S1'Last 'Image); Put (S1 (S1'Last ));
    Put (S2'Last 'Image); Put (S2 (S2'Last )); New_Line;
  end Renaming;

This results in
 1H 1H 5o 5o

Rememner that renamings ignore constraints.


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

* Re: How to access an array using two different indexing schemes
  2017-11-24 11:42 How to access an array using two different indexing schemes Jerry
  2017-11-24 12:33 ` Jeffrey R. Carter
@ 2017-11-24 17:37 ` A. Cervetti
  2017-11-24 21:48   ` Jerry
  2017-11-28  1:25   ` Randy Brukardt
  2017-11-24 18:37 ` Per Sandberg
  2017-11-24 22:12 ` Robert A Duff
  3 siblings, 2 replies; 48+ messages in thread
From: A. Cervetti @ 2017-11-24 17:37 UTC (permalink / raw)


Il giorno venerdì 24 novembre 2017 12:42:08 UTC+1, Jerry ha scritto:

> I'm currently looking at System.Address_To_Access_Conversions but I thought I would query the list in the meantime.

use attribute 'Address:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Numerics.Generic_Real_Arrays;

procedure Double_Array is
   package Real_Array is new Ada.Numerics.Generic_Real_Arrays(Float);
   use Real_Array;
   
   x : Real_Vector(0 .. 4) := (1.0, 2.0, 3.0, 4.0, 5.0);
   y : Real_Vector(1 .. 5);
   for Y'Address use X'Address; -- <<<<<<<<<<<<<<<<<<<<
   
begin
   X(0) := 20.0;
   Put(X(0), 0, 0, 0); New_Line;
   Put(Y(1), 0, 0, 0); New_Line;
end Double_Array;

A.

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

* Re: How to access an array using two different indexing schemes
  2017-11-24 11:42 How to access an array using two different indexing schemes Jerry
  2017-11-24 12:33 ` Jeffrey R. Carter
  2017-11-24 17:37 ` A. Cervetti
@ 2017-11-24 18:37 ` Per Sandberg
  2017-11-24 21:28   ` Jerry
  2017-11-24 22:12 ` Robert A Duff
  3 siblings, 1 reply; 48+ messages in thread
From: Per Sandberg @ 2017-11-24 18:37 UTC (permalink / raw)


Hi a reflection.
Why the different indexing ?
/P



Den 2017-11-24 kl. 12:42, skrev Jerry:
> I want to access an array such as Real_Vector (built-in for Ada >= 2005) with two different indexing schemes. For example, I would access a Real_Vector indexed (0 .. 4) when thinking of it as a times series and the same data indexed (1 .. 5) when thinking of it as a vector. Another application would be a vector indexed (-128 .. 127) because it fits my problem domain, perhaps a spatial variable, but I need to index it as (0 .. 255) when thinking of doing a Fast Fourier Transform on it.
> 
> I could declare two arrays such as
> 
> x : Real_Vector(0 .. 4);
> y : Real_Vector(1 .. 5);
> 
> Ada then allows the assignments
> 
> y := x;
> and
> x := y;
> 
> but this has two problems. First, I have wasted memory and cycles by declaring y and copying x into it. Second, I would then have a kind of version control problem where every time I modified one, I have to copy it to the other.
> 
> I have the notion that I should be able to accomplish this using pointers but I can’t seem to crack how do to this. I’ve tried all sorts of things and am resigned to doing the above duplication if I have to. (My arrays can be _much_ larger so the overhead might be significant. They might also be two-dimensional, as in Real_Matrix or Complex_Matrix.)
> 
> I'm currently looking at System.Address_To_Access_Conversions but I thought I would query the list in the meantime.
> 
> Any ideas?
> 
> Thanks,
> Jerry
> 

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

* Re: How to access an array using two different indexing schemes
  2017-11-24 18:37 ` Per Sandberg
@ 2017-11-24 21:28   ` Jerry
  2017-11-24 21:40     ` Dmitry A. Kazakov
  2017-11-28  1:33     ` Randy Brukardt
  0 siblings, 2 replies; 48+ messages in thread
From: Jerry @ 2017-11-24 21:28 UTC (permalink / raw)


On Friday, November 24, 2017 at 11:37:42 AM UTC-7, Per Sandberg wrote:
> Hi a reflection.
> Why the different indexing ?
> /P
> 
Maybe I was unclear in my original post.

There are numerous situations when being able to use different indexing would be a wonderful feature. And I believe doing so is in the spirit of abstraction in Ada, possibly contrary to the comment in Norman Cohen's book to the contrary, at least in this one case.

In signal processing* and other fields where sequences of numbers are often used to represent values of a function over time, it is traditional to start indexing from zero, representing "zero time," the beginning of the existence of the function. If sampling of a continuous function is involved, then with a sample period of T, the time t_n of each sample can be derived from the sample index n as simply t_n =  nT, aligning n = 0 with t = 0. All time series are indexed this way in books and papers. Further, the same data are often subject to mathematical manipulations involving such operations as the discrete Fourier transform, an operation which is naturally expressed with a zero-based indexing and which becomes a symbolic nightmare when other indexing is used.

However, the same people who deal with time series often need to place these data into vectors and matrices for a whole host of reasons. Any textbook in the world will use one-based indexing for both vectors and matrices. If the programmer is locked into either zero-based indexing (e.g. C, Python, etc.) or one-based indexing (e.g. (old?) Fortran, Matlab, Mathematica), there is a constant struggle to convert indices from one scheme to the other for the various representations and operations, or else to adapt algorithms themselves to accommodate the awkward scheme, and this not only makes ugly and hard-to-read code, it is very prone to off-by-one errors.

Alternately, in image processing, it might be convenient to assume the center of the image is indexed as (0, 0), or it might derive naturally from the underlying problem such as the center of a radar ground patch, thus requiring both negative and positive indices, perhaps (-1024 .. 1023, -1024 .. 1023). This convenience, again, might refer directly to the user's application space. Now, if a DFT is required, it is almost certainly required that the matrix be indexed as (0, 2047, 0, 2047); if the indexing cannot be changed, either the data must be copied to a suitable new matrix or the offsets computed implicitly before being passed to the DFT or the DFT rewritten with the offsets built into its internal indexing; none of these three methods clears the programmer's head of the indexing mess, as it never actually goes away.

Generally, I find languages that force a fixed indexing (always either 0 or 1) to be unsuitable for the kind of work that I describe above. I find the flame wars over zero- or one-based indexing to be humorously irrelevant and I would be delighted to see an Ada solution for this problem. As an electrical engineer working in signal processing, Pascal and then Ada have been my workhorses for many years.

I hope that helps.

Jerry

* Within the Institute of Electrical and Electronics Engineers, IEEE, the largest professional society in the world, the Signal Processing Society is the second-largest after the Computer Society. The signal processing community is always at pains to have people understand what they do, when in fact signal processing theory and practice permeates virtually every electronic device.


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

* Re: How to access an array using two different indexing schemes
  2017-11-24 21:28   ` Jerry
@ 2017-11-24 21:40     ` Dmitry A. Kazakov
  2017-11-28  1:33     ` Randy Brukardt
  1 sibling, 0 replies; 48+ messages in thread
From: Dmitry A. Kazakov @ 2017-11-24 21:40 UTC (permalink / raw)


Yet another example is encoding. For example UTF-8 string is an array of 
octets and an array of code points (or characters).

The problem is that Ada does not have array interfaces to support 
user-defined indexing, nor it allows superclasses so that such an 
interface could be added to an existing type without meaningless record 
extension.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: How to access an array using two different indexing schemes
  2017-11-24 17:37 ` A. Cervetti
@ 2017-11-24 21:48   ` Jerry
  2017-11-24 22:15     ` Robert A Duff
  2017-11-28  1:25   ` Randy Brukardt
  1 sibling, 1 reply; 48+ messages in thread
From: Jerry @ 2017-11-24 21:48 UTC (permalink / raw)


On Friday, November 24, 2017 at 10:37:30 AM UTC-7, A. Cervetti wrote:
> use attribute 'Address:
> 
> with Ada.Text_IO; use Ada.Text_IO;
> with Ada.Float_Text_IO; use Ada.Float_Text_IO;
> with Ada.Numerics.Generic_Real_Arrays;
> 
> procedure Double_Array is
>    package Real_Array is new Ada.Numerics.Generic_Real_Arrays(Float);
>    use Real_Array;
>    
>    x : Real_Vector(0 .. 4) := (1.0, 2.0, 3.0, 4.0, 5.0);
>    y : Real_Vector(1 .. 5);
>    for Y'Address use X'Address; -- <<<<<<<<<<<<<<<<<<<<
>    
> begin
>    X(0) := 20.0;
>    Put(X(0), 0, 0, 0); New_Line;
>    Put(Y(1), 0, 0, 0); New_Line;
> end Double_Array;

Nice. Thank you. That solves the "version control" or "bookkeeping" problem, but it still requires allocating double memory, for y.

I had hoped to use an access variable for the role of y so that only a tiny amount of additional memory would be required, and somehow do a type conversion of the y-pointer as it points at x, but I haven't been able yet to figure out how to do the pointer type conversion. Is this even possible?

Jerry


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

* Re: How to access an array using two different indexing schemes
  2017-11-24 11:42 How to access an array using two different indexing schemes Jerry
                   ` (2 preceding siblings ...)
  2017-11-24 18:37 ` Per Sandberg
@ 2017-11-24 22:12 ` Robert A Duff
  2017-11-28  1:39   ` Randy Brukardt
  3 siblings, 1 reply; 48+ messages in thread
From: Robert A Duff @ 2017-11-24 22:12 UTC (permalink / raw)


Jerry <list_email@icloud.com> writes:

> I could declare two arrays such as
>
> x : Real_Vector(0 .. 4);
> y : Real_Vector(1 .. 5); 
>
> Ada then allows the assignments
>
> y := x;
> and
> x := y;

This is called "sliding".  So long as X'Length = Y'Length,
the bounds can change in this way on assignment statements.

I don't think the RM defines the term "sliding", but
you can find it in the AARM, as part of array subtype
conversions.

> but this has two problems. First, I have wasted memory and cycles by
> declaring y and copying x into it. ...

Sliding is also allowed in various other contexts,
including parameter passing, which is typically
done by reference for the types you're interested in.

Here's an example.  P expects a String starting at 5.
Q is a wrapper, that takes a String with any lower
bound, and slides it as appropriate, and then passes
it to P.  The program prints " 5 5 5 5".

Strings with various lower bounds are passed to Q, but they all have
'First = 5 inside P.  And there's no copying of the strings (on any
sensible compiler).

This is admittedly kind of convoluted.

with Text_IO; use Text_IO;
package Sliding is

   subtype String_1 is String with Predicate => String_1'First = 1;
   subtype String_5 is String with Predicate => String_5'First = 5;

   procedure P(X: String_5);
   procedure Q(X: String);

end Sliding;

package body Sliding is

   procedure P(X: String_5) is
   begin
      Put(X'First'Image);
   end P;

   procedure Q(X: String) is
      subtype Slide_To_5 is String_5(5 .. 5 + X'Length - 1);
      procedure Call_P (X: Slide_To_5) is
      begin
         P(X); -- Predicate check (that X'First = 5) occurs here.
      end Call_P;

   begin
      Call_P(X); -- Sliding to 5..something occurs here.
   end Q;

end Sliding;

procedure Sliding.Main is
   S1: constant String := "";
   S2: constant String(-100..-1_000_000) := "";
   S3: constant String := "Hello";
   S4: constant String(101..105) := "Hello";
begin
   Q(S1);
   Q(S2);
   Q(S3);
   Q(S4);
end Sliding.Main;

- Bob

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

* Re: How to access an array using two different indexing schemes
  2017-11-24 21:48   ` Jerry
@ 2017-11-24 22:15     ` Robert A Duff
  2017-11-24 23:38       ` Jerry
  0 siblings, 1 reply; 48+ messages in thread
From: Robert A Duff @ 2017-11-24 22:15 UTC (permalink / raw)


Jerry <list_email@icloud.com> writes:

> On Friday, November 24, 2017 at 10:37:30 AM UTC-7, A. Cervetti wrote:
>>    for Y'Address use X'Address; -- <<<<<<<<<<<<<<<<<<<<

> Nice. Thank you. That solves the "version control" or "bookkeeping"
> problem, but it still requires allocating double memory, for y.

No, X and Y are allocated at the same address.
That's the point of this low-level hack.

- Bob

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

* Re: How to access an array using two different indexing schemes
  2017-11-24 12:33 ` Jeffrey R. Carter
  2017-11-24 15:52   ` AdaMagica
@ 2017-11-24 22:23   ` Jerry
  2017-11-25 15:39   ` Robin
  2 siblings, 0 replies; 48+ messages in thread
From: Jerry @ 2017-11-24 22:23 UTC (permalink / raw)


On Friday, November 24, 2017 at 5:33:25 AM UTC-7, Jeffrey R. Carter wrote:
> 
> I can't think of any situation when I'd need to access the same component of an 
> array using different indices, but I note that this compiles:
> 
> procedure Renaming is
>     subtype T1 is String (1 .. 5);
>     subtype T2 is String (2 .. T1'Length + 1);
> 
>     S1 : T1 := "Hello";
>     S2 : T2 renames S1;
> begin -- Renaming
>     null;
> end Renaming;

This doesn't seem to solve the "duplicate indexing" need. I think my remark here restates AdaMagica's remark nearby.

with Ada.Text_IO; use Ada.Text_IO;

procedure Renaming is
    subtype T1 is String (1 .. 5);
    subtype T2 is String (2 .. T1'Length + 1);

    S1 : T1 := "Hello";
    S2 : T2 renames S1;
begin -- Renaming
    Put_Line("S1");
    for i in S1'range loop
         Put(i'img & " "); Put(S1(i)); New_Line;
    end loop;
    New_Line;
 
    Put_Line("S2");
    for i in S2'range loop
         Put(i'img & " "); Put(S2(i)); New_Line;
    end loop;
    New_Line;
end Renaming;

This outputs

S1
 1 H
 2 e
 3 l
 4 l
 5 o

S2
 1 H
 2 e
 3 l
 4 l
 5 o

But a question: Is memory allocated for S2?

Jerry


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

* Re: How to access an array using two different indexing schemes
  2017-11-24 15:52   ` AdaMagica
@ 2017-11-24 22:25     ` Jerry
  2017-11-25 13:57       ` AdaMagica
  0 siblings, 1 reply; 48+ messages in thread
From: Jerry @ 2017-11-24 22:25 UTC (permalink / raw)


On Friday, November 24, 2017 at 8:52:50 AM UTC-7, AdaMagica wrote:
>   procedure Renaming is
>     subtype T1 is String (1 .. 5);
>     subtype T2 is String (1 .. 0);   -- !
>     S1 : T1 := "Hello";
>     S2 : T2 renames S1;
>   begin -- Renaming
>     Put (S1'First'Image); Put (S1 (S1'First));
>     Put (S2'First'Image); Put (S2 (S2'First));
>     Put (S1'Last 'Image); Put (S1 (S1'Last ));
>     Put (S2'Last 'Image); Put (S2 (S2'Last )); New_Line;
>   end Renaming;
> 
> This results in
>  1H 1H 5o 5o
> 
> Rememner that renamings ignore constraints.

But at least it solves the double memory allocation with your

subtype T2 is String (1 .. 0);   -- ! 

Jerry


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

* Re: How to access an array using two different indexing schemes
  2017-11-24 22:15     ` Robert A Duff
@ 2017-11-24 23:38       ` Jerry
  2017-11-26  1:07         ` Jerry
  0 siblings, 1 reply; 48+ messages in thread
From: Jerry @ 2017-11-24 23:38 UTC (permalink / raw)


On Friday, November 24, 2017 at 3:15:35 PM UTC-7, Robert A Duff wrote:
> Jerry writes:
> 
> > On Friday, November 24, 2017 at 10:37:30 AM UTC-7, A. Cervetti wrote:
> >>    for Y'Address use X'Address; -- <<<<<<<<<<<<<<<<<<<<
> 
> > Nice. Thank you. That solves the "version control" or "bookkeeping"
> > problem, but it still requires allocating double memory, for y.
> 
> No, X and Y are allocated at the same address.
> That's the point of this low-level hack.
> 
> - Bob

Oh. That seems to solve all my problems. I did it this way:

with Ada.Numerics.Long_Real_Arrays; use Ada.Numerics.Long_Real_Arrays;
procedure Double_Array_1 is
    x : Real_Vector(-4 .. 3);
    y : Real_Vector(0 .. 7);
    for y'Address use x'Address; -- <<<< 'Address trick
begin
    null;
end Double;

Now I wonder if I could get greedy.

I frequently allocate memory for arrays from the heap, as discussed previously on this list, like this:

with Ada.Numerics.Long_Real_Arrays; use Ada.Numerics.Long_Real_Arrays;
procedure Double_Array_2 is
    type Real_Vector_Access is access Real_Vector;
    x_Ptr : Real_Vector_Access := new Real_Vector(-4 .. 3);
        x : Real_Vector renames x_Ptr.all;
    y_Ptr : Real_Vector_Access := new Real_Vector(0 .. 7);
        y : Real_Vector renames y_Ptr.all;
begin
    null;
end Double_Array_2;

How would I use the 'Address trick in this situation?

Jerry

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

* Re: How to access an array using two different indexing schemes
  2017-11-24 22:25     ` Jerry
@ 2017-11-25 13:57       ` AdaMagica
  0 siblings, 0 replies; 48+ messages in thread
From: AdaMagica @ 2017-11-25 13:57 UTC (permalink / raw)


Am Freitag, 24. November 2017 23:25:47 UTC+1 schrieb Jerry:
> On Friday, November 24, 2017 at 8:52:50 AM UTC-7, AdaMagica wrote:
> >   procedure Renaming is
> >     subtype T1 is String (1 .. 5);
> >     subtype T2 is String (1 .. 0);   -- !
> >     S1 : T1 := "Hello";
> >     S2 : T2 renames S1;
> > Rememner that renamings ignore constraints.
> 
> But at least it solves the double memory allocation with your
> 
> subtype T2 is String (1 .. 0);   -- ! 

No, renamings *never ever allocate* any memory. As the keyword tells, it's giving a new name to something. The *subtype* name involved (T2) here is irrelevant as are its possible constraints; indeed, it's the *type of T2* that's relevant.

In fact, renaming clauses are one example where Ada plainly lies!


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

* Re: How to access an array using two different indexing schemes
  2017-11-24 12:33 ` Jeffrey R. Carter
  2017-11-24 15:52   ` AdaMagica
  2017-11-24 22:23   ` Jerry
@ 2017-11-25 15:39   ` Robin
  2017-11-25 20:58     ` Jerry
  2 siblings, 1 reply; 48+ messages in thread
From: Robin @ 2017-11-25 15:39 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> wrote in message 
news:ov93ij$njh$1@dont-email.me...
> On 11/24/2017 12:42 PM, Jerry wrote:
>> I want to access an array such as Real_Vector (built-in for Ada >= 2005) with two different 
>> indexing schemes. For example, I would access a Real_Vector indexed (0 .. 4) when thinking of it 
>> as a times series and the same data indexed (1 .. 5) when thinking of it as a vector. Another 
>> application would be a vector indexed (-128 .. 127) because it fits my problem domain, perhaps a 
>> spatial variable, but I need to index it as (0 .. 255) when thinking of doing a Fast Fourier 
>> Transform on it.
>
> I can't think of any situation when I'd need to access the same component of an array using 
> different indices,

It can arise in boundary value problems, where operations are carried out
on all elements of a matrix except the first and last rows, and first and
last columns.

In PL/I, such situations are handled using the iSUB facility. 



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

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

* Re: How to access an array using two different indexing schemes
  2017-11-25 15:39   ` Robin
@ 2017-11-25 20:58     ` Jerry
  2017-11-26 10:22       ` Robin
  2017-11-28 21:57       ` G. B.
  0 siblings, 2 replies; 48+ messages in thread
From: Jerry @ 2017-11-25 20:58 UTC (permalink / raw)


On Saturday, November 25, 2017 at 8:37:25 AM UTC-7, Robin wrote:
> "Jeffrey R. Carter"  wrote in message 
> ...
> > On 11/24/2017 12:42 PM, Jerry wrote:
> >> I want to access an array such as Real_Vector (built-in for Ada >= 2005) with two different 
> >> indexing schemes. For example, I would access a Real_Vector indexed (0 .. 4) when thinking of it 
> >> as a times series and the same data indexed (1 .. 5) when thinking of it as a vector. Another 
> >> application would be a vector indexed (-128 .. 127) because it fits my problem domain, perhaps a 
> >> spatial variable, but I need to index it as (0 .. 255) when thinking of doing a Fast Fourier 
> >> Transform on it.
> >
> > I can't think of any situation when I'd need to access the same component of an array using 
> > different indices,
> 
> It can arise in boundary value problems, where operations are carried out
> on all elements of a matrix except the first and last rows, and first and
> last columns.
> 
> In PL/I, such situations are handled using the iSUB facility. 
> 
If I understand correctly, this is actually different from what I described, which is accessing the same data with different indexing schemes.

You describe a situation where the data are actually different (deleting rows and columns). Your problem could be handled by array slicing for nD arrays where n > 1.

As has been discussed here before, Ada strangely and annoyingly lacks slicing for multidimensional arrays, leading the programmer to devise ugly error-prone ad hoc methods, and that is very un-Ada-like.

Your problem would also be solved by a sub-matrix capability which is apparently what you describe for PL/1. Of course Ada also lacks this capability along with matrix compositions, both of which Matlab excels at.

These missing array features and the fairly trivial but readability-reducing method of using () for array indexing instead of the highly suggestive [] are probably the main things that keep Ada from being a first-class language for numerical work.

It seems that if some of these concerns were addressed it would help Ada expand its fields of use, a desire which has been expressed in the past.

Jerry


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

* Re: How to access an array using two different indexing schemes
  2017-11-24 23:38       ` Jerry
@ 2017-11-26  1:07         ` Jerry
  2017-11-26  8:58           ` Dmitry A. Kazakov
  0 siblings, 1 reply; 48+ messages in thread
From: Jerry @ 2017-11-26  1:07 UTC (permalink / raw)


On Friday, November 24, 2017 at 4:38:10 PM UTC-7, Jerry wrote:
> I frequently allocate memory for arrays from the heap, as discussed previously on this list, like this:
> 
> with Ada.Numerics.Long_Real_Arrays; use Ada.Numerics.Long_Real_Arrays;
> procedure Double_Array_2 is
>     type Real_Vector_Access is access Real_Vector;
>     x_Ptr : Real_Vector_Access := new Real_Vector(-4 .. 3);
>         x : Real_Vector renames x_Ptr.all;
>     y_Ptr : Real_Vector_Access := new Real_Vector(0 .. 7);
>         y : Real_Vector renames y_Ptr.all;
> begin
>     null;
> end Double_Array_2;
> 
> How would I use the 'Address trick in this situation?
> 
Any help here? I've spend several hours on this one. I'm not out of ideas but I'm pretty challenged by this. Currently trying things with System.Address_To_Access_Conversions.

Jerry

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

* Re: How to access an array using two different indexing schemes
  2017-11-26  1:07         ` Jerry
@ 2017-11-26  8:58           ` Dmitry A. Kazakov
  2017-11-26 10:31             ` Jerry
  2017-11-28  1:31             ` Randy Brukardt
  0 siblings, 2 replies; 48+ messages in thread
From: Dmitry A. Kazakov @ 2017-11-26  8:58 UTC (permalink / raw)


On 2017-11-26 02:07, Jerry wrote:
> On Friday, November 24, 2017 at 4:38:10 PM UTC-7, Jerry wrote:
>> I frequently allocate memory for arrays from the heap, as discussed previously on this list, like this:
>>
>> with Ada.Numerics.Long_Real_Arrays; use Ada.Numerics.Long_Real_Arrays;
>> procedure Double_Array_2 is
>>      type Real_Vector_Access is access Real_Vector;
>>      x_Ptr : Real_Vector_Access := new Real_Vector(-4 .. 3);
>>          x : Real_Vector renames x_Ptr.all;
>>      y_Ptr : Real_Vector_Access := new Real_Vector(0 .. 7);
>>          y : Real_Vector renames y_Ptr.all;
>> begin
>>      null;
>> end Double_Array_2;
>>
>> How would I use the 'Address trick in this situation?
>>
> Any help here? I've spend several hours on this one. I'm not out of
> ideas but I'm pretty challenged by this. Currently trying things with
> System.Address_To_Access_Conversions.

Where is a problem?

with Ada.Numerics.Long_Real_Arrays; use Ada.Numerics.Long_Real_Arrays;
procedure Double_Array_2 is
    type Real_Vector_Access is access Real_Vector;
    x_Ptr : Real_Vector_Access := new Real_Vector(-4 .. 3);
    x : Real_Vector renames x_Ptr.all;
    y : Real_Vector(0..7);
    for y'Address use x'Address;
begin
    null;
end Double_Array_2;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: How to access an array using two different indexing schemes
  2017-11-25 20:58     ` Jerry
@ 2017-11-26 10:22       ` Robin
  2017-11-28 21:57       ` G. B.
  1 sibling, 0 replies; 48+ messages in thread
From: Robin @ 2017-11-26 10:22 UTC (permalink / raw)



"Jerry" <list_email@icloud.com> wrote in message 
news:370d6fa0-568a-4a97-9189-7839c91cbe7c@googlegroups.com...
> On Saturday, November 25, 2017 at 8:37:25 AM UTC-7, Robin wrote:
>> "Jeffrey R. Carter"  wrote in message
>> ...
>> > On 11/24/2017 12:42 PM, Jerry wrote:
>> >> I want to access an array such as Real_Vector (built-in for Ada >= 2005) with two different
>> >> indexing schemes. For example, I would access a Real_Vector indexed (0 .. 4) when thinking of 
>> >> it
>> >> as a times series and the same data indexed (1 .. 5) when thinking of it as a vector. Another
>> >> application would be a vector indexed (-128 .. 127) because it fits my problem domain, perhaps 
>> >> a
>> >> spatial variable, but I need to index it as (0 .. 255) when thinking of doing a Fast Fourier
>> >> Transform on it.
>> >
>> > I can't think of any situation when I'd need to access the same component of an array using
>> > different indices,
>>
>> It can arise in boundary value problems, where operations are carried out
>> on all elements of a matrix except the first and last rows, and first and
>> last columns.
>>
>> In PL/I, such situations are handled using the iSUB facility.
>>
> If I understand correctly, this is actually different from what I described, which is accessing 
> the same data with different indexing schemes.

I cited an instance where elements of an array can be accessed with different
indexes.
It is more complex than the one that you illustrated, but it is still an instance of the same thing.

Your example is trivially handled with the iSUB facility.

> You describe a situation where the data are actually different (deleting rows and columns).

It is not "deleting" rows and columns; it omits them, using different indices for the
same elements.

Even so, bounds checking is available for all uses.

> Your problem could be handled by array slicing for nD arrays where n > 1.

No need for slicing.

> As has been discussed here before, Ada strangely and annoyingly lacks slicing for multidimensional 
> arrays, leading the programmer to devise ugly error-prone ad hoc methods, and that is very 
> un-Ada-like.
>
> Your problem would also be solved by a sub-matrix capability which is apparently what you describe 
> for PL/1. Of course Ada also lacks this capability along with matrix compositions, both of which 
> Matlab excels at.
>
> These missing array features and the fairly trivial but readability-reducing method of using () 
> for array indexing instead of the highly suggestive [] are probably the main things that keep Ada 
> from being a first-class language for numerical work.
>
> It seems that if some of these concerns were addressed it would help Ada expand its fields of use, 
> a desire which has been expressed in the past.
>
> Jerry 



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

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

* Re: How to access an array using two different indexing schemes
  2017-11-26  8:58           ` Dmitry A. Kazakov
@ 2017-11-26 10:31             ` Jerry
  2017-11-28  1:31             ` Randy Brukardt
  1 sibling, 0 replies; 48+ messages in thread
From: Jerry @ 2017-11-26 10:31 UTC (permalink / raw)


On Sunday, November 26, 2017 at 1:58:32 AM UTC-7, Dmitry A. Kazakov wrote:
> On 2017-11-26 02:07, Jerry wrote:
> > On Friday, November 24, 2017 at 4:38:10 PM UTC-7, Jerry wrote:
> >> I frequently allocate memory for arrays from the heap, as discussed previously on this list, like this:
> >>
> >> with Ada.Numerics.Long_Real_Arrays; use Ada.Numerics.Long_Real_Arrays;
> >> procedure Double_Array_2 is
> >>      type Real_Vector_Access is access Real_Vector;
> >>      x_Ptr : Real_Vector_Access := new Real_Vector(-4 .. 3);
> >>          x : Real_Vector renames x_Ptr.all;
> >>      y_Ptr : Real_Vector_Access := new Real_Vector(0 .. 7);
> >>          y : Real_Vector renames y_Ptr.all;
> >> begin
> >>      null;
> >> end Double_Array_2;
> >>
> >> How would I use the 'Address trick in this situation?
> >>
> > Any help here? I've spend several hours on this one. I'm not out of
> > ideas but I'm pretty challenged by this. Currently trying things with
> > System.Address_To_Access_Conversions.
> 
> Where is a problem?
> 
> with Ada.Numerics.Long_Real_Arrays; use Ada.Numerics.Long_Real_Arrays;
> procedure Double_Array_2 is
>     type Real_Vector_Access is access Real_Vector;
>     x_Ptr : Real_Vector_Access := new Real_Vector(-4 .. 3);
>     x : Real_Vector renames x_Ptr.all;
>     y : Real_Vector(0..7);
>     for y'Address use x'Address;
> begin
>     null;
> end Double_Array_2;
> 
> -- 
> Regards,
> Dmitry A. Kazakov
> http://www.dmitry-kazakov.de

Thanks, Dmitry. I kept trying to use a pointer for y. I anticipated that I would feel like an idiot when someone showed me how to do this and I was right.

Jerry


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

* Re: How to access an array using two different indexing schemes
  2017-11-24 17:37 ` A. Cervetti
  2017-11-24 21:48   ` Jerry
@ 2017-11-28  1:25   ` Randy Brukardt
  2017-11-29  4:57     ` Jerry
  1 sibling, 1 reply; 48+ messages in thread
From: Randy Brukardt @ 2017-11-28  1:25 UTC (permalink / raw)


"A. Cervetti" <andrea.cervetti@gmail.com> wrote in message 
news:e3a1f97e-7cbd-4327-aef6-c18ca9e74bc2@googlegroups.com...
>use attribute 'Address:
>
>with Ada.Text_IO; use Ada.Text_IO;
>with Ada.Float_Text_IO; use Ada.Float_Text_IO;
>with Ada.Numerics.Generic_Real_Arrays;
>
>procedure Double_Array is
>   package Real_Array is new Ada.Numerics.Generic_Real_Arrays(Float);
>   use Real_Array;
>
>   x : Real_Vector(0 .. 4) := (1.0, 2.0, 3.0, 4.0, 5.0);
>   y : Real_Vector(1 .. 5);
>   for Y'Address use X'Address; -- <<<<<<<<<<<<<<<<<<<<

An evil and not guaranteed to work on all implementation technique.

The OP just needs to use an appropriate type conversion to change the bounds 
of their array (odd no one has mentioned that):

If one has:

   subtype RV1_5 is Real_Vector(1..5);

Then the type conversion:
    RV1_5(X) has the new bounds, and (by itself) doesn't copy any memory.

Thus, using a subprogram to temporarily bind the object shouldn't result in 
any copying of the data (no guarantees, of course, but it will always work - 
rememberm avoid premature optimization!):

    procedure Do_Y (Y : RV1_5) is
    begin
         -- Operations on Y.
    end Do_Y;

    ...
    Do_Y (RV1_5(X));

This is the best way to change the bounds of an array (or part of an array) 
in Ada.

                                                            Randy.



begin
   X(0) := 20.0;
   Put(X(0), 0, 0, 0); New_Line;
   Put(Y(1), 0, 0, 0); New_Line;
end Double_Array;

A. 


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

* Re: How to access an array using two different indexing schemes
  2017-11-26  8:58           ` Dmitry A. Kazakov
  2017-11-26 10:31             ` Jerry
@ 2017-11-28  1:31             ` Randy Brukardt
  1 sibling, 0 replies; 48+ messages in thread
From: Randy Brukardt @ 2017-11-28  1:31 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:ovdvnm$3dt$1@gioia.aioe.org...
> On 2017-11-26 02:07, Jerry wrote:
>> On Friday, November 24, 2017 at 4:38:10 PM UTC-7, Jerry wrote:
>>> I frequently allocate memory for arrays from the heap, as discussed 
>>> previously on this list, like this:
>>>
>>> with Ada.Numerics.Long_Real_Arrays; use Ada.Numerics.Long_Real_Arrays;
>>> procedure Double_Array_2 is
>>>      type Real_Vector_Access is access Real_Vector;
>>>      x_Ptr : Real_Vector_Access := new Real_Vector(-4 .. 3);
>>>          x : Real_Vector renames x_Ptr.all;
>>>      y_Ptr : Real_Vector_Access := new Real_Vector(0 .. 7);
>>>          y : Real_Vector renames y_Ptr.all;
>>> begin
>>>      null;
>>> end Double_Array_2;
>>>
>>> How would I use the 'Address trick in this situation?

Don't. Just type convert X as needed:

with Ada.Numerics.Long_Real_Arrays; use Ada.Numerics.Long_Real_Arrays;
procedure Double_Array_2 is
      type Real_Vector_Access is access Real_Vector;
      x_Ptr : Real_Vector_Access := new Real_Vector(-4 .. 3);
             x : Real_Vector renames x_Ptr.all;
--   y_Ptr : Real_Vector_Access := new Real_Vector(0 .. 7);
-->> This is allocating a new object for Real_Vector, which I don't think 
you want to do.
      subtype RV0_7 is Real_Vector(0..7);
      procedure Do_Y (Y : in out RV0_7) is
      begin
           -- Operations on Y.
      end Do_Y;

     begin
          Do_Y (RV0_7(X));
     end Double_Array_2;

If you used the Address clause hack here, you'd be subject to termination. 
GIGO.

                                  Randy.



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

* Re: How to access an array using two different indexing schemes
  2017-11-24 21:28   ` Jerry
  2017-11-24 21:40     ` Dmitry A. Kazakov
@ 2017-11-28  1:33     ` Randy Brukardt
  1 sibling, 0 replies; 48+ messages in thread
From: Randy Brukardt @ 2017-11-28  1:33 UTC (permalink / raw)


"Jerry" <list_email@icloud.com> wrote in message 
news:a45bcd73-6a3b-4450-b06d-d5982ca1f175@googlegroups.com...
...
>There are numerous situations when being able to use different indexing 
>would
>be a wonderful feature.

And it is relatively easy to do in Ada, as I showed in my posts. (Type 
conversions can change the bounds of an array or slice.) So why all of the 
complaining? (And why don't all of the other Ada experts here know this???)

                           Randy.


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

* Re: How to access an array using two different indexing schemes
  2017-11-24 22:12 ` Robert A Duff
@ 2017-11-28  1:39   ` Randy Brukardt
  0 siblings, 0 replies; 48+ messages in thread
From: Randy Brukardt @ 2017-11-28  1:39 UTC (permalink / raw)


"Robert A Duff" <bobduff@TheWorld.com> wrote in message 
news:wccy3mv1g3v.fsf@TheWorld.com...
...
> This is called "sliding".  So long as X'Length = Y'Length,
> the bounds can change in this way on assignment statements.

Ah, thanks Bob -- someone with the REAL answer to this question (but which 
has been completely ignored so far as I can see). All of the other answers 
(other than mine, of course ;-) are garbage - there isn't the least 
difficulty in "changing the bounds" of an array in Ada other than that you 
have to declare a name for each such set of bounds. Such a hardship... :-)

                                         Randy.


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

* Re: How to access an array using two different indexing schemes
  2017-11-25 20:58     ` Jerry
  2017-11-26 10:22       ` Robin
@ 2017-11-28 21:57       ` G. B.
  1 sibling, 0 replies; 48+ messages in thread
From: G. B. @ 2017-11-28 21:57 UTC (permalink / raw)


Jerry <list_email@icloud.com> wrote:

> Your problem would also be solved by a sub-matrix capability which is
> apparently what you describe for PL/1. Of course Ada also lacks this
> capability along with matrix compositions, both of which Matlab excels at.

Except that these are different, higher level things.
A matrix or vector in R or in Matlab or in APL or in ...
is not at all the same thing as in Ada, physically.
The former are opaque objects, e.g. a stretch of storage 
associated with and “typed” by a dimension object,
giving meaning to runtime operations such as
“column 4” or “convert to 2x3”
Whereas an array in Ada is more like a bounded 
layout of typed data in storage. The Ada data structure 
can be used for implementing the higher level 
dimensioned objects mentioned.

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

* Re: How to access an array using two different indexing schemes
  2017-11-28  1:25   ` Randy Brukardt
@ 2017-11-29  4:57     ` Jerry
  2017-11-29 15:01       ` AdaMagica
                         ` (2 more replies)
  0 siblings, 3 replies; 48+ messages in thread
From: Jerry @ 2017-11-29  4:57 UTC (permalink / raw)


On Monday, November 27, 2017 at 6:25:26 PM UTC-7, Randy Brukardt wrote:
> 
> An evil and not guaranteed to work on all implementation technique.
> 
> The OP just needs to use an appropriate type conversion to change the bounds 
> of their array (odd no one has mentioned that):
> 
> If one has:
> 
>    subtype RV1_5 is Real_Vector(1..5);
> 
> Then the type conversion:
>     RV1_5(X) has the new bounds, and (by itself) doesn't copy any memory.
> 
> Thus, using a subprogram to temporarily bind the object shouldn't result in 
> any copying of the data (no guarantees, of course, but it will always work - 
> rememberm avoid premature optimization!):
> 
>     procedure Do_Y (Y : RV1_5) is
>     begin
>          -- Operations on Y.
>     end Do_Y;
> 
>     ...
>     Do_Y (RV1_5(X));
> 
> This is the best way to change the bounds of an array (or part of an array) 
> in Ada.
> 
>                                                             Randy.

Your method shows how to pass the re-dimensioned array to a subprogram for processing with a typecast. But I also need to process the array in the same program that the subtype is declared. How would I do the typecast without allocating more memory?

Something like this:

procedure Doubly_Indexed_Array is
   declare x with dimensions (0 .. 4)
   declare y with dimensions (1 .. 5) using the same memory as x but
	                              accessed with different indices
begin
   x(0) := 10.0;
   y(3) := 20.0;
   print x(0) resulting in 10.0
   print y(1) resulting in 10.0
   print x(2) resulting in 20.0
   print y(3) resulting in 20.0
end Doubly_Indexed_Array;

Jerry

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

* Re: How to access an array using two different indexing schemes
  2017-11-29  4:57     ` Jerry
@ 2017-11-29 15:01       ` AdaMagica
  2017-11-29 15:21         ` AdaMagica
  2017-11-30  5:30         ` Jerry
  2017-11-29 15:03       ` AdaMagica
  2017-11-29 16:03       ` Shark8
  2 siblings, 2 replies; 48+ messages in thread
From: AdaMagica @ 2017-11-29 15:01 UTC (permalink / raw)


A bit clumsy, but:

X(0) := ...;
RV1_5(X) (1) := ...;  -- didn't try, might work
print (x(0));
print (RV1_5(X)(1));


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

* Re: How to access an array using two different indexing schemes
  2017-11-29  4:57     ` Jerry
  2017-11-29 15:01       ` AdaMagica
@ 2017-11-29 15:03       ` AdaMagica
  2017-11-29 20:53         ` Randy Brukardt
  2017-11-29 16:03       ` Shark8
  2 siblings, 1 reply; 48+ messages in thread
From: AdaMagica @ 2017-11-29 15:03 UTC (permalink / raw)


I'd guess you would not need it in both ways at the same place, so split the uses in different subprograms.

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

* Re: How to access an array using two different indexing schemes
  2017-11-29 15:01       ` AdaMagica
@ 2017-11-29 15:21         ` AdaMagica
  2017-11-30  5:32           ` Jerry
  2017-11-30  5:30         ` Jerry
  1 sibling, 1 reply; 48+ messages in thread
From: AdaMagica @ 2017-11-29 15:21 UTC (permalink / raw)


Am Mittwoch, 29. November 2017 16:01:35 UTC+1 schrieb AdaMagica:
> A bit clumsy, but:
> 
> X(0) := ...;
> RV1_5(X) (1) := ...;  -- didn't try, might work
> print (x(0));
> print (RV1_5(X)(1));

Y: RV1_5 renames RV1_5(X);

X(0)=Y(1)

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

* Re: How to access an array using two different indexing schemes
  2017-11-29  4:57     ` Jerry
  2017-11-29 15:01       ` AdaMagica
  2017-11-29 15:03       ` AdaMagica
@ 2017-11-29 16:03       ` Shark8
  2017-11-29 17:04         ` AdaMagica
  2 siblings, 1 reply; 48+ messages in thread
From: Shark8 @ 2017-11-29 16:03 UTC (permalink / raw)


On Tuesday, November 28, 2017 at 9:57:48 PM UTC-7, Jerry wrote:
> 
> Your method shows how to pass the re-dimensioned array to a subprogram for processing with a typecast. But I also need to process the array in the same program that the subtype is declared. How would I do the typecast without allocating more memory?
> 
> Something like this:
> 
> procedure Doubly_Indexed_Array is
>    declare x with dimensions (0 .. 4)
>    declare y with dimensions (1 .. 5) using the same memory as x but
> 	                              accessed with different indices
> begin
>    x(0) := 10.0;
>    y(3) := 20.0;
>    print x(0) resulting in 10.0
>    print y(1) resulting in 10.0
>    print x(2) resulting in 20.0
>    print y(3) resulting in 20.0
> end Doubly_Indexed_Array;

Subtype Real is Float range Float'Range; -- Get rid of non-numeric.
Type General_Real_Array is Array(Integer range <>) of Real;

Procedure Do_Stuffs( Data : in out General_Real_Array ) is
  Subtype Indexing is General_Real_Array(1..Data'Length);
  Subtype Offset   is General_Real_Array(0..Integer'Pred(Data'Length));
  
  Indexed  : Indexing renames Data;
  Offseted : Offset renames Data;
Begin
  -- Stuffs!
  null;
End Do_Stuffs;

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

* Re: How to access an array using two different indexing schemes
  2017-11-29 16:03       ` Shark8
@ 2017-11-29 17:04         ` AdaMagica
  2017-11-29 20:56           ` Randy Brukardt
  0 siblings, 1 reply; 48+ messages in thread
From: AdaMagica @ 2017-11-29 17:04 UTC (permalink / raw)


Am Mittwoch, 29. November 2017 17:03:04 UTC+1 schrieb Shark8:

> Procedure Do_Stuffs( Data : in out General_Real_Array ) is
>   Subtype Indexing is General_Real_Array(1..Data'Length);
>   Subtype Offset   is General_Real_Array(0..Integer'Pred(Data'Length));
>   
>   Indexed  : Indexing renames Data;
>   Offseted : Offset renames Data;

No, that doesn't work! See previous posts.


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

* Re: How to access an array using two different indexing schemes
  2017-11-29 15:03       ` AdaMagica
@ 2017-11-29 20:53         ` Randy Brukardt
  0 siblings, 0 replies; 48+ messages in thread
From: Randy Brukardt @ 2017-11-29 20:53 UTC (permalink / raw)


"AdaMagica" <christ-usch.grein@t-online.de> wrote in message 
news:66ee0bde-123e-4713-800f-7eb692e52bd2@googlegroups.com...
> I'd guess you would not need it in both ways at the same place, so split 
> the uses in different subprograms.

Exactly. Accessing the same thing different ways in the same chunk of code 
is going to confuse your readers (which might be you five years from now). 
Think long and hard before you do such a thing. (And then don't do it. ;-)

                                      Randy.


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

* Re: How to access an array using two different indexing schemes
  2017-11-29 17:04         ` AdaMagica
@ 2017-11-29 20:56           ` Randy Brukardt
  2017-11-30  5:56             ` Jerry
  0 siblings, 1 reply; 48+ messages in thread
From: Randy Brukardt @ 2017-11-29 20:56 UTC (permalink / raw)


"AdaMagica" <christ-usch.grein@t-online.de> wrote in message 
news:227d5a7f-6d5b-4b76-ac31-7d15bb6dc284@googlegroups.com...
> Am Mittwoch, 29. November 2017 17:03:04 UTC+1 schrieb Shark8:
>
>> Procedure Do_Stuffs( Data : in out General_Real_Array ) is
>>   Subtype Indexing is General_Real_Array(1..Data'Length);
>>   Subtype Offset   is General_Real_Array(0..Integer'Pred(Data'Length));
>>
>>   Indexed  : Indexing renames Data;
>>   Offseted : Offset renames Data;
>
> No, that doesn't work! See previous posts.

Should have showed what does work:

  Indexed  : Indexing renames Indexing(Data);
  Offseted : Offset renames Offset(Data);

The type conversions change the bounds as needed. Renames does not allocate 
new memory.

                                     Randy.


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

* Re: How to access an array using two different indexing schemes
  2017-11-29 15:01       ` AdaMagica
  2017-11-29 15:21         ` AdaMagica
@ 2017-11-30  5:30         ` Jerry
  1 sibling, 0 replies; 48+ messages in thread
From: Jerry @ 2017-11-30  5:30 UTC (permalink / raw)


On Wednesday, November 29, 2017 at 8:01:35 AM UTC-7, AdaMagica wrote:
> A bit clumsy, but:
> 
> X(0) := ...;
> RV1_5(X) (1) := ...;  -- didn't try, might work
> print (x(0));
> print (RV1_5(X)(1));

With declarations

    x : Real_Vector(0 .. 4);  
    subtype RV1_5 is Real_Vector(1 .. 5);

then in the procedure

    RV1_5(x) (1) := 20.0;

GNAT complains

"left hand side of assignment must be a variable"


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

* Re: How to access an array using two different indexing schemes
  2017-11-29 15:21         ` AdaMagica
@ 2017-11-30  5:32           ` Jerry
  0 siblings, 0 replies; 48+ messages in thread
From: Jerry @ 2017-11-30  5:32 UTC (permalink / raw)


On Wednesday, November 29, 2017 at 8:21:57 AM UTC-7, AdaMagica wrote:
> Am Mittwoch, 29. November 2017 16:01:35 UTC+1 schrieb AdaMagica:
> > A bit clumsy, but:
> > 
> > X(0) := ...;
> > RV1_5(X) (1) := ...;  -- didn't try, might work
> > print (x(0));
> > print (RV1_5(X)(1));
> 
> Y: RV1_5 renames RV1_5(X);

GNAT complains "renaming of conversion only allowed for tagged types"
> 
> X(0)=Y(1)

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

* Re: How to access an array using two different indexing schemes
  2017-11-29 20:56           ` Randy Brukardt
@ 2017-11-30  5:56             ` Jerry
  2017-11-30 11:11               ` AdaMagica
  2017-11-30 21:50               ` Randy Brukardt
  0 siblings, 2 replies; 48+ messages in thread
From: Jerry @ 2017-11-30  5:56 UTC (permalink / raw)


On Wednesday, November 29, 2017 at 1:56:59 PM UTC-7, Randy Brukardt wrote:
> "AdaMagica"  wrote in message 
> > Am Mittwoch, 29. November 2017 17:03:04 UTC+1 schrieb Shark8:
> >
> >> Procedure Do_Stuffs( Data : in out General_Real_Array ) is
> >>   Subtype Indexing is General_Real_Array(1..Data'Length);
> >>   Subtype Offset   is General_Real_Array(0..Integer'Pred(Data'Length));
> >>
> >>   Indexed  : Indexing renames Data;
> >>   Offseted : Offset renames Data;
> >
> > No, that doesn't work! See previous posts.
> 
> Should have showed what does work:
> 
>   Indexed  : Indexing renames Indexing(Data);
>   Offseted : Offset renames Offset(Data);
> 
> The type conversions change the bounds as needed. Renames does not allocate 
> new memory.
> 
>                                      Randy.

Combining Shark8's plan with Randy's change and wrapping into a main program like this:

Procedure Shark8_And_Randy is
    Subtype Real is Float range Float'Range; -- Get rid of non-numeric.
    Type General_Real_Array is Array(Integer range <>) of Real;

    Procedure Do_Stuffs( Data : in out General_Real_Array ) is
        Subtype Indexing is General_Real_Array(1..Data'Length);
        Subtype Offset   is General_Real_Array(0..Integer'Pred(Data'Length));

        Indexed  : Indexing renames Indexing(Data);
        Offseted : Offset renames Offset(Data);
    Begin
        -- Stuffs!
        null;
    End Do_Stuffs;
Begin
    null;
End Shark8_And_Randy;

Results in GNAT complaining as such:

 shark8_and_randy.adb:9:37: renaming of conversion only allowed for tagged types
shark8_and_randy.adb:10:35: renaming of conversion only allowed for tagged types

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

* Re: How to access an array using two different indexing schemes
  2017-11-30  5:56             ` Jerry
@ 2017-11-30 11:11               ` AdaMagica
  2017-11-30 11:40                 ` AdaMagica
  2017-11-30 22:49                 ` Jerry
  2017-11-30 21:50               ` Randy Brukardt
  1 sibling, 2 replies; 48+ messages in thread
From: AdaMagica @ 2017-11-30 11:11 UTC (permalink / raw)


Am Donnerstag, 30. November 2017 06:56:26 UTC+1 schrieb Jerry:
> On Wednesday, November 29, 2017 at 1:56:59 PM UTC-7, Randy Brukardt wrote:
> > "AdaMagica"  wrote in message 
> > > Am Mittwoch, 29. November 2017 17:03:04 UTC+1 schrieb Shark8:
> > >
> > >> Procedure Do_Stuffs( Data : in out General_Real_Array ) is
> > >>   Subtype Indexing is General_Real_Array(1..Data'Length);
> > >>   Subtype Offset   is General_Real_Array(0..Integer'Pred(Data'Length));
> > >>
> > >>   Indexed  : Indexing renames Data;
> > >>   Offseted : Offset renames Data;
> > >
> > > No, that doesn't work! See previous posts.
> > 
> > Should have showed what does work:
> > 
> >   Indexed  : Indexing renames Indexing(Data);
> >   Offseted : Offset renames Offset(Data);
> > 
> > The type conversions change the bounds as needed. Renames does not allocate 
> > new memory.
> > 
> >                                      Randy.
> 
> Combining Shark8's plan with Randy's change and wrapping into a main program like this:
> 
> Procedure Shark8_And_Randy is
>     Subtype Real is Float range Float'Range; -- Get rid of non-numeric.
>     Type General_Real_Array is Array(Integer range <>) of Real;
> 
>     Procedure Do_Stuffs( Data : in out General_Real_Array ) is
>         Subtype Indexing is General_Real_Array(1..Data'Length);
>         Subtype Offset   is General_Real_Array(0..Integer'Pred(Data'Length));
> 
>         Indexed  : Indexing renames Indexing(Data);
>         Offseted : Offset renames Offset(Data);
>     Begin
>         -- Stuffs!
>         null;
>     End Do_Stuffs;
> Begin
>     null;
> End Shark8_And_Randy;
> 
> Results in GNAT complaining as such:
> 
>  shark8_and_randy.adb:9:37: renaming of conversion only allowed for tagged types
> shark8_and_randy.adb:10:35: renaming of conversion only allowed for tagged types

Then try:

  procedure Do_Stuffs (Data: in out General_Real_Array) is

    subtype Indexing is General_Real_Array (1..Data'Length);
    subtype Offset   is General_Real_Array (0..Integer'Pred (Data'Length));

    procedure Use_1_Based (X: in out Indexing) is separate;
    procedure Use_0_Based (X: in out Offset  ) is separate;

  begin

    Use_1_Based (Indexing (Data));
    Use_0_Based (Offset   (Data));

  end Do_Stuffs;


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

* Re: How to access an array using two different indexing schemes
  2017-11-30 11:11               ` AdaMagica
@ 2017-11-30 11:40                 ` AdaMagica
  2017-11-30 14:47                   ` Niklas Holsti
  2017-11-30 22:49                 ` Jerry
  1 sibling, 1 reply; 48+ messages in thread
From: AdaMagica @ 2017-11-30 11:40 UTC (permalink / raw)


OK, I dived into the RM:

8.5.1(4) The renamed entity shall be an object.
3.3(12) [An object is a] view conversion of another object. 
4.6(5/2) A type_conversion whose operand is the name of an object is called a view conversion if both its target type and operand type are tagged, or if it appears in a call as an actual parameter of mode out or in out; other type_conversions are called value conversions.

So my conclusion is:

The renamings here are illegal (not tagged).
The subprogram calls as shown in my previous post are legal.

Didn't try however. Good luck for you!

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

* Re: How to access an array using two different indexing schemes
  2017-11-30 11:40                 ` AdaMagica
@ 2017-11-30 14:47                   ` Niklas Holsti
  2017-11-30 17:30                     ` AdaMagica
  2017-11-30 21:57                     ` Randy Brukardt
  0 siblings, 2 replies; 48+ messages in thread
From: Niklas Holsti @ 2017-11-30 14:47 UTC (permalink / raw)


On 17-11-30 13:40 , AdaMagica wrote:
> OK, I dived into the RM:
>
> 8.5.1(4) The renamed entity shall be an object.
> 3.3(12) [An object is a] view conversion of another object.
> 4.6(5/2) A type_conversion whose operand is the name of an
> object is called a view conversion if both its target type
> and operand type are tagged, or if it appears in a call as
> an actual parameter of mode out or in out; other
> type_conversions are called value conversions.
>
> So my conclusion is:
>
> The renamings here are illegal (not tagged).
> The subprogram calls as shown in my previous post are legal.
>
> Didn't try however. Good luck for you!

Perhaps the next Ada revision could extend 4.6(5/2) to include a type 
conversion in a renaming, as a view conversion?

It seems to me that on the implementation level, this case (declaring a 
new local variable to be a renaming of a type conversion of an object) 
should be very similar to the actual-parameter case of 4.6(5/2).

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


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

* Re: How to access an array using two different indexing schemes
  2017-11-30 14:47                   ` Niklas Holsti
@ 2017-11-30 17:30                     ` AdaMagica
  2017-11-30 19:31                       ` G. B.
  2017-11-30 21:57                     ` Randy Brukardt
  1 sibling, 1 reply; 48+ messages in thread
From: AdaMagica @ 2017-11-30 17:30 UTC (permalink / raw)


Am Donnerstag, 30. November 2017 15:47:40 UTC+1 schrieb Niklas Holsti:
> Perhaps the next Ada revision could extend 4.6(5/2) to include a type 
> conversion in a renaming, as a view conversion?

I sent this request to Ada Comment.


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

* Re: How to access an array using two different indexing schemes
  2017-11-30 17:30                     ` AdaMagica
@ 2017-11-30 19:31                       ` G. B.
  2017-11-30 19:43                         ` Shark8
  0 siblings, 1 reply; 48+ messages in thread
From: G. B. @ 2017-11-30 19:31 UTC (permalink / raw)


AdaMagica <christ-usch.grein@t-online.de> wrote:
> Am Donnerstag, 30. November 2017 15:47:40 UTC+1 schrieb Niklas Holsti:
>> Perhaps the next Ada revision could extend 4.6(5/2) to include a type 
>> conversion in a renaming, as a view conversion?
> 
> I sent this request to Ada Comment.
> 

Isn‘t a type conversion potentially copying?


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

* Re: How to access an array using two different indexing schemes
  2017-11-30 19:31                       ` G. B.
@ 2017-11-30 19:43                         ` Shark8
  2017-11-30 22:10                           ` Randy Brukardt
  0 siblings, 1 reply; 48+ messages in thread
From: Shark8 @ 2017-11-30 19:43 UTC (permalink / raw)


On Thursday, November 30, 2017 at 12:31:53 PM UTC-7, G. B. wrote:
> AdaMagica wrote:
> > Am Donnerstag, 30. November 2017 15:47:40 UTC+1 schrieb Niklas Holsti:
> >> Perhaps the next Ada revision could extend 4.6(5/2) to include a type 
> >> conversion in a renaming, as a view conversion?
> > 
> > I sent this request to Ada Comment.
> > 
> 
> Isn‘t a type conversion potentially copying?

Perhaps, but a renaming [of extant data] shouldn't be... Especially in this particular case since we have bounds-sliding anyways.

What is wanted here is a View Conversion, not really a Type Conversion.


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

* Re: How to access an array using two different indexing schemes
  2017-11-30  5:56             ` Jerry
  2017-11-30 11:11               ` AdaMagica
@ 2017-11-30 21:50               ` Randy Brukardt
  2017-11-30 23:13                 ` Jerry
  1 sibling, 1 reply; 48+ messages in thread
From: Randy Brukardt @ 2017-11-30 21:50 UTC (permalink / raw)


"Jerry" <list_email@icloud.com> wrote in message 
news:16c980c2-1fa6-4084-97ea-b576c75b99c3@googlegroups.com...
> On Wednesday, November 29, 2017 at 1:56:59 PM UTC-7, Randy Brukardt wrote:
>> "AdaMagica"  wrote in message
>> > Am Mittwoch, 29. November 2017 17:03:04 UTC+1 schrieb Shark8:
>> >
>> >> Procedure Do_Stuffs( Data : in out General_Real_Array ) is
>> >>   Subtype Indexing is General_Real_Array(1..Data'Length);
>> >>   Subtype Offset   is 
>> >> General_Real_Array(0..Integer'Pred(Data'Length));
>> >>
>> >>   Indexed  : Indexing renames Data;
>> >>   Offseted : Offset renames Data;
>> >
>> > No, that doesn't work! See previous posts.
>>
>> Should have showed what does work:
>>
>>   Indexed  : Indexing renames Indexing(Data);
>>   Offseted : Offset renames Offset(Data);
>>
>> The type conversions change the bounds as needed. Renames does not 
>> allocate
>> new memory.
>>
>>                                      Randy.
>
> Combining Shark8's plan with Randy's change and wrapping into a main 
> program like this:
>
> Procedure Shark8_And_Randy is
>    Subtype Real is Float range Float'Range; -- Get rid of non-numeric.
>    Type General_Real_Array is Array(Integer range <>) of Real;
>
>    Procedure Do_Stuffs( Data : in out General_Real_Array ) is
>        Subtype Indexing is General_Real_Array(1..Data'Length);
>        Subtype Offset   is 
> General_Real_Array(0..Integer'Pred(Data'Length));
>
>        Indexed  : Indexing renames Indexing(Data);
>        Offseted : Offset renames Offset(Data);
>    Begin
>        -- Stuffs!
>        null;
>    End Do_Stuffs;
> Begin
>    null;
> End Shark8_And_Randy;
>
> Results in GNAT complaining as such:
>
> shark8_and_randy.adb:9:37: renaming of conversion only allowed for tagged 
> types
> shark8_and_randy.adb:10:35: renaming of conversion only allowed for tagged 
> types

Sorry, my mistake. That probably will work in Ada 2020, but of course that's 
in the future. And even if it worked, the renames would be of a constant (as 
it would act like a renaming of a function call), which probably isn't what 
you had in mind.

The only thing that does work is the type conversion in parameter passing 
(as I originally illustrated). So something like the following should work 
(should because I didn't try it):

Procedure Randy_V2 is
    Subtype Real is Float range Float'Range; -- Get rid of non-numeric.
    Type General_Real_Array is Array(Integer range <>) of Real;

    Procedure Do_Stuffs( Data : in out General_Real_Array ) is
        Subtype Indexing is General_Real_Array(1..Data'Length);
        Subtype Offset   is 
General_Real_Array(0..Integer'Pred(Data'Length));

        Procedure Really_Do_Stuff (Indexed : in out Indexing; Offseted : in 
out Offset) is
        Begin
            -- Stuff using Indexed and Offseted!!
            null;
        End Really_Do_Stuff;

    Begin
        Really_Do_Stuff (Indexing (Data), Offset(Data));
            -- Parameter passing is the only way in Ada 83-2012 to get a 
view conversion
            -- of an untagged object.
    End Do_Stuffs;
 Begin
    null;
 End Randy_V2;



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

* Re: How to access an array using two different indexing schemes
  2017-11-30 14:47                   ` Niklas Holsti
  2017-11-30 17:30                     ` AdaMagica
@ 2017-11-30 21:57                     ` Randy Brukardt
  1 sibling, 0 replies; 48+ messages in thread
From: Randy Brukardt @ 2017-11-30 21:57 UTC (permalink / raw)


"Niklas Holsti" <niklas.holsti@tidorum.invalid> wrote in message 
news:f8ak08F916jU1@mid.individual.net...
> On 17-11-30 13:40 , AdaMagica wrote:
>> OK, I dived into the RM:
>>
>> 8.5.1(4) The renamed entity shall be an object.
>> 3.3(12) [An object is a] view conversion of another object.
>> 4.6(5/2) A type_conversion whose operand is the name of an
>> object is called a view conversion if both its target type
>> and operand type are tagged, or if it appears in a call as
>> an actual parameter of mode out or in out; other
>> type_conversions are called value conversions.
>>
>> So my conclusion is:
>>
>> The renamings here are illegal (not tagged).
>> The subprogram calls as shown in my previous post are legal.
>>
>> Didn't try however. Good luck for you!
>
> Perhaps the next Ada revision could extend 4.6(5/2) to include a type 
> conversion in a renaming, as a view conversion?

The first part is already proposed: see AI12-0226-1. The second part would 
be problematic (at least in general), because of the rather wacky semantics 
of view conversions for untagged types (where were designed only for 
parameter passing with copy-in/copy-out semantics). For instance, you'd be 
making the following legal:

           Some_Seconds : Duration;

           Flt_Sec : Float renames Float(Some_Seconds);

           Flt_Sec := <Some_Complex_Float_Expression>;

which would most likely hide some interesting rounding issues (including 
precisely when those would happen). Definitely not just a drop in change.

The array case discussed here would work well, though.

                               Randy.


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

* Re: How to access an array using two different indexing schemes
  2017-11-30 19:43                         ` Shark8
@ 2017-11-30 22:10                           ` Randy Brukardt
  0 siblings, 0 replies; 48+ messages in thread
From: Randy Brukardt @ 2017-11-30 22:10 UTC (permalink / raw)


"Shark8" <onewingedshark@gmail.com> wrote in message 
news:ed357f21-d318-4768-a756-b86e9532a1ae@googlegroups.com...
>On Thursday, November 30, 2017 at 12:31:53 PM UTC-7, G. B. wrote:
>> AdaMagica wrote:
>> > Am Donnerstag, 30. November 2017 15:47:40 UTC+1 schrieb Niklas Holsti:
>> >> Perhaps the next Ada revision could extend 4.6(5/2) to include a type
>> >> conversion in a renaming, as a view conversion?
>> >
>> > I sent this request to Ada Comment.
>> >
>>
>> Isn't a type conversion potentially copying?
>
>Perhaps, but a renaming [of extant data] shouldn't be... Especially in this
>particular case since we have bounds-sliding anyways.

This particular case is safe, but it is not safe if the type conversion does 
any sort of representation change. (In that case, the representation change 
would be hidden from the uses, which Ada has never wanted to do.) Not sure 
how to reconcile that. In any case, parameter passing does work for the OPs 
problem (as I showed), so there is a solution, even if it is a bit clunky.

                                 Randy.



What is wanted here is a View Conversion, not really a Type Conversion.



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

* Re: How to access an array using two different indexing schemes
  2017-11-30 11:11               ` AdaMagica
  2017-11-30 11:40                 ` AdaMagica
@ 2017-11-30 22:49                 ` Jerry
  2017-11-30 23:00                   ` Jerry
  1 sibling, 1 reply; 48+ messages in thread
From: Jerry @ 2017-11-30 22:49 UTC (permalink / raw)


On Thursday, November 30, 2017 at 4:11:05 AM UTC-7, AdaMagica wrote:
> 
> Then try:
> 
>   procedure Do_Stuffs (Data: in out General_Real_Array) is
> 
>     subtype Indexing is General_Real_Array (1..Data'Length);
>     subtype Offset   is General_Real_Array (0..Integer'Pred (Data'Length));
> 
>     procedure Use_1_Based (X: in out Indexing) is separate;
>     procedure Use_0_Based (X: in out Offset  ) is separate;
> 
>   begin
> 
>     Use_1_Based (Indexing (Data));
>     Use_0_Based (Offset   (Data));
> 
>   end Do_Stuffs;

I tried this:

with Ada.Text_IO; use Ada.Text_IO;

Procedure AdaMagica is
    Subtype Real is Float range Float'Range; -- Get rid of non-numeric.
    Type General_Real_Array is Array(Integer range <>) of Real;
    My_Data : General_Real_Array(0 .. 4) := (0.0, 1.0, 2.0, 3.0, 4.0);

    procedure Do_Stuffs (Data: in out General_Real_Array) is

        subtype Indexing is General_Real_Array (1..Data'Length);
        subtype Offset   is General_Real_Array (0..Integer'Pred (Data'Length));

        procedure Use_1_Based (X: in out Indexing) is
        begin
            for i in X'range loop
                X(i) := X(i) + 0.1;
                Put_Line(i'img & "  " & X(i)'img);
            end loop;
        end Use_1_Based;

        procedure Use_0_Based (X: in out Offset) is
        begin
            for i in X'range loop
                X(i) := X(i) + 0.2;
                Put_Line(i'img & "  " & X(i)'img);
            end loop;
        end Use_0_Based;

    begin
        Use_1_Based (Indexing (Data));
        New_Line;
        Use_0_Based (Offset   (Data));
    end Do_Stuffs;
     
Begin
    Do_Stuffs(My_Data);
End AdaMagica;

Resulting in

 1   1.00000E-01
 2   1.10000E+00
 3   2.10000E+00
 4   3.10000E+00
 5   4.10000E+00

 0   3.00000E-01
 1   1.30000E+00
 2   2.30000E+00
 3   3.30000E+00
 4   4.30000E+00

At the subprogram level that you have devised, this works. However:

    * It requires a two-level subprogram construction to achieve the result, requiring a wrapper procedure to set things up;
    * Does not result in a view conversion (or whatever you want to call it) at the main program level or at the subprogram level, and thus;
    * Does not allow the dual indexing scheme.

I believe Randy showed how to accomplish the type conversion at the subroutine level a few days ago in this thread, also including the case where the array is allocated with access variables.

Jerry

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

* Re: How to access an array using two different indexing schemes
  2017-11-30 22:49                 ` Jerry
@ 2017-11-30 23:00                   ` Jerry
  0 siblings, 0 replies; 48+ messages in thread
From: Jerry @ 2017-11-30 23:00 UTC (permalink / raw)


On Thursday, November 30, 2017 at 3:49:07 PM UTC-7, Jerry wrote:

> I believe Randy showed how to accomplish the type conversion at the subroutine level a few days ago in this thread, also including the case where the array is allocated with access variables.
> 
> Jerry

Oops. Randy posted twice while I was testing and writing my previous post.

Jerry

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

* Re: How to access an array using two different indexing schemes
  2017-11-30 21:50               ` Randy Brukardt
@ 2017-11-30 23:13                 ` Jerry
  0 siblings, 0 replies; 48+ messages in thread
From: Jerry @ 2017-11-30 23:13 UTC (permalink / raw)


On Thursday, November 30, 2017 at 2:50:31 PM UTC-7, Randy Brukardt wrote:
> 
> The only thing that does work is the type conversion in parameter passing 
> (as I originally illustrated). So something like the following should work 
> (should because I didn't try it):
> 
The type conversion at the subroutine level also works for an open array in the formal argument. The following uses an array which is accessed by an access variable--the "usual" declaration method also works, i.e,. if x is declared as 

x : Real_Vector(-4 .. 3);


with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Long_Real_Arrays; use Ada.Numerics.Long_Real_Arrays;

procedure Randy_V3 is
    type Real_Vector_Access is access Real_Vector;
    x_Ptr : Real_Vector_Access := new Real_Vector(-4 .. 3);
        x : Real_Vector renames x_Ptr.all;
    subtype RV0_7 is Real_Vector(0..7);

    procedure Do_X(x : in out Real_Vector) is
    begin
        for i in x'range loop
            x(i) := x(i) + 0.1;
            Put_Line(i'img & "  " & x(i)'img);
        end loop;
    end Do_X;

    procedure Do_Y(y : in out RV0_7) is
    begin
        for i in y'range loop
            y(i) := y(i) + 0.2;
            Put_Line(i'img & "  " & y(i)'img);
        end loop;
    end Do_Y;

    procedure Do_Y_Open(y : in out Real_Vector) is
    begin
        for i in y'range loop
            y(i) := y(i) + 0.3;
            Put_Line(i'img & "  " & y(i)'img);
        end loop;
    end Do_Y_Open;

begin
    x := (-4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0);
    Do_X(x);
    New_Line;
    Do_Y(RV0_7(x));
    New_Line;
    Do_Y_Open(RV0_7(x));
end Randy_V3;


-4  -3.90000000000000E+00
-3  -2.90000000000000E+00
-2  -1.90000000000000E+00
-1  -9.00000000000000E-01
 0   1.00000000000000E-01
 1   1.10000000000000E+00
 2   2.10000000000000E+00
 3   3.10000000000000E+00

 0  -3.70000000000000E+00
 1  -2.70000000000000E+00
 2  -1.70000000000000E+00
 3  -7.00000000000000E-01
 4   3.00000000000000E-01
 5   1.30000000000000E+00
 6   2.30000000000000E+00
 7   3.30000000000000E+00

 0  -3.40000000000000E+00
 1  -2.40000000000000E+00
 2  -1.40000000000000E+00
 3  -4.00000000000000E-01
 4   6.00000000000000E-01
 5   1.60000000000000E+00
 6   2.60000000000000E+00
 7   3.60000000000000E+00

Jerry

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

end of thread, other threads:[~2017-11-30 23:13 UTC | newest]

Thread overview: 48+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-24 11:42 How to access an array using two different indexing schemes Jerry
2017-11-24 12:33 ` Jeffrey R. Carter
2017-11-24 15:52   ` AdaMagica
2017-11-24 22:25     ` Jerry
2017-11-25 13:57       ` AdaMagica
2017-11-24 22:23   ` Jerry
2017-11-25 15:39   ` Robin
2017-11-25 20:58     ` Jerry
2017-11-26 10:22       ` Robin
2017-11-28 21:57       ` G. B.
2017-11-24 17:37 ` A. Cervetti
2017-11-24 21:48   ` Jerry
2017-11-24 22:15     ` Robert A Duff
2017-11-24 23:38       ` Jerry
2017-11-26  1:07         ` Jerry
2017-11-26  8:58           ` Dmitry A. Kazakov
2017-11-26 10:31             ` Jerry
2017-11-28  1:31             ` Randy Brukardt
2017-11-28  1:25   ` Randy Brukardt
2017-11-29  4:57     ` Jerry
2017-11-29 15:01       ` AdaMagica
2017-11-29 15:21         ` AdaMagica
2017-11-30  5:32           ` Jerry
2017-11-30  5:30         ` Jerry
2017-11-29 15:03       ` AdaMagica
2017-11-29 20:53         ` Randy Brukardt
2017-11-29 16:03       ` Shark8
2017-11-29 17:04         ` AdaMagica
2017-11-29 20:56           ` Randy Brukardt
2017-11-30  5:56             ` Jerry
2017-11-30 11:11               ` AdaMagica
2017-11-30 11:40                 ` AdaMagica
2017-11-30 14:47                   ` Niklas Holsti
2017-11-30 17:30                     ` AdaMagica
2017-11-30 19:31                       ` G. B.
2017-11-30 19:43                         ` Shark8
2017-11-30 22:10                           ` Randy Brukardt
2017-11-30 21:57                     ` Randy Brukardt
2017-11-30 22:49                 ` Jerry
2017-11-30 23:00                   ` Jerry
2017-11-30 21:50               ` Randy Brukardt
2017-11-30 23:13                 ` Jerry
2017-11-24 18:37 ` Per Sandberg
2017-11-24 21:28   ` Jerry
2017-11-24 21:40     ` Dmitry A. Kazakov
2017-11-28  1:33     ` Randy Brukardt
2017-11-24 22:12 ` Robert A Duff
2017-11-28  1:39   ` Randy Brukardt

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