comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Normalizing array indices
Date: Sat, 29 Oct 2011 12:18:03 -0700
Date: 2011-10-29T12:18:03-07:00	[thread overview]
Message-ID: <j8hnkv$30l$1@tornado.tornevall.net> (raw)
In-Reply-To: <4eabab47$0$30757$ba4acef3@reader.news.orange.fr>

On 10/29/2011 12:29 AM, Pascal Obry wrote:
>
> 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)

The problem is the use of Positive for the generic formal array index. One 
thinks of numbers as being infinite, so failing to think about the effect of 
adding 1 to an index is natural.

Why should a general-purpose sort procedure restrict the client to arrays 
indexed by Positive?

generic -- Sort
    type Element is private;
    type Index is (<>);
    type Sort_List is array (Index range <>) of Element;

    with function "<" (Left : Element; Right : Element) return Boolean is <>;
procedure Sort (List : in out Sort_List);

Now the developer is forced to think in terms of Index being finite, and so the 
cases of trying to take the Succ to Index'Last and the Pred of Index'First.

The PragmAda Reusable Components contain insertion, heap, quick, and radix 
sorts, all of which allow any discrete subtype for the indices and handle 
Index'Last correctly:

http://pragmada.co.cc/

-- 
Jeff Carter
"I don't know why I ever come in here. The
flies get the best of everything."
Never Give a Sucker an Even Break
102



  reply	other threads:[~2011-10-29 19:19 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 [this message]
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
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