comp.lang.ada
 help / color / mirror / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* Re: Normalizing array indices
  @ 2011-10-29 20:41  7%       ` Randy Brukardt
  0 siblings, 0 replies; 1+ results
From: Randy Brukardt @ 2011-10-29 20:41 UTC (permalink / raw)


"Jeffrey Carter" <spam.jrcarter.not@spam.not.acm.org> wrote in message 
news:j8hnkv$30l$1@tornado.tornevall.net...
> 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?

Of course, there is little reason to create a general purpose sort of your 
own, given that the Standard already contains two (and there is a third one 
added in Ada 2012). You pretty much have to have some special purpose to 
justify the work. The ones in the Standard have the following 
specifications:

generic
   type Index_Type is (<>);
   type Element_Type is private;
   type Array_Type is array (Index_Type range <>) of Element_Type;
   with function "<" (Left, Right : Element_Type)
      return Boolean is <>;
procedure Ada.Containers.Generic_Array_Sort (Container : in out Array_Type);
pragma Pure(Ada.Containers.Generic_Array_Sort);

and

generic
   type Index_Type is (<>);
   with function Before (Left, Right : Index_Type) return Boolean;
   with procedure Swap (Left, Right : Index_Type);
procedure Ada.Containers.Generic_Sort
      (First, Last : Index_Type'Base);
pragma Pure(Ada.Containers.Generic_Sort);

(There is also a constrained version that's similar to the first.)

The last version allows sorting anything ordered (it does not have to be an 
array).

There are also sorting routines for the vector and list containers.

                           Randy.







^ permalink raw reply	[relevance 7%]

Results 1-1 of 1 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2011-10-28 18:58     Normalizing array indices 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 20:41  7%       ` Randy Brukardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox