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,80435549e92d4e0c X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!g47g2000cwa.googlegroups.com!not-for-mail From: "Matthew Heaney" Newsgroups: comp.lang.ada Subject: Re: Charles container library usage examples Date: 4 Sep 2005 02:49:20 -0700 Organization: http://groups.google.com Message-ID: <1125827360.091136.285250@g47g2000cwa.googlegroups.com> References: NNTP-Posting-Host: 165.121.144.196 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1125827365 28171 127.0.0.1 (4 Sep 2005 09:49:25 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 4 Sep 2005 09:49:25 +0000 (UTC) In-Reply-To: User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/103u (KHTML, like Gecko) Safari/100,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: g47g2000cwa.googlegroups.com; posting-host=165.121.144.196; posting-account=Zl1UPAwAAADEsUSm1PMMiDjihtBlZUi_ Xref: g2news1.google.com comp.lang.ada:4426 Date: 2005-09-04T02:49:20-07:00 List-Id: David Trudgett wrote: > > It's seeming to me at the moment that to make a simple data structure > consisting of a hash table of lists, I need to instantiate two generic > packages (requiring two separate files in GNAT, I assume), work out > the necessary WITH and USE clauses, figure out how to declare the > containers, and then put the code in to extract the data from the map > (for which you've given me a useful hint below, in "PROCEDURE OP"). Yes, you need to make two instantiations: one to create a list of integers, and another to create a map of lists. If your list always comprises two elements, then you can use a constrained array instead. Something like: type Integer_Array is array (Positive range <>) of Integer; subtype Integer_Array_Subtype is Integer_Array (1 .. 2); or the simpler: type Integer_Array is (1 .. 2) of Integer; Then you can use that as the generic actual Element_Type in the instantiation of the map (and hence get rid of the list instantiation entirely). > Of course, in this case, I could certainly use an Ada array instead of > the list; and, in fact, I could also use an array instead of a > map. Yes, that's correct. > But I didn't think it was going to be hard to create and use a > map and list in Ada! :-) I'm hoping that it's looking harder than it > really is at the moment, simply because I'm not very conversant with > Ada generics yet. If you're using an Ada 95 compiler, then you'll have to instantiate the container packages at "library level," for subtle reasons. (Things are a lot simpler in Ada 05, and you'll be able to instantiate the containers in a local scope.) > When I tried to instantiate a hashed map, it seemed to want me to > supply a hash function. Then just use an ordered map. Subtype Integer has a built-in less-than operator, so you don't need anything extra to instantiate the ordered map. > OK, thanks for that. That second line ("L :") is particularly useful. For now, it's probably easiest for you to just say: procedure Op (M : Map_Subtype, K : Integer) is L : constant List_Subtype := Element (M, K); begin ... > > This is similar to the C++ code: > > > > void f(const map_t& m) > > { > > const map_t::const_iterator i = m.find (2); > > const list_t& L = *i; > > //... > > } Actually, that should have been: const list_t& L = i.second; //or i->second??? > Unfortunately, I'm unfamiliar with the STL. :-( But I can see the > similarity. The C++ declaration above creates a (const) reference to the map element; it does not make a copy. In your case, you element comprises only a 2-tuple, so you probably don't need to worry about optimizing away copy overhead. (Ada and C++ are low-level, systems programming languages, so we typically have to think about these things.) Hence my advice above, to simply say: L : constant List_Subtype := Element (M, K);