comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Shootout: Word Frequency
Date: Thu, 14 Apr 2005 01:28:55 GMT
Date: 2005-04-14T01:28:55+00:00	[thread overview]
Message-ID: <rHj7e.5869$lP1.5576@newsread1.news.pas.earthlink.net> (raw)

There have been some postings a solution to this in Ada, but I've only 
now had a chance to check out the problem. I see that the Ada solution 
is 168 LOC and 111 terminator semicolons. I'd be interested to know how 
it compares to this version, which I posted here some time ago in 
response to something else (some lines may wrap):

with Ada.Characters.Handling;
with Ada.Text_IO;

with Word_Count_Help;
procedure Word_Count is
    use Ada.Characters.Handling;
    use Ada.Text_IO;
    use Word_Count_Help;

    Word   : Word_Input.Word;
    Result : Word_Search.Result;
    Set    : Word_Search.Skip_List;
    Item   : Word_Info;
begin -- Word_Count
    All_Words : loop
       exit All_Words when End_Of_File;

       Word_Input.Get (Word);
       Item.Word := +To_Lower (+Word);
       Result := Word_Search.Search (Set, Item);

       if Result.Found then
          Item.Count := Result.Item.Count + 1;
       else
          Item.Count := 1;
       end if;

       Word_Search.Insert (List => Set, Item => Item);
    end loop All_Words;

    Sort_Words : declare
       type Context_Info is record
          Index : Positive := 1;
          List  : Word_List (1 .. Word_Search.Length (Set) );
       end record;

       procedure Word_To_List (Item : in Word_Info; Context : in out 
Context_Info; Continue : out Boolean) is
          -- null;
       begin -- Word_To_List
          Continue := True;
          Context.List (Context.Index) := Item;
          Context.Index := Context.Index + 1;
       end Word_To_List;

       procedure Set_To_List is new Word_Search.Iterate (Context_Data => 
Context_Info, Action => Word_To_List);

       Context : Context_Info;
    begin -- Sort_Words
       Set_To_List (List => Set, Context => Context);
       Sort (Set => Context.List);

       Output : for I in Context.List'range loop
          Put_Line (Item => +Context.List (I).Word & Integer'Image 
(Context.List (I).Count) );
       end loop Output;
    end Sort_Words;
end Word_Count;

with PragmARC.Assignment;
with PragmARC.Skip_List_Unbounded;
with PragmARC.Sort_Quick_In_Place;
with PragmARC.Word_Input;
package Word_Count_Help is
    package Word_Input is new PragmARC.Word_Input;

    function "+" (Right : Word_Input.Word) return String renames 
Word_Input.V_String.To_String;
    function "+" (Right : String) return Word_Input.Word;

    type Word_Info is record
       Word  : Word_Input.Word;
       Count : Natural := 0;
    end record;

    function "<" (Left : Word_Info; Right : Word_Info) return Boolean;
    function ">" (Left : Word_Info; Right : Word_Info) return Boolean;
    function "=" (Left : Word_Info; Right : Word_Info) return Boolean;

    type Word_List is array (Positive range <>) of Word_Info;

    procedure Assign is new PragmARC.Assignment (Element => Word_Info);
    procedure Sort is new PragmARC.Sort_Quick_In_Place (Element => 
Word_Info, Index => Positive, Sort_Set => Word_List, "<" => ">");

    package Word_Search is new PragmARC.Skip_List_Unbounded (Element => 
Word_Info);
end Word_Count_Help;

package body Word_Count_Help is
    use type Word_Input.V_String.Bounded_String;

    function "+" (Right : String) return Word_Input.Word is
       -- null;
    begin -- "+"
       return Word_Input.V_String.To_Bounded_String (Right);
    end "+";

    function "<" (Left : Word_Info; Right : Word_Info) return Boolean is
       -- null;
    begin -- "<"
       return Left.Word < Right.Word;
    end "<";

    function ">" (Left : Word_Info; Right : Word_Info) return Boolean is
       -- null;
    begin -- ">"
       if Left.Count = Right.Count then
          return Left.Word < Right.Word;
       else
          return Left.Count > Right.Count;
       end if;
    end ">";

    function "=" (Left : Word_Info; Right : Word_Info) return Boolean is
       -- null;
    begin -- "="
       return Left.Word = Right.Word;
    end "=";
end Word_Count_Help;

which is 115 SLOC and 64 terminator semicolons. It could perhaps be sped 
up by not checking End_Of_File and handling End_Error instead.

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



             reply	other threads:[~2005-04-14  1:28 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-14  1:28 Jeffrey Carter [this message]
2005-04-14  8:08 ` Shootout: Word Frequency Martin Dowie
2005-04-14 19:09   ` Jeffrey Carter
2005-04-14 20:41     ` Martin Dowie
2005-04-15  1:01       ` Jeffrey Carter
2005-04-15  7:52       ` Ludovic Brenta
2005-04-15 10:30         ` Martin 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