comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@on2.com (Matthew Heaney)
Subject: Re: Computer Language Shootout
Date: 14 Jul 2003 16:08:58 -0700
Date: 2003-07-14T23:08:59+00:00	[thread overview]
Message-ID: <1ec946d1.0307141508.7f51fbe3@posting.google.com> (raw)
In-Reply-To: 1ec946d1.0307111358.fb772@posting.google.com

mheaney@on2.com (Matthew Heaney) wrote in message news:<1ec946d1.0307111358.fb772@posting.google.com>...
> Craig Carey <research@ijs.co.nz> wrote in message news:<n54tgvgugor0ag2qsv97t9mf9sn9rr542i@4ax.com>...
> > 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



  parent reply	other threads:[~2003-07-14 23:08 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-11 12:05 Computer Language Shootout Craig Carey
2003-07-11 12:18 ` Preben Randhol
2003-07-11 12:50 ` Preben Randhol
2003-07-15 15:15   ` Matthew Heaney
2003-07-15 23:46     ` Robert I. Eachus
2003-07-16  4:37       ` Matthew Heaney
2003-07-16 18:01         ` Robert I. Eachus
2003-07-16 21:35           ` Matthew Heaney
2003-07-17  9:38           ` Preben Randhol
2003-07-17 16:29             ` Wesley Groleau
2003-07-17 17:36               ` Jean-Pierre Rosen
2003-07-17 20:56                 ` Preben Randhol
2003-07-17 21:47                 ` Robert A Duff
2003-07-18 18:04                   ` Robert Spooner
2003-07-18 18:48                     ` David C. Hoos
2003-07-19  9:51                     ` Preben Randhol
2003-07-21  7:21                     ` Jean-Pierre Rosen
2003-07-23 20:34                       ` Robert Spooner
2003-07-23 22:22                         ` Robert I. Eachus
2003-07-18 21:22                   ` Pascal Obry
2003-07-19 19:04                     ` Robert A Duff
2003-07-19 21:29                       ` Pascal Obry
2003-07-19 23:14                       ` Samuel Tardieu
2003-07-20  0:26                         ` Robert I. Eachus
2003-07-20  8:44                           ` Samuel Tardieu
2003-07-20 13:28                             ` Robert I. Eachus
2003-07-20 17:10                               ` Samuel Tardieu
2003-07-20 22:43                                 ` Robert I. Eachus
2003-07-21  8:31                                   ` Samuel Tardieu
2003-07-21 14:39                                     ` Hyman Rosen
2003-07-21 15:23                                       ` Samuel Tardieu
2003-07-21 15:46                                         ` Hyman Rosen
2003-07-21 16:09                                           ` tmoran
2003-07-21 17:52                                             ` Hyman Rosen
2003-07-21 16:14                                           ` Samuel Tardieu
2003-07-21 17:55                                             ` Hyman Rosen
2003-07-22  9:07                                           ` Preben Randhol
2003-07-22 13:21                                             ` Hyman Rosen
2003-07-23  9:28                                               ` Preben Randhol
2003-07-23  9:42                                                 ` Vinzent Hoefler
2003-07-23 10:35                                                   ` Preben Randhol
2003-07-23 22:19                                                     ` Randy Brukardt
2003-07-21  8:53                     ` Dmitry A. Kazakov
2003-07-21 11:00                       ` Jeffrey Creem
2003-07-21 13:39                         ` Dmitry A. Kazakov
2003-07-17 23:32                 ` Wesley Groleau
2003-07-18  7:36                 ` Dmitry A. Kazakov
2003-07-18  8:05                   ` Thomas Wolf
2003-07-18  9:11                   ` Jean-Pierre Rosen
2003-07-18 17:31                   ` Matthew Heaney
2003-07-17 20:53               ` Preben Randhol
2003-07-17 23:36                 ` Wesley Groleau
2003-07-16  7:11     ` Preben Randhol
2003-07-11 21:58 ` Matthew Heaney
2003-07-14 18:46   ` Matthew Heaney
2003-07-14 23:08   ` Matthew Heaney [this message]
2003-07-16  3:20     ` Isaac Gouy
2003-07-16  4:42       ` Matthew Heaney
2003-07-16 15:27         ` Isaac Gouy
  -- strict thread matches above, loose matches on Subject: below --
2001-11-03  4:15 Using "with function" Mark Lundquist
2001-11-03  5:11 ` Computer Language Shootout Eric Merritt
2001-11-03  6:50   ` tmoran
2001-11-03  7:15     ` Al Christians
2001-11-03  8:52   ` martin.m.dowie
2001-11-03 14:04     ` Ted Dennison
2001-11-03 14:24       ` martin.m.dowie
2001-11-03 14:49       ` Larry Kilgallen
2001-11-03 23:03         ` research@ijs.co.nz
2001-11-04  6:39           ` tmoran
2001-11-04 13:44             ` Larry Kilgallen
2001-11-05  0:59               ` Adrian Hoe
2001-11-05  8:04               ` David Brown
2001-11-06  6:36             ` AG
2001-11-06  8:05               ` tmoran
2001-11-07  8:58                 ` AG
2001-11-06 12:07               ` Larry Kilgallen
2001-11-07  6:19               ` Richard Riehle
2001-11-04 15:59     ` Preben Randhol
2001-11-04 20:04       ` martin.m.dowie
replies disabled

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