From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: How to use associative arrays in Ada 2005?
Date: Fri, 24 Nov 2006 11:49:53 GMT
Date: 2006-11-24T11:49:53+00:00 [thread overview]
Message-ID: <uu00phxnn.fsf@earthlink.net> (raw)
In-Reply-To: 1164328429.5590.61.camel@localhost.localdomain
Georg Bauhaus <bauhaus@futureapps.de> 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;
next prev parent reply other threads:[~2006-11-24 11:49 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-11-21 10:11 How to use associative arrays in Ada 2005? snoopysalive
2006-11-21 11:49 ` Georg Bauhaus
2006-11-21 14:18 ` Matthew Heaney
2006-11-21 23:35 ` snoopysalive
2006-11-23 19:27 ` snoopysalive
2006-11-23 19:40 ` Georg Bauhaus
2006-11-24 0:33 ` Georg Bauhaus
2006-11-24 11:49 ` Matthew Heaney [this message]
2006-11-24 8:27 ` Dmitry A. Kazakov
2006-11-24 11:51 ` Matthew Heaney
2006-11-26 19:05 ` snoopysalive
2006-11-26 20:30 ` Matthew Heaney
2006-11-27 9:15 ` Dmitry A. Kazakov
2006-11-27 19:53 ` Matthew Heaney
2006-11-27 21:11 ` Dmitry A. Kazakov
2006-11-27 21:52 ` Matthew Heaney
2006-11-28 8:29 ` Alex R. Mosteo
2006-11-28 13:19 ` Matthew Heaney
2006-11-28 8:34 ` Dmitry A. Kazakov
2006-11-28 13:21 ` Matthew Heaney
2006-11-27 21:08 ` Simon Wright
2006-11-27 22:22 ` Matthew Heaney
2006-11-27 22:58 ` Simon Wright
2006-11-28 1:55 ` Matthew Heaney
2006-11-24 11:35 ` Matthew Heaney
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox