• Non-standard functions in GNAT's Ada.Containers packages?

    From G.B.@21:1/5 to All on Wed Sep 14 14:36:07 2022
    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?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Egil H H@21:1/5 to G.B. on Wed Sep 14 09:04:44 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From G.B.@21:1/5 to Egil H H on Thu Sep 15 09:13:58 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marius Amado-Alves@21:1/5 to All on Thu Sep 15 07:26:17 2022
    ... 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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Niklas Holsti@21:1/5 to Marius Amado-Alves on Thu Sep 15 18:03:16 2022
    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".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marius Amado-Alves@21:1/5 to All on Thu Sep 15 10:11:18 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Marius Amado-Alves on Thu Sep 15 19:22:15 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Bj=c3=b6rn_Lundin?=@21:1/5 to Marius Amado-Alves on Fri Sep 16 13:33:43 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marius Amado-Alves@21:1/5 to All on Fri Sep 16 08:00:28 2022
    "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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Egil H H@21:1/5 to amado...@gmail.com on Fri Sep 16 08:42:06 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jere@21:1/5 to amado...@gmail.com on Fri Sep 16 08:47:03 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marius Amado-Alves@21:1/5 to All on Fri Sep 16 09:03:32 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marius Amado-Alves@21:1/5 to All on Fri Sep 16 09:30:49 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jere@21:1/5 to amado...@gmail.com on Fri Sep 16 10:08:22 2022
    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Bj=c3=b6rn_Lundin?=@21:1/5 to Marius Amado-Alves on Fri Sep 16 20:53:07 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)