comp.lang.ada
 help / color / mirror / Atom feed
* Non-standard functions in GNAT's Ada.Containers packages?
@ 2022-09-14 12:36 G.B.
  2022-09-14 16:04 ` Egil H H
  0 siblings, 1 reply; 15+ messages in thread
From: G.B. @ 2022-09-14 12:36 UTC (permalink / raw)


In Ada.Containers.{Kind}s of GCC 11.2, I find functions defined
by the pattern:

   function Empty (Capacity : Count_Type := 1000) return {Kind};

I couldn't find these functions in the LRM's package specifications.

Upon type derivation, then, these functions appear to require
non-portable source text when using GNAT's standard Ada
containers.  For example,

     package Real_Vectors is new Ada.Containers.Vectors (...);
     type Fancy_Vectors is new Real_Vectors.Vector with private;

     18.     type Fancy_Vectors is new Real_Vectors.Vector with record
                  |
         >>> type must be declared abstract or "Empty" overridden
         >>> "Empty" has been inherited at line 14
         >>> "Empty" has been inherited from subprogram at a-convec.ads:125, instance at line 8


While composition would not incur this effect, etc. etc., still
the resulting source text would need to override a "standard" function,
but this function, Empty, might not exist at all when using other
standard Ada compilers.

Am I missing something?

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-14 12:36 Non-standard functions in GNAT's Ada.Containers packages? G.B.
@ 2022-09-14 16:04 ` Egil H H
  2022-09-15  7:13   ` G.B.
  0 siblings, 1 reply; 15+ messages in thread
From: Egil H H @ 2022-09-14 16:04 UTC (permalink / raw)


On Wednesday, September 14, 2022 at 2:36:11 PM UTC+2, G.B. wrote:
> In Ada.Containers.{Kind}s of GCC 11.2, I find functions defined 
> by the pattern: 
> 
> function Empty (Capacity : Count_Type := 1000) return {Kind}; 
> 
> I couldn't find these functions in the LRM's package specifications. 
<snip> 
> Am I missing something?

They're part of Ada 2022, needed for the new container aggregate syntax.

See,
http://www.ada-auth.org/standards/2xrm/html/RM-A-18-5.html
http://www.ada-auth.org/standards/2xrm/html/RM-4-3-5.html

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-14 16:04 ` Egil H H
@ 2022-09-15  7:13   ` G.B.
  2022-09-15 14:26     ` Marius Amado-Alves
  0 siblings, 1 reply; 15+ messages in thread
From: G.B. @ 2022-09-15  7:13 UTC (permalink / raw)


On 14.09.22 18:04, Egil H H wrote:
> On Wednesday, September 14, 2022 at 2:36:11 PM UTC+2, G.B. wrote:
>>
>> I couldn't find these functions in the LRM's package specifications.
> <snip>
>> Am I missing something?
> 
> They're part of Ada 2022, needed for the new container aggregate syntax.
> 
> See,
> http://www.ada-auth.org/standards/2xrm/html/RM-A-18-5.html
> http://www.ada-auth.org/standards/2xrm/html/RM-4-3-5.html

Thanks. As the interface of Ada's containers is chang^H^H^H^H^Hbeing
improved, it seems best to have compilers for pre-202x at hand
in case a program needs Ada 2005 containers or Ada 2012 containers.

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-15  7:13   ` G.B.
@ 2022-09-15 14:26     ` Marius Amado-Alves
  2022-09-15 15:03       ` Niklas Holsti
  0 siblings, 1 reply; 15+ messages in thread
From: Marius Amado-Alves @ 2022-09-15 14:26 UTC (permalink / raw)


> ... it seems best to have compilers for pre-202x at hand 
> in case a program needs Ada 2005 containers or Ada 2012 containers. (G.B.)

Sorry, but is all this new stuff not backward-compatible? (Except maybe a teeny thing or two, easy to fix.)

BTW, do the new containers fix map iteration? I'm always stupefied at not being possible to iterate a map like you do a vector.

Thanks.

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-15 14:26     ` Marius Amado-Alves
@ 2022-09-15 15:03       ` Niklas Holsti
  2022-09-15 17:11         ` Marius Amado-Alves
  0 siblings, 1 reply; 15+ messages in thread
From: Niklas Holsti @ 2022-09-15 15:03 UTC (permalink / raw)


On 2022-09-15 17:26, Marius Amado-Alves wrote:
>> ... it seems best to have compilers for pre-202x at hand in case a
>> program needs Ada 2005 containers or Ada 2012 containers. (G.B.)
> 
> Sorry, but is all this new stuff not backward-compatible? (Except
> maybe a teeny thing or two, easy to fix.)


The incompatibility seems to arise only if one derives from the 
container types defined in the library. I don't think that is often 
needed in an application.


> BTW, do the new containers fix map iteration? I'm always stupefied at
> not being possible to iterate a map like you do a vector.


Maps supported iteration already in Ada 2005, when the standard 
containers were added to Ada. There are functions First and Next, to 
give the first map element and to advance from an element to the next 
element, and also a procedure Iterate that traverses the whole map and 
performs some action on each element. In current Ada, one can also 
iterate over a map with the "generalized loop iteration" syntax, as in 
"for X of M loop ... end loop".

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-15 15:03       ` Niklas Holsti
@ 2022-09-15 17:11         ` Marius Amado-Alves
  2022-09-15 17:22           ` Dmitry A. Kazakov
                             ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Marius Amado-Alves @ 2022-09-15 17:11 UTC (permalink / raw)


Thanks, Niklas.

> ... There are functions First and Next ... procedure Iterate ...

Too verbose and error-prone (forget the Next and you get an endless loop).

> "for X of M loop ... end loop".

Not possible for maps.

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-15 17:11         ` Marius Amado-Alves
@ 2022-09-15 17:22           ` Dmitry A. Kazakov
  2022-09-16 16:03             ` Marius Amado-Alves
  2022-09-16 11:33           ` Björn Lundin
  2022-09-16 15:47           ` Jere
  2 siblings, 1 reply; 15+ messages in thread
From: Dmitry A. Kazakov @ 2022-09-15 17:22 UTC (permalink / raw)


On 2022-09-15 19:11, Marius Amado-Alves wrote:
> Thanks, Niklas.
> 
>> ... There are functions First and Next ... procedure Iterate ...
> 
> Too verbose and error-prone (forget the Next and you get an endless loop).

That is cursors/iterators for you. They are nothing but dressed up 
pointers. Should never be there, IMO.

In my containers I use positive index 1..<size> instead.

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

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-15 17:11         ` Marius Amado-Alves
  2022-09-15 17:22           ` Dmitry A. Kazakov
@ 2022-09-16 11:33           ` Björn Lundin
  2022-09-16 15:00             ` Marius Amado-Alves
  2022-09-16 15:47           ` Jere
  2 siblings, 1 reply; 15+ messages in thread
From: Björn Lundin @ 2022-09-16 11:33 UTC (permalink / raw)


On 2022-09-15 19:11, Marius Amado-Alves wrote:
> Thanks, Niklas.
> 
>> ... There are functions First and Next ... procedure Iterate ...
> 
> Too verbose and error-prone (forget the Next and you get an endless loop).
> 
>> "for X of M loop ... end loop".
> 
> Not possible for maps.


but you can as
 >https://programming-idioms.org/idiom/13/iterate-over-map-keys-and-value/1511/ada>

shows (as below)

--Access each key k with its value x from an associative array mymap,
-- and print them.

with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;

use Ada.Containers;

for C in My_Map.Iterate loop
    Put_Line ("Key = " & Key (C) & ", Value = " & Element (C));
end loop;



-- 
/Björn

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-16 11:33           ` Björn Lundin
@ 2022-09-16 15:00             ` Marius Amado-Alves
  2022-09-16 15:42               ` Egil H H
  2022-09-16 18:53               ` Björn Lundin
  0 siblings, 2 replies; 15+ messages in thread
From: Marius Amado-Alves @ 2022-09-16 15:00 UTC (permalink / raw)


> >> "for X of M loop ... end loop". 
> > 
> > Not possible for maps.
> but you can as 
> >https://programming-idioms.org/idiom/13/iterate-over-map-keys-and-value/1511/ada> 
> 
> with Ada.Containers.Indefinite_Hashed_Maps; 
> with Ada.Strings.Hash; 
> use Ada.Containers; 
> for C in My_Map.Iterate loop 
> Put_Line ("Key = " & Key (C) & ", Value = " & Element (C)); 
> end loop; 

Thanks, but this is "in", not "of", requires cursors, and DOES NOT COMPILE, as probably needs like ten lines of boiler plate not show.

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-16 15:00             ` Marius Amado-Alves
@ 2022-09-16 15:42               ` Egil H H
  2022-09-16 18:53               ` Björn Lundin
  1 sibling, 0 replies; 15+ messages in thread
From: Egil H H @ 2022-09-16 15:42 UTC (permalink / raw)


On Friday, September 16, 2022 at 5:00:29 PM UTC+2, amado...@gmail.com wrote:
> > >> "for X of M loop ... end loop". 
> > > 
> > > Not possible for maps. 
> > but you can as 
> > >https://programming-idioms.org/idiom/13/iterate-over-map-keys-and-value/1511/ada> 
> >
> > with Ada.Containers.Indefinite_Hashed_Maps; 
> > with Ada.Strings.Hash; 
> > use Ada.Containers; 
> > for C in My_Map.Iterate loop 
> > Put_Line ("Key = " & Key (C) & ", Value = " & Element (C)); 
> > end loop;
> Thanks, but this is "in", not "of", requires cursors, and DOES NOT COMPILE, as probably needs like ten lines of boiler plate not show.

(sorry for any botched formatting...)

with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;
with Ada.Text_IO;

procedure Iteration is
   package My_Maps is 
      new Ada.Containers.Indefinite_Hashed_Maps
         (String, Integer, Ada.Strings.Hash, "=");
   
   My_Map : My_Maps.Map;
begin
   My_Map.Include("One", 1);
   My_Map.Include("Two", 2);
   My_Map.Include("Three", 3);

   for C in My_Map.Iterate loop
      Ada.Text_IO.Put_Line(My_Maps.Key(C) & My_Maps.Element(C)'Image);
   end loop;
   
   for Element of My_Map loop
      Ada.Text_IO.Put_Line(Element'Image);
   end loop;
   
   -- Ada_2022:
   -- for (C : My_Maps.Cursor) of My_Map.Iterate loop
   --    Ada.Text_IO.Put_Line(My_Maps.Key(C) & My_Maps.Element(C)'Image);
   -- end loop Ada_2022;
   
end Iteration;

I don't know why they left out a two-parameter version of the Iterate procedure for Ada 2022 Maps,
   -- for (Key, Element) of My_Map.Iterate loop
would've been nice, just like
   -- for (Name, Val) of Ada.Environment_Variables.Iterate(<>) loop



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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-15 17:11         ` Marius Amado-Alves
  2022-09-15 17:22           ` Dmitry A. Kazakov
  2022-09-16 11:33           ` Björn Lundin
@ 2022-09-16 15:47           ` Jere
  2 siblings, 0 replies; 15+ messages in thread
From: Jere @ 2022-09-16 15:47 UTC (permalink / raw)


On Thursday, September 15, 2022 at 1:11:19 PM UTC-4, amado...@gmail.com wrote:
> Thanks, Niklas. 
> 
> > ... There are functions First and Next ... procedure Iterate ... 
> 
> Too verbose and error-prone (forget the Next and you get an endless loop).
> > "for X of M loop ... end loop".
> Not possible for maps.

I'm not sure I understand.  I tried it in gnat 11 and it compiled just fine:
*************************************************
    with Ada.Text_IO; use Ada.Text_IO;
    with Ada.Containers.Ordered_Maps;
     
    procedure Program is
        package Maps is new Ada.Containers.Ordered_Maps(Integer,Integer);
     
        Map : Maps.Map;
     
    begin
     
        Map.Insert(1,10);
        Map.Insert(2,20);
        Map.Insert(3,30);
        Map.Insert(4,40);
     
        for Element of Map loop
            Put_Line(Element'Image);
        end loop;   
    end Program;
*************************************************
Output:
10
 20
 30
 40
*************************************************

Even tried it on some online compilers and it compiled and ran:
IDEONE online compiler:  https://ideone.com/H2oEZt

If this isn't what you mean, what is the missing feature.?  the "for of" version does work for maps as far as I can tell.

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-15 17:22           ` Dmitry A. Kazakov
@ 2022-09-16 16:03             ` Marius Amado-Alves
  2022-09-16 16:30               ` Marius Amado-Alves
  0 siblings, 1 reply; 15+ messages in thread
From: Marius Amado-Alves @ 2022-09-16 16:03 UTC (permalink / raw)


> That is cursors/iterators for you. They are nothing but dressed up 
> pointers. Should never be there, IMO.  (Dmitry)

Indeed. I've participated in the initial Containers Ada Issue. Managed to get indefinite types in, but lost the fight against the C++STL copy-over (cf. my paper in Ada-Europe 2004).

> In my containers I use positive index 1..<size> instead.

As it should be. I've been pondering using your containers for a long time. Only reason I'm still using Ada's is that they come ready to use with the language. I hate configuration management. But now with Alire I expect configuration work be unavoidable, so probably I'll start using your library at last:-) Read your B_Tree stuff recently. Cool stuff. The separation of storage (RAM, disk...) and structure (vector, map...) still not clear cut. Theoretically storage pools could do this, but Ada does not help her self. I tried a design once with generics (instead of storage pools): also too verbose

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

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-16 16:03             ` Marius Amado-Alves
@ 2022-09-16 16:30               ` Marius Amado-Alves
  2022-09-16 17:08                 ` Jere
  0 siblings, 1 reply; 15+ messages in thread
From: Marius Amado-Alves @ 2022-09-16 16:30 UTC (permalink / raw)


Jere, ehh.p..., thanks a lot, your complete code was very helpful.
For some reason I tried to write for maps as for vectors and it did not pass.
I see now some forms do pass.
Sorry for the entropy.
As you note, still not possible to access the Key with the "of" form.

/*
And the form
for (C : My_Maps.Cursor) of My_Map.Iterate loop
does not pass. Must be "in"
*/

Thanks all.

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-16 16:30               ` Marius Amado-Alves
@ 2022-09-16 17:08                 ` Jere
  0 siblings, 0 replies; 15+ messages in thread
From: Jere @ 2022-09-16 17:08 UTC (permalink / raw)


On Friday, September 16, 2022 at 12:30:51 PM UTC-4, amado...@gmail.com wrote:
> Jere, ehh.p..., thanks a lot, your complete code was very helpful. 
> For some reason I tried to write for maps as for vectors and it did not pass. 
> I see now some forms do pass. 
> Sorry for the entropy. 
> As you note, still not possible to access the Key with the "of" form. 
> 
> /* 
> And the form
> for (C : My_Maps.Cursor) of My_Map.Iterate loop
> does not pass. Must be "in" 
> */ 
> 
> Thanks all.
 No problem at all.  Yeah, all the standard Ada containers use the "of" form to iterate
over elements and the "in" form to iterate over cursors.  Keys are more like cursors 
from the perspective of the container, so you would need to use some form of "in" 
to get the keys.  

for what it is worth, the example I gave is usable for vectors.  you have to change
names and use append() instead of insert(), but the rest is pretty similar:

*******************************************
    with Ada.Text_IO; use Ada.Text_IO;
    with Ada.Containers.Vectors;
     
    procedure Program is
        package Vectors is new Ada.Containers.Vectors(Positive,Integer);
     
        Vector : Vectors.Vector;
     
    begin
     
        Vector.Append(10);
        Vector.Append(20);
        Vector.Append(30);
        Vector.Append(40);
     
        for Element of Vector loop
            Put_Line(Element'Image);
        end loop;   
    end Program;
**********************************************
Output:
10
 20
 30
 40
*******************************************
IDEONE compiler link:  https://ideone.com/3Ic49d#stdout

So it may depend on how your vector code was originally setup.  
The above is how I typically loop through a vector, which works
for maps and other Ada containers as well.  

Hope that helps!

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

* Re: Non-standard functions in GNAT's Ada.Containers packages?
  2022-09-16 15:00             ` Marius Amado-Alves
  2022-09-16 15:42               ` Egil H H
@ 2022-09-16 18:53               ` Björn Lundin
  1 sibling, 0 replies; 15+ messages in thread
From: Björn Lundin @ 2022-09-16 18:53 UTC (permalink / raw)


On 2022-09-16 17:00, Marius Amado-Alves wrote:
>>>> "for X of M loop ... end loop".
>>>
>>> Not possible for maps.
>> but you can as
>>> https://programming-idioms.org/idiom/13/iterate-over-map-keys-and-value/1511/ada>
>>
>> with Ada.Containers.Indefinite_Hashed_Maps;
>> with Ada.Strings.Hash;
>> use Ada.Containers;
>> for C in My_Map.Iterate loop
>> Put_Line ("Key = " & Key (C) & ", Value = " & Element (C));
>> end loop;
> 
> Thanks, but this is "in", not "of", requires cursors, and DOES NOT COMPILE, as probably needs like ten lines of boiler plate not show.

well, yes . I thought the for looping stuff was the important part, 
since you done want to call Next.

in or of - does it really matter here?

   for C in My_Map.Iterate loop
or
   for C of My_Map loop
is not that hurtful to my eyes.

   for K,V in My_Map.Iterate loop
or
   for K,V of My_Map loop

getting Key and Value as a tuple would be nicer of course


Anyway, I forgot to instantiate the map. Here's a compileable variant



It outputs
Key = AA, Value =  1
Key = AB, Value =  5

----------------

with Ada.Containers.Hashed_Maps;
with Ada.Strings.Hash;
use Ada.Containers;
with Text_Io ; use Text_IO;

procedure Ite is

   subtype Test_Type is String(1..2);

   package Test_Map_Pkg is new Ada.Containers.Hashed_Maps
     (Test_Type,
      Integer,
      Ada.Strings.Hash,
      "=",
      "=");

   My_Map : Test_Map_Pkg.Map;
   use Test_Map_Pkg;
begin
   My_Map.Insert("AA",1);
   My_Map.Insert("AB",5);

   for C in My_Map.Iterate loop
     Put_Line ("Key = " & Key (C) & ", Value = " & Element (C)'Img);
   end loop;

end Ite;



-- 
/Björn

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

end of thread, other threads:[~2022-09-16 18:53 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-14 12:36 Non-standard functions in GNAT's Ada.Containers packages? G.B.
2022-09-14 16:04 ` Egil H H
2022-09-15  7:13   ` G.B.
2022-09-15 14:26     ` Marius Amado-Alves
2022-09-15 15:03       ` Niklas Holsti
2022-09-15 17:11         ` Marius Amado-Alves
2022-09-15 17:22           ` Dmitry A. Kazakov
2022-09-16 16:03             ` Marius Amado-Alves
2022-09-16 16:30               ` Marius Amado-Alves
2022-09-16 17:08                 ` Jere
2022-09-16 11:33           ` Björn Lundin
2022-09-16 15:00             ` Marius Amado-Alves
2022-09-16 15:42               ` Egil H H
2022-09-16 18:53               ` Björn Lundin
2022-09-16 15:47           ` Jere

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