comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: Newbie - HashMap!
Date: Mon, 21 Mar 2005 00:19:30 GMT
Date: 2005-03-21T00:19:30+00:00	[thread overview]
Message-ID: <u3buponou.fsf@earthlink.net> (raw)
In-Reply-To: 1111061037.535775.69180@l41g2000cwc.googlegroups.com

fphsml@gmail.com writes:

> What is the equivalent of the following C++ STL code?
> 
> hash_map<string, string> hm;
> hm["key"] = "value";

Ada doesn't have a user-definable index operator, so to find the Ada
analog of the C++ code fragment above you have to unwind it a bit.  The
code above is equivalent to:

   typedef hash_map<string, string> hm_t;

   hm_t hm;

   typedef hm_t::iterator iter_t;
   typedef pair<iter_t, bool> status_t;

   const hm_t::value_type value(string("key"), string());

   const status_t status = hm.insert(value);

   status.first->second = "value";

The Ada equivalent is:

declare
   package Hashed_Map_Types is
      new Ada.Containers.Indefinite_Hashed_Maps
        (Key_Type     => String,
         Element_Type => String,
         Hash         => Ada.Strings.Hash,
         Equivalent_Keys => "=");

   use Hashed_Map_Types;

   HM : Map;

   C  : Cursor;
   B  : Boolean;
begin
   Insert (HM, "Key", "", C, B);
   Replace_Element (C, By => "Value");
end;

Note that in both the C++ and Ada cases, an element with a default value
is allocated, and then immediately assigned a new value.  It's probably
easier to say instead:

  Include (HM, "Key", "Value");

This inserts value "Value" if "Key" doesn't exist, and replaces the
existing value with "Value" if "Key" does exist.

The operations Insert, Include, and Replace all insert elements, but
vary by their exception behavior.


> I just need simple key lookup. Which library should I use?

You can use a reference implementation of the Ada 2005 standard
container library:

http://charles.tigris.org/source/browse/charles/src/ai302/

You want the indefinite hashed map: a-cihama.ad[sb] .

However, this library is written in Ada 2005, so it might not compile
for you.  (Check with your vendor.)

You could try using the Charles library, which is based on the STL, and
is written in Ada95:

http://charles.tigris.org/source/browse/charles/src/

The file is: charles-maps-hashed-strings-unbounded.ads .

However, the generic formal Element_Type is definite, so you'd probably
have to use an unbounded string:

package Hashed_Map_Types is
  new Charles.Maps.Hashed.Strings.Unbounded 
    (Unbounded_String);  --default everthing else

declare
   HM : Hashed_Map_Types.Container_Type;
begin
   Replace (HM, "Key", To_Unbounded_String ("Value"));
begin



-Matt



      parent reply	other threads:[~2005-03-21  0:19 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
2005-03-21  0:19 ` 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