comp.lang.ada
 help / color / mirror / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* Re: Non-standard functions in GNAT's Ada.Containers packages?
  @ 2022-09-16 18:53  5%               ` Björn Lundin
  0 siblings, 0 replies; 65+ results
From: Björn Lundin @ 2022-09-16 18:53 UTC (permalink / raw)


On 2022-09-16 17:00, Marius Amado-Alves wrote:
>>>> "for X of M loop ... end loop".
>>>
>>> Not possible for maps.
>> but you can as
>>> https://programming-idioms.org/idiom/13/iterate-over-map-keys-and-value/1511/ada>
>>
>> with Ada.Containers.Indefinite_Hashed_Maps;
>> with Ada.Strings.Hash;
>> use Ada.Containers;
>> for C in My_Map.Iterate loop
>> Put_Line ("Key = " & Key (C) & ", Value = " & Element (C));
>> end loop;
> 
> Thanks, but this is "in", not "of", requires cursors, and DOES NOT COMPILE, as probably needs like ten lines of boiler plate not show.

well, yes . I thought the for looping stuff was the important part, 
since you done want to call Next.

in or of - does it really matter here?

   for C in My_Map.Iterate loop
or
   for C of My_Map loop
is not that hurtful to my eyes.

   for K,V in My_Map.Iterate loop
or
   for K,V of My_Map loop

getting Key and Value as a tuple would be nicer of course


Anyway, I forgot to instantiate the map. Here's a compileable variant



It outputs
Key = AA, Value =  1
Key = AB, Value =  5

----------------

with Ada.Containers.Hashed_Maps;
with Ada.Strings.Hash;
use Ada.Containers;
with Text_Io ; use Text_IO;

procedure Ite is

   subtype Test_Type is String(1..2);

   package Test_Map_Pkg is new Ada.Containers.Hashed_Maps
     (Test_Type,
      Integer,
      Ada.Strings.Hash,
      "=",
      "=");

   My_Map : Test_Map_Pkg.Map;
   use Test_Map_Pkg;
begin
   My_Map.Insert("AA",1);
   My_Map.Insert("AB",5);

   for C in My_Map.Iterate loop
     Put_Line ("Key = " & Key (C) & ", Value = " & Element (C)'Img);
   end loop;

end Ite;



-- 
/Björn

^ permalink raw reply	[relevance 5%]

* Re: How to Iterate over all elements of a hashed_map.
  2019-10-29 13:43  6% How to Iterate over all elements of a hashed_map Alain De Vos
@ 2019-10-29 16:48  0% ` Jeffrey R. Carter
  0 siblings, 0 replies; 65+ results
From: Jeffrey R. Carter @ 2019-10-29 16:48 UTC (permalink / raw)


On 10/29/19 2:43 PM, Alain De Vos wrote:
> 
>     package My_Hash is new Ada.Containers.Hashed_Maps (Key_Type => Unbounded_String,
>         Element_Type => Unbounded_String,
>         Hash => Hash_Func,
>         Equivalent_Keys => Equivalent_Key);
> 
> But now I want to iterate over all elements of this Hash and print the keys and items ?

My_Hash.Iterate?

-- 
Jeff Carter
"You cheesy lot of second-hand electric donkey-bottom biters."
Monty Python & the Holy Grail
14


^ permalink raw reply	[relevance 0%]

* How to Iterate over all elements of a hashed_map.
@ 2019-10-29 13:43  6% Alain De Vos
  2019-10-29 16:48  0% ` Jeffrey R. Carter
  0 siblings, 1 reply; 65+ results
From: Alain De Vos @ 2019-10-29 13:43 UTC (permalink / raw)


I want to use an "associative array" , and have some code for a "string -> string" hash,

with Ada.Containers.Hashed_Maps;
----BEGIN MYHASH
   function Equivalent_Key (Left, Right : Unbounded_String) return Boolean is
   begin
      return Left = Right;
   end Equivalent_Key;
   function Hash_Func (Key : Unbounded_String) return Ada.Containers.Hash_Type is
   begin
      return Ada.Strings.Hash (To_String (Key));
   end Hash_Func;
   package My_Hash is new Ada.Containers.Hashed_Maps (Key_Type => Unbounded_String,
       Element_Type => Unbounded_String,
       Hash => Hash_Func,
       Equivalent_Keys => Equivalent_Key);
----END MYHASH
declare
Hash : My_Hash.Map;
begin
Hash.Insert ( Key => ... , New_Item => ...)
Hash.Insert ( Key => ... , New_Item => ...)

But now I want to iterate over all elements of this Hash and print the keys and items ?


^ permalink raw reply	[relevance 6%]

* Re: fyi, GNAT and SPARK GPL 2016 are out
  @ 2016-06-04 16:13  5% ` gautier_niouzes
  0 siblings, 0 replies; 65+ results
From: gautier_niouzes @ 2016-06-04 16:13 UTC (permalink / raw)


Nice release - with new optimizations in the run-time library... and a nice banana skin. In some cases, an application working well with GNAT for years may crash with a nasty raised PROGRAM_ERROR : EXCEPTION_ACCESS_VIOLATION - but only in a typical release mode; in a typical debug mode it works as before.

In a nutshell, if you are using Ada.Containers.*Maps, and have code like the P_KO procedure below, which is legal Ada 2005 & 2012, the executable will bomb when built with the -gnatp switch.

The cure is to use a cursor like in P_OK. It removes an exception part as well.

Is there an inclusion of pragma Suppress(Container_Checks) into the standard on its way ? Then the remarks such as A.18.4, 69/2 could be updated accordingly.
_________________________ 
Gautier's Ada programming 
http://gautiersblog.blogspot.com/search/label/Ada 
NB: follow the above link for a valid e-mail address 

-----------8<-----------8<-----------8<-----------8<-------

--  GNAT GPL 2016 pragma, suppression included in -gnatp switch
pragma Suppress(Container_Checks); 

with Ada.Strings.Unbounded.Hash;             use Ada.Strings.Unbounded;
with Ada.Containers.Hashed_Maps;
with Ada.Text_IO; use Ada.Text_IO;

procedure Test_2016 is

  package T_Dic is new Ada.Containers.Hashed_Maps
    (Key_Type        => Ada.Strings.Unbounded.Unbounded_String,
     Element_Type    => Positive,
     Hash            => Ada.Strings.Unbounded.Hash,
     Equivalent_Keys => Ada.Strings.Unbounded."=");
  
  dic: T_Dic.Map;
  n: Positive:= 1;
  
  procedure P_KO(s: String) is
    i: Integer;
  begin
    i:= dic.Element(To_Unbounded_String(s));
    Put_Line("Key found, element=" & Integer'Image(i));
  exception
    when Constraint_Error =>  --  A.18.4, 69/2
      Put_Line("Key not found");
      dic.Insert(To_Unbounded_String(s), n);
      n:= n + 1;
  end P_KO;
  
  procedure P_OK(s: String) is
    i: Integer;
    use T_Dic;
    curs: Cursor;
  begin
    curs:= dic.Find(To_Unbounded_String(s));
    if curs = No_Element then
      Put_Line("Key not found");
      dic.Insert(To_Unbounded_String(s), n);
      n:= n + 1;
    else
      i:= Element(curs);
      Put_Line("Key found, element=" & Integer'Image(i));
    end if;
  end P_OK;

begin
  P_OK("A");
  P_OK("A");
  P_KO("B");
  P_KO("B");
end;

^ permalink raw reply	[relevance 5%]

* Re: Two approaches of iterators for the key-value pairs
  2015-11-27 19:38  0%   ` ytomino
@ 2015-11-27 23:11  0%     ` Brad Moore
  0 siblings, 0 replies; 65+ results
From: Brad Moore @ 2015-11-27 23:11 UTC (permalink / raw)


On Friday, November 27, 2015 at 12:38:27 PM UTC-7, ytomino wrote:
> Thank you, Brad. I'm glad your reply!
> 
> > I'm not sure what is being referred to here as a "boolean trick". Perhaps you are referring to the mention of "Rosen Trick", which as Randy points out, should probably be called "Rosen Technique". However that has nothing to do with "Boolean". The Rosen technique is a coding technique that allows one to treat a read only "in" parameter as a modifiable "in out" parameter.
> 
> I called the below point as "boolean trick".
> 
>     type Entry_Presence is new Boolean; -- the "cursor" type
>     function Has_Element
>       (EP : Entry_Presence) return Boolean is (Boolean (EP));
> 

OK, I see what you mean now.
If we were to go forward with this proposal, I think the type should probably be called Cursor instead of Entry_Presence, (to be consistent with other Container types), and it should be a private type. The fact that its just a boolean type is an implementation detail, and of no concern to the user's of the abstraction.

> It's interesting for me because I had tried to implement the iterator for Ada.Environment_Variables.
> Sorry a personal matter, and please excuse the difference from AI because I wrote this code with my image before the specific content has not been written into AI.
> 
> https://github.com/ytomino/drake/blob/master/source/environment/a-envvar.ads
> https://github.com/ytomino/drake/blob/master/source/environment/machine-apple-darwin/s-naenva.ads
> 
> I made Cursor as pointer in the environment-block(envp), and used address-arithmetic to scan it.
> So, "Cursor is new Boolean" is shocking for me.
> 
> Thanks to teach "Rosen Trick".
> I has known this technique, but not known the name of the technique.
> 
> > > A loop for Ada.Environment_Variables would be like below, according to this AI:
> > > 
> > > for E *of* Ada.Environment_Variables.All_Variables loop
> > >    -- E is Iterator_Element (Name_Value_Pair_Type)
> > >    Name (E) -- key
> > >    Value (E) -- value
> > > end loop;
> > > 
> > > On the other hand, as we already know, a loop for Ada.Containers.Hashed_Maps/Ordered_Maps:
> > > 
> > > for I *in* The_Map_Object.Iterate loop
> > >    -- I is Cursor
> > >    Key (E) -- key
> > >    Element (E), Reference (E).Element.all -- value
> > > end loop;
> > 
> > All the existing containers are iterable containers, so they support
> > both using "of" and "in" syntax for loops. Using "of" is generally preferable
> > because cursors are implicit, and the code written by the user is therefore simpler. Using "in" syntax can also be used by obtaining an Iterator object, but this gives you a cursor instead of a container element, so you typically would need to write some extra calls to get to the container element from the cursor object.
> > 
> > 
> > This code excerpt you had above would not compile, so it is probably confusing people.
> > It think it would be better to perhaps show a full simple working example, such as incrementing each element of the container;
> > Here I show both forms of loop using a Hashed_Map container. As you can see,
> > the "of" format is simpler, and hopefully easier to read and understand.
> > 
> > with Ada.Containers.Hashed_Maps; use Ada;
> > procedure Test_Iterator is
> >    
> >    type Student_Id is new Positive;
> >    type Grade is new Natural range 0 .. 100;
> >    function Hash (Student : Student_Id) return Containers.Hash_Type is
> >      (Containers.Hash_Type (Student));
> >    
> >    package Maps is new Ada.Containers.Hashed_Maps 
> >      (Key_Type        => Student_Id,
> >       Element_Type    => Grade,
> >       Hash            => Hash,
> >       Equivalent_Keys => "=");
> > 
> >    School : Maps.Map;
> >    
> > begin
> > 
> >    School.Insert (Key      => 123456,    -- Student A
> >                   New_Item => 90);
> >    School.Insert (Key      => 789123,    -- Student B
> >                   New_Item => 65);
> > 
> >    -- Iterate using "in" syntax (for an iterator)
> >    for Cursor in School.Iterate loop
> >       School.Replace (Key => Maps.Key (Cursor), 
> >                       New_Item => Maps.Element (Cursor) + 1);
> >    end loop;
> > 
> >    -- Iterate using "of" syntax (for an iterable containter)
> >    for Grade of School loop
> >       Grade := Grade + 1;
> >    end loop;
> > 
> >    
> > end Test_Iterator;
> 
> Thanks for writing the example.
> However, I feel it's unfair because Hashed_Maps has Variable_Indexing.
> 
>    -- Iterate using "in" syntax (for an iterator)
>    for Cursor in School.Iterate loop
>       School (Cursor) := School (Cursor) + 1;
>    end loop;

Good point..., a better way to write this using an Iterator.
> 
> > For the AI for Ada 202x on Ada.Directories and Ada.Environment_Variables,
> > this is a work in progress, so it may end up looking very different in the end, or may even not make the cut for Ada 202x.
> > 
> > Essentially, the design choice discussed in the AI is whether to expose an Iterable Container type, or just an Iterator type.
> 
> Sorry, I didn't have the intention to interrupt the discussion of AI.
> I already has made some my iterators with self‐taught. So I want to know the preference of people.
> I don't want to affect your work... Of course, I'm looking forward to the complete of this AI.

Getting feedback on the preferences of people could influence the direction that the AI takes, and ultimately we want to make the best decisions, so its good to hear the feedback.

>  
> > Both choices could be made to have the same interface for the user, with the only difference being whether "in" or "of" appears in the loop.
> 
> Yes.
> (And, Hashed_Maps/Ordered_Maps already use "in". So I felt "in" is suitable.)

Not sure what you mean here. Hashed_Maps/Ordered_Maps also already uses "of", 
you can use either today, so how is one more suitable than the other?
I think the "of" form involves less clutter that that user has to write, so I would generally try to use that, unless you really need a Cursor somewhere in the loop.

> 
> > If we expose an Iterable container though, that also gives the ability to use "in" syntax since an Iterable container is associated with an Iterator type, and that Iterator type could be used in the loop. However, in that case, as with other existing standard container types and as seen in my example above, using the "in" form would be a bit more awkward to use, compared to the "of" form, so I suspect most people would just use "of".
> 
> Well...
> 
> > So the question really is, if we add Ada 2012 Iterator support to Ada.Directories and/or Ada.Environment_Variables, should the design be to 
> > expose just an Iterator type, or both an Iterator Type, and an Iterable container type?
> > 
> > My thought is that the latter might be better mostly because it would be more consistent with the other standard container types, but I could go either way.
> 
> If I were to venture my opinion, the former is better to write a generic subprogram:
> 
>   generic
>      type Cursor is private;
>      with package Iterator_Interfaces is new Ada.Iterator_Interfaces (Cursor);
>      with function Key (P : Cursor) return String;
>      with function Element (P : Cursor) return String;
>   procedure My_Logic (...);
> 
> This generic subprogram can be compatible with Hashed_Maps/Ordered_Maps and Environment_Variables.
> Conversely, the latter is acceptable if Hashed_Maps/Ordered_Maps expose Key_Element_Pair_Type.
> 
> > A secondary question might be for anyone creating such abstractions of their own, are there cases where creating an Iterator type makes better sense than creating an Iterable container type?  The ACATS test involving the Prime number iterator could be an example of such an iterator type, but note I could have written that also as an Iterable Container type instead.
> > 
> > In other words, can general guidance be given about how to choose an approach, or does it not really matter, and the writer of such abstractions should choose which ever approach they fancy? 
> 
> I want to know it, too.
> I sometime try to write the generic package like below:

My sense is that if you dont need a container as part of the abstraction, then perhaps it's better to just provide an Iterator type. All the existing containers already were containers, so for those it made sense to make them Iterable containers.  Ada.Directories and Ada.Environment_Variables do not currently have Containers associated with them, so perhaps an argument can be made that they should be Iterators, rather than concoct a Container type so that Iterable container syntax can be used.

Brad

> 
>   generic
>      type Input_Cursor is private;
>      with package Input_Iterator_Interfaces is new Ada.Iterator_Interfaces (Input_Cursor);
>      Input_Iterator : Input_Iterator_Interfaces.Forward_Iterator'Class;
>      type Element_Type (<>) is limited private;
>      with function Element (Position : Input_Cursor) return Element_Type;
>   package My_Filter is
>      type Output_Cursor is private;
>      package Output_Iterator_Interfaces is new Ada.Iterator_Interfaces (Output_Cursor);
>      Output_Iterator : Output_Iterator_Interfaces.Forward_Iterator'Class := ...;
>      ...
> 
> But, I can't get a sense of satisfaction.
> 
> Thanks.

^ permalink raw reply	[relevance 0%]

* Re: Two approaches of iterators for the key-value pairs
  2015-11-27 18:08  0%   ` ytomino
@ 2015-11-27 20:50  0%     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 65+ results
From: Dmitry A. Kazakov @ 2015-11-27 20:50 UTC (permalink / raw)


On Fri, 27 Nov 2015 10:08:23 -0800 (PST), ytomino wrote:

> On Saturday, November 28, 2015 at 1:30:23 AM UTC+9, Dmitry A. Kazakov wrote:
>> On Fri, 27 Nov 2015 07:25:57 -0800 (PST), ytomino wrote:
>> 
>>> Hello, I'm reading AI12-0009-1 "Iterators for Directories and Environment_Variables".
>>> http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0009-1.txt?rev=1.4
>>> 
>>> And there is interesting difference between the new iterator of Ada.Environment_Variables and the existing iterators.
>>> (The boolean trick of these new iterators is also interesting, but set aside.)
>>> 
>>> A loop for Ada.Environment_Variables would be like below, according to this AI:
>>> 
>>> for E *of* Ada.Environment_Variables.All_Variables loop
>>>    -- E is Iterator_Element (Name_Value_Pair_Type)
>>>    Name (E) -- key
>>>    Value (E) -- value
>>> end loop;
>>> 
>>> On the other hand, as we already know, a loop for Ada.Containers.Hashed_Maps/Ordered_Maps:
>>> 
>>> for I *in* The_Map_Object.Iterate loop
>>>    -- I is Cursor
>>>    Key (E) -- key
>>>    Element (E), Reference (E).Element.all -- value
>>> end loop;
>>> 
>>> If you create new iterator for some key-value pairs, which approach do you like?
>> 
>> Rather this:
>> 
>> for Variable in All_Variables loop
>>    Put_Line (Variable.Name & "=" & Variable.Value);
>> end loop;
> 
> It sounds good.
> 
> However, the Cursor probably have to be tagged to implement it for
> dot-notation, and, I think it will be an double-dispatching error on
> First/Next because the iterator is also tagged.

Why should it be cursor? As Pascal said, Variable is a plain record type,
the element of the container (seen as a set).

And of course indices must work too:

   for Key in All_Variables.Keys loop
      Put_Line (Key & "=" & All_Variables (Key));
   end loop;

'Range instead of .Keys would be even better, but it is probably too much
to ask.

> I wish dot-notation for non-tagged type is allowed (with pragma?)

There is already mechanism for this:

   function Add (X, Y : T) return T;
   function "+" (X, Y : T) return T renames Add;

This is not any different to

   function Name (X : T) return String;
   function ".Name" (X : T) return String renames Name;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[relevance 0%]

* Re: Two approaches of iterators for the key-value pairs
  2015-11-27 17:43  4% ` Brad Moore
@ 2015-11-27 19:38  0%   ` ytomino
  2015-11-27 23:11  0%     ` Brad Moore
  0 siblings, 1 reply; 65+ results
From: ytomino @ 2015-11-27 19:38 UTC (permalink / raw)


Thank you, Brad. I'm glad your reply!

> I'm not sure what is being referred to here as a "boolean trick". Perhaps you are referring to the mention of "Rosen Trick", which as Randy points out, should probably be called "Rosen Technique". However that has nothing to do with "Boolean". The Rosen technique is a coding technique that allows one to treat a read only "in" parameter as a modifiable "in out" parameter.

I called the below point as "boolean trick".

    type Entry_Presence is new Boolean; -- the "cursor" type
    function Has_Element
      (EP : Entry_Presence) return Boolean is (Boolean (EP));

It's interesting for me because I had tried to implement the iterator for Ada.Environment_Variables.
Sorry a personal matter, and please excuse the difference from AI because I wrote this code with my image before the specific content has not been written into AI.

https://github.com/ytomino/drake/blob/master/source/environment/a-envvar.ads
https://github.com/ytomino/drake/blob/master/source/environment/machine-apple-darwin/s-naenva.ads

I made Cursor as pointer in the environment-block(envp), and used address-arithmetic to scan it.
So, "Cursor is new Boolean" is shocking for me.

Thanks to teach "Rosen Trick".
I has known this technique, but not known the name of the technique.

> > A loop for Ada.Environment_Variables would be like below, according to this AI:
> > 
> > for E *of* Ada.Environment_Variables.All_Variables loop
> >    -- E is Iterator_Element (Name_Value_Pair_Type)
> >    Name (E) -- key
> >    Value (E) -- value
> > end loop;
> > 
> > On the other hand, as we already know, a loop for Ada.Containers.Hashed_Maps/Ordered_Maps:
> > 
> > for I *in* The_Map_Object.Iterate loop
> >    -- I is Cursor
> >    Key (E) -- key
> >    Element (E), Reference (E).Element.all -- value
> > end loop;
> 
> All the existing containers are iterable containers, so they support
> both using "of" and "in" syntax for loops. Using "of" is generally preferable
> because cursors are implicit, and the code written by the user is therefore simpler. Using "in" syntax can also be used by obtaining an Iterator object, but this gives you a cursor instead of a container element, so you typically would need to write some extra calls to get to the container element from the cursor object.
> 
> 
> This code excerpt you had above would not compile, so it is probably confusing people.
> It think it would be better to perhaps show a full simple working example, such as incrementing each element of the container;
> Here I show both forms of loop using a Hashed_Map container. As you can see,
> the "of" format is simpler, and hopefully easier to read and understand.
> 
> with Ada.Containers.Hashed_Maps; use Ada;
> procedure Test_Iterator is
>    
>    type Student_Id is new Positive;
>    type Grade is new Natural range 0 .. 100;
>    function Hash (Student : Student_Id) return Containers.Hash_Type is
>      (Containers.Hash_Type (Student));
>    
>    package Maps is new Ada.Containers.Hashed_Maps 
>      (Key_Type        => Student_Id,
>       Element_Type    => Grade,
>       Hash            => Hash,
>       Equivalent_Keys => "=");
> 
>    School : Maps.Map;
>    
> begin
> 
>    School.Insert (Key      => 123456,    -- Student A
>                   New_Item => 90);
>    School.Insert (Key      => 789123,    -- Student B
>                   New_Item => 65);
> 
>    -- Iterate using "in" syntax (for an iterator)
>    for Cursor in School.Iterate loop
>       School.Replace (Key => Maps.Key (Cursor), 
>                       New_Item => Maps.Element (Cursor) + 1);
>    end loop;
> 
>    -- Iterate using "of" syntax (for an iterable containter)
>    for Grade of School loop
>       Grade := Grade + 1;
>    end loop;
> 
>    
> end Test_Iterator;

Thanks for writing the example.
However, I feel it's unfair because Hashed_Maps has Variable_Indexing.

   -- Iterate using "in" syntax (for an iterator)
   for Cursor in School.Iterate loop
      School (Cursor) := School (Cursor) + 1;
   end loop;

> For the AI for Ada 202x on Ada.Directories and Ada.Environment_Variables,
> this is a work in progress, so it may end up looking very different in the end, or may even not make the cut for Ada 202x.
> 
> Essentially, the design choice discussed in the AI is whether to expose an Iterable Container type, or just an Iterator type.

Sorry, I didn't have the intention to interrupt the discussion of AI.
I already has made some my iterators with self‐taught. So I want to know the preference of people.
I don't want to affect your work... Of course, I'm looking forward to the complete of this AI.
 
> Both choices could be made to have the same interface for the user, with the only difference being whether "in" or "of" appears in the loop.

Yes.
(And, Hashed_Maps/Ordered_Maps already use "in". So I felt "in" is suitable.)

> If we expose an Iterable container though, that also gives the ability to use "in" syntax since an Iterable container is associated with an Iterator type, and that Iterator type could be used in the loop. However, in that case, as with other existing standard container types and as seen in my example above, using the "in" form would be a bit more awkward to use, compared to the "of" form, so I suspect most people would just use "of".

Well...

> So the question really is, if we add Ada 2012 Iterator support to Ada.Directories and/or Ada.Environment_Variables, should the design be to 
> expose just an Iterator type, or both an Iterator Type, and an Iterable container type?
> 
> My thought is that the latter might be better mostly because it would be more consistent with the other standard container types, but I could go either way.

If I were to venture my opinion, the former is better to write a generic subprogram:

  generic
     type Cursor is private;
     with package Iterator_Interfaces is new Ada.Iterator_Interfaces (Cursor);
     with function Key (P : Cursor) return String;
     with function Element (P : Cursor) return String;
  procedure My_Logic (...);

This generic subprogram can be compatible with Hashed_Maps/Ordered_Maps and Environment_Variables.
Conversely, the latter is acceptable if Hashed_Maps/Ordered_Maps expose Key_Element_Pair_Type.

> A secondary question might be for anyone creating such abstractions of their own, are there cases where creating an Iterator type makes better sense than creating an Iterable container type?  The ACATS test involving the Prime number iterator could be an example of such an iterator type, but note I could have written that also as an Iterable Container type instead.
> 
> In other words, can general guidance be given about how to choose an approach, or does it not really matter, and the writer of such abstractions should choose which ever approach they fancy? 

I want to know it, too.
I sometime try to write the generic package like below:

  generic
     type Input_Cursor is private;
     with package Input_Iterator_Interfaces is new Ada.Iterator_Interfaces (Input_Cursor);
     Input_Iterator : Input_Iterator_Interfaces.Forward_Iterator'Class;
     type Element_Type (<>) is limited private;
     with function Element (Position : Input_Cursor) return Element_Type;
  package My_Filter is
     type Output_Cursor is private;
     package Output_Iterator_Interfaces is new Ada.Iterator_Interfaces (Output_Cursor);
     Output_Iterator : Output_Iterator_Interfaces.Forward_Iterator'Class := ...;
     ...

But, I can't get a sense of satisfaction.

Thanks.

^ permalink raw reply	[relevance 0%]

* Re: Two approaches of iterators for the key-value pairs
  2015-11-27 16:30  0% ` Dmitry A. Kazakov
@ 2015-11-27 18:08  0%   ` ytomino
  2015-11-27 20:50  0%     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 65+ results
From: ytomino @ 2015-11-27 18:08 UTC (permalink / raw)


On Saturday, November 28, 2015 at 1:30:23 AM UTC+9, Dmitry A. Kazakov wrote:
> On Fri, 27 Nov 2015 07:25:57 -0800 (PST), ytomino wrote:
> 
> > Hello, I'm reading AI12-0009-1 "Iterators for Directories and Environment_Variables".
> > http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0009-1.txt?rev=1.4
> > 
> > And there is interesting difference between the new iterator of Ada.Environment_Variables and the existing iterators.
> > (The boolean trick of these new iterators is also interesting, but set aside.)
> > 
> > A loop for Ada.Environment_Variables would be like below, according to this AI:
> > 
> > for E *of* Ada.Environment_Variables.All_Variables loop
> >    -- E is Iterator_Element (Name_Value_Pair_Type)
> >    Name (E) -- key
> >    Value (E) -- value
> > end loop;
> > 
> > On the other hand, as we already know, a loop for Ada.Containers.Hashed_Maps/Ordered_Maps:
> > 
> > for I *in* The_Map_Object.Iterate loop
> >    -- I is Cursor
> >    Key (E) -- key
> >    Element (E), Reference (E).Element.all -- value
> > end loop;
> > 
> > If you create new iterator for some key-value pairs, which approach do you like?
> 
> Rather this:
> 
> for Variable in All_Variables loop
>    Put_Line (Variable.Name & "=" & Variable.Value);
> end loop;

It sounds good.

However, the Cursor probably have to be tagged to implement it for dot-notation, and, I think it will be an double-dispatching error on First/Next because the iterator is also tagged.

I wish dot-notation for non-tagged type is allowed (with pragma?)


^ permalink raw reply	[relevance 0%]

* Re: Two approaches of iterators for the key-value pairs
  2015-11-27 15:25  4% Two approaches of iterators for the key-value pairs ytomino
  2015-11-27 16:30  0% ` Dmitry A. Kazakov
@ 2015-11-27 17:43  4% ` Brad Moore
  2015-11-27 19:38  0%   ` ytomino
  1 sibling, 1 reply; 65+ results
From: Brad Moore @ 2015-11-27 17:43 UTC (permalink / raw)


On Friday, November 27, 2015 at 8:26:02 AM UTC-7, ytomino wrote:
> Hello, I'm reading AI12-0009-1 "Iterators for Directories and Environment_Variables".
> http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0009-1.txt?rev=1.4
> 
> And there is interesting difference between the new iterator of Ada.Environment_Variables and the existing iterators.
> (The boolean trick of these new iterators is also interesting, but set aside.)

I'm not sure what is being referred to here as a "boolean trick". Perhaps you are referring to the mention of "Rosen Trick", which as Randy points out, should probably be called "Rosen Technique". However that has nothing to do with "Boolean". The Rosen technique is a coding technique that allows one to treat a read only "in" parameter as a modifiable "in out" parameter.

> 
> A loop for Ada.Environment_Variables would be like below, according to this AI:
> 
> for E *of* Ada.Environment_Variables.All_Variables loop
>    -- E is Iterator_Element (Name_Value_Pair_Type)
>    Name (E) -- key
>    Value (E) -- value
> end loop;
> 
> On the other hand, as we already know, a loop for Ada.Containers.Hashed_Maps/Ordered_Maps:
> 
> for I *in* The_Map_Object.Iterate loop
>    -- I is Cursor
>    Key (E) -- key
>    Element (E), Reference (E).Element.all -- value
> end loop;

All the existing containers are iterable containers, so they support
both using "of" and "in" syntax for loops. Using "of" is generally preferable
because cursors are implicit, and the code written by the user is therefore simpler. Using "in" syntax can also be used by obtaining an Iterator object, but this gives you a cursor instead of a container element, so you typically would need to write some extra calls to get to the container element from the cursor object.


This code excerpt you had above would not compile, so it is probably confusing people.
It think it would be better to perhaps show a full simple working example, such as incrementing each element of the container;
Here I show both forms of loop using a Hashed_Map container. As you can see,
the "of" format is simpler, and hopefully easier to read and understand.

with Ada.Containers.Hashed_Maps; use Ada;
procedure Test_Iterator is
   
   type Student_Id is new Positive;
   type Grade is new Natural range 0 .. 100;
   function Hash (Student : Student_Id) return Containers.Hash_Type is
     (Containers.Hash_Type (Student));
   
   package Maps is new Ada.Containers.Hashed_Maps 
     (Key_Type        => Student_Id,
      Element_Type    => Grade,
      Hash            => Hash,
      Equivalent_Keys => "=");

   School : Maps.Map;
   
begin

   School.Insert (Key      => 123456,    -- Student A
                  New_Item => 90);
   School.Insert (Key      => 789123,    -- Student B
                  New_Item => 65);

   -- Iterate using "in" syntax (for an iterator)
   for Cursor in School.Iterate loop
      School.Replace (Key => Maps.Key (Cursor), 
                      New_Item => Maps.Element (Cursor) + 1);
   end loop;

   -- Iterate using "of" syntax (for an iterable containter)
   for Grade of School loop
      Grade := Grade + 1;
   end loop;

   
end Test_Iterator;

For the AI for Ada 202x on Ada.Directories and Ada.Environment_Variables,
this is a work in progress, so it may end up looking very different in the end, or may even not make the cut for Ada 202x.

Essentially, the design choice discussed in the AI is whether to expose an Iterable Container type, or just an Iterator type.

Both choices could be made to have the same interface for the user, with the only difference being whether "in" or "of" appears in the loop.

If we expose an Iterable container though, that also gives the ability to use "in" syntax since an Iterable container is associated with an Iterator type, and that Iterator type could be used in the loop. However, in that case, as with other existing standard container types and as seen in my example above, using the "in" form would be a bit more awkward to use, compared to the "of" form, so I suspect most people would just use "of".

So the question really is, if we add Ada 2012 Iterator support to Ada.Directories and/or Ada.Environment_Variables, should the design be to 
expose just an Iterator type, or both an Iterator Type, and an Iterable container type?

My thought is that the latter might be better mostly because it would be more consistent with the other standard container types, but I could go either way.

A secondary question might be for anyone creating such abstractions of their own, are there cases where creating an Iterator type makes better sense than creating an Iterable container type?  The ACATS test involving the Prime number iterator could be an example of such an iterator type, but note I could have written that also as an Iterable Container type instead.

In other words, can general guidance be given about how to choose an approach, or does it not really matter, and the writer of such abstractions should choose which ever approach they fancy? 

Brad

> 
> If you create new iterator for some key-value pairs, which approach do you like?
> 
> -- 
> Yuta Tomino

^ permalink raw reply	[relevance 4%]

* Re: Two approaches of iterators for the key-value pairs
  2015-11-27 15:25  4% Two approaches of iterators for the key-value pairs ytomino
@ 2015-11-27 16:30  0% ` Dmitry A. Kazakov
  2015-11-27 18:08  0%   ` ytomino
  2015-11-27 17:43  4% ` Brad Moore
  1 sibling, 1 reply; 65+ results
From: Dmitry A. Kazakov @ 2015-11-27 16:30 UTC (permalink / raw)


On Fri, 27 Nov 2015 07:25:57 -0800 (PST), ytomino wrote:

> Hello, I'm reading AI12-0009-1 "Iterators for Directories and Environment_Variables".
> http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0009-1.txt?rev=1.4
> 
> And there is interesting difference between the new iterator of Ada.Environment_Variables and the existing iterators.
> (The boolean trick of these new iterators is also interesting, but set aside.)
> 
> A loop for Ada.Environment_Variables would be like below, according to this AI:
> 
> for E *of* Ada.Environment_Variables.All_Variables loop
>    -- E is Iterator_Element (Name_Value_Pair_Type)
>    Name (E) -- key
>    Value (E) -- value
> end loop;
> 
> On the other hand, as we already know, a loop for Ada.Containers.Hashed_Maps/Ordered_Maps:
> 
> for I *in* The_Map_Object.Iterate loop
>    -- I is Cursor
>    Key (E) -- key
>    Element (E), Reference (E).Element.all -- value
> end loop;
> 
> If you create new iterator for some key-value pairs, which approach do you like?

Rather this:

for Variable in All_Variables loop
   Put_Line (Variable.Name & "=" & Variable.Value);
end loop;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[relevance 0%]

* Two approaches of iterators for the key-value pairs
@ 2015-11-27 15:25  4% ytomino
  2015-11-27 16:30  0% ` Dmitry A. Kazakov
  2015-11-27 17:43  4% ` Brad Moore
  0 siblings, 2 replies; 65+ results
From: ytomino @ 2015-11-27 15:25 UTC (permalink / raw)


Hello, I'm reading AI12-0009-1 "Iterators for Directories and Environment_Variables".
http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0009-1.txt?rev=1.4

And there is interesting difference between the new iterator of Ada.Environment_Variables and the existing iterators.
(The boolean trick of these new iterators is also interesting, but set aside.)

A loop for Ada.Environment_Variables would be like below, according to this AI:

for E *of* Ada.Environment_Variables.All_Variables loop
   -- E is Iterator_Element (Name_Value_Pair_Type)
   Name (E) -- key
   Value (E) -- value
end loop;

On the other hand, as we already know, a loop for Ada.Containers.Hashed_Maps/Ordered_Maps:

for I *in* The_Map_Object.Iterate loop
   -- I is Cursor
   Key (E) -- key
   Element (E), Reference (E).Element.all -- value
end loop;

If you create new iterator for some key-value pairs, which approach do you like?

-- 
Yuta Tomino

^ permalink raw reply	[relevance 4%]

* Re: container of a container...
  @ 2015-08-01 11:56  6% ` Björn Lundin
  0 siblings, 0 replies; 65+ results
From: Björn Lundin @ 2015-08-01 11:56 UTC (permalink / raw)


On 2015-07-31 23:13, Hedley Rainnie wrote:
> Is it possible to code that? I am coming from the C++ STL world where, it  relatively easy to put that together. (a vector of lists as an example).
> 
> There are many wonderful examples for Ada containers (stacks etc). But I have not seen any container[container[...]..] examples.
> 
> Being new to Ada, I am sure I have missed something obvious...
> 
> 


--Below creates a map, keyed on 2 character length strings
--and hold a list of integers;
--it also prints the items of each list in the two map-entries

with Ada.Containers.Doubly_Linked_Lists;
with Ada.Containers.Hashed_Maps;
with Ada.Strings;
with Ada.Strings.Hash;
with Text_Io;

procedure Lists is



  package Integer_Pack is new Ada.Containers.Doubly_Linked_Lists(integer);

  subtype String_Key_Type is String(1..2);

  package List_Maps is new Ada.Containers.Hashed_Maps (
         String_Key_Type,
         Integer_Pack.List,
         Ada.Strings.Hash,
         "=",
         Integer_Pack."=");

   Map         : List_Maps.Map ;
   Integer_List : Integer_Pack.List;
begin


  for i in 1 .. 20 loop
    Integer_List.Append(i);
  end loop;


  Map.Insert("12", Integer_List);

  Integer_List.Clear;

  for i in 21 .. 40 loop
    Integer_List.Append(i);
  end loop;

  Map.Insert("11", Integer_List);


  for L of map("11") loop
     Text_Io.Put_Line(L'img);
  end loop;

  for L of map("12") loop
     Text_Io.Put_Line(L'img);
  end loop;

end Lists;




-- 
--
Björn

^ permalink raw reply	[relevance 6%]

* Re: GNAT GPL is not shareware
  2015-01-30 18:21  4%                             ` Jeffrey Carter
@ 2015-01-30 18:49  5%                               ` Simon Wright
  0 siblings, 0 replies; 65+ results
From: Simon Wright @ 2015-01-30 18:49 UTC (permalink / raw)


Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org> writes:

> On 01/30/2015 10:34 AM, Simon Wright wrote:
>> 
>> I don't believe there are any language-defined units that GNAT
>> doesn't implement as Ada source in the RTS? so it's the licensing
>> terms on the RTS you actually build against that matters?
>
> I somehow doubt that Ada.Containers.Hashed_Maps is part of the
> RTL. It's a generic pkg that exists only as source. If that source
> lacks the exception, and you "with" it, then your code must be GPL,
> regardless of the RTL the compiler links in.

In GNAT, an RTS is a directory containing adainclude/ (all the RTS
source) and adalib/ (all the corresponding .ali files, and binary
libraries: either static or dynamic (perhaps both)).

Ada.Containers.Hashed_Maps is in files a-cohama.ad[bs], and the version
here in the GNAT GPL library has had the run time exception removed;
whereas the one in the FSF GCC library still has it.

I see no reason in principle why one shouldn't compile the FSF version
with the GPL compiler. A lot of work for no benefit.

I can't at the moment include Ada.Containers.* in my STM32F4 RTS,
because I don't support memory allocation, and AdaCore don't include any
Containers in their Ravenscar RTSs for the same board, and I only have
192K of RAM to play with! but if I did, I'd use the FSF version.


^ permalink raw reply	[relevance 5%]

* Re: GNAT GPL is not shareware
  @ 2015-01-30 18:21  4%                             ` Jeffrey Carter
  2015-01-30 18:49  5%                               ` Simon Wright
  0 siblings, 1 reply; 65+ results
From: Jeffrey Carter @ 2015-01-30 18:21 UTC (permalink / raw)


On 01/30/2015 10:34 AM, Simon Wright wrote:
> 
> I don't believe there are any language-defined units that GNAT doesn't
> implement as Ada source in the RTS? so it's the licensing terms on the
> RTS you actually build against that matters?

I somehow doubt that Ada.Containers.Hashed_Maps is part of the RTL. It's a
generic pkg that exists only as source. If that source lacks the exception, and
you "with" it, then your code must be GPL, regardless of the RTL the compiler
links in.

-- 
Jeff Carter
It's better to be root than to reboot.
119

^ permalink raw reply	[relevance 4%]

* Re: Ada.Containers warnings with gnat
  2014-11-16 10:05  0%   ` Björn Lundin
@ 2014-11-16 11:37  6%     ` Björn Lundin
  0 siblings, 0 replies; 65+ results
From: Björn Lundin @ 2014-11-16 11:37 UTC (permalink / raw)


On 2014-11-16 11:05, Björn Lundin wrote:

It seems to be ok until I - in the body - do

with Ada.Streams;
with Ada.Streams.Stream_IO;

With no code actually using stream_io I get



   21.   package Sample_Map_Pack is new Ada.Containers.Ordered_Maps
          |
        >>> warning: in instantiation at a-coorma.ads:266
        >>> warning: no entities of package "Ada.Streams" are referenced

    27.   package Marketid_Map_Pack is new Ada.Containers.Hashed_Maps
          |
        >>> warning: in instantiation at a-cohama.ads:342
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:154
        >>> warning: in instantiation at a-cohama.adb:85
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:165
        >>> warning: in instantiation at a-cohama.adb:85
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:165
        >>> warning: in instantiation at a-cohama.adb:103
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:154
        >>> warning: in instantiation at a-cohama.adb:104
        >>> warning: no entities of package "Ada.Streams" are referenced

    34.   package Winner_Map_Pack is new Ada.Containers.Hashed_Maps
          |
        >>> warning: in instantiation at a-cohama.ads:342
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:154
        >>> warning: in instantiation at a-cohama.adb:85
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:165
        >>> warning: in instantiation at a-cohama.adb:85
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:165
        >>> warning: in instantiation at a-cohama.adb:103
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:154
        >>> warning: in instantiation at a-cohama.adb:104
        >>> warning: no entities of package "Ada.Streams" are referenced

    41.   package Win_Place_Map_Pack is new Ada.Containers.Hashed_Maps
          |
        >>> warning: in instantiation at a-cohama.ads:342
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:154
        >>> warning: in instantiation at a-cohama.adb:85
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:165
        >>> warning: in instantiation at a-cohama.adb:85
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:165
        >>> warning: in instantiation at a-cohama.adb:103
        >>> warning: no entities of package "Ada.Streams" are referenced
        >>> warning: in instantiation at a-chtgop.ads:154
        >>> warning: in instantiation at a-cohama.adb:104
        >>> warning: no entities of package "Ada.Streams" are referenced

 763 lines: No errors, 50 warnings



If I then move on to actually use Streams_io, the warnings are reduced to


==============Error messages for source file:
/home/bnl/bnlbot/botstart/bot-1-0/source/ada/local/utils/simulation_storage.ads
    21.   package Sample_Map_Pack is new Ada.Containers.Ordered_Maps
          |
        >>> warning: in instantiation at a-coorma.ads:266
        >>> warning: no entities of package "Ada.Streams" are referenced

    27.   package Marketid_Map_Pack is new Ada.Containers.Hashed_Maps
          |
        >>> warning: in instantiation at a-cohama.ads:342
        >>> warning: no entities of package "Ada.Streams" are referenced

    34.   package Winner_Map_Pack is new Ada.Containers.Hashed_Maps
          |
        >>> warning: in instantiation at a-cohama.ads:342
        >>> warning: no entities of package "Ada.Streams" are referenced

    41.   package Win_Place_Map_Pack is new Ada.Containers.Hashed_Maps
          |
        >>> warning: in instantiation at a-cohama.ads:342
        >>> warning: no entities of package "Ada.Streams" are referenced

 763 lines: No errors, 8 warnings



the actual usage is just

    declare
     File   : Ada.Streams.Stream_IO.File_Type;
     Stream : Ada.Streams.Stream_IO.Stream_Access;
     Filename : String := Global_Map_Files(Marketid).Filename.Fix_String;
    begin
      Ada.Streams.Stream_IO.Open
          (File => File,
           Name => Filename,
           Mode => Ada.Streams.Stream_IO.In_File);
      Stream := Ada.Streams.Stream_IO.Stream (File);
      Marketid_Map_Pack.Map'Read(Stream, Marketid_Map);
      Ada.Streams.Stream_IO.Close(File);
      Log(Object & Service, "Marketid_Map read from file " & Filename);
    end;

for 3 different maps, and 3 corresponding Write sections


however I have put the generic instansiation between
pragma Warnings(Off)
pragma Warnings(On)

but it is still somewhat strange I think




--
Björn


^ permalink raw reply	[relevance 6%]

* Re: Ada.Containers warnings with gnat
  2014-11-15 18:01  0% ` Jeffrey Carter
@ 2014-11-16 10:05  0%   ` Björn Lundin
  2014-11-16 11:37  6%     ` Björn Lundin
  0 siblings, 1 reply; 65+ results
From: Björn Lundin @ 2014-11-16 10:05 UTC (permalink / raw)


On 2014-11-15 19:01, Jeffrey Carter wrote:
> On 11/15/2014 07:35 AM, Björn Lundin wrote:
>>
>>
>> The simulator was in a single file before -> no problem.
>> So I then refactored and created a storage package and suddenly I get
>> warnings like
>>
>>  /home/bnl/bnlbot/botstart/bot-1-0/source/ada/local/utils/simulation_storage.ads
>>     17.   package Sample_Map_Pack is new Ada.Containers.Ordered_Maps
>>           |
>>         >>> warning: in instantiation at a-coorma.ads:266
>>         >>> warning: no entities of package "Ada.Streams" are referenced
>>
>>     23.   package Marketid_Map_Pack is new Ada.Containers.Hashed_Maps
>>           |
>>         >>> warning: in instantiation at a-cohama.ads:342
>>         >>> warning: no entities of package "Ada.Streams" are referenced
>>
>>     30.   package Winner_Map_Pack is new Ada.Containers.Hashed_Maps
>>           |
>>         >>> warning: in instantiation at a-cohama.ads:342
>>         >>> warning: no entities of package "Ada.Streams" are referenced
>>
>>     37.   package Win_Place_Map_Pack is new Ada.Containers.Hashed_Maps
>>           |
>>         >>> warning: in instantiation at a-cohama.ads:342
>>         >>> warning: no entities of package "Ada.Streams" are referenced
> 
> Did you change your compiler options? It appears that the compiler's
> implementations of these standard pkgs with Ada.Streams but never reference it,
> and that you're compiling with warnings of unreferenced things turned on.
> 


No, I did not. I just put the code into a package instead. That's it.
And I have never seen these kind of warnings on compiler/language
packages. It is like it recompiles them.

--
Björn

^ permalink raw reply	[relevance 0%]

* Re: Ada.Containers warnings with gnat
  2014-11-15 14:35  7% Ada.Containers warnings with gnat Björn Lundin
@ 2014-11-15 18:01  0% ` Jeffrey Carter
  2014-11-16 10:05  0%   ` Björn Lundin
  0 siblings, 1 reply; 65+ results
From: Jeffrey Carter @ 2014-11-15 18:01 UTC (permalink / raw)


On 11/15/2014 07:35 AM, Björn Lundin wrote:
> 
> 
> The simulator was in a single file before -> no problem.
> So I then refactored and created a storage package and suddenly I get
> warnings like
> 
>  /home/bnl/bnlbot/botstart/bot-1-0/source/ada/local/utils/simulation_storage.ads
>     17.   package Sample_Map_Pack is new Ada.Containers.Ordered_Maps
>           |
>         >>> warning: in instantiation at a-coorma.ads:266
>         >>> warning: no entities of package "Ada.Streams" are referenced
> 
>     23.   package Marketid_Map_Pack is new Ada.Containers.Hashed_Maps
>           |
>         >>> warning: in instantiation at a-cohama.ads:342
>         >>> warning: no entities of package "Ada.Streams" are referenced
> 
>     30.   package Winner_Map_Pack is new Ada.Containers.Hashed_Maps
>           |
>         >>> warning: in instantiation at a-cohama.ads:342
>         >>> warning: no entities of package "Ada.Streams" are referenced
> 
>     37.   package Win_Place_Map_Pack is new Ada.Containers.Hashed_Maps
>           |
>         >>> warning: in instantiation at a-cohama.ads:342
>         >>> warning: no entities of package "Ada.Streams" are referenced

Did you change your compiler options? It appears that the compiler's
implementations of these standard pkgs with Ada.Streams but never reference it,
and that you're compiling with warnings of unreferenced things turned on.

-- 
Jeff Carter
"Gentlemen, you can't fight in here. This is the War Room!"
Dr. Strangelove
30


^ permalink raw reply	[relevance 0%]

* Ada.Containers warnings with gnat
@ 2014-11-15 14:35  7% Björn Lundin
  2014-11-15 18:01  0% ` Jeffrey Carter
  0 siblings, 1 reply; 65+ results
From: Björn Lundin @ 2014-11-15 14:35 UTC (permalink / raw)



Hi!
I'm using a db to keep data in, that I retrieve and
put into ordered and hashed maps from Ada.Containers.

I do this to
 * learn to use Ada standard containers, instead of home-rolled lists
 * to simulate outcome of different parameters into a business modell

Reading from db takes about 1 hour, with i5/ssd/12 gb ram
needless to say, I'd like this to go faster.

So, I rewrote the simulator to use the mentioned containers.
I then run it once, and save the maps into (largish ) files
instead via streams and 'write/'read.

fantastic performance.

The simulator was in a single file before -> no problem.
So I then refactored and created a storage package and suddenly I get
warnings like

 /home/bnl/bnlbot/botstart/bot-1-0/source/ada/local/utils/simulation_storage.ads
    17.   package Sample_Map_Pack is new Ada.Containers.Ordered_Maps
          |
        >>> warning: in instantiation at a-coorma.ads:266
        >>> warning: no entities of package "Ada.Streams" are referenced

    23.   package Marketid_Map_Pack is new Ada.Containers.Hashed_Maps
          |
        >>> warning: in instantiation at a-cohama.ads:342
        >>> warning: no entities of package "Ada.Streams" are referenced

    30.   package Winner_Map_Pack is new Ada.Containers.Hashed_Maps
          |
        >>> warning: in instantiation at a-cohama.ads:342
        >>> warning: no entities of package "Ada.Streams" are referenced

    37.   package Win_Place_Map_Pack is new Ada.Containers.Hashed_Maps
          |
        >>> warning: in instantiation at a-cohama.ads:342
        >>> warning: no entities of package "Ada.Streams" are referenced



what do the mean ?
The body of the package uses streams like

    Log("read Marketid_Map from file ");
    declare
     File   : Ada.Streams.Stream_IO.File_Type;
     Stream : Ada.Streams.Stream_IO.Stream_Access;
     Filename : String := Map_files(Marketid).Filename.Fix_String;
    begin
      Ada.Streams.Stream_IO.Open
          (File => File,
           Name => Filename,
           Mode => Ada.Streams.Stream_IO.In_File);
      Stream := Ada.Streams.Stream_IO.Stream (File);
      Marketid_Map_Pack.Map'Read(Stream, Marketid_Map);
      Ada.Streams.Stream_IO.Close(File);
      Log("Marketid_Map read from file " & Filename);
    end;




package spec is

---------------------------
with Ada;
with Ada.Strings;
with Ada.Strings.Hash;

with Ada.Containers;
with Ada.Containers.Hashed_Maps;
with Ada.Containers.Ordered_Maps;

with Calendar2;
with Bot_Types; use Bot_Types;
with Table_Apricesfinish;
with Table_Arunners;

package Simulation_Storage is

  package Sample_Map_Pack is new Ada.Containers.Ordered_Maps
        (Key_Type     => Calendar2.Time_Type,
         Element_Type => Table_Apricesfinish.Apricesfinish_List_Pack2.List,
         "<"          => Calendar2."<",
         "="          => Table_Apricesfinish.Apricesfinish_List_Pack2."=");

  package Marketid_Map_Pack is new Ada.Containers.Hashed_Maps
        (Market_Id_Type,
         Sample_Map_Pack.Map,
         Ada.Strings.Hash,
         "=",
         Sample_Map_Pack."=");

  package Winner_Map_Pack is new Ada.Containers.Hashed_Maps
        (Market_Id_Type,
         Table_Arunners.Arunners_List_Pack2.List,
         Ada.Strings.Hash,
         "=",
         Table_Arunners.Arunners_List_Pack2."=");

  package Win_Place_Map_Pack is new Ada.Containers.Hashed_Maps
        (Market_Id_Type,
         Market_Id_Type,
         Ada.Strings.Hash,
         "=",
         "=");


  procedure Create_Files_From_Database;
  procedure Fill_Maps(Marketid_Map  : out Marketid_Map_Pack.Map;
                      Winner_Map    : out Winner_Map_Pack.Map;
                      Win_Place_Map : out Win_Place_Map_Pack.Map) ;


end Simulation_Storage;



--
Björn

^ permalink raw reply	[relevance 7%]

* Re: Optimizing Ada
  @ 2013-10-02 18:58  4%       ` John B. Matthews
  0 siblings, 0 replies; 65+ results
From: John B. Matthews @ 2013-10-02 18:58 UTC (permalink / raw)


In article <l2g6i7$8mm$1@dont-email.me>,
 Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org> wrote:

> On 10/01/2013 08:53 PM, kennethesills@gmail.com wrote:
> >> Ada is the fastest correct implementation you have.
> >
> > Yes. However, is case sensitivity the reason for a 2.7x slow down? 
> > Highly unlikely. In fact, using case-sensitive comparisons in Ada 
> > only reduce the time taken by around 50ns. So I just disregarded 
> > that fact.
> 
> I agree that the implementation of Indefinite_Hashed_Maps is probably 
> the culprit, but until you have an apples-to-apples comparison, you 
> have no complaint. (Actually, I can't think of any application that 
> could use such a function where the difference would prevent it from 
> meeting reasonable timing requirements, so you probably have no 
> complaint anyway.)

kennethesills: For comparison with Indefinite_Hashed_Maps, this example 
uses an instance of Ada.Strings.Bounded.Generic_Bounded_Length as the 
Key_Type in an instance of Ada.Containers.Hashed_Maps. Your problem 
domain may suggest a suitable maximum length.

<http://home.roadrunner.com/~jbmatthews/jumble.html>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>


^ permalink raw reply	[relevance 4%]

* Re: Multiple keys for a map
  @ 2013-09-18  5:44  3%   ` Peter Brooks
  0 siblings, 0 replies; 65+ results
From: Peter Brooks @ 2013-09-18  5:44 UTC (permalink / raw)


On Wednesday, 18 September 2013 07:19:18 UTC+2, Shark8  wrote:
>  
> I'm vaguely reminded of Prolog by this question: this problem was basically the intro for the book I had.
>
Well remembered! It's exactly the sort of question Prolog was designed to answer. Actually, Prolog might be a much better front-end to a triplestore than SPARQL. I'm not sure, I'll look into it. I see that there's a simple Prolog interpreter with an Ada API here: xhttp://goanna.cs.rmit.edu.au/~dale/software/ it might be just the thing.

Anyway, that's for a bit later, I'm still, obviously, working to get the structure right.

Unfortunately the array of hash-keys doesn't work with the with Ada.Containers.Hashed_Maps package.

It looks as if the sensible thing will to create a container for the subject, then simply point the predicate, object and group items to it. It makes some sense as the object space is much smaller than subject space, and the predicate space is very small and the graph space tiny.

It probably makes sense to have graphs and predicates in a simple array, there are so few of them, and only have efficient searching for subjects and objects. It makes the model much simpler. Even if all three have the same treatment, it makes sense for them to occupy separate spaces because that makes the chance of duplicates turning up even smaller.

^ permalink raw reply	[relevance 3%]

* Re: Ada Containers
  2012-10-07 19:58  5% Ada Containers rwilco19
  2012-10-07 20:40  0% ` Maciej Sobczak
@ 2012-10-07 22:00  6% ` Simon Wright
  1 sibling, 0 replies; 65+ results
From: Simon Wright @ 2012-10-07 22:00 UTC (permalink / raw)


rwilco19@gmail.com writes:

> Can someone please explain to me the difference between
> Ada.Containers.Hashed_Tables and Ada.Containers.Hashed_Maps, and when
> you might want to use a Hashed_Table over a Hashed_Map?

Ada.Containers.Hash_Tables, as you noted.

Hashed_Maps is in the Standard, Hash_Tables is not; it's part of
AdaCore's implementation (and maybe others, for all I know).

GCC 4.7.0 says for Hashed_Maps

   with Ada.Iterator_Interfaces;

   private with Ada.Containers.Hash_Tables;
   private with Ada.Finalization;
   private with Ada.Streams;

   generic
      type Key_Type is private;
      type Element_Type is private;

      with function Hash (Key : Key_Type) return Hash_Type;
      with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
      with function "=" (Left, Right : Element_Type) return Boolean is <>;

   package Ada.Containers.Hashed_Maps is

and you will see that Hash_Tables is withed privately (i.e., it's only
used in the private part, "not specified by the language").

A further clue: the comment at the start of Hash_Tables says "This
package declares the hash-table type used to implement hashed
containers."



^ permalink raw reply	[relevance 6%]

* Re: Ada Containers
  @ 2012-10-07 21:56  6%     ` Georg Bauhaus
  0 siblings, 0 replies; 65+ results
From: Georg Bauhaus @ 2012-10-07 21:56 UTC (permalink / raw)


On 07.10.12 23:30, rwilco19@gmail.com wrote:
> Maby I caused some confusion earlier with a type. I misspelled the package name. It's Ada.Containers.Hash_Tables not Ada.Containers.Hashed_Tables.
>

In GNAT, package Ada.Containers.Hash_Tables
"declares the hash-table type used to implement hashed containers."

In Ada, the hashed containers are Ada.Containers.Hashed_Maps
and Ada.Containers.Hashed_Sets, and variants like Bounded_*
(new in Ada 2012) and Indefinite_*. So Hash_Tables is part of
GNAT's implementation of Ada's hashed containers, perhaps for
use by implementers only.

(You can see in the sources of GNAT's implementation of
Ada's library, this context clause in Ada.Containers.Hashed_Maps:

(private with Ada.Containers.Hash_Tables;)




^ permalink raw reply	[relevance 6%]

* Re: Ada Containers
  2012-10-07 20:40  0% ` Maciej Sobczak
@ 2012-10-07 21:25  6%   ` rwilco19
    1 sibling, 0 replies; 65+ results
From: rwilco19 @ 2012-10-07 21:25 UTC (permalink / raw)


Do you mean doesn't exist, or cannot be used?

Here's a reference page for it:
http://www.martin.dowie.btinternet.co.uk/Containers/adabrowse/ada-containers-hash_tables.html

This page shows it in the dependency list for Hashed_Maps
http://www.martin.dowie.btinternet.co.uk/Containers/adabrowse/ada-containers-hashed_maps.html

"with Ada.Containers.Hash_Tables;"



^ permalink raw reply	[relevance 6%]

* Re: Ada Containers
  2012-10-07 19:58  5% Ada Containers rwilco19
@ 2012-10-07 20:40  0% ` Maciej Sobczak
  2012-10-07 21:25  6%   ` rwilco19
    2012-10-07 22:00  6% ` Simon Wright
  1 sibling, 2 replies; 65+ results
From: Maciej Sobczak @ 2012-10-07 20:40 UTC (permalink / raw)


W dniu niedziela, 7 października 2012 21:58:41 UTC+2 użytkownik rwil...@gmail.com napisał:

> Can someone please explain to me the difference between Ada.Containers.Hashed_Tables and Ada.Containers.Hashed_Maps,

Yes, it is very easy - Hashed_Tables does not exists at all, whereas Hashed_Maps is a pretty useful package. :-)

Did you perhaps meant some other package? Which one?

-- 
Maciej Sobczak * http://www.msobczak.com * http://www.inspirel.com



^ permalink raw reply	[relevance 0%]

* Ada Containers
@ 2012-10-07 19:58  5% rwilco19
  2012-10-07 20:40  0% ` Maciej Sobczak
  2012-10-07 22:00  6% ` Simon Wright
  0 siblings, 2 replies; 65+ results
From: rwilco19 @ 2012-10-07 19:58 UTC (permalink / raw)


Can someone please explain to me the difference between Ada.Containers.Hashed_Tables and Ada.Containers.Hashed_Maps, and when you might want to use a Hashed_Table over a Hashed_Map?



^ permalink raw reply	[relevance 5%]

* Abstraction over container iterators
@ 2012-08-03 12:01  5% ms
  0 siblings, 0 replies; 65+ results
From: ms @ 2012-08-03 12:01 UTC (permalink / raw)


I'll let the code speak first:

-----------------------------------------------
with Ada.Containers.Hashed_Maps;                                                
with Ada.Strings.Unbounded;                                                     
with Ada.Strings.Unbounded.Hash_Case_Insensitive;                               
with Ada.Strings.Unbounded.Equal_Case_Insensitive;                              

package Environments is                                                         

   type Environment is tagged private;                                          

   function Variable (                                                          
      E    : in Environment;                                                    
      Name : in Ada.Strings.Unbounded.Unbounded_String                          
   )                                                                            
      return Ada.Strings.Unbounded.Unbounded_String                             
      with Inline;                                                              

   procedure Set_Variable (                                                     
      E     : in out Environment;                                               
      Name  : in Ada.Strings.Unbounded.Unbounded_String;                        
      Value : in Ada.Strings.Unbounded.Unbounded_String                         
   )                                                                            
      with Inline;                                                              

private                                                                         

   package Variable_Maps is new Ada.Containers.Hashed_Maps (                    
      Key_Type        => Ada.Strings.Unbounded.Unbounded_String,                
      Element_Type    => Ada.Strings.Unbounded.Unbounded_String,                
      Hash            => Ada.Strings.Unbounded.Hash_Case_Insensitive,           
      Equivalent_Keys => Ada.Strings.Unbounded.Equal_Case_Insensitive,          
      "="             => Ada.Strings.Unbounded."="                              
   );                                                                           

   type Environment is tagged record                                            
      Variables : Variable_Maps.Map;                                            
   end record;                                                                  

end Environments;
-----------------------------------------------

What we have here is an example package fairly well illustrating my problem. I'm storing some environment variables in Hashed_Map, but I want to build a abstraction layer over the standard container, so I can in future change the underlaying container without changing any code in my package's customers.

Getting and setting variables is easy - as declared above. The real problem is iterating. I'd like to let my package's customers to iterate over the environment and get both key and value for each element easily.

As I'm using Ada 2012 the best way would be to use iterators, but how? I could return a cursor to the underlaying container, but again, this cursor's interface would be container-dependent.

What would you say is the best way to achieve such abstraction over standard container iteration?



^ permalink raw reply	[relevance 5%]

* Re: reference to a name variable
  2009-07-28 18:25  4%     ` Ludovic Brenta
@ 2009-07-30 14:37  0%       ` Chrono
  0 siblings, 0 replies; 65+ results
From: Chrono @ 2009-07-30 14:37 UTC (permalink / raw)


On 28 jul, 15:25, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote:
> On Jul 28, 3:51 pm, Chrono <pablit...@gmail.com> wrote:
>
>
>
> > On 24 jul, 08:03, Stephen Leake <stephen_le...@stephe-leake.org>
> > wrote:
>
> > > Pablo <pablit...@gmail.com> writes:
> > > > I have an integer variable which I'd like to set dinamically equal to
> > > > a value of a variable that I don't know at priori. Say as:
> > > > I want to set My_Integer = IntegerValue(MyString),
>
> > > My_Integer := Integer'Value (MyString);
>
> > > --
> > > -- Stephe
>
> > Stephe, the idea is this really, instead of MyString is not a number
> > string, it's a string whose name is the name of a integer variable,
> > and it's the value of this variable what I want. This would work if I
> > had a string like "123", so Integer'Value("123") would give me 123.
> > When I try with a real naming string (instead of a number string),
> > program keeps waiting for an entry so it suspends itself...
>
> Like Dmitry said, a map (i.e. a container of name-value pairs, where
> names are Strings and values are integers) seems the best solution.
> See Ada.Containers.Hashed_Maps.
>
> However I'm curious to know why you need this? What is the higher-
> level problem you're trying to solve?
Basically I need test run time variables (thousands!) of a real-time
program machine..
>
> --
> Ludovic Brenta.




^ permalink raw reply	[relevance 0%]

* Re: reference to a name variable
  @ 2009-07-28 18:25  4%     ` Ludovic Brenta
  2009-07-30 14:37  0%       ` Chrono
  0 siblings, 1 reply; 65+ results
From: Ludovic Brenta @ 2009-07-28 18:25 UTC (permalink / raw)


On Jul 28, 3:51 pm, Chrono <pablit...@gmail.com> wrote:
> On 24 jul, 08:03, Stephen Leake <stephen_le...@stephe-leake.org>
> wrote:
>
> > Pablo <pablit...@gmail.com> writes:
> > > I have an integer variable which I'd like to set dinamically equal to
> > > a value of a variable that I don't know at priori. Say as:
> > > I want to set My_Integer = IntegerValue(MyString),
>
> > My_Integer := Integer'Value (MyString);
>
> > --
> > -- Stephe
>
> Stephe, the idea is this really, instead of MyString is not a number
> string, it's a string whose name is the name of a integer variable,
> and it's the value of this variable what I want. This would work if I
> had a string like "123", so Integer'Value("123") would give me 123.
> When I try with a real naming string (instead of a number string),
> program keeps waiting for an entry so it suspends itself...

Like Dmitry said, a map (i.e. a container of name-value pairs, where
names are Strings and values are integers) seems the best solution.
See Ada.Containers.Hashed_Maps.

However I'm curious to know why you need this? What is the higher-
level problem you're trying to solve?

--
Ludovic Brenta.



^ permalink raw reply	[relevance 4%]

* Re: find words that contains some specific letters
  @ 2009-06-07 19:30  4%                                   ` John B. Matthews
  0 siblings, 0 replies; 65+ results
From: John B. Matthews @ 2009-06-07 19:30 UTC (permalink / raw)


In article <alpine.DEB.1.10.0906070035110.5405@urchin.earth.li>,
 Tom Anderson <twic@urchin.earth.li> wrote:

> > John B. Matthews wrote:
> >> The jumble algorithm relies on a hashed map for efficiency, but a 
> >> perfect hash is not essential. I recently implemented the 
> >> algorithm in both Java and Ada using library routines:
> >> 
> >> <http://en.wikipedia.org/wiki/Jumble>
> >> <http://sites.google.com/site/drjohnbmatthews/jumble>
> >> <http://home.roadrunner.com/~jbmatthews/jumble.html>
> >> 
> >> I was intrigued to compare the two languages' library 
> >> implementations of a general purpose string hash. Using a 
> >> multiplicative function with an initial value of zero, both offer 
> >> excellent performance. Java uses a multiplier of 31 on a signed 
> >> 32-bit integer; Ada uses a multiplier of eight on an unsigned 
> >> (modular) value whose size is implementation defined (e.g. mod 
> >> 2**32):
> >> 
> >> <http://www.docjar.org/html/api/java/lang/String.java.html>
> >> <http://gcc.gnu.org/viewcvs/trunk/gcc/ada/a-strhas.adb>
> 
> John, i think you've missed a trick there - doesn't Rotate_Left do a 
> barrel shift, where the bits that fall off the left-hand end get 
> shoved back in at the right, rather than a plain shift? If so, the 
> basic algebraic structure is actually a bit different to java's hash.

D'oh, you're absolutely right. I overlooked this when I was tinkering 
with my own hash function, too. Thank you for looking at this.

[...]

> I still think this is a shoddy hash.

Well, it's a legacy from GNAT.HTable.Hash, but it does very well in the 
context of Ada.Containers.Hashed_Maps. Java uses a bucket list whose 
size is an integral power of two, while (this implementation of) Ada 
uses the next largest prime. Modeling an Ada collision counter on your 
example <http://urchin.earth.li/~twic/tmp/Collisions.java>, I got these 
results for collision count, number of buckets having that count, and 
the percentage of the total:

$ ./collisions 
 0: 218586 (55.6%)
 1: 126250 (32.1%)
 2: 38432 (9.8%)
 3: 8362 (2.1%)
 4: 1354 (0.3%)
 5: 229 (0.1%)
 6: 24 (0.0%)
 7: 4 (0.0%)
 8: 1 (0.0%)

The code is here: <http://home.roadrunner.com/~jbmatthews/jumble.html>

[Followup-To set to comp.lang.ada.]
-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>



^ permalink raw reply	[relevance 4%]

* Re: Bug in GCC ?
  2008-05-19 15:50  5% Bug in GCC ? Sébastien
@ 2008-05-19 15:57  0% ` Matthew Heaney
  0 siblings, 0 replies; 65+ results
From: Matthew Heaney @ 2008-05-19 15:57 UTC (permalink / raw)


On May 19, 11:50 am, Sébastien <seb.mor...@gmail.com> wrote:
>
> with Ada.Containers.Hashed_Maps;

Why didn't you use the indefinite form?  That would allow you to ditch
the Unbounded_String.

with Ada.Containers.Indefinite_Hashed_Maps;
pragma Elaborate_All (Ada.Containers.Indefinite_Hashed_Maps);

package SCMAL.Tools.HashedMaps is

     package HashedStrStr is
         new Ada.Containers.Indefinite_Hashed_Maps(
         Key_Type => String,
         Element_Type => String,
         Hash => Ada.Strings.Hash,
         Equivalent_Keys => "=");

end SCMAL.Tools.HashedMaps;





^ permalink raw reply	[relevance 0%]

* Bug in GCC ?
@ 2008-05-19 15:50  5% Sébastien
  2008-05-19 15:57  0% ` Matthew Heaney
  0 siblings, 1 reply; 65+ results
From: Sébastien @ 2008-05-19 15:50 UTC (permalink / raw)


He is the sample code I'm trying to compile:

with Ada.Containers.Hashed_Maps;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Hash;

package SCMAL.Tools.HashedMaps is

     package HashedStrStr is
         new Ada.Containers.Hashed_Maps(
         Key_Type => Unbounded_String,
         Element_Type => Unbounded_String,
         Hash => Ada.Strings.Unbounded.Hash,
         Equivalent_Keys => "=");

end SCMAL.Tools.HashedMaps;


At compile time:

$ gnatmake -Pgentest -cargs -gnatn
gcc-4.1 -c -gnat05 -g3 -O2 -gnatn -I- -gnatA 
/var/local/mscm/ada/tests/generic/scmal-tools-hashedmaps.ads
+===========================GNAT BUG DETECTED==============================+
| 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu3) 
(i486-pc-linux-gnu) GCC error:|
| in referenced_var_lookup, at tree-dfa.c:578                              |
| No source file position information available                            |
| Please submit a bug report; see http://gcc.gnu.org/bugs.html.            |
| Use a subject line meaningful to you and us to track the bug.            |
| Include the entire contents of this bug box in the report.               |
| Include the exact gcc-4.1 or gnatmake command that you entered.          |
| Also include sources listed below in gnatchop format                     |
| (concatenated together with no headers between files).                   |
+==========================================================================+

It works fine if I remove the -gnatn and -O2

Sebastien



^ permalink raw reply	[relevance 5%]

* Re: Dynamic allocation of named arrays
  2008-04-29 20:14  4% ` Gautier
@ 2008-04-29 20:59  5%   ` Gautier
  0 siblings, 0 replies; 65+ results
From: Gautier @ 2008-04-29 20:59 UTC (permalink / raw)


Mmmmh - always better to test examples before posting...
This one is working:

-----8<-----------8<-----------8<-----------8<------

with Ada.Containers.Hashed_Maps;
with Ada.Strings.Unbounded.Hash;
with Ada.Text_IO;

procedure Dummy_dico is

   package Dictionaries is new Ada.Containers.Hashed_Maps
             (Ada.Strings.Unbounded.Unbounded_String,
              Ada.Strings.Unbounded.Unbounded_String,
              Ada.Strings.Unbounded.Hash,
              Ada.Strings.Unbounded."=",
              Ada.Strings.Unbounded."=");

   use Dictionaries;

   function "-" (Source : Ada.Strings.Unbounded.Unbounded_String) return String
     renames Ada.Strings.Unbounded.To_String;
   function "+" (Source : String) return Ada.Strings.Unbounded.Unbounded_String
     renames Ada.Strings.Unbounded.To_Unbounded_String;

   procedure Translate( dico: Map; from: String ) is
     use Ada.Text_IO;
   begin
     Put_Line( from & " ---> " & (-Element(dico,+from)) );
   end Translate;

   dico_F_to_E: Map;
   c: Cursor;
   s: Boolean;

begin
   Insert(dico_F_to_E, +"souris", +"mouse", c, s);
   Insert(dico_F_to_E, +"chat",   +"cat",   c, s);
   Insert(dico_F_to_E, +"chien",  +"dog",   c, s);
   Insert(dico_F_to_E, +"cheval", +"horse", c, s);
   --
   Translate(dico_F_to_E, "chien");
   Translate(dico_F_to_E, "chat");
end;



^ permalink raw reply	[relevance 5%]

* Re: Dynamic allocation of named arrays
  @ 2008-04-29 20:14  4% ` Gautier
  2008-04-29 20:59  5%   ` Gautier
  0 siblings, 1 reply; 65+ results
From: Gautier @ 2008-04-29 20:14 UTC (permalink / raw)


Kim Rostgaard Christensen wrote:

> Hello
> 
> I need an array that has both variable length key and value (strings)
> that is dynamically allocated at run-time.
> 
> I have looked at ada05's "Containers.Hashed_maps" but am missing a good
> example on usage

with Ada.Strings.Unbounded.Hash;
...

   package Dictionaries is new Ada.Containers.Hashed_Maps
             (Ada.Strings.Unbounded.Unbounded_String,
              Ada.Strings.Unbounded.Unbounded_String,
              Ada.Strings.Unbounded.Hash,
              equivalent_keys => Ada.Strings.Unbounded."=");

For a full example, search "Texture_Name_Mapping" in:

http://globe3d.svn.sourceforge.net/viewvc/globe3d/src/globe_3d-textures.adb?revision=78&view=markup

 > , and would like to use something more portable.

Well, how can it be more portable ?!...
Do you mean with some Ada 95 compilers ?
______________________________________________________________
Gautier         -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



^ permalink raw reply	[relevance 4%]

* Re: Ada Hash
  2007-04-24 15:37  4% Ada Hash markp
  2007-04-24 18:12  0% ` Randy Brukardt
@ 2007-04-29 18:33  0% ` Matthew Heaney
  1 sibling, 0 replies; 65+ results
From: Matthew Heaney @ 2007-04-29 18:33 UTC (permalink / raw)


markp <markwork66@yahoo.com> writes:

> I need to use a hash table to store data using an integer as the key.
> I would like to use Ada.Containers.Hashed_Maps. Is there a default
> hash function for an integer that I can supply to the instantiation
> and could somebody provide a quick sample piece of code to do this?

You just need an identity function, as Randy pointed out:

  function Hash (N : Natural) return Hash_Type is
  begin
     return Hash_Type (N);
  end;

If your integer subtype has negative values, then you could do something like:

  function Hash is
    new Unchecked_Conversion (Integer, Hash_Type);

You could also use one of the ordered forms directly.  Integer subtypes have a
less-than relational operator by default, so there's nothing else you would
need to do.



^ permalink raw reply	[relevance 0%]

* Re: Ada Hash
  2007-04-24 15:37  4% Ada Hash markp
@ 2007-04-24 18:12  0% ` Randy Brukardt
  2007-04-29 18:33  0% ` Matthew Heaney
  1 sibling, 0 replies; 65+ results
From: Randy Brukardt @ 2007-04-24 18:12 UTC (permalink / raw)


"markp" <markwork66@yahoo.com> wrote in message
news:1177429033.247571.9090@s33g2000prh.googlegroups.com...
> I need to use a hash table to store data using an integer as the key.
> I would like to use Ada.Containers.Hashed_Maps. Is there a default
> hash function for an integer that I can supply to the instantiation
> and could somebody provide a quick sample piece of code to do this?

There isn't a default function for this; but generally you don't need one -
the identity function would work fine unless your integers are distributed
unusually.

In any case, writing hash functions is an art; I generally test as many as a
dozen possibilities on sample data sets to see which one(s) work best. What
works best depends totally on your data; there is no such thing as a good
predefined hash function. (OTOH, if you don't have much data, anything might
be good enough -- but if that is true, why use the hashed container form at
all? The Ordered forms are easier to use and cost about the same on small
data sets.)

                                                Randy.






^ permalink raw reply	[relevance 0%]

* Ada Hash
@ 2007-04-24 15:37  4% markp
  2007-04-24 18:12  0% ` Randy Brukardt
  2007-04-29 18:33  0% ` Matthew Heaney
  0 siblings, 2 replies; 65+ results
From: markp @ 2007-04-24 15:37 UTC (permalink / raw)


I need to use a hash table to store data using an integer as the key.
I would like to use Ada.Containers.Hashed_Maps. Is there a default
hash function for an integer that I can supply to the instantiation
and could somebody provide a quick sample piece of code to do this?




^ permalink raw reply	[relevance 4%]

* Re: How to use associative arrays in Ada 2005?
  @ 2006-11-27 22:22  3%               ` Matthew Heaney
  0 siblings, 0 replies; 65+ results
From: Matthew Heaney @ 2006-11-27 22:22 UTC (permalink / raw)


Simon Wright wrote:
> 
> There's no reason I can see to use maps of maps *for this problem*. I
> would use a hash function (Nation x Family_Name x Name) => Hash_Type
> in a single map.

Yes, that's right, you could do it either way.

I prefer that OP's approach of using a map of maps of maps, since that 
allows you to perform queries like "give me all the entries for this 
nation", etc.


> Matt, I see (A.18.4(5)) that Ada.Containers doesn't allow hash
> collisions (which, if true, is demanding a lot of hash function
> designers). 

A.18.4 (5) says:

5/2{AI95-00302-03} {node (of a map)} A map contains pairs of keys and 
elements, called nodes. Map cursors designate nodes, but also can be 
thought of as designating an element (the element contained in the node) 
for consistency with the other containers. There exists an equivalence 
relation on keys, whose definition is different for hashed maps and 
ordered maps. A map never contains two or more nodes with equivalent 
keys. The length of a map is the number of nodes it contains.{length (of 
a map)}

The RM is here:

http://www.adaic.com/standards/05aarm/html/AA-A-18-4.html


You're misreading that paragraph.  It does say that:

"A map never contains two or more nodes with equivalent keys."

but that's very different from saying "hash collisions aren't allowed."

That sentence is simply saying that this is a (unique-key) map, not a 
multi-map.

Hash functions can map key values to the same hash value (that can 
happen easily if your hash function is poor).  That's not good if there 
are many such collisions, but it's certainly allowed by the standard.

But the hash value is only one aspect of a key used to determine its 
uniqueness: the other is equivalence.  The hash value of a key 
determines its bucket, and the equivalence relation is then used to 
compare the key to keys already in that bucket.

So it doesn't matter whether multiple keys hash to the same value (it 
would just mean they all get put in the same bucket), since the success 
of an insertion (say) is ultimately decided by equivalence.

Take a look at the generic formal region of the hashed map:

generic
    type Key_Type is private;

    type Element_Type is private;

    with function Hash (Key : Key_Type) return Hash_Type;

    with function Equivalent_Keys (Left, Right : Key_Type)
       return Boolean;

    with function "=" (Left, Right : Element_Type)
       return Boolean is <>;

package Ada.Containers.Hashed_Maps is

You have to supply both a hash function and an equivalence function. 
Typically you pass "=" as the generic actual for Equivalent_Keys, but 
not always.  (The reason why the general formal is declared that way is 
because equivalence is orthogonal to equality.)


 > If collisions aren't allowed we could go for an ordered
> map based on ... or some likely-to-be-more-efficient order.

If you have as your key a record comprising the 3 strings (nation, 
family name, given name), then "=" would be adequate for Equivalent_Keys.



^ permalink raw reply	[relevance 3%]

* Re: How to use associative arrays in Ada 2005?
    2006-11-21 11:49  5% ` Georg Bauhaus
@ 2006-11-21 14:18  4% ` Matthew Heaney
    1 sibling, 1 reply; 65+ results
From: Matthew Heaney @ 2006-11-21 14:18 UTC (permalink / raw)


"snoopysalive" <matthias.kistler@gmx.de> writes:

> Can anybody explain me how to use associative arrays in Ada 2005? I
> mean hashes like in Perl. 

There are actually two kinds of "associative arrays" in Ada05: ordered maps and
hashed maps.  Each of these container types accept both a key and element as
generic formal types.  (There are also sets, that accept just an element type.)

If both the key and element are "definite" types (types that don't require a
constraint to specify the size), then you can use:

ada.containers.hashed_maps
ada.containers.ordered_maps

If either the key or element is an "indefinite" type (such as String), then you
can use:

ada.containers.indefinite_hashed_maps
ada.containers.indefinite_ordered_maps

If you use the hashed version, you'll need a hash function for your key type.
The library comes with a hash function for type String (and Wide_String, etc);
see Ada.Strings.Hash.

If you use the ordered version, you'll need a relation operation ("<") for your
key type.


> In my opinion, C++ is cool but Ada's syntax is
> better to avoid dirty code.

If you're familiar with the STL, then you should have no trouble with the Ada
container library.


> So, when I say "hash", I mean something Perl-like like
> "$hashname{key1}{key2} = 'something' ". Or something C++-like like "map
> <string,int> hashname; hashname["key1"] = 42;".

Right.  People often say "hash" but in reality that's just one way of
implementing an associative container.  In Ada05 you can use either the hashed
version or the ordered version.

Your example above would be:

package String_Integer_Maps is
  new Ada.Containers.Indefinite_Hashed_Maps
    (String,
     Integer,
     Ada.Strings.Hash,
     Equivalent_Keys => "=");

Note that there's no index operator (operator[]()) in Ada, so you say:

  procedure Op (M : in out String_Integer_Maps.Map) is
  begin
    M.Include ("key1", 42);
  end;

Note that this is not exactly equivalent to the C++ operation, since in Ada the
key is replaced too (if it already exists).  That's unnecessary here so a
slightly more efficient technique might be:

  procedure Op (M : in out String_Integer_Maps.Map) is
    C : Cursor;
    B : Boolean;
  begin
    M.Insert ("key1", 42, C, B);
    if not B then
      M.Replace_Element (C, 42);
    end if;
  end Op;


> Is there anything similar in Ada 2005, too? I bought the book
> "Programming in Ada 2005" by John Barnes but I don't understand the
> author's way of explaining a programming language (for me, the book is
> more confusing than explaining).

That book has a chapter (17?) on the container library.  If you have a specific
question just post here or send me some email.


> Or does anybody know a good tutorial similar to the lots of C- and
> Perl-tutorials? I found many Ada-tutorials but most of them just list
> the contents of the built-in packages and don't explain how to use
> them. As a beginner this is confusing.

If just gave a tutorial on the Ada05 container library.  I can give you the
power-point slides or send you a pdf version.  Drop me a line if you're
interested.

But as a said above, the Ada standard container library is very similar to the
C++ STL, so if you already know the latter than the former shouldn't be much of
a stretch.

-Matt



^ permalink raw reply	[relevance 4%]

* Re: How to use associative arrays in Ada 2005?
  @ 2006-11-21 11:49  5% ` Georg Bauhaus
  2006-11-21 14:18  4% ` Matthew Heaney
  1 sibling, 0 replies; 65+ results
From: Georg Bauhaus @ 2006-11-21 11:49 UTC (permalink / raw)


On Tue, 2006-11-21 at 02:11 -0800, snoopysalive wrote:

> So, when I say "hash", I mean something Perl-like like
> "$hashname{key1}{key2} = 'something' ". Or something C++-like like "map
> <string,int> hashname; hashname["key1"] = 42;".

With Ada 2005, you have Ada.Containers.Hashed_Maps;

 hashname.insert("key1", 42);

See
http://en.wikibooks.org/wiki/Ada_Programming/Containers#First_Example:_Maps
for an example.

The original Ada.Containers distribution at 
http://charles.tigris.org/

has a Containers tutorial linked at the bottom of the page.


>  I found many Ada-tutorials but most of them just list
> the contents of the built-in packages and don't explain how to use
> them. As a beginner this is confusing.

Maybe try John English's 
http://burks.bton.ac.uk/burks/language/ada/adacraft/contents.htm

It doesn't cover the Ada 2005 containers, though.





^ permalink raw reply	[relevance 5%]

* Re: The Computer Language Shootout Benchmarks
  2006-05-03 14:55  4%       ` Matthew Heaney
  2006-05-03 16:15  0%         ` Martin Krischik
@ 2006-05-03 17:11  3%         ` Tapio Kelloniemi
  1 sibling, 0 replies; 65+ results
From: Tapio Kelloniemi @ 2006-05-03 17:11 UTC (permalink / raw)


"Matthew Heaney" <mheaney@on2.com> wrote:
>Tapio Kelloniemi wrote:
>> "Matthew Heaney" <mheaney@on2.com> wrote:
>> >And why do you assume that???
>>
>> Because GNAT.[Dynamic_]HTable has been designed to be extremely efficient.
>
>And why do you assume that Ada.Containers.Hashed_Maps aren't also
>designed to be extremely efficient?  Why would the designer do it any
>other way?

To support some Ada features (such as controlled types) or to increase safety 
with additional run-time checks. This is of course how a standard library 
implementation must behave, but some particular hash table implemetation could 
drop support for controlled types and suppress all checks, if such features 
woudn't be required (or were considered to be too expensive).

>> Note that this is just an assumption, I have not read the code of GNAT's
>> HTable implementation,
>
>If you haven't read the sources, then how do you know?

I expected that they had taken the same aggressive approach with Htables as 
with Tables, but diving into the code reveals that this is not the case (at 
least not with the Simple_Htable). However, the Static_Htable can be more 
efficient than Ada.Containers.Hashed_Maps, depending on the situation. Sorry 
for misinforming.

>> it allows
>> direct access to the data structure
>
>Again, this is a specious argument, since having access to the
>underlying data structure is irrelevant.  It's direct access to an
>element that's important.
>
>The standard library provides something very close to direct access: it
>allows in situ access to elements via Query_Element and Update_Element.

But if we want to get every microsecond out of it, though generally we don't 
want to.

>> and it uses realloc to resize the table.
>
>Well obviously this will only work if the element type isn't
>controlled.  So you're comparing apples to oranges, since the standard
>containers must support any non-limited type, including controlled type
>and discriminated types.

I was considering something that could be used in the shootout tests and I 
think that support for controlled types is not needed there. But this is now 
unrelevant since GNAT.HTable does not use realloc as I thought, so the only 
benefit gained by using GNAT.HTable is that it reduces the number of lines of 
code when there is no need to include the hash table implementation in the 
code (LOCs are also measured in shootout).

-- 
Tapio



^ permalink raw reply	[relevance 3%]

* Re: The Computer Language Shootout Benchmarks
  2006-05-03 14:55  4%       ` Matthew Heaney
@ 2006-05-03 16:15  0%         ` Martin Krischik
  2006-05-03 17:11  3%         ` Tapio Kelloniemi
  1 sibling, 0 replies; 65+ results
From: Martin Krischik @ 2006-05-03 16:15 UTC (permalink / raw)


Matthew Heaney wrote:

>> Because GNAT.[Dynamic_]HTable has been designed to be extremely
>> efficient.
> 
> And why do you assume that Ada.Containers.Hashed_Maps aren't also
> designed to be extremely efficient? ï¿œWhy would the designer do it any
> other way?

Well, when the code is finished we can have a container shootout as well.
But for the main shootout it will be GNAT.HTable - it does the trick and it
is there (won't increase out lines of code rating).

Martin 
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



^ permalink raw reply	[relevance 0%]

* Re: The Computer Language Shootout Benchmarks
  2006-05-02 21:32  4% ` Tapio Kelloniemi
  2006-05-02 22:37  0%   ` Matthew Heaney
@ 2006-05-03 16:05  0%   ` Martin Krischik
  1 sibling, 0 replies; 65+ results
From: Martin Krischik @ 2006-05-03 16:05 UTC (permalink / raw)


Tapio Kelloniemi wrote:

> Martin Krischik <krischik@users.sourceforge.net> wrote:
>>Hello
>>
>>Ada is currently missing 2 programs on the "The Computer Language Shootout
>>Benchmarks" [1].
>>
>>I would not mind having a go. But before I start:
>>
>>One test [2] needs a hash table implementation. The compiler used has
>>no Ada 2005 features so one need to add the implementation to the code
>>itself. Anybody can got a stand alone implementation? Or knows which
>>collections class lib could easily been torn apart?
> 
> Since the compiler is most likely GNAT, there are always the
> GNAT.[Dynamic_]HTable packages. I assume they are also faster than
> Ada.Containers.Hashed_Maps would be.

I found them and I think they will do nicely.

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



^ permalink raw reply	[relevance 0%]

* Re: The Computer Language Shootout Benchmarks
  2006-05-03 10:12  0%     ` Tapio Kelloniemi
@ 2006-05-03 14:55  4%       ` Matthew Heaney
  2006-05-03 16:15  0%         ` Martin Krischik
  2006-05-03 17:11  3%         ` Tapio Kelloniemi
  0 siblings, 2 replies; 65+ results
From: Matthew Heaney @ 2006-05-03 14:55 UTC (permalink / raw)



Tapio Kelloniemi wrote:
> "Matthew Heaney" <mheaney@on2.com> wrote:
> >And why do you assume that???
>
> Because GNAT.[Dynamic_]HTable has been designed to be extremely efficient.

And why do you assume that Ada.Containers.Hashed_Maps aren't also
designed to be extremely efficient?  Why would the designer do it any
other way?


> Note that this is just an assumption, I have not read the code of GNAT's
> HTable implementation,

If you haven't read the sources, then how do you know?


> but at least GNAT.Dynamic_Tables is more efficient
> than Ada.COntainers.Vectors

But we were talking about Dynamic_Tables vs. Hashed_Maps.


> since it does not use controlled types,

A specious argument, since the only thing controlled types are used for
is to reclaim storage when the scope of the declaration of the
container object ends.  The fact that the hashed_map container type
privately derives from type Controlled has no bearing on the efficiency
of insertions, deletions, or searches.



> it allows
> direct access to the data structure

Again, this is a specious argument, since having access to the
underlying data structure is irrelevant.  It's direct access to an
element that's important.

The standard library provides something very close to direct access: it
allows in situ access to elements via Query_Element and Update_Element.


> and it uses realloc to resize the table.

Well obviously this will only work if the element type isn't
controlled.  So you're comparing apples to oranges, since the standard
containers must support any non-limited type, including controlled type
and discriminated types.


> When Ada.Containers container is resized, new memory must be allocated, data
> must be copied and the old memory must be freed. Realloc may avoid this.

This is all very confused.

In the case of the vector container, then yes, to expand the container
(that is, increase its capacity), then you must allocate a new, longer
internal array (assuming it's implemented that way), and then copy the
existing items onto the new array.

It is specious to argue that "realloc can avoid this," since you can't
use realloc to expand an array whose components are controlled or have
default initialization.  (Or to put it another way, you could use
realloc, but then you'd have to use compiler magic to perform
controlled actions.)

Furthermore, your argument ignores the fact the expansion of the vector
capacity is not done willy-nilly.  Expansion occurs in a specific way,
such that the *amortized* cost of insertion has a time complexity of
O(1).  Hard to beat that!  So the realloc argument is a red herring,
since you can't do any better then O(1).

The situation for hashed containers is a different.  When a hashed
container expands, then elements aren't copied, they are moved.  This
is distinctly unlike the case for vector.

A hashed container has an internal hash table, which is just an array
whose components are pointers to nodes, which hold an element and a
pointer to the next node in that bucket.  When you expand a hashed
container, you allocate a new buckets array, then re-hash elements from
the old buckets array onto the new one.  To be sure there is a cost for
this, but it's not because of copying of elements.

Again, realloc wouldn't help here, since you have to ensure that each
bucket is initialized to null (indicating that there are no elements in
that bucket).


> However, my opinion is that Ada.Containers is much more convenient and also
> safer.

And on that point we agree.

Note that I'm giving a tutorial on the standard container library in
Porto on 5 June.

Regards,
Matt




^ permalink raw reply	[relevance 4%]

* Re: The Computer Language Shootout Benchmarks
  2006-05-02 22:37  0%   ` Matthew Heaney
@ 2006-05-03 10:12  0%     ` Tapio Kelloniemi
  2006-05-03 14:55  4%       ` Matthew Heaney
  0 siblings, 1 reply; 65+ results
From: Tapio Kelloniemi @ 2006-05-03 10:12 UTC (permalink / raw)


"Matthew Heaney" <mheaney@on2.com> wrote:
>Tapio Kelloniemi wrote:
>>
>> Since the compiler is most likely GNAT, there are always the
>> GNAT.[Dynamic_]HTable packages. I assume they are also faster than
>> Ada.Containers.Hashed_Maps would be.
>
>And why do you assume that???

Because GNAT.[Dynamic_]HTable has been designed to be extremely efficient.
Note that this is just an assumption, I have not read the code of GNAT's
HTable implementation, but at least GNAT.Dynamic_Tables is more efficient
than Ada.COntainers.Vectors since it does not use controlled types, it allows
direct access to the data structure and it uses realloc to resize the table.
When Ada.Containers container is resized, new memory must be allocated, data
must be copied and the old memory must be freed. Realloc may avoid this.

However, my opinion is that Ada.Containers is much more convenient and also
safer.

-- 
Tapio



^ permalink raw reply	[relevance 0%]

* Re: The Computer Language Shootout Benchmarks
  2006-05-02 21:32  4% ` Tapio Kelloniemi
@ 2006-05-02 22:37  0%   ` Matthew Heaney
  2006-05-03 10:12  0%     ` Tapio Kelloniemi
  2006-05-03 16:05  0%   ` Martin Krischik
  1 sibling, 1 reply; 65+ results
From: Matthew Heaney @ 2006-05-02 22:37 UTC (permalink / raw)



Tapio Kelloniemi wrote:
>
> Since the compiler is most likely GNAT, there are always the
> GNAT.[Dynamic_]HTable packages. I assume they are also faster than
> Ada.Containers.Hashed_Maps would be.

And why do you assume that???




^ permalink raw reply	[relevance 0%]

* Re: The Computer Language Shootout Benchmarks
  @ 2006-05-02 21:32  4% ` Tapio Kelloniemi
  2006-05-02 22:37  0%   ` Matthew Heaney
  2006-05-03 16:05  0%   ` Martin Krischik
  0 siblings, 2 replies; 65+ results
From: Tapio Kelloniemi @ 2006-05-02 21:32 UTC (permalink / raw)


Martin Krischik <krischik@users.sourceforge.net> wrote:
>Hello
>
>Ada is currently missing 2 programs on the "The Computer Language Shootout
>Benchmarks" [1].
>
>I would not mind having a go. But before I start:
>
>One test [2] needs a hash table implementation. The compiler used has
>no Ada 2005 features so one need to add the implementation to the code
>itself. Anybody can got a stand alone implementation? Or knows which
>collections class lib could easily been torn apart?

Since the compiler is most likely GNAT, there are always the
GNAT.[Dynamic_]HTable packages. I assume they are also faster than
Ada.Containers.Hashed_Maps would be.

-- 
Tapio



^ permalink raw reply	[relevance 4%]

* Re: hashed_maps
  @ 2005-10-13 14:55  5%         ` Matthew Heaney
  0 siblings, 0 replies; 65+ results
From: Matthew Heaney @ 2005-10-13 14:55 UTC (permalink / raw)



Lucretia wrote:
>
> This is why I'm using a map to map between them, i.e. so I can find the
> Ada instance associated with the C++ instance.

Well, I still don't know what you're doing, but if you need a map then
Ada has those:

ada.containers.hashed_maps
ada.containers.ordered_maps

If you're using GCC 4.0, then you should have these in your path.
Otherwise, you might have to download a reference implementation.

http://charles.tigris.org/

For your problem, either map will do.  Type System.Address has a
relational operator ("<") so you can use that type (or equivalently, an
access type) as the key of the ordered map; or, you can use To_Integer
(declared in package System, or one of its children -- I forget which)
to implement a hash function, and hence use it as the key of the hashed
map.

-Matt




^ permalink raw reply	[relevance 5%]

* Re: puzzled re hyperthreaded performance
  @ 2005-09-20  0:26  4% ` tmoran
  0 siblings, 0 replies; 65+ results
From: tmoran @ 2005-09-20  0:26 UTC (permalink / raw)


> I think the hypertreading is more like multiplexing the cpu over different
> threads without the
> cost of taskswitching but t does not give you additional CPU performance.
> This effect can make measurements very confusing.
>
> I whould like to know if anyone has a different experience.
  Running a sort on 100K floats (so about 400K bytes) I find a substantial
speedup (about 30%) if it's split into two simultaneous tasks, the main
program and an Ada task.
  Using Ada.Containers.Hashed_Maps to find all the n-character strings in
a given 50K string of just 4 letters (simulated DNA) with the main
program doing it for n=12 and the extra Ada task for n=18, there is no
improvement, and in fact a loss.  But the same program run with n=6 and
n=8 does show significant speedup.  In the former case, there are roughly
50K strings of the indicated sizes, so the hash table presumably has about
50000*18 bytes of key data or roughly 1MB, so the second task, which needs
another 1MB hash table, is probably fighting over the processor's total 1MB
cache space, while in the latter case there is probably room for both in
cache.  If I switch to Ada.Containers.Indefinite_Hashed_Maps, then even
the small one shows no multitasking speedup.  I haven't analyzed
Indefinite_Hashed_Maps to see why that might be the case.
  Running a word counting program, there was a substantial speedup by
processing alternate buffer loads via the environment task and a second
task.  In that case there was probably no cache contention, and one
task was probably processing its bufferload while the other was doing
IO (which probably did not come off disk except on the first run of
the program).
  The above is on a hyperthreaded Pentium under Windows 2000. YMMV



^ permalink raw reply	[relevance 4%]

* Re: puzzled re hyperthreaded performance
  @ 2005-09-15 21:43  5% ` tmoran
  0 siblings, 0 replies; 65+ results
From: tmoran @ 2005-09-15 21:43 UTC (permalink / raw)


Further data:

>     Cache thrashing certainly comes to mind as a possible problem with
> multi-threading.  But the program at hand is small.  I cut the problem
> size so there are at most two simultaneous hash tables of Length 4091 for
> 6 character Strings, which, assuming 20 bytes/entry would be 2*4000*20 or
> 160K, plus 50K input String = 210K which I would think would fit well
> inside the cache without thrashing.  But it's still slower (about 10%) to
> run multithreaded than sequential.
   Since this used exclusively 6 character Strings, I changed from
Ada.Containers.Indefinite_Hashed_Maps to
Ada.Containers.Hashed_Maps
Now the parallel version runs 30% faster than the sequential one.
Increasing the string (and thus hash table) size to more probably
thrash the cache brings it back to parallel being slower than sequential.
So part of the problem is cache thrashing, but there's still some
remaining problem when using Indefinite_Hashed_Maps.



^ permalink raw reply	[relevance 5%]

* Re: Hash table
  @ 2005-08-13 23:58  4% ` Matthew Heaney
  0 siblings, 0 replies; 65+ results
From: Matthew Heaney @ 2005-08-13 23:58 UTC (permalink / raw)


David Trudgett <wpower@zeta.org.au.nospamplease>  writes:

> I'm looking for a Free Software Ada95 container library, or in
> particular, a hash table (or associative list).

Ada 2005 will have both sets and maps.  You're asking specifically for a
hashed version, but keep in mind that an ordered container (having an
identical interface, but different time complexity) might satisfy your
needs just as well.

The hashed containers are:

ada.containers.hashed_sets
ada.containers.indefinite_hashed_sets
ada.containers.hashed_maps
ada.containers.indefinite_hashed_maps

The ordered forms are:

ada.containers.ordered_sets
ada.containers.indefinite_ordered_sets
ada.containers.ordered_maps
ada.containers.indefinite_ordered_maps


> I noticed that Ada 2005 may include a standard Ada.Containers library,
> but is there a working prototype of this available now?

Yes, there's a public reference implementation available here:

http://charles.tigris.org/

Follow the links to the CVS repository, to the ai302 subdirectory.  The
names use gnat run-time syntax, so you're looking for:

a-cohama.ad[sb]
a-cihama.ad[sb]
a-cohase.ad[sb]
a-cihase.ad[sb]

Note that these will only compile with an Ada 2005 compiler.  If you're
using the latest version of gnat, use the -gnat05 switch to compile them.

If you don't have an Ada 2005 compiler, then you can modify the sources
to your liking.  That will mean replacing an anonymous access subprogram
parameters with something else, typically a generic operation that
passes the subprogram as a generic formal.  For example, the procedure:

procedure Iterate
  (Container : in Map;
   Process   : not null access procedure (Position : Cursor));

should be re-written as:

generic
   with procedure Process (Position : Cursor);
procedure Generic_Iteration (Container : in Map);

(Note that replacing the anonymous access subprogram parameters with
named access parameters is probably not what you want, because of the
accessibility rules.  Hence the generic declaration above.)


> A bit of searching found the ASL (Ada Structured Library --
> http://sourceforge.net/projects/adasl), which hasn't had a new release
> since 2001 (which means it must be perfect ;-)). Should I use ASL, and
> if I do, will it be completely different from the proposed
> Ada.Containers standard?

I would try to use a reference implementation of the standard library,
modified as necessary to run under pure Ada 95.  I can help you make the
necessary modifications.

In general, we prefer that users use the standard library now, since
that helps us find bugs in the design of the library (early adopters
have been extremely helpful here), and in the actual implementation of
the library.


> I'm using Gnat (3.15p) on Debian Linux.

Do you have access to gcc 4.x?  The standard container library is
already bundled with the gcc 4 release.

-Matt



^ permalink raw reply	[relevance 4%]

* Re: Spellcheck.adb
  2005-04-27 15:11  0%     ` Spellcheck.adb Matthew Heaney
@ 2005-04-27 22:49  0%       ` Albert Bachmann
  0 siblings, 0 replies; 65+ results
From: Albert Bachmann @ 2005-04-27 22:49 UTC (permalink / raw)


On Wed, 27 Apr 2005 11:11:06 -0400, Matthew Heaney wrote:
> 
> "Albert Bachmann" <albert.bachmann@gmx.de> wrote in message 
> news:1114548638.851249.246280@f14g2000cwb.googlegroups.com...
>>
>> In the meantime I followed the advice from Matthew Heaney (thanks
>> Matthew) and went on with  Ada.Containers. I again tried a simple
>> implementation. Unfortunately I recognized that controlled types have
>> to be instantiated at library level
> 
> This is no longer true in Ada 2005.  Eventually you'll be able to 
> instantiate the containers in your main subprogram.

That may be true but with GNAT (as of GCC-4.0.0) I get an error:

gcc -c -gnat05 -O3 spellcheck3.adb
spellcheck3.adb:10:09: instantiation error at a-cohata.ads:29
spellcheck3.adb:10:09: instantiation error at a-cihase.ads:214
spellcheck3.adb:10:09: controlled type must be declared at the library level
gnatmake: "spellcheck3.adb" compilation error

> 
> 
>> which requires now a total of 3
>> files since the instantiation of ada.containers.hashed_maps needs a
>> definite subtype for its key_type.
> 
> True, but that's why we have the Indefinite_Hashed_Maps container package. 
> If you're manipulating strings, you should probably be using that.
> 
> Also, as I mentioned in an earlier post, you don't really need a map, since 
> all you're doing is performing a membership test.  Hence, a (hashed) set 
> will do.
> 

When I use and instance of indefinite_hashed_sets or
indefinite_hashed_maps the program compiles fine but I get an exception
during runtime:

raised CONSTRAINT_ERROR : a-cihama.adb:443 explicit raise (for the map)
raised CONSTRAINT_ERROR : a-cihase.adb:375 explicit raise (for the set)


>> types.ads:
>> ----------
>>
>> package types is
>> subtype word is string(1 .. 128);
>> end types;
> 
> Get rid of this.  Instantiate the Indefinite_Hashed_Sets (or _Maps, if you 
> prefer) with type String.
> 

Ok.

> 
>> hashmap.ads:
>> ------------
>>
>> package hashmap is new ada.containers.hashed_maps(key_type  =>
>> types.word,
> 
> No.  This should just be type String.
> 

Ok.

> 
>> and finally spellcheck2.adb:
>> ----------------------------
>>
>> strings.fixed.delete(item, item_offset + 1, word'last);
> 
> You can get rid of this.

I did.
> 
> 
>> hashmap.insert(dict, item, true);
> 
> If you used a (hashed) set, then all you'd have to say is
> 
>   insert(dict, item);
> 
> 
>> strings.fixed.delete(item, item_offset + 1, word'last);
> 
> You can get rid of this.
> 

Here too.

> 
>> if not hashmap.contains(dict, item) then
> 
> See, you're not using the map element at all.  You're only using the key.
> 
> 
>> Please note that those attempts are only quick and dirty hacks. If
>> someone knows how to get rid of the seperate types.ads file I would
>> welcome his advice.
> 
> You can get rid of the types file, by using the indefinite hashed map (or 
> set), and instantiating it with type String.
> 
> -Matt

I post the modified source. Maybe I've overlooked something because as I
said above during runtime I get an exception. However thanks for those
valuable information Matthew!

hashed_set.ads:
--------------

with ada.containers.indefinite_hashed_sets;
with ada.strings.hash;
--with hash_function;

package hashed_set is new ada.containers.indefinite_hashed_set(
				element_type    => string,
				hash            => ada.strings.hash, --hash_function,
				equivalent_keys => "=");

spellcheck3.adb:
----------------

with ada.strings.fixed;
with ada.text_io;
with hashed_set;
use  ada;

procedure spellcheck3 is

	file        : text_io.file_type;
	item        : string(1 .. 128);
	item_offset : natural;
	dict        : hashed_set.set;

begin

	text_io.open(file, text_io.in_file, "spellcheck-dict.txt");
	
	while not text_io.end_of_file(file) loop
		text_io.get_line(file, item, item_offset);
		hashed_set.insert(dict, item);
	end loop;

	while not text_io.end_of_file loop
		text_io.get_line(item, item_offset);
		if not hashed_set.contains(dict, item) then
			text_io.put_line(item);
		end if;
	end loop;
	
	text_io.close(file);

end spellcheck3;


Regards,
Albert



^ permalink raw reply	[relevance 0%]

* Re: Spellcheck.adb
  2005-04-26 20:50  5%   ` Spellcheck.adb Albert Bachmann
@ 2005-04-27 15:11  0%     ` Matthew Heaney
  2005-04-27 22:49  0%       ` Spellcheck.adb Albert Bachmann
  0 siblings, 1 reply; 65+ results
From: Matthew Heaney @ 2005-04-27 15:11 UTC (permalink / raw)



"Albert Bachmann" <albert.bachmann@gmx.de> wrote in message 
news:1114548638.851249.246280@f14g2000cwb.googlegroups.com...
>
> In the meantime I followed the advice from Matthew Heaney (thanks
> Matthew) and went on with  Ada.Containers. I again tried a simple
> implementation. Unfortunately I recognized that controlled types have
> to be instantiated at library level

This is no longer true in Ada 2005.  Eventually you'll be able to 
instantiate the containers in your main subprogram.


> which requires now a total of 3
> files since the instantiation of ada.containers.hashed_maps needs a
> definite subtype for its key_type.

True, but that's why we have the Indefinite_Hashed_Maps container package. 
If you're manipulating strings, you should probably be using that.

Also, as I mentioned in an earlier post, you don't really need a map, since 
all you're doing is performing a membership test.  Hence, a (hashed) set 
will do.

> types.ads:
> ----------
>
> package types is
> subtype word is string(1 .. 128);
> end types;

Get rid of this.  Instantiate the Indefinite_Hashed_Sets (or _Maps, if you 
prefer) with type String.


> hashmap.ads:
> ------------
>
> package hashmap is new ada.containers.hashed_maps(key_type  =>
> types.word,

No.  This should just be type String.


> and finally spellcheck2.adb:
> ----------------------------
>
> strings.fixed.delete(item, item_offset + 1, word'last);

You can get rid of this.


> hashmap.insert(dict, item, true);

If you used a (hashed) set, then all you'd have to say is

  insert(dict, item);


> strings.fixed.delete(item, item_offset + 1, word'last);

You can get rid of this.


> if not hashmap.contains(dict, item) then

See, you're not using the map element at all.  You're only using the key.


> Please note that those attempts are only quick and dirty hacks. If
> someone knows how to get rid of the seperate types.ads file I would
> welcome his advice.

You can get rid of the types file, by using the indefinite hashed map (or 
set), and instantiating it with type String.

-Matt






^ permalink raw reply	[relevance 0%]

* Re: Spellcheck.adb
  @ 2005-04-26 20:50  5%   ` Albert Bachmann
  2005-04-27 15:11  0%     ` Spellcheck.adb Matthew Heaney
  0 siblings, 1 reply; 65+ results
From: Albert Bachmann @ 2005-04-26 20:50 UTC (permalink / raw)


Hello David,

my system is a Fedora Core 3 Linux box with a Pentium 4 running at 2.4
GHz with 512MB RAM. I use GNAT from GCC-4.0.0. with -O3. All my timings
are for n = 1.

In the meantime I followed the advice from Matthew Heaney (thanks
Matthew) and went on with  Ada.Containers. I again tried a simple
implementation. Unfortunately I recognized that controlled types have
to be instantiated at library level which requires now a total of 3
files since the instantiation of ada.containers.hashed_maps needs a
definite subtype for its key_type.

Anyway the resulting solution runs with the following times:

real    0m0.352s
user    0m0.318s
sys     0m0.015s

Here are the file contents:

types.ads:
----------

package types is
	subtype word is string(1 .. 128);
end types;

hashmap.ads:
------------

with ada.strings.fixed;
with ada.strings.hash;
with ada.containers.hashed_maps;
with types;

package hashmap is new ada.containers.hashed_maps(key_type  =>
types.word,
					element_type    => boolean,
					hash            => ada.strings.hash,
					equivalent_keys => "=");

and finally spellcheck2.adb:
----------------------------

with ada.strings.fixed;
with ada.strings.hash;
with ada.text_io;
with ada.integer_text_io;
with hashmap;
with types;
use  types;
use  ada;

procedure spellcheck2 is

	subtype index is natural range 0 .. 2760;

	file        : text_io.file_type;
	item        : word;
	item_offset : natural;
	dict        : hashmap.map;

begin

	text_io.open(file, text_io.in_file, "spellcheck-dict.txt");

	while not text_io.end_of_file(file) loop
		text_io.get_line(file, item, item_offset);
		strings.fixed.delete(item, item_offset + 1, word'last);
		hashmap.insert(dict, item, true);
	end loop;

	while not text_io.end_of_file loop
		text_io.get_line(item, item_offset);
		strings.fixed.delete(item, item_offset + 1, word'last);
		if not hashmap.contains(dict, item) then
			text_io.put_line(item);
		end if;
	end loop;

	text_io.close(file);

end spellcheck2;

Please note that those attempts are only quick and dirty hacks. If
someone knows how to get rid of the seperate types.ads file I would
welcome his advice.

So far I have not tested your proposed solution but eventually I will
do so and let you know about any possible issues.

Regards,
Albert




^ permalink raw reply	[relevance 5%]

* Re: Ada bench
  2005-04-19 23:22  0%           ` David Sauvage
@ 2005-04-20  0:49  0%             ` Matthew Heaney
  0 siblings, 0 replies; 65+ results
From: Matthew Heaney @ 2005-04-20  0:49 UTC (permalink / raw)


"David Sauvage" <pariakaman@gmail.com> writes:

> - for the GNAT.Spitbol.Table method, i just instantiate the generic
> with 40_000 as discriminant (same value as the C spellcheck
> implementation)

I would also call Reserve_Capacity with a value of 40_000, in both the
hashed map and hashed set versions.


> - for the AI302.Containers.Indefinite_Hashed_Maps method i used the
> default AI302.Strings.Hash as hash function
> - When using the Ada.Streams.Stream_IO method, i used a 4_096 buffer.
> 
> GNAT.Spitbol.Table + Ada.Streams.Stream_IO    (*1)              : 145
> GNAT.Spitbol.Table + Ada.Text_IO                                : 175
> AI302.Containers.Indefinite_Hashed_Maps + Ada.Streams.Stream_IO : 278
> AI302.Containers.Indefinite_Hashed_Maps + Ada.Text_IO           : 310

This is good data.  Can you make a version that uses the (indefinite)
hashed set too, and post those results?

Also, can you post a data set with the new hash function (see below)?


> "Matthew Heaney" <mhea...@on2.com> wrote
> > Have you tried using the C++ hash function with an Ada program that
> > uses Ada.Containers.Hashed_Maps?
> 
> nop, excellent i will try this soon.

Here's the hash function:

function Hash_Word (S : String) return Hash_Type is
   H : Hash_Type := 0;
begin
   for I in S'Range loop
      H := 5 * H + Character'Pos (S (I));
   end loop;
   return H;
end Hash_Word;


-Matt



^ permalink raw reply	[relevance 0%]

* Re: Ada bench
  2005-04-19 16:43  4%         ` Matthew Heaney
@ 2005-04-19 23:22  0%           ` David Sauvage
  2005-04-20  0:49  0%             ` Matthew Heaney
  0 siblings, 1 reply; 65+ results
From: David Sauvage @ 2005-04-19 23:22 UTC (permalink / raw)


Hi,
thanks for your advised comments,

"Matthew Heaney" <mhea...@on2.com> wrote
> How do you know from where the improvement came -- the change in
> data structure or the change in read mechanism?

please not i run the program just a few times to get an average value.
(System config : GNU/Debian Sid kernel 2.6.10, AMD 1.2 Ghz)
- Values are in millisecond, N=1
- for the GNAT.Spitbol.Table method, i just instantiate the generic
with 40_000 as discriminant (same value as the C spellcheck
implementation)
- for the AI302.Containers.Indefinite_Hashed_Maps method i used the
default AI302.Strings.Hash as hash function
- When using the Ada.Streams.Stream_IO method, i used a 4_096 buffer.

GNAT.Spitbol.Table + Ada.Streams.Stream_IO    (*1)              : 145
GNAT.Spitbol.Table + Ada.Text_IO                                : 175
AI302.Containers.Indefinite_Hashed_Maps + Ada.Streams.Stream_IO : 278
AI302.Containers.Indefinite_Hashed_Maps + Ada.Text_IO           : 310

"Matthew Heaney" <mhea...@on2.com> wrote
> Have you tried using the C++ hash function with an Ada program that
> uses Ada.Containers.Hashed_Maps?

nop, excellent i will try this soon.

(*1) this implementaton can be also find here
http://psycose.apinc.org/ada_shootout/

--
David




^ permalink raw reply	[relevance 0%]

* Re: Ada bench
  @ 2005-04-19 16:43  4%         ` Matthew Heaney
  2005-04-19 23:22  0%           ` David Sauvage
  0 siblings, 1 reply; 65+ results
From: Matthew Heaney @ 2005-04-19 16:43 UTC (permalink / raw)



David Sauvage wrote:
>
> I've proposed a new spellcheck version without using the AI-302
> Containers, i've used GNAT.Spitbol.Table. The app is twice faster. I
> also used Ada.Streams.Stream_IO.Read.

How do you know from where the improvement came -- the change in data
structure or the change in read mechanism?

Also, what hash function have you been using?  I just compared
Ada.Strings.Hash to the hash function used in the C++ example, and the
C++ hash function does much better on the words in spellcheck-dict.txt.

Have you tried using the C++ hash function with an Ada program that
uses Ada.Containers.Hashed_Maps?

Note that strictly speaking, you don't need to use the hashed map,
since the shootout is comparing "hash tables."  In the standard
library, there are two kinds of hash tables, hashed maps and hashed
sets.  In the shootout spellcheck example, you only need a membership
test, so a hashed set will do just as well.  (Probably better, in
fact.)

-Matt




^ permalink raw reply	[relevance 4%]

* Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)
  2005-03-23 13:49  0%                           ` Matthew Heaney
@ 2005-03-23 15:27  0%                             ` Chris Jefferson
  0 siblings, 0 replies; 65+ results
From: Chris Jefferson @ 2005-03-23 15:27 UTC (permalink / raw)


Matthew Heaney wrote:
> "Dr. Adrian Wrigley" <amtw@linuxchip.demon.co.uk.uk.uk> writes:
> 
> 
>>>>Wouldn't that be std::map in C++?
>>>
>>>and in Ada 2005,
>>>
>>>Ada.Containers.Hashed_Maps  and  Ada.Containers.Hashed_Maps
> 
> 
> Ada 2005 will have both ordered ("sorted") maps and hashed maps.  The
> current C++ standard only has ordered maps.
> 

Just replying to this point.. The TR1 library edition will also have 
hashed maps, although I suspect Ada 2005 might beat it out :) (Although 
G++ 4.0 will have them as contained in the (almost) final version of the 
TR1 standard).



^ permalink raw reply	[relevance 0%]

* Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)
  2005-03-14 14:40  4%                         ` Dr. Adrian Wrigley
  2005-03-14 15:42  0%                           ` Peter Koch Larsen
@ 2005-03-23 13:49  0%                           ` Matthew Heaney
  2005-03-23 15:27  0%                             ` Chris Jefferson
  1 sibling, 1 reply; 65+ results
From: Matthew Heaney @ 2005-03-23 13:49 UTC (permalink / raw)


"Dr. Adrian Wrigley" <amtw@linuxchip.demon.co.uk.uk.uk> writes:

> >> Wouldn't that be std::map in C++?
> > 
> > and in Ada 2005,
> > 
> > Ada.Containers.Hashed_Maps  and  Ada.Containers.Hashed_Maps

Ada 2005 will have both ordered ("sorted") maps and hashed maps.  The
current C++ standard only has ordered maps.


> I have probably missed a trick in the C++, but I couldn't get
> std::map code to compile (except in the trivial cases):
> 
> #include <map>
> 
> struct compoundindex {
>   int a, b, c;
> };

This type doesn't have a relational operator, which you need to
instantiate the map.


> --
> The Ada associative arrays from the new draft standard
> are specified as something like:
> 
> generic
>    type Key_Type is private;
>    type Element_Type is private;
>    with function Hash (Key : Key_Type)
>       return Hash_Type is <>;
>    with function Is_Equal_Key (Left, Right : Key_Type)
>       return Boolean is "=";
>    with function "=" (Left, Right : Element_Type)
>       return Boolean is <>;
> package Ada.Containers.Hashed_Maps is...

Yes, but the analog to the C++ map is Ada.Containers.Ordered_Maps, not
the hashed map.


> Which clearly won't work unless you can find the three
> function generic parameters.  I don't see how this can be
> used easily in a generic context.

But this is true of C++ as well.  There's no magic: your key either
needs a relational operator (for the ordered map), or it needs a hash
function (for the hashed map).


> I don't think I am being *that* unreasonable in asking for arrays
> indexed by arbitrary types, without jumping through hoops :)

Requiring that the key used to instantiate a hashed map have a hash
function hardly constitutes "jumping through hoops."  In any event, you
probably want an ordered map anyway:

generic

   type Key_Type is private;

   type Element_Type is private;

   with function "<" (Left, Right : Key_Type) return Boolean is <>;

   with function "=" (Left, Right : Element_Type) return Boolean is <>;

package Ada.Containers.Ordered_Maps is


in which case all you need is a relational op:

  function "<" (L, R : Compound_Index) return Boolean is
  begin
    if L.A < R.A then
       return True;
    end if;

    if L.A > R.A then
      return False;
    end if;

    if L.B < R.B then ...
  end;

  package Compound_Index_Maps is
    new Ada.Containers.Ordered_Maps (Compound_Index, Float);

  declare
    M : Compound_Index_Maps.Map;
  begin
    M.Include (Key => (1, 2, 4), New_Item => 0.123);
  end;




^ permalink raw reply	[relevance 0%]

* Re: Newbie - HashMap!
  2005-03-17 15:19  4% ` Georg Bauhaus
  2005-03-17 16:42  0%   ` Martin Dowie
@ 2005-03-21  0:33  0%   ` Matthew Heaney
  1 sibling, 0 replies; 65+ results
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	[relevance 0%]

* Re: Newbie - HashMap!
  2005-03-17 15:19  4% ` Georg Bauhaus
@ 2005-03-17 16:42  0%   ` Martin Dowie
  2005-03-21  0:33  0%   ` Matthew Heaney
  1 sibling, 0 replies; 65+ results
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	[relevance 0%]

* Re: Newbie - HashMap!
  @ 2005-03-17 15:19  4% ` Georg Bauhaus
  2005-03-17 16:42  0%   ` Martin Dowie
  2005-03-21  0:33  0%   ` Matthew Heaney
  0 siblings, 2 replies; 65+ results
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	[relevance 4%]

* Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)
  2005-03-14 14:40  4%                         ` Dr. Adrian Wrigley
@ 2005-03-14 15:42  0%                           ` Peter Koch Larsen
  2005-03-23 13:49  0%                           ` Matthew Heaney
  1 sibling, 0 replies; 65+ results
From: Peter Koch Larsen @ 2005-03-14 15:42 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2548 bytes --]


"Dr. Adrian Wrigley" <amtw@linuxchip.demon.co.uk.uk.uk> skrev i en 
meddelelse 
news:pan.2005.03.14.14.41.47.813082@linuxchip.demon.co.uk.uk.uk...
> On Sat, 12 Mar 2005 09:42:26 +0100, Georg Bauhaus wrote:
>
>> Falk Tannh�user wrote:
>>> Dr. Adrian Wrigley wrote:
>>>
>>>> But what of features not present in either?
>>>
>>> [...]
>>>
>>>> associative arrays (from Perl)
>>>
>>> Wouldn't that be std::map in C++?
>>
>> and in Ada 2005,
>>
>> Ada.Containers.Hashed_Maps  and  Ada.Containers.Hashed_Maps
>
> I have probably missed a trick in the C++, but I couldn't get
> std::map code to compile (except in the trivial cases):
>
> #include <map>
>
> struct compoundindex {
>  int a, b, c;
> };
>
> int main()
> {
>    std::map<compoundindex, float> hash;
>    compoundindex fred = { 1, 2, 4 };
>
>    hash[fred] = 0.123;

First off, std::map is not hash-based. It is a sorted container. Now in 
order to use this, you need a comparing function. It can be the "standard" 
ordering function for your key or it can be a user specified function.

> }
>
> cpptest.cpp: In function `int main()':
> cpptest.cpp:14: error: `main()::compoundindex' uses local type
> `main()::compoundindex'
> (8 more lines of errors here!)

You must have snipped the most interesting errormessage.
>
> (what is the simplest C++ code to get this intent?)

What you need here is a comparator:

bool operator<(compoundindex const& lhs,compoundindex const& rhs)
{
....
}

And the map should work.
> --
> The Ada associative arrays from the new draft standard
> are specified as something like:
>
> generic
>   type Key_Type is private;
>   type Element_Type is private;
>   with function Hash (Key : Key_Type)
>      return Hash_Type is <>;
>   with function Is_Equal_Key (Left, Right : Key_Type)
>      return Boolean is "=";
>   with function "=" (Left, Right : Element_Type)
>      return Boolean is <>;
> package Ada.Containers.Hashed_Maps is...
>
>
> Which clearly won't work unless you can find the three
> function generic parameters.  I don't see how this can be
> used easily in a generic context.
>
> I don't think I am being *that* unreasonable in asking for arrays
> indexed by arbitrary types, without jumping through hoops :)

But I do! How can you index without at least knowing if two values are 
different or the same?
The C++ standard will have a hash-based container later. Those interested in 
having one can download one from e.g. boost (I believe this container also 
to be implemented by boost and prbably several others).

> -- 
> Adrian
>
/Peter 





^ permalink raw reply	[relevance 0%]

* Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)
  2005-03-12  8:42  6%                       ` Georg Bauhaus
@ 2005-03-14 14:40  4%                         ` Dr. Adrian Wrigley
  2005-03-14 15:42  0%                           ` Peter Koch Larsen
  2005-03-23 13:49  0%                           ` Matthew Heaney
  0 siblings, 2 replies; 65+ results
From: Dr. Adrian Wrigley @ 2005-03-14 14:40 UTC (permalink / raw)


On Sat, 12 Mar 2005 09:42:26 +0100, Georg Bauhaus wrote:

> Falk Tannh�user wrote:
>> Dr. Adrian Wrigley wrote:
>> 
>>> But what of features not present in either?
>> 
>> [...]
>> 
>>> associative arrays (from Perl)
>> 
>> Wouldn't that be std::map in C++?
> 
> and in Ada 2005,
> 
> Ada.Containers.Hashed_Maps  and  Ada.Containers.Hashed_Maps

I have probably missed a trick in the C++, but I couldn't get
std::map code to compile (except in the trivial cases):

#include <map>

struct compoundindex {
  int a, b, c;
};

int main()
{
    std::map<compoundindex, float> hash;
    compoundindex fred = { 1, 2, 4 };

    hash[fred] = 0.123;
}

cpptest.cpp: In function `int main()':
cpptest.cpp:14: error: `main()::compoundindex' uses local type
`main()::compoundindex'
(8 more lines of errors here!)

(what is the simplest C++ code to get this intent?) 
--
The Ada associative arrays from the new draft standard
are specified as something like:

generic
   type Key_Type is private;
   type Element_Type is private;
   with function Hash (Key : Key_Type)
      return Hash_Type is <>;
   with function Is_Equal_Key (Left, Right : Key_Type)
      return Boolean is "=";
   with function "=" (Left, Right : Element_Type)
      return Boolean is <>;
package Ada.Containers.Hashed_Maps is...


Which clearly won't work unless you can find the three
function generic parameters.  I don't see how this can be
used easily in a generic context.

I don't think I am being *that* unreasonable in asking for arrays
indexed by arbitrary types, without jumping through hoops :)
-- 
Adrian




^ permalink raw reply	[relevance 4%]

* Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)
  @ 2005-03-12  8:42  6%                       ` Georg Bauhaus
  2005-03-14 14:40  4%                         ` Dr. Adrian Wrigley
  0 siblings, 1 reply; 65+ results
From: Georg Bauhaus @ 2005-03-12  8:42 UTC (permalink / raw)


Falk Tannh�user wrote:
> Dr. Adrian Wrigley wrote:
> 
>> But what of features not present in either?
> 
> [...]
> 
>> associative arrays (from Perl)
> 
> 
> Wouldn't that be std::map in C++?

and in Ada 2005,

Ada.Containers.Hashed_Maps  and  Ada.Containers.Hashed_Maps


Georg



^ permalink raw reply	[relevance 6%]

* Re: Bye, bye?
  @ 2005-02-09 19:04  5%     ` mheaney
  0 siblings, 0 replies; 65+ results
From: mheaney @ 2005-02-09 19:04 UTC (permalink / raw)



Wes Groleau wrote:
>
> But much of what I do depends on hashes
> (associative arrays), which are built-in to perl.
> Plus I attach the hash to a DBM file.
>
> To do that in Ada, I'd have to not only upgrade Xcode
> (not an option right now) and install Ada (easy, but
> requires Xcode), but I'd also have to find or build
> a hash package (not hard) and/or a DBM binding (a little
> harder).


Associative arrays will be included in the next version of Ada, as part
of the standard container library:

ada.containers.hashed_maps
ada.containers.indefinite_hashed_maps

There will also be an ordered version (which does the same thing as the
hashed version, but with a different time complexity; it's also a bit
simpler to instantiate):

ada.containers.ordered_maps
ada.containers.indefinite_ordered_maps

There are also hashed and ordered sets (for use when you don't need a
separate key and element).

You can get a reference implementation in the ai302 subdirectory at the
charles.tigris.org website.

Look for the files named a-c*.ad[sb] .

-Matt




^ permalink raw reply	[relevance 5%]

Results 1-65 of 65 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2005-02-02  2:48     Bye, bye? Wes Groleau
2005-02-02 21:12     ` Ludovic Brenta
2005-02-03  4:24       ` Wes Groleau
2005-02-09 19:04  5%     ` mheaney
2005-03-05 13:42     Teaching new tricks to an old dog (C++ -->Ada) Turamnvia Suouriviaskimatta
2005-03-05 14:17     ` EventHelix.com
2005-03-05 14:25       ` Ludovic Brenta
2005-03-05 15:02         ` [OT] " Peter Koch Larsen
2005-03-05 15:39           ` Ludovic Brenta
2005-03-05 19:48             ` Ioannis Vranos
2005-03-10 18:39               ` Preben Randhol
2005-03-10 20:55                 ` xpyttl
2005-03-10 21:15                   ` Ed Falis
2005-03-10 22:39                     ` REH
2005-03-11 18:01                       ` Dr. Adrian Wrigley
2005-03-11 18:34                         ` Falk Tannhäuser
2005-03-12  8:42  6%                       ` Georg Bauhaus
2005-03-14 14:40  4%                         ` Dr. Adrian Wrigley
2005-03-14 15:42  0%                           ` Peter Koch Larsen
2005-03-23 13:49  0%                           ` Matthew Heaney
2005-03-23 15:27  0%                             ` Chris Jefferson
2005-03-17 12:03     Newbie - HashMap! fphsml
2005-03-17 15:19  4% ` Georg Bauhaus
2005-03-17 16:42  0%   ` Martin Dowie
2005-03-21  0:33  0%   ` Matthew Heaney
2005-03-19 16:22     Ada bench Pascal Obry
2005-03-19 16:55     ` Dr. Adrian Wrigley
2005-04-07 20:59       ` David Sauvage
2005-04-08 17:11         ` Pascal Obry
2005-04-18 19:14           ` David Sauvage
2005-04-19 16:43  4%         ` Matthew Heaney
2005-04-19 23:22  0%           ` David Sauvage
2005-04-20  0:49  0%             ` Matthew Heaney
2005-04-25 21:30     [Shootout] Spellcheck.adb albert.bachmann
2005-04-26 19:36     ` David Sauvage
2005-04-26 20:50  5%   ` Spellcheck.adb Albert Bachmann
2005-04-27 15:11  0%     ` Spellcheck.adb Matthew Heaney
2005-04-27 22:49  0%       ` Spellcheck.adb Albert Bachmann
2005-08-13  0:43     Hash table David Trudgett
2005-08-13 23:58  4% ` Matthew Heaney
2005-09-15 20:44     puzzled re hyperthreaded performance tmoran
2005-09-15 21:43  5% ` tmoran
2005-09-19 20:21     Wiljan Derks
2005-09-20  0:26  4% ` tmoran
2005-10-11 15:58     hashed_maps Lucretia
2005-10-11 17:45     ` hashed_maps Jeffrey R. Carter
2005-10-12 17:04       ` hashed_maps Lucretia
2005-10-13  5:17         ` hashed_maps Matthew Heaney
2005-10-13 14:45           ` hashed_maps Lucretia
2005-10-13 14:55  5%         ` hashed_maps Matthew Heaney
2006-05-02 17:33     The Computer Language Shootout Benchmarks Martin Krischik
2006-05-02 21:32  4% ` Tapio Kelloniemi
2006-05-02 22:37  0%   ` Matthew Heaney
2006-05-03 10:12  0%     ` Tapio Kelloniemi
2006-05-03 14:55  4%       ` Matthew Heaney
2006-05-03 16:15  0%         ` Martin Krischik
2006-05-03 17:11  3%         ` Tapio Kelloniemi
2006-05-03 16:05  0%   ` Martin Krischik
2006-11-21 10:11     How to use associative arrays in Ada 2005? snoopysalive
2006-11-21 11:49  5% ` Georg Bauhaus
2006-11-21 14:18  4% ` Matthew Heaney
2006-11-21 23:35       ` snoopysalive
2006-11-23 19:27         ` snoopysalive
2006-11-24  8:27           ` Dmitry A. Kazakov
2006-11-24 11:51             ` Matthew Heaney
2006-11-26 19:05               ` snoopysalive
2006-11-27 21:08                 ` Simon Wright
2006-11-27 22:22  3%               ` Matthew Heaney
2007-04-24 15:37  4% Ada Hash markp
2007-04-24 18:12  0% ` Randy Brukardt
2007-04-29 18:33  0% ` Matthew Heaney
2008-04-29  9:12     Dynamic allocation of named arrays Kim Rostgaard Christensen
2008-04-29 20:14  4% ` Gautier
2008-04-29 20:59  5%   ` Gautier
2008-05-19 15:50  5% Bug in GCC ? Sébastien
2008-05-19 15:57  0% ` Matthew Heaney
     [not found]     <edaeda6b-3793-48f2-8f8f-6c9701759de5@r33g2000yqn.googlegroups.com>
     [not found]     ` <f350aaae-d045-450d-b255-308eb7fb8f8a@e24g2000vbe.googlegroups.com>
     [not found]       ` <d121e6f6-6afa-43e7-bcd1-c9a770e12704@z9g2000yqi.googlegroups.com>
     [not found]         ` <nospam-83CE7B.07382201062009@news.aioe.org>
     [not found]           ` <78i1lbF1m69gkU1@mid.individual.net>
     [not found]             ` <78i2ajF1m4nglU1@mid.individual.net>
     [not found]               ` <h00l40$r5h$1@news.albasani.net>
     [not found]                 ` <78i4i2F1magkfU1@mid.individual.net>
     [not found]                   ` <3f1d007f-bcae-42b4-afb0-215b18f51b9c@n21g2000vba.googlegroups.com>
     [not found]                     ` <78ib19F1mfh7qU1@mid.individual.net>
     [not found]                       ` <3b519936-3db9-4dad-85ba-371fa4b29c8f@z5g2000vba.googlegroups.com>
     [not found]                         ` <78quiaF1n95fsU1@mid.individual.net>
     [not found]                           ` <h09q6f$41t$1@news.albasani.net>
     [not found]                             ` <MJWdnUzVI6hADrXXnZ2dnUVZ_q2dnZ2d@earthlink.com>
     [not found]                               ` <78rpbkF1mqrc7U1@mid.individual.net>
2009-06-06 16:13                                 ` find words that contains some specific letters John B. Matthews
2009-06-06 18:23                                   ` Lew
2009-06-06 23:58                                     ` Tom Anderson
2009-06-07 19:30  4%                                   ` John B. Matthews
2009-07-23 19:08     reference to a name variable Pablo
2009-07-24 11:03     ` Stephen Leake
2009-07-28 13:51       ` Chrono
2009-07-28 18:25  4%     ` Ludovic Brenta
2009-07-30 14:37  0%       ` Chrono
2012-08-03 12:01  5% Abstraction over container iterators ms
2012-10-07 19:58  5% Ada Containers rwilco19
2012-10-07 20:40  0% ` Maciej Sobczak
2012-10-07 21:25  6%   ` rwilco19
2012-10-07 21:30       ` rwilco19
2012-10-07 21:56  6%     ` Georg Bauhaus
2012-10-07 22:00  6% ` Simon Wright
2013-09-17 14:51     Multiple keys for a map Peter Brooks
2013-09-18  5:19     ` Shark8
2013-09-18  5:44  3%   ` Peter Brooks
2013-10-02  2:58     Optimizing Ada kennethesills
2013-10-02  3:47     ` Jeffrey Carter
2013-10-02  3:53       ` kennethesills
2013-10-02  4:13         ` Jeffrey Carter
2013-10-02 18:58  4%       ` John B. Matthews
2014-11-15 14:35  7% Ada.Containers warnings with gnat Björn Lundin
2014-11-15 18:01  0% ` Jeffrey Carter
2014-11-16 10:05  0%   ` Björn Lundin
2014-11-16 11:37  6%     ` Björn Lundin
2015-01-04 19:43     What is the best license to use for open source software? Hubert
2015-01-04 20:50     ` David Botton
2015-01-04 21:27       ` Ludovic Brenta
2015-01-04 22:13         ` David Botton
2015-01-04 23:14           ` Ludovic Brenta
2015-01-05  0:56             ` David Botton
2015-01-05  1:20               ` Ludovic Brenta
2015-01-05  2:28                 ` David Botton
2015-01-05 11:24                   ` GNAT GPL is not shareware (was: Re: What is the best license to use for open source software?) Dirk Heinrichs
2015-01-05 18:43                     ` GNAT GPL is not shareware Jeffrey Carter
2015-01-30 11:48                       ` Marius Amado-Alves
2015-01-30 12:12                         ` Björn Lundin
2015-01-30 13:50                           ` Simon Wright
2015-01-30 17:13                             ` Jeffrey Carter
2015-01-30 17:34                               ` Simon Wright
2015-01-30 18:21  4%                             ` Jeffrey Carter
2015-01-30 18:49  5%                               ` Simon Wright
2015-07-31 21:13     container of a container Hedley Rainnie
2015-08-01 11:56  6% ` Björn Lundin
2015-11-27 15:25  4% Two approaches of iterators for the key-value pairs ytomino
2015-11-27 16:30  0% ` Dmitry A. Kazakov
2015-11-27 18:08  0%   ` ytomino
2015-11-27 20:50  0%     ` Dmitry A. Kazakov
2015-11-27 17:43  4% ` Brad Moore
2015-11-27 19:38  0%   ` ytomino
2015-11-27 23:11  0%     ` Brad Moore
2016-06-01 13:33     fyi, GNAT and SPARK GPL 2016 are out Nasser M. Abbasi
2016-06-04 16:13  5% ` gautier_niouzes
2019-10-29 13:43  6% How to Iterate over all elements of a hashed_map Alain De Vos
2019-10-29 16:48  0% ` Jeffrey R. Carter
2022-09-14 12:36     Non-standard functions in GNAT's Ada.Containers packages? G.B.
2022-09-14 16:04     ` Egil H H
2022-09-15  7:13       ` G.B.
2022-09-15 14:26         ` Marius Amado-Alves
2022-09-15 15:03           ` Niklas Holsti
2022-09-15 17:11             ` Marius Amado-Alves
2022-09-16 11:33               ` Björn Lundin
2022-09-16 15:00                 ` Marius Amado-Alves
2022-09-16 18:53  5%               ` Björn Lundin

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