From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Normalizing array indices
Date: Sat, 29 Oct 2011 11:23:28 +0200
Date: 2011-10-29T11:23:28+02:00 [thread overview]
Message-ID: <8bb6cnkdx9ax$.fee5pu0k4zwx$.dlg@40tude.net> (raw)
In-Reply-To: m2mxckp3q1.fsf@pushface.org
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
next prev parent reply other threads:[~2011-10-29 9:26 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-10-28 18:58 Normalizing array indices Stefan.Lucks
2011-10-28 20:36 ` Adam Beneschan
2011-11-01 20:18 ` Stefan.Lucks
2011-10-28 21:13 ` Randy Brukardt
2011-10-29 7:29 ` Pascal Obry
2011-10-29 19:18 ` Jeffrey Carter
2011-10-29 19:58 ` tmoran
2011-10-29 21:15 ` Simon Wright
2011-10-29 20:41 ` Randy Brukardt
2011-11-01 20:49 ` stefan-lucks
2011-11-01 20:44 ` stefan-lucks
2011-11-01 20:43 ` stefan-lucks
2011-11-02 12:16 ` Robert A Duff
2011-10-29 9:05 ` Simon Wright
2011-10-29 9:23 ` Dmitry A. Kazakov [this message]
2011-11-01 20:55 ` stefan-lucks
2011-11-02 12:14 ` Robert A Duff
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox