comp.lang.ada
 help / color / mirror / Atom feed
* How to Iterate over all elements of a hashed_map.
@ 2019-10-29 13:43 Alain De Vos
  2019-10-29 14:20 ` Alain De Vos
  2019-10-29 16:48 ` Jeffrey R. Carter
  0 siblings, 2 replies; 5+ messages in thread
From: Alain De Vos @ 2019-10-29 13:43 UTC (permalink / raw)


I want to use an "associative array" , and have some code for a "string -> string" hash,

with Ada.Containers.Hashed_Maps;
----BEGIN MYHASH
   function Equivalent_Key (Left, Right : Unbounded_String) return Boolean is
   begin
      return Left = Right;
   end Equivalent_Key;
   function Hash_Func (Key : Unbounded_String) return Ada.Containers.Hash_Type is
   begin
      return Ada.Strings.Hash (To_String (Key));
   end Hash_Func;
   package My_Hash is new Ada.Containers.Hashed_Maps (Key_Type => Unbounded_String,
       Element_Type => Unbounded_String,
       Hash => Hash_Func,
       Equivalent_Keys => Equivalent_Key);
----END MYHASH
declare
Hash : My_Hash.Map;
begin
Hash.Insert ( Key => ... , New_Item => ...)
Hash.Insert ( Key => ... , New_Item => ...)

But now I want to iterate over all elements of this Hash and print the keys and items ?


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

* Re: How to Iterate over all elements of a hashed_map.
  2019-10-29 13:43 How to Iterate over all elements of a hashed_map Alain De Vos
@ 2019-10-29 14:20 ` Alain De Vos
  2019-10-29 15:02   ` joakimds
  2019-10-29 16:48 ` Jeffrey R. Carter
  1 sibling, 1 reply; 5+ messages in thread
From: Alain De Vos @ 2019-10-29 14:20 UTC (permalink / raw)


I found code which takes a different approach, here there is a function .Iterate public ... ,

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

with Ada.Text_IO; use Ada.Text_IO;

procedure Show_Hashed_Map is

   package Integer_Hashed_Maps is new
     Ada.Containers.Indefinite_Hashed_Maps
       (Key_Type        => String,
        Element_Type    => Integer,
        Hash            => Ada.Strings.Hash,
        Equivalent_Keys => "=");

   use Integer_Hashed_Maps;

   M : Map;
   --  Same as:  M : Integer_Hashed_Maps.Map;
begin
   M.Include ("Alice", 24);
   M.Include ("John",  40);
   M.Include ("Bob",   28);

   if M.Contains ("Alice") then
      Put_Line ("Alice's age is "
                & Integer'Image (M ("Alice")));
   end if;

   --  Update Alice's age
   --  Key must already exist in M.
   --  Otherwise an exception is raised.
   M ("Alice") := 25;

   New_Line; Put_Line ("Name & Age:");
   for C in M.Iterate loop
      Put_Line (Key (C) & ": " & Integer'Image (M (C)));
   end loop;

end Show_Hashed_Map;


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

* Re: How to Iterate over all elements of a hashed_map.
  2019-10-29 14:20 ` Alain De Vos
@ 2019-10-29 15:02   ` joakimds
  2019-10-29 21:56     ` Randy Brukardt
  0 siblings, 1 reply; 5+ messages in thread
From: joakimds @ 2019-10-29 15:02 UTC (permalink / raw)


Den tisdag 29 oktober 2019 kl. 15:20:43 UTC+1 skrev Alain De Vos:
> I found code which takes a different approach, here there is a function .Iterate public ... ,
> 
> with Ada.Containers.Indefinite_Hashed_Maps;
> with Ada.Strings.Hash;
> 
> with Ada.Text_IO; use Ada.Text_IO;
> 
> procedure Show_Hashed_Map is
> 
>    package Integer_Hashed_Maps is new
>      Ada.Containers.Indefinite_Hashed_Maps
>        (Key_Type        => String,
>         Element_Type    => Integer,
>         Hash            => Ada.Strings.Hash,
>         Equivalent_Keys => "=");
> 
>    use Integer_Hashed_Maps;
> 
>    M : Map;
>    --  Same as:  M : Integer_Hashed_Maps.Map;
> begin
>    M.Include ("Alice", 24);
>    M.Include ("John",  40);
>    M.Include ("Bob",   28);
> 
>    if M.Contains ("Alice") then
>       Put_Line ("Alice's age is "
>                 & Integer'Image (M ("Alice")));
>    end if;
> 
>    --  Update Alice's age
>    --  Key must already exist in M.
>    --  Otherwise an exception is raised.
>    M ("Alice") := 25;
> 
>    New_Line; Put_Line ("Name & Age:");
>    for C in M.Iterate loop
>       Put_Line (Key (C) & ": " & Integer'Image (M (C)));
>    end loop;
> 
> end Show_Hashed_Map;

I confirm that when you want to iterate over a hashed map and want both key and value you need to use the .Iterate function as you do in the example.

Best regards,
Joakim

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

* Re: How to Iterate over all elements of a hashed_map.
  2019-10-29 13:43 How to Iterate over all elements of a hashed_map Alain De Vos
  2019-10-29 14:20 ` Alain De Vos
@ 2019-10-29 16:48 ` Jeffrey R. Carter
  1 sibling, 0 replies; 5+ messages in thread
From: Jeffrey R. Carter @ 2019-10-29 16:48 UTC (permalink / raw)


On 10/29/19 2:43 PM, Alain De Vos wrote:
> 
>     package My_Hash is new Ada.Containers.Hashed_Maps (Key_Type => Unbounded_String,
>         Element_Type => Unbounded_String,
>         Hash => Hash_Func,
>         Equivalent_Keys => Equivalent_Key);
> 
> But now I want to iterate over all elements of this Hash and print the keys and items ?

My_Hash.Iterate?

-- 
Jeff Carter
"You cheesy lot of second-hand electric donkey-bottom biters."
Monty Python & the Holy Grail
14


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

* Re: How to Iterate over all elements of a hashed_map.
  2019-10-29 15:02   ` joakimds
@ 2019-10-29 21:56     ` Randy Brukardt
  0 siblings, 0 replies; 5+ messages in thread
From: Randy Brukardt @ 2019-10-29 21:56 UTC (permalink / raw)


<joakimds@kth.se> wrote in message 
news:1559a685-7483-4f2f-8674-d7b61c23bfe8@googlegroups.com...
> Den tisdag 29 oktober 2019 kl. 15:20:43 UTC+1 skrev Alain De Vos:
>> I found code which takes a different approach, here there is a function 
>> .Iterate public ... ,
>>
>> with Ada.Containers.Indefinite_Hashed_Maps;
>> with Ada.Strings.Hash;
>>
>> with Ada.Text_IO; use Ada.Text_IO;
>>
>> procedure Show_Hashed_Map is
>>
>>    package Integer_Hashed_Maps is new
>>      Ada.Containers.Indefinite_Hashed_Maps
>>        (Key_Type        => String,
>>         Element_Type    => Integer,
>>         Hash            => Ada.Strings.Hash,
>>         Equivalent_Keys => "=");
>>
>>    use Integer_Hashed_Maps;
>>
>>    M : Map;
>>    --  Same as:  M : Integer_Hashed_Maps.Map;
>> begin
>>    M.Include ("Alice", 24);
>>    M.Include ("John",  40);
>>    M.Include ("Bob",   28);
>>
>>    if M.Contains ("Alice") then
>>       Put_Line ("Alice's age is "
>>                 & Integer'Image (M ("Alice")));
>>    end if;
>>
>>    --  Update Alice's age
>>    --  Key must already exist in M.
>>    --  Otherwise an exception is raised.
>>    M ("Alice") := 25;
>>
>>    New_Line; Put_Line ("Name & Age:");
>>    for C in M.Iterate loop
>>       Put_Line (Key (C) & ": " & Integer'Image (M (C)));
>>    end loop;
>>
>> end Show_Hashed_Map;
>
> I confirm that when you want to iterate over a hashed map and want both 
> key and value you need to use the .Iterate function as you do in the 
> example.

That's the reason it exists, of course. Not everything makes sense just with 
elements alone. (A cursor-based iterator is also much more like the existing 
iteration of Ada 95 than the element-based iterators of Ada 2005 and later.)

                          Randy. 



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

end of thread, other threads:[~2019-10-29 21:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-29 13:43 How to Iterate over all elements of a hashed_map Alain De Vos
2019-10-29 14:20 ` Alain De Vos
2019-10-29 15:02   ` joakimds
2019-10-29 21:56     ` Randy Brukardt
2019-10-29 16:48 ` Jeffrey R. Carter

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