comp.lang.ada
 help / color / mirror / Atom feed
* Array index overloading
@ 2014-08-06 16:47 marmaduke.woodman
  2014-08-06 16:53 ` Pascal Obry
  2014-08-06 17:36 ` Peter Chapin
  0 siblings, 2 replies; 9+ messages in thread
From: marmaduke.woodman @ 2014-08-06 16:47 UTC (permalink / raw)


Hi all,

I am new to Ada and have perhaps a boring question, coming from array oriented languages such as MATLAB & NumPy in Python: 

Is it possible to overload the indexing operator? For example, in the aforementioned languages, one could index with the value 1 to get the first element, or index with a vector [2, 3, 4] to obtain a vector of the 2nd, 3rd and 4th elements. 

Handling index arrays and generating an output array is something I can write, so my question is if the syntax can be overloaded, or I should expect to implement an explicit "slice" function? 


A more general question: from my brief searches on the internet, I have not uncovered a generic N-dimensional array library, and I am curious how users of Ada in the scientific domains proceed: have I missed something or is idiomatic Ada array code written in looped style?


regards,
Marmaduke


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

* Re: Array index overloading
  2014-08-06 16:47 Array index overloading marmaduke.woodman
@ 2014-08-06 16:53 ` Pascal Obry
  2014-08-06 17:38   ` marmaduke.woodman
  2014-08-06 17:36 ` Peter Chapin
  1 sibling, 1 reply; 9+ messages in thread
From: Pascal Obry @ 2014-08-06 16:53 UTC (permalink / raw)


Le mercredi 06 août 2014 à 09:47 -0700, marmaduke.woodman@univ-amu.fr a
écrit : 
> I am new to Ada and have perhaps a boring question, coming from array oriented languages such as MATLAB & NumPy in Python: 
> 
> Is it possible to overload the indexing operator? For example, in the aforementioned languages, one could index with the value 1 to get the first element, or index with a vector [2, 3, 4] to obtain a vector of the 2nd, 3rd and 4th elements. 

If I understand correctly what you want, it is built-in in Ada:

   type Arr is array (positive range <>) of Natural;

   V : Arr (1 .. 78);

Then

   V (1) is the first element (a Natural)

   V (2 .. 4) is an array of 3 naturals with first index being 2
   and last 4.

-- 
  Pascal Obry /  Magny Les Hameaux (78)

  The best way to travel is by means of imagination

  http://v2p.fr.eu.org
  http://www.obry.net

  gpg --keyserver keys.gnupg.net --recv-key F949BD3B


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

* Re: Array index overloading
  2014-08-06 16:47 Array index overloading marmaduke.woodman
  2014-08-06 16:53 ` Pascal Obry
@ 2014-08-06 17:36 ` Peter Chapin
  2014-08-07  5:17   ` Randy Brukardt
  1 sibling, 1 reply; 9+ messages in thread
From: Peter Chapin @ 2014-08-06 17:36 UTC (permalink / raw)


On 2014-08-06 12:47, marmaduke.woodman@univ-amu.fr wrote:

> Handling index arrays and generating an output array is something I can write, so my question is if the syntax can be overloaded, or I should expect to implement an explicit "slice" function? 

As Pascal mentioned one dimensional array slices are supported in Ada by
just indexing using notation A(I .. J) for array A. If you want
non-unity strides, or multi-dimensional slices, or some other fancier
effects, you will need to turn to a library of some kind.

Although as I write this I'm wondering if a subtype with a static
predicate could cover some of those bases. Hmm.

Peter


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

* Re: Array index overloading
  2014-08-06 16:53 ` Pascal Obry
@ 2014-08-06 17:38   ` marmaduke.woodman
  2014-08-06 19:25     ` Georg Bauhaus
                       ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: marmaduke.woodman @ 2014-08-06 17:38 UTC (permalink / raw)


So V(2 .. 4) is possible, but is the equivalent V((2, 3, 4)) ? How about V((9, 1, 2)) or V(((3, 3), (4, 5)))? It seems these cases fall outside the intrinsic indexing support providing by Ada.

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

* Re: Array index overloading
  2014-08-06 17:38   ` marmaduke.woodman
@ 2014-08-06 19:25     ` Georg Bauhaus
  2014-08-06 19:43     ` Adam Beneschan
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Georg Bauhaus @ 2014-08-06 19:25 UTC (permalink / raw)


On 06.08.14 19:38, marmaduke.woodman@univ-amu.fr wrote:
> So V(2 .. 4) is possible, but is the equivalent V((2, 3, 4)) ? How about V((9, 1, 2)) or V(((3, 3), (4, 5)))? It seems these cases fall outside the intrinsic indexing support providing by Ada.
>

These facilities are found in languages that are implemented
in languages like Ada. Consequently, if Ada or one of its
relatives had had these facilities, the aforementioned languages
would not have been created.



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

* Re: Array index overloading
  2014-08-06 17:38   ` marmaduke.woodman
  2014-08-06 19:25     ` Georg Bauhaus
@ 2014-08-06 19:43     ` Adam Beneschan
  2014-08-06 21:51     ` Pascal Obry
  2014-08-06 23:53     ` Shark8
  3 siblings, 0 replies; 9+ messages in thread
From: Adam Beneschan @ 2014-08-06 19:43 UTC (permalink / raw)


On Wednesday, August 6, 2014 10:38:46 AM UTC-7, marmaduk...@univ-amu.fr wrote:
> So V(2 .. 4) is possible, but is the equivalent V((2, 3, 4)) ? How about V((9, 1, 2)) or V(((3, 3), (4, 5)))? It seems these cases fall outside the intrinsic indexing support providing by Ada.

V((2, 3, 4)) isn't legal Ada syntax.  If you want to create a three-element array taken from elements 9, 1, and 2 of V, you can write V(9) & V(1..2).  "&" is an array concatenation operator for 1-dimensional arrays (but it also accepts single values of the element type, such as V(9) in my example).

                                 -- Adam


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

* Re: Array index overloading
  2014-08-06 17:38   ` marmaduke.woodman
  2014-08-06 19:25     ` Georg Bauhaus
  2014-08-06 19:43     ` Adam Beneschan
@ 2014-08-06 21:51     ` Pascal Obry
  2014-08-06 23:53     ` Shark8
  3 siblings, 0 replies; 9+ messages in thread
From: Pascal Obry @ 2014-08-06 21:51 UTC (permalink / raw)


Le mercredi 06 août 2014 à 10:38 -0700, marmaduke.woodman@univ-amu.fr a
écrit :
> So V(2 .. 4) is possible, but is the equivalent V((2, 3, 4)) ? How
> about V((9, 1, 2)) 

V(9) & V(1) & V(2)

> or V(((3, 3), 

T'(1 => V (3));

If T is the base type of V.

> (4, 5)))?

V (4 .. 5)

> It seems these cases fall outside the intrinsic indexing support
> providing by Ada.

Not outside as described above.

-- 
  Pascal Obry /  Magny Les Hameaux (78)

  The best way to travel is by means of imagination

  http://v2p.fr.eu.org
  http://www.obry.net

  gpg --keyserver keys.gnupg.net --recv-key F949BD3B


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

* Re: Array index overloading
  2014-08-06 17:38   ` marmaduke.woodman
                       ` (2 preceding siblings ...)
  2014-08-06 21:51     ` Pascal Obry
@ 2014-08-06 23:53     ` Shark8
  3 siblings, 0 replies; 9+ messages in thread
From: Shark8 @ 2014-08-06 23:53 UTC (permalink / raw)


On 06-Aug-14 11:38, marmaduke.woodman@univ-amu.fr wrote:
> So V(2 .. 4) is possible, but is the equivalent V((2, 3, 4)) ?


Kinda; the following should show how you'd do your select-by-vector sort 
of thing:

     -- This is your index-type, and a 'vector' thereof.
     Subtype Index_Type is Positive;
     Type Index_Array is Array(Positive range <>) of Index_Type;

     -- This is the Array-type you're interested in.
     Type Collection_Array is Array(Index_Type range <>) of Float;

     -- This is how you can apply a 'vector' to that array to select
     -- disjoint components.
     Function Get_Items( Indices  : Index_Array;
                         From     : Collection_Array
                       ) return Collection_Array is
     Begin
         Return Result: Collection_Array(1..Indices'Length) do
             for Index in Indices'Range loop
                 Offsets:
                 declare
                     From_Index : Constant Positive := Indices(Index);
                     To_Index   : Constant Positive := 
Result'First+Index-Indices'First;
                 begin
                     Result(To_Index) := From(From_Index);
                 end Offsets;
             end loop;
         end return;
     End Get_Items;

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

* Re: Array index overloading
  2014-08-06 17:36 ` Peter Chapin
@ 2014-08-07  5:17   ` Randy Brukardt
  0 siblings, 0 replies; 9+ messages in thread
From: Randy Brukardt @ 2014-08-07  5:17 UTC (permalink / raw)


"Peter Chapin" <PChapin@vtc.vsc.edu> wrote in message 
news:ppKdnfmkdIYX-n_ORVn_vwA@giganews.com...
> On 2014-08-06 12:47, marmaduke.woodman@univ-amu.fr wrote:
>
>> Handling index arrays and generating an output array is something I can 
>> write, so my question is if the syntax can be overloaded, or I should 
>> expect to implement an explicit "slice" function?
>
> As Pascal mentioned one dimensional array slices are supported in Ada by
> just indexing using notation A(I .. J) for array A. If you want
> non-unity strides, or multi-dimensional slices, or some other fancier
> effects, you will need to turn to a library of some kind.
>
> Although as I write this I'm wondering if a subtype with a static
> predicate could cover some of those bases. Hmm.

You'll find that it is illegal to declare an array with an index subtype 
that has one or more predicates. We didn't want compilers to have to support 
"holey" arrays or slices.

One could create a container that supported almost any sort of indexing that 
someone wanted. In particular, the Map containers support using the indexed 
notation on the Key_Type, and the Key_Type can be pretty much anything you 
want, so long as it is non-limited.

So, with appropriate declarations, you can write:

     Storage ("Bob") := Obj1;
     Storage ("Randy") := Obj2;
     if Storage ("Tuck") = Storage ("Randy") then ...

and so on.

                              Randy.



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

end of thread, other threads:[~2014-08-07  5:17 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-06 16:47 Array index overloading marmaduke.woodman
2014-08-06 16:53 ` Pascal Obry
2014-08-06 17:38   ` marmaduke.woodman
2014-08-06 19:25     ` Georg Bauhaus
2014-08-06 19:43     ` Adam Beneschan
2014-08-06 21:51     ` Pascal Obry
2014-08-06 23:53     ` Shark8
2014-08-06 17:36 ` Peter Chapin
2014-08-07  5:17   ` Randy Brukardt

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