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,ce306343df782d85 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!newsread2.news.atl.earthlink.net.POSTED!14bb18d8!not-for-mail Sender: mheaney@MHEANEYX200 Newsgroups: comp.lang.ada Subject: Re: Newbie - HashMap! References: <1111061037.535775.69180@l41g2000cwc.googlegroups.com> 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: Mon, 21 Mar 2005 00:19:30 GMT NNTP-Posting-Host: 24.149.57.125 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.news.atl.earthlink.net 1111364370 24.149.57.125 (Sun, 20 Mar 2005 16:19:30 PST) NNTP-Posting-Date: Sun, 20 Mar 2005 16:19:30 PST Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: g2news1.google.com comp.lang.ada:9652 Date: 2005-03-21T00:19:30+00:00 List-Id: fphsml@gmail.com writes: > What is the equivalent of the following C++ STL code? > > hash_map 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 hm_t; hm_t hm; typedef hm_t::iterator iter_t; typedef pair 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