comp.lang.ada
 help / color / mirror / Atom feed
From: "Björn Lundin" <bnl@nowhere.com>
Subject: Re: Non-standard functions in GNAT's Ada.Containers packages?
Date: Fri, 16 Sep 2022 20:53:07 +0200	[thread overview]
Message-ID: <tg2gmj$3toq1$1@dont-email.me> (raw)
In-Reply-To: <a7171ebe-279f-4cc8-9b0d-0db197e42c0bn@googlegroups.com>

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

  parent reply	other threads:[~2022-09-16 18:53 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2022-09-16 15:47           ` Jere
replies disabled

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