comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: Charles container library usage examples
Date: 4 Sep 2005 02:49:20 -0700
Date: 2005-09-04T02:49:20-07:00	[thread overview]
Message-ID: <1125827360.091136.285250@g47g2000cwa.googlegroups.com> (raw)
In-Reply-To: <m3d5np1xwa.fsf@rr.trudgett>


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);




  reply	other threads:[~2005-09-04  9:49 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-03  2:38 Charles container library usage examples David Trudgett
2005-09-03  5:15 ` Matthew Heaney
2005-09-03  9:45   ` Simon Wright
2005-09-04  6:25     ` David Trudgett
2005-09-05 11:28       ` Georg Bauhaus
2005-09-04  0:26   ` David Trudgett
2005-09-04  9:49     ` Matthew Heaney [this message]
2005-09-06  3:01       ` David Trudgett
2005-09-06 16:22         ` Jeffrey Carter
2005-09-07  0:15         ` Matthew Heaney
2005-09-04 17:19     ` Ludovic Brenta
2005-09-06  3:01       ` David Trudgett
2005-09-06  5:08         ` Ludovic Brenta
2005-09-06  6:46           ` David Trudgett
2005-09-06  7:26             ` Ludovic Brenta
2005-09-24  0:05               ` Randy Brukardt
2005-09-09 14:57         ` James Alan Farrell
2005-09-10  7:38           ` David Trudgett
2005-09-10 14:55             ` Matthew Heaney
2005-09-10 15:26               ` Ludovic Brenta
2005-09-10 17:58                 ` Matthew Heaney
2005-09-12  0:24               ` Robert A Duff
2005-09-11 10:52             ` Georg Bauhaus
2005-09-11 21:14               ` David Trudgett
2005-09-13 23:41                 ` Björn Persson
2005-09-14  6:39                   ` David Trudgett
2005-09-12  0:21             ` Robert A Duff
2005-09-12  0:57               ` David Trudgett
2005-09-12  1:01                 ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox