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: a07f3367d7,7b6305d0d57a9f34 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.35.68 with SMTP id f4mr5620421pbj.5.1319880393965; Sat, 29 Oct 2011 02:26:33 -0700 (PDT) Path: p6ni21632pbn.0!nntp.google.com!news1.google.com!news3.google.com!feeder.news-service.com!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Normalizing array indices Date: Sat, 29 Oct 2011 11:23:28 +0200 Organization: cbb software GmbH Message-ID: <8bb6cnkdx9ax$.fee5pu0k4zwx$.dlg@40tude.net> References: Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: ahYLEM6D05No+MbbmU0ccw.user.speranza.aioe.org Mime-Version: 1.0 X-Complaints-To: abuse@aioe.org User-Agent: 40tude_Dialog/2.0.15.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news1.google.com comp.lang.ada:18744 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Date: 2011-10-29T11:23:28+02:00 List-Id: On Sat, 29 Oct 2011 10:05:42 +0100, Simon Wright wrote: > Stefan.Lucks@uni-weimar.de writes: > >> In many cases, array ranges starting with an arbitrary index are >> better (higher level) to model an application's demands. But >> sometimes, like when applying a sorting routine, this extra >> information is actually some ballast. >> >> Ideally, the specification of a subprogram would carry the information >> that the subprogram only uses "normalized" array indices, to free the >> tester from having to consider test cases with different A'First: >> >> procedure Sort(A: in out Sort_Array_Type(1 .. <>)); >> >> The user can still call Sort with any array of range, say, 4711 .. 9421, >> but Sort coldn't tell that apart from an array of range 1 .. 4711. Thus, >> there is no reason for additional test cases with different values >> for A'First. > > But your problem was with A'Last, surely? > > I've had more surprises with > > type Arr is array (Integer range <>) of Float; > A : Arr := (1.0, 2.0, 3.0); > > where A'First is Integer'First (on GNAT), ie -2**31. The problem is that for some algorithms for any valid array index Index, there should exist Index - 1 and/or Index + 1. One possible solution might be some sugar for subtype Safe_Integer is range Integer'First + 1..Integer'Last - 1; type Arr is array (Safe_Integer range <>) of Float; Another solution is array interfaces with universal integer as the position (offset) in addition to index. Some people are fond of [] brackets, positional interfaces could use them. Algorithms written in terms of indices sometimes become simpler when rewritten in terms of offsets. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de