From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,386228a37afe967f X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-07-14 16:08:59 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: mheaney@on2.com (Matthew Heaney) Newsgroups: comp.lang.ada Subject: Re: Computer Language Shootout Date: 14 Jul 2003 16:08:58 -0700 Organization: http://groups.google.com/ Message-ID: <1ec946d1.0307141508.7f51fbe3@posting.google.com> References: <1ec946d1.0307111358.fb772@posting.google.com> NNTP-Posting-Host: 66.162.65.162 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1058224139 3978 127.0.0.1 (14 Jul 2003 23:08:59 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 14 Jul 2003 23:08:59 GMT Xref: archiver1.google.com comp.lang.ada:40271 Date: 2003-07-14T23:08:59+00:00 List-Id: mheaney@on2.com (Matthew Heaney) wrote in message news:<1ec946d1.0307111358.fb772@posting.google.com>... > Craig Carey wrote in message news:... > > The Ada 95 entries have appeared that the new > > "Computer Language Shootout" website: > > > > http://dada.perl.it/shootout/gnat.html > > I just cobbled together the "hash table (associative array)" example > using the hashed set in Charles. Here it is: Here's the word frequency example: procedure Wordfreq is Line : String (1 .. 133); Last : Natural; Word : Charles.Strings.Unbounded.Container_Type; Map : String_Integer_Maps.Container_Type; type Integer_Access is access all Integer; for Integer_Access'Storage_Size use 0; function To_Access is new Generic_Element (Integer_Access); procedure Insert is I : Iterator_Type; B : Boolean; begin if Is_Empty (Word) then return; end if; Insert (Map, To_String (Word), 0, I, B); declare Count : Integer renames To_Access (I).all; begin Count := Count + 1; end; Clear (Word); end Insert; begin while not End_Of_File loop Get_Line (Line, Last); for I in Line'First .. Last loop if Is_Alphanumeric (Line (I)) then Append (Word, To_Lower (Line (I))); else Insert; end if; end loop; if Last < Line'Last then Insert; end if; end loop; declare A : array (1 .. Length (Map)) of Iterator_Type; function Is_Less (R, L : Positive) return Boolean is RI : constant Iterator_Type := A (R); LI : constant Iterator_Type := A (L); begin if Element (RI) = Element (LI) then return Key (RI) > Key (LI); else return Element (RI) > Element (LI); end if; end Is_Less; procedure Swap (I, J : Positive) is E : constant Iterator_Type := A (I); begin A (I) := A (J); A (J) := E; end; procedure Sort is new Charles.Algorithms.Generic_Quicksort (Positive); I : Iterator_Type := First (Map); begin for Index in A'Range loop A (Index) := I; I := Succ (I); end loop; Sort (First => A'First, Back => A'First + A'Length); for Index in A'Range loop Put (Element (A (Index)), Width => 0); Put (':'); Put (Key (A (Index))); New_Line; end loop; end; end Wordfreq; The Charles library includes the map used in the example, as well as the generic algorithm for sorting a container. http://home.earthlink.net/~matthewjheaney/charles/index.html Instead of using an array as in the example, I could have used a list, which comes with its own sort operation. (For a list, the sort is stable.) -Matt