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-Thread: 103376,228892b9e0286266,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.news.pas.earthlink.net.POSTED!a6202946!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.3) Gecko/20040910 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Shootout: Word Frequency Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Thu, 14 Apr 2005 01:28:55 GMT NNTP-Posting-Host: 4.240.51.53 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.news.pas.earthlink.net 1113442135 4.240.51.53 (Wed, 13 Apr 2005 18:28:55 PDT) NNTP-Posting-Date: Wed, 13 Apr 2005 18:28:55 PDT Xref: g2news1.google.com comp.lang.ada:10445 Date: 2005-04-14T01:28:55+00:00 List-Id: 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