comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: How to use associative arrays in Ada 2005?
Date: Fri, 24 Nov 2006 11:35:31 GMT
Date: 2006-11-24T11:35:31+00:00	[thread overview]
Message-ID: <uy7q1hybl.fsf@earthlink.net> (raw)
In-Reply-To: 1164310051.811802.237400@l12g2000cwl.googlegroups.com

"snoopysalive" <matthias.kistler@gmx.de> writes:

> And now, my next question: How to handle "hashes of hashes" in Ada?

You have to make two distinct instantiations.


> procedure book is
>     package Str_Int_Maps is
>         new Ada.Containers.Indefinite_Hashed_Maps
>             (String,
>              Integer,
>              Ada.Strings.Hash,
>              "=");
>     use Str_Int_Maps;
>     package Str_Map_Maps is
>         new Ada.Containers.Indefinite_Hashed_Maps
>             (String,
>              Str_Int_Maps.Map,
>              Ada.Strings.Hash,
>              "=");
> 
>     Ages : Str_Map_Maps.Map; -- That's the "hash of a hash"
> 
> begin
>     Ages.Insert("family name",Insert("name",23));
> end book;
> -- Here, the code ends

You have to make two distinct insertions.  The first uses the form of Insert
that inserts a key and a default element value (the element here is itself
another map), and the second uses an in-place update to insert a key/elem pair
into the secondary map.  Something like:

declare
   C : Str_Map_Maps.Cursor;
   B : Boolean;
begin
   -- this is the insert that inserts an element with its
   -- "default value" into the map:
   Ages.Insert ("family name", C, B);  -- first insert

   -- C now designates the map we care about (note that
   -- it doesn't matter what value B has)

   declare
     procedure Update 
      (S : String;  -- the family name
       M : in out Str_Int_Maps.Map) is
     begin
       M.Insert ("name", 23);  -- second insert
     end;
   begin
     Ages.Update_Element (C, Update'Access);
   end;
end;
   

> The statement "Ages.Insert("family name",Insert("name",23));" doesn't
> work. So, how is it possible to do something like this in C++:
> "...
> map<string, map<string,int>> ages;
> ages["family name"]["name"] = 23;
> ..."

Ada doesn't have an index operator, so it's not going to be as concise as the
C++.  If you're doing this a lot then you can always refactor the code above
into its own stand-alone operation.  Something like:

procedure Insert
  (M : in out Str_Map_Maps.Map;
   Family_Name : String;
   Name : String;
   Age  : Integer) is ... -- as above



      parent reply	other threads:[~2006-11-24 11:35 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
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 [this message]
replies disabled

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