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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,587e0e0a16d65b10 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Invade wikipedia! Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <49a6d506$0$32680$9b4e6d93@newsspool2.arcor-online.net> <4a18f335-30f6-4216-8376-0310eb560445@p11g2000yqe.googlegroups.com> <49a7ced2$0$31342$9b4e6d93@newsspool4.arcor-online.net> Date: Fri, 27 Feb 2009 18:33:09 +0100 Message-ID: NNTP-Posting-Date: 27 Feb 2009 18:33:11 CET NNTP-Posting-Host: d1c083bd.newsspool2.arcor-online.net X-Trace: DXC=ONNK5RGbB:hfF8a^:6>b7eA9EHlD;3Ycb4Fo<]lROoRa^YC2XCjHcbihofVi>d<=UaDNcfSJ;bb[eIRnRBaCd On Fri, 27 Feb 2009 12:30:25 +0100, Georg Bauhaus wrote: > Adding the declarations of suitable number types in an example > algorithm maybe doesn't seem to hurt that much. Taking > http://en.wikipedia.org/wiki/Insertion_sort as an example, > I tried > > type Mug is (Metal, Plastic, Thermo, Porcelain); > type Mug_Number is range 1 .. 10; > type Mug_List is array(Mug_Number) of Mug; > > procedure Insertion_Sort(A: in out Mug_List) is > j: Mug_Number'Base; > value: Mug; > begin > for i in A'First + 1 .. A'Last loop > value := A(i); > j := i-1; > while j >= A'First and then A(j) > value loop > A(j+1) := A(j); > j := j-1; > end loop; > A(j+1) := value; > end loop; > end Insertion_Sort; > > No big changes due to going from Pseudo-Pascal to real Ada, > I think, other than having to explain 'Base. > The "and then" of the shifting loop actually clarifies that > short circuit control is required. You do not need T'Base in this case because it is cleaner and more efficient to rewrite it in a form that would always keep all indices within the array range: procedure Insertion_Sort (A: in out Mug_List) is begin for I in A'First + 1..A'Last loop declare Value : constant Mug := A (I); J : Mug_Number := I - 1; begin while A (J) > Value loop A (J + 1) := A (J); exit when J = A'First; J := J - 1; end loop; A (J) := Value; end; end loop; end Insertion_Sort; Of course an Ada purist would also replace integer index arithmetic with I'Succ and I'Pred. But that would likely be too much for wiki brains... (:-)) -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de