From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!gandalf.srv.welterde.de!news.jacob-sparre.dk!franka.jacob-sparre.dk!pnx.dk!.POSTED.rrsoftware.com!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: How to Iterate over all elements of a hashed_map. Date: Tue, 29 Oct 2019 16:56:08 -0500 Organization: JSA Research & Innovation Message-ID: References: <6ea868a2-6f4b-413c-955d-08e8735f2880@googlegroups.com> <1559a685-7483-4f2f-8674-d7b61c23bfe8@googlegroups.com> Injection-Date: Tue, 29 Oct 2019 21:56:10 -0000 (UTC) Injection-Info: franka.jacob-sparre.dk; posting-host="rrsoftware.com:24.196.82.226"; logging-data="13784"; mail-complaints-to="news@jacob-sparre.dk" X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.7246 Xref: reader01.eternal-september.org comp.lang.ada:57382 Date: 2019-10-29T16:56:08-05:00 List-Id: 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.