* 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 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-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-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