comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: Newbie - HashMap!
Date: Mon, 21 Mar 2005 00:33:08 GMT
Date: 2005-03-21T00:33:08+00:00	[thread overview]
Message-ID: <uwts1n8hn.fsf@earthlink.net> (raw)
In-Reply-To: 42399f5c$0$26540$9b4e6d93@newsread4.arcor-online.net

Georg Bauhaus <bauhaus@futureapps.de> writes:

>    package Lookup_tables is
>       new Ada.Containers.Hashed_Maps (Unbounded_String,
>                                       Unbounded_String,
>                                       Equivalent_Keys => "=",
>                                       Hash => Hash);

Strictly speaking, the C++ example used type std::string, and the Ada
analog of that type is probably Unbounded_String, so what you have above
is technically correct.

However, Ada doesn't have constructors like you have in C++, that
automatically convert a const char* to std::string, so what you have
above is going to be awkward to use.  Therefore, I recommend the
indefinite hashed map instead:

package Lookup_Tables is
  new Ada.Containers.Indefinite_Hashed_Map 
    (String,
     String,
     Hash,
     "=");

Now you can using type String directly:

  Include (HM, "Key", "Value");  --yes: Include, not Insert

Note that this code is probably wrong:

>  procedure ex is
>  
>    use Lookup_Tables;
>  
>    hm: Map;
>  begin
>    hm.insert(To_Unbounded_String("key"), To_Unbounded_String("value"));
>  end ex;


The reason is that the 3-parameter Insert will raise Constraint_Error if
the key is already in the map.  You probably want to use Include
instead, which replaces the old value with the new value, if the key
already exists.  See above.





  parent reply	other threads:[~2005-03-21  0:33 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-17 12:03 Newbie - HashMap! fphsml
2005-03-17 14:59 ` Dmitry A. Kazakov
2005-03-18 16:02   ` fphsml
2005-03-17 15:19 ` Georg Bauhaus
2005-03-17 16:42   ` Martin Dowie
2005-03-21  0:33   ` Matthew Heaney [this message]
2005-03-21  0:19 ` 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