* Newbie - HashMap! @ 2005-03-17 12:03 fphsml 2005-03-17 14:59 ` Dmitry A. Kazakov ` (2 more replies) 0 siblings, 3 replies; 7+ messages in thread From: fphsml @ 2005-03-17 12:03 UTC (permalink / raw) What is the equivalent of the following C++ STL code? hash_map<string, string> hm; hm["key"] = "value"; I saw a sample at http://www.adapower.com/index.php?Command=Class&ClassID=GNAT&CID=240 which looks scary. I just need simple key lookup. Which library should I use? Thanks. ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie - HashMap! 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-21 0:19 ` Matthew Heaney 2 siblings, 1 reply; 7+ messages in thread From: Dmitry A. Kazakov @ 2005-03-17 14:59 UTC (permalink / raw) On 17 Mar 2005 04:03:57 -0800, fphsml@gmail.com wrote: > What is the equivalent of the following C++ STL code? > > hash_map<string, string> hm; > hm["key"] = "value"; > > I saw a sample at > http://www.adapower.com/index.php?Command=Class&ClassID=GNAT&CID=240 > which looks scary. > > I just need simple key lookup. Which library should I use? If that scares you (I don't know why, maybe because of unbounded strings?) then you could try: http://www.dmitry-kazakov.de/ada/tables.htm It uses plain string keys (though it's implementation is not a hash). However, unbounded strings (or pointers to strings) will be needed anyway on the "values" side. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie - HashMap! 2005-03-17 14:59 ` Dmitry A. Kazakov @ 2005-03-18 16:02 ` fphsml 0 siblings, 0 replies; 7+ messages in thread From: fphsml @ 2005-03-18 16:02 UTC (permalink / raw) Scary was too strong a word :-). Just felt it was more verbose than what I was used to. The examples in the responses I got look fine. Thanks all. ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie - HashMap! 2005-03-17 12:03 Newbie - HashMap! fphsml 2005-03-17 14:59 ` Dmitry A. Kazakov @ 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 2 siblings, 2 replies; 7+ messages in thread From: Georg Bauhaus @ 2005-03-17 15:19 UTC (permalink / raw) fphsml@gmail.com wrote: > What is the equivalent of the following C++ STL code? > > hash_map<string, string> hm; > hm["key"] = "value"; Has the hash_map already made it into the C++ STL? I thought it was an extension to be included later? Anyway, package Lookup_tables is new Ada.Containers.Hashed_Maps (Unbounded_String, Unbounded_String, Equivalent_Keys => "=", Hash => Hash); procedure ex is use Lookup_Tables; hm: Map; begin hm.insert(To_Unbounded_String("key"), To_Unbounded_String("value")); end ex; ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie - HashMap! 2005-03-17 15:19 ` Georg Bauhaus @ 2005-03-17 16:42 ` Martin Dowie 2005-03-21 0:33 ` Matthew Heaney 1 sibling, 0 replies; 7+ messages in thread From: Martin Dowie @ 2005-03-17 16:42 UTC (permalink / raw) Georg Bauhaus wrote: > package Lookup_tables is > new Ada.Containers.Hashed_Maps (Unbounded_String, > Unbounded_String, > Equivalent_Keys => "=", > Hash => Hash); > > procedure ex is > > use Lookup_Tables; > > hm: Map; > begin > hm.insert(To_Unbounded_String("key"), To_Unbounded_String("value")); > end ex; Or you could use: Ada.Containers.Indefinite_Hash_Maps (String, String, ...); NB: Haven't tried this myself!... Cheers -- Martin ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie - HashMap! 2005-03-17 15:19 ` Georg Bauhaus 2005-03-17 16:42 ` Martin Dowie @ 2005-03-21 0:33 ` Matthew Heaney 1 sibling, 0 replies; 7+ messages in thread From: Matthew Heaney @ 2005-03-21 0:33 UTC (permalink / raw) 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. ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Newbie - HashMap! 2005-03-17 12:03 Newbie - HashMap! fphsml 2005-03-17 14:59 ` Dmitry A. Kazakov 2005-03-17 15:19 ` Georg Bauhaus @ 2005-03-21 0:19 ` Matthew Heaney 2 siblings, 0 replies; 7+ messages in thread From: Matthew Heaney @ 2005-03-21 0:19 UTC (permalink / raw) 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 ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2005-03-21 0:33 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 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 is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox