comp.lang.ada
 help / color / mirror / Atom feed
* Ada.Containers.Indefinite_Hashed_Maps
@ 2007-04-25 14:13 markp
  2007-04-25 17:43 ` Ada.Containers.Indefinite_Hashed_Maps Jeffrey R. Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: markp @ 2007-04-25 14:13 UTC (permalink / raw)


I am using a hash table via Ada.Containers.Indefinite_Hashed_Maps to
hash an array of records. We have a predfined array of records called
X. The insert function works fine by passing in X(1), X(2), etc. I am
having trouble with the syntax of the Update call, specifically the
Process parameter. Could somebody provide a quick snippet of code that
show how to setup this code and how the procedure actually looks to
update the data?

Thanks a lot!




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada.Containers.Indefinite_Hashed_Maps
  2007-04-25 14:13 Ada.Containers.Indefinite_Hashed_Maps markp
@ 2007-04-25 17:43 ` Jeffrey R. Carter
  2007-04-26 12:12   ` Ada.Containers.Indefinite_Hashed_Maps Maciej Sobczak
  2007-04-26 15:52 ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney
  2007-04-26 16:12 ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney
  2 siblings, 1 reply; 9+ messages in thread
From: Jeffrey R. Carter @ 2007-04-25 17:43 UTC (permalink / raw)


markp wrote:
> I am using a hash table via Ada.Containers.Indefinite_Hashed_Maps to
> hash an array of records. We have a predfined array of records called
> X. The insert function works fine by passing in X(1), X(2), etc. I am
> having trouble with the syntax of the Update call, specifically the
> Process parameter. Could somebody provide a quick snippet of code that
> show how to setup this code and how the procedure actually looks to
> update the data?

There is no update; I presume you mean Update_Element.

Given a map:

package Maps is new Ada.Containers.Indefinite_Hashed_Maps
    (Key_Type => Key_Value, Element_Type => Integer, ...);

procedure Process (Key : in Key_Value; Item : in out Integer) is
    -- null;
begin -- Process
    if Some_Quality (Key) then
       Item := Item + 1;
    end if;
end Process;

Map      : Maps.Map;
Some_Key : Key_Value;
Position : Maps.Cursor;

-- Put some values in Map.

Some_Key := Get;

if Maps.Contains (Map, Some_Key) then
    Position := Maps.Find (Map, Key => Some_Key);
    Maps.Update_Element (Container => Map,
                         Position  => Position,
                         Process   => Process'access);
end if;

If you've put a value in Map for the key with the value in Some_Key, and 
Some_Quality (Some_Key) returns True, then this will increment the value 
associated with that key value.

For this kind of situation, you'd probably use Element and Replace 
rather than Find and Update_Element. Update_Element is more useful when 
you've got a cursor without knowing the key value, and want to modify 
the corresponding element.

-- 
Jeff Carter
"Apart from the sanitation, the medicine, education, wine,
public order, irrigation, roads, the fresh water system,
and public health, what have the Romans ever done for us?"
Monty Python's Life of Brian
80



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada.Containers.Indefinite_Hashed_Maps
  2007-04-25 17:43 ` Ada.Containers.Indefinite_Hashed_Maps Jeffrey R. Carter
@ 2007-04-26 12:12   ` Maciej Sobczak
  2007-04-26 12:55     ` Ada.Containers.Indefinite_Hashed_Maps Stefan Bellon
  2007-04-27  4:39     ` Ada.Containers.Indefinite_Hashed_Maps Jeffrey R. Carter
  0 siblings, 2 replies; 9+ messages in thread
From: Maciej Sobczak @ 2007-04-26 12:12 UTC (permalink / raw)


Jeffrey R. Carter wrote:

> if Maps.Contains (Map, Some_Key) then
>    Position := Maps.Find (Map, Key => Some_Key);

You do the same key search once for Contains and once for Find. That 
gives twice the same work. Is there a way to make the search once?

-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada.Containers.Indefinite_Hashed_Maps
  2007-04-26 12:12   ` Ada.Containers.Indefinite_Hashed_Maps Maciej Sobczak
@ 2007-04-26 12:55     ` Stefan Bellon
  2007-04-26 16:13       ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney
  2007-04-27  4:39     ` Ada.Containers.Indefinite_Hashed_Maps Jeffrey R. Carter
  1 sibling, 1 reply; 9+ messages in thread
From: Stefan Bellon @ 2007-04-26 12:55 UTC (permalink / raw)


Maciej Sobczak wrote:

> Jeffrey R. Carter wrote:
> 
> > if Maps.Contains (Map, Some_Key) then
> >    Position := Maps.Find (Map, Key => Some_Key);
> 
> You do the same key search once for Contains and once for Find. That 
> gives twice the same work. Is there a way to make the search once?

   Position := Maps.Find (Map, Key => Some_Key);
   if Position /= Maps.No_Element then
      ...

Untested, but this is how I understand it.

-- 
Stefan Bellon



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada.Containers.Indefinite_Hashed_Maps
  2007-04-25 14:13 Ada.Containers.Indefinite_Hashed_Maps markp
  2007-04-25 17:43 ` Ada.Containers.Indefinite_Hashed_Maps Jeffrey R. Carter
@ 2007-04-26 15:52 ` Matthew Heaney
  2007-04-26 19:56   ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney
  2007-04-26 16:12 ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney
  2 siblings, 1 reply; 9+ messages in thread
From: Matthew Heaney @ 2007-04-26 15:52 UTC (permalink / raw)


On Apr 25, 10:13 am, markp <markwor...@yahoo.com> wrote:
> I am using a hash table via Ada.Containers.Indefinite_Hashed_Maps to
> hash an array of records.

What do you mean by "hash an array of records"?  Does the data live
inside an array, or inside a map?  What is the key type?  What is the
element type?


> We have a predfined array of records called
> X. The insert function works fine by passing in X(1), X(2), etc.

What is the map key type?

Are you inserting an array component value into the map?  Does this
mean the data is getting stored twice (first as an array element, and
again as a map element)?


> I am
> having trouble with the syntax of the Update call, specifically the
> Process parameter.

Update_Element works fine if you already have a cursor, and you want
to modify an element in-place.  Something like:

procedure My_Update (M : in out Map; C : Cursor) is
   procedure Process (K : KT; E : in out ET) is
   begin
      ... -- modify E as necessary
   end;
begin
   M.Update_Element (C);
end;

If you want to simply replace what's there (instead of modifying the
element in-place), then you can use Replace_Element:

M.Replace_Element (C, E);

If you don't have a cursor, then you can use Insert:

M.Insert (K, E);

Note that Insert will raise an exception if K is already in the map.

You could also use Replace:

M.Replace (K, E);

Note that Replace will raise an exception if K is not in the map.

You could also use Include:

M.Include (K. E);

Note that Include does not raise an exception.  If K is in the map,
then E will replace what's already there, and if K is not in the map,
then E will be inserted.

There is also a conditional form of Insert, that does not raise an
exception.  It reports back (with a status value) whether the key/
element pair was actually inserted ("successful" insertion depends on
whether the key was already in the map).




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada.Containers.Indefinite_Hashed_Maps
  2007-04-25 14:13 Ada.Containers.Indefinite_Hashed_Maps markp
  2007-04-25 17:43 ` Ada.Containers.Indefinite_Hashed_Maps Jeffrey R. Carter
  2007-04-26 15:52 ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney
@ 2007-04-26 16:12 ` Matthew Heaney
  2 siblings, 0 replies; 9+ messages in thread
From: Matthew Heaney @ 2007-04-26 16:12 UTC (permalink / raw)


On Apr 25, 10:13 am, markp <markwor...@yahoo.com> wrote:
> I am using a hash table via Ada.Containers.Indefinite_Hashed_Maps to
> hash an array of records. We have a predfined array of records called
> X. The insert function works fine by passing in X(1), X(2), etc. I am
> having trouble with the syntax of the Update call, specifically the
> Process parameter. Could somebody provide a quick snippet of code that
> show how to setup this code and how the procedure actually looks to
> update the data?

Given map M and cursor C you would say:

declare
   procedure Process (K : KT; E : in out ET) is
   begin
      ... -- modify E as necessary
   end;
begin
   M.Update_Element (C, Process'Access);
end;





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada.Containers.Indefinite_Hashed_Maps
  2007-04-26 12:55     ` Ada.Containers.Indefinite_Hashed_Maps Stefan Bellon
@ 2007-04-26 16:13       ` Matthew Heaney
  0 siblings, 0 replies; 9+ messages in thread
From: Matthew Heaney @ 2007-04-26 16:13 UTC (permalink / raw)


On Apr 26, 8:55 am, Stefan Bellon <sbel...@sbellon.de> wrote:
>
>    Position := Maps.Find (Map, Key => Some_Key);
>    if Position /= Maps.No_Element then

Or:

   C := M.Find (K);
   if Has_Element (C) then ... ;





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada.Containers.Indefinite_Hashed_Maps
  2007-04-26 15:52 ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney
@ 2007-04-26 19:56   ` Matthew Heaney
  0 siblings, 0 replies; 9+ messages in thread
From: Matthew Heaney @ 2007-04-26 19:56 UTC (permalink / raw)


On Apr 26, 11:52 am, Matthew Heaney <mhea...@on2.com> wrote:
>
> Update_Element works fine if you already have a cursor, and you want
> to modify an element in-place.  Something like:
>
> procedure My_Update (M : in out Map; C : Cursor) is
>    procedure Process (K : KT; E : in out ET) is
>    begin
>       ... -- modify E as necessary
>    end;
> begin
>    M.Update_Element (C);
> end;

Oops!  Of course I meant:

M.Update_Element (C, Process'Access);




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ada.Containers.Indefinite_Hashed_Maps
  2007-04-26 12:12   ` Ada.Containers.Indefinite_Hashed_Maps Maciej Sobczak
  2007-04-26 12:55     ` Ada.Containers.Indefinite_Hashed_Maps Stefan Bellon
@ 2007-04-27  4:39     ` Jeffrey R. Carter
  1 sibling, 0 replies; 9+ messages in thread
From: Jeffrey R. Carter @ 2007-04-27  4:39 UTC (permalink / raw)


Maciej Sobczak wrote:
> 
> You do the same key search once for Contains and once for Find. That 
> gives twice the same work. Is there a way to make the search once?

As it was a quick example about using Update_Element, I didn't really 
care. In real code I might pay that kind of thing more attention.

-- 
Jeff Carter
"I fart in your general direction."
Monty Python & the Holy Grail
05



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-04-27  4:39 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-25 14:13 Ada.Containers.Indefinite_Hashed_Maps markp
2007-04-25 17:43 ` Ada.Containers.Indefinite_Hashed_Maps Jeffrey R. Carter
2007-04-26 12:12   ` Ada.Containers.Indefinite_Hashed_Maps Maciej Sobczak
2007-04-26 12:55     ` Ada.Containers.Indefinite_Hashed_Maps Stefan Bellon
2007-04-26 16:13       ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney
2007-04-27  4:39     ` Ada.Containers.Indefinite_Hashed_Maps Jeffrey R. Carter
2007-04-26 15:52 ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney
2007-04-26 19:56   ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney
2007-04-26 16:12 ` Ada.Containers.Indefinite_Hashed_Maps Matthew Heaney

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