comp.lang.ada
 help / color / mirror / Atom feed
From: stefan-lucks@see-the.signature
Subject: Re: Normalizing array indices
Date: Tue, 1 Nov 2011 21:43:27 +0100
Date: 2011-11-01T21:43:27+01:00	[thread overview]
Message-ID: <Pine.LNX.4.64.1111012119360.10832@medsec1.medien.uni-weimar.de> (raw)
In-Reply-To: <j8f5up$cg5$1@munin.nbi.dk>

On Fri, 28 Oct 2011, Randy Brukardt wrote:

> This is pretty typical. We've often talked about the need in Ada for 
> one-sided array subtypes, but we don't have any first class ones at this 
> point.
> 
> I probably would constrain the parameter to have a lower bound of 1. Using 
> Ada 2012:
> 
> generic
>    type Element_Type is private;
>    type Sort_Array_Type is array (Positive range <>) of Element_Type;
>    with function "<" (Left, Right: Element_Type) return Boolean is <>;
> procedure Sort(A: in out Sort_Array_Type)
>    with Pre => A'First 1;

Well, once I want to sort a slice X(Y .. Z) of X, this fails. The ease of 
slicing is one of the coolest features of Ada's arrays. In fact, the 
implementation of sort I mentioned would recursively perform 
  Sort(A(A'First .. M)); 
and 
  Sort(A(M+1 .. A'Last)); 
for M = (A'First+A'Last)/2 (with some exception treatment for 
A'First+A'Last>'Positive'Last and only if A'Length is above some 
threshold), followed by merging the two slices. So at some point of time, 
the slicing/renaming would become necessary -- either always at the 
beginning of Sort, or to re-slice A(M+1 .. A'Last). A better Ada-2012 
precondition would actually be
  with Pre => A'Last < Positive'Last;

> Then, if you had any calls that don't have the right bounds (which is likely 
> to be rare), I'd use a sliding trick similar to the one Adam showed. (But 
> I'd probably try hard to figure out how to avoid passing the slice in the 
> first place.)
> 
> Of course, it's better to make the routine work for all possible bounds. And 
> if you do that, you certainly need to add that to the testing burden (it's 
> commonly forgotten and probably is one of the most common Ada bugs).

I am relieved that I am not the only one who happened to suffer from such a 
problem. Still, I think a way to *specify* a formal subprogram parameter F 
such that F'First is a given constant, regardless of A'First if A is the 
actual parameter, would be ease testing and might be a good extension to 
Ada (2020?).

So long

Stefan


-- 
---- Stefan.Lucks (at) uni-weimar.de, University of Weimar, Germany ----
    <http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html> 
------     I love the taste of Cryptanalysis in the morning!      ------








  parent reply	other threads:[~2011-11-01 20:47 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 [this message]
2011-11-02 12:16     ` Robert A Duff
2011-10-29  9:05 ` Simon Wright
2011-10-29  9:23   ` Dmitry A. Kazakov
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