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=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,693e247f5dca709d X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread4.news.pas.earthlink.net.POSTED!14bb18d8!not-for-mail Sender: Matthew Heaney@MHEANEYIBMT43 Newsgroups: comp.lang.ada Subject: Re: How to use associative arrays in Ada 2005? References: <1164103903.240838.37230@j44g2000cwa.googlegroups.com> <1164152113.623461.130190@e3g2000cwe.googlegroups.com> <1164310051.811802.237400@l12g2000cwl.googlegroups.com> <1164328429.5590.61.camel@localhost.localdomain> From: Matthew Heaney Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Fri, 24 Nov 2006 11:49:53 GMT NNTP-Posting-Host: 4.238.121.243 X-Complaints-To: abuse@earthlink.net X-Trace: newsread4.news.pas.earthlink.net 1164368993 4.238.121.243 (Fri, 24 Nov 2006 03:49:53 PST) NNTP-Posting-Date: Fri, 24 Nov 2006 03:49:53 PST Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: g2news2.google.com comp.lang.ada:7684 Date: 2006-11-24T11:49:53+00:00 List-Id: Georg Bauhaus writes: > I think that in this case the Ada.Containers requirement > of being minimal building blocks applies. Right, the library is designed to be composable. Here's a case where a container needs to contain another container. > If the elements in a map are containers themselves (or are > otherwise big), you may want to use Update_Element. Right. Use Insert to create the element in the outer container, and then use Update_Element to manipulate the inner container. > That is in order to manipulate one of the 2nd level maps, > you could either: Ouch! A lot of copying here... > Or, > > - Get a cursor for the element at key "family name". > - Define a subprogram that manipulates the 2nd level map > (the one containing "name" as key). > - Call Update_Element with the cursor and the subprogram. Right, this is the correct approach. > Ages : Str_Map_Maps.Map; -- That's the "hash of a hash" > Named_Age: Str_Int_Maps.Map; You don't need the Named_Age map. There's a version of Insert that accepts just a key (which is different from the other versions, which accept both key and element). > procedure Set_Age(name: String; value: in out Str_Int_Maps.Map) is > begin > Named_Age.Include("name", Age); -- Note how `Age` is used here Here you can just say: Value.Include ("name", Age); > end Set_Age; > > begin > Ages.Insert("family name", Named_Age); It's probably better to use the insert that returns a cursor, and then reuse that cursor value, since that will obviate the need for a separate Find. You can also eliminate the Name_Age map object, if you use the Insert that accepts a key only. (If the key doesn't already exists, then it inserts an element with a default value. Here that would mean an empty map, which is just what we want.) > Ages.Update_Element(position => Ages.Find("family name"), > process => Set_Age'access); -- in situ But note that Ages.Find duplicates the work of Ages.Insert (since Insert must perform an internal search). If you use an Insert that returns a cursor, then you can just reuse the cursor, instead of Find'ing it again. Your method works but it's less efficient. > end loop; > end book2;