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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: If not Ada, what else... Date: Sun, 23 Aug 2015 12:32:39 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <8ba@googlegroups98-b70e-cb5f0c32e4ba@googlegrou7b.com> <5e6cb30b-5f8c-4fed-969e-3941315ecba0@googlegroups.com> <87si87nf8k.fsf@jester.gateway.sonic.net> <87bneuony6.fsf@jester.gateway.sonic.net> <87pp2gqa2g.fsf@nightsong.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Injection-Date: Sun, 23 Aug 2015 19:30:58 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="ee44d3db9c41f5ad88d7e8e8f0268f05"; logging-data="3975"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+i98ej9GGaicqlPQomhWXQAoqFU2k8eLY=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: Cancel-Lock: sha1:YNdh/43keaMmQ9o8PyBNxpipGFg= X-Enigmail-Draft-Status: N1110 Xref: news.eternal-september.org comp.lang.ada:27586 Date: 2015-08-23T12:32:39-07:00 List-Id: Making it look a bit more like Ada, and using an unbounded-integer pkg: with Ada.Text_IO; with PragmARC.Unbounded_Integers; procedure Hamming is use PragmARC.Unbounded_Integers; Max : constant := 1_000_000; One : constant Unbounded_Integer := To_Unbounded_Integer (1); Two : constant Unbounded_Integer := To_Unbounded_Integer (2); Three : constant Unbounded_Integer := To_Unbounded_Integer (3); Five : constant Unbounded_Integer := To_Unbounded_Integer (5); type Buffer is array (1 .. Max) of Unbounded_Integer; procedure Hamming (Buff : out Buffer) is type Work_List is array (1 .. 3) of Unbounded_Integer; type Index_List is array (Work_List'Range) of Positive; P : constant Work_List := (Two, Three, Five); Cand : Work_List; Ind : Index_List := (1, 1, 1); Min : Unbounded_Integer; begin -- Hamming Buff (Buff'First) := One; All_Results : for I in Buff'First + 1 .. Buff'Last loop Candidates : for J in Work_List'Range loop Cand (J) := P (J) * Buff (Ind (J) ); end loop Candidates; Min := Cand (Cand'First); Find_Min : for J in Cand'First + 1 .. Cand'Last loop if Cand (J) < Min then Min := Cand (J); end if; end loop Find_Min; Buff (I) := Min; Increment_Mins : for J in Work_List'Range loop if Cand (J) = Min then Ind (J) := Ind (J) + 1; end if; end loop Increment_Mins; end loop All_Results; end Hamming; type Buffer_Ptr is access Buffer; Buff : constant Buffer_Ptr := new Buffer; begin -- Hamming Hamming (Buff => Buff.all); First_20 : for I in 1 .. 20 loop Ada.Text_IO.Put_Line (Item => Integer'Image (I) & ' ' & Image (Buff (I) ) ); end loop First_20; Ada.Text_IO.Put_Line (Item => " 1691" & ' ' & Image (Buff (1691) ) ); Ada.Text_IO.Put_Line (Item => Integer'Image (Max) & ' ' & Image (Buff (Max) ) ); end Hamming; Which gives $ time ./hamming 1 1 2 2 3 3 4 4 5 5 6 6 7 8 8 9 9 10 10 12 11 15 12 16 13 18 14 20 15 24 16 25 17 27 18 30 19 32 20 36 1691 2125764000 1000000 519312780448388736089589843750000000000000000000000000000000000000000000000000000000 real 0m6.534s user 0m6.432s sys 0m0.092s on a 2-GHz Core 2 Duo. More modern processors will no doubt be faster. The PragmAda Reusable Components are available at https://pragmada.x10hosting.com/pragmarc.htm PragmARC.Unbounded_Integers is only available in the beta version for ISO/IEC 8652:2007 since it uses Ada.Containers. -- Jeff Carter "I blow my nose on you." Monty Python & the Holy Grail 03