comp.lang.ada
 help / color / mirror / Atom feed
* How to extract range from array
@ 2006-03-17 11:23 Maciej Sobczak
  2006-03-17 12:29 ` Ed Falis
  2006-03-17 15:12 ` Martin Krischik
  0 siblings, 2 replies; 12+ messages in thread
From: Maciej Sobczak @ 2006-03-17 11:23 UTC (permalink / raw)


Hi,

Consider this:

    type My_Range is range 1..10;
    type My_Array is array(My_Range) of Integer;

now, the other way round:

    type My_Array is array(1..10) of Integer;
    type My_Range is ???;

In this second example I would like to extract the type of range from 
the already defined array type and give it a name. Is it possible? How?


-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-17 11:23 How to extract range from array Maciej Sobczak
@ 2006-03-17 12:29 ` Ed Falis
  2006-03-17 15:12 ` Martin Krischik
  1 sibling, 0 replies; 12+ messages in thread
From: Ed Falis @ 2006-03-17 12:29 UTC (permalink / raw)


On Fri, 17 Mar 2006 06:23:40 -0500, Maciej Sobczak <no.spam@no.spam.com>  
wrote:

> type My_Array is array(1..10) of Integer;
>    type My_Range is ???;

type My_Range is new Integer range My_Array'Range;

However, if you intend to use My_Range to index an object of type  
My_Array, the following is more useful because it won't require a type  
conversion to integer:

subtype My_Range is Integer range My_Array'Range;



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-17 11:23 How to extract range from array Maciej Sobczak
  2006-03-17 12:29 ` Ed Falis
@ 2006-03-17 15:12 ` Martin Krischik
  2006-03-20 10:04   ` Maciej Sobczak
  2006-03-20 13:14   ` Maciej Sobczak
  1 sibling, 2 replies; 12+ messages in thread
From: Martin Krischik @ 2006-03-17 15:12 UTC (permalink / raw)


Hi

Read:

http://en.wikibooks.org/wiki/Ada_Programming/Types/array#Array_Attributes

Martin




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-17 15:12 ` Martin Krischik
@ 2006-03-20 10:04   ` Maciej Sobczak
  2006-03-20 13:14   ` Maciej Sobczak
  1 sibling, 0 replies; 12+ messages in thread
From: Maciej Sobczak @ 2006-03-20 10:04 UTC (permalink / raw)


Martin Krischik wrote:

> Read:
> 
> http://en.wikibooks.org/wiki/Ada_Programming/Types/array#Array_Attributes

Thank you, this is exactly what I was missing - the constraining of the 
Integer part, and thought that it can be done by just simple attribute 
usage, like in:

type My_Range is My_Array'Range;

which does not work.


-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-17 15:12 ` Martin Krischik
  2006-03-20 10:04   ` Maciej Sobczak
@ 2006-03-20 13:14   ` Maciej Sobczak
  2006-03-20 13:58     ` Ed Falis
                       ` (3 more replies)
  1 sibling, 4 replies; 12+ messages in thread
From: Maciej Sobczak @ 2006-03-20 13:14 UTC (permalink / raw)


Martin Krischik wrote:

> Read:
> 
> http://en.wikibooks.org/wiki/Ada_Programming/Types/array#Array_Attributes

subtype My_Range is Integer range My_Array'Range;

This suggests that arrays cannot have more elements than Integer'Last.
Which cannot be true.

What am I missing?

-- 
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-20 13:14   ` Maciej Sobczak
@ 2006-03-20 13:58     ` Ed Falis
  2006-03-20 14:20     ` Dmitry A. Kazakov
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: Ed Falis @ 2006-03-20 13:58 UTC (permalink / raw)


On Mon, 20 Mar 2006 08:14:37 -0500, Maciej Sobczak <no.spam@no.spam.com>  
wrote:

> subtype My_Range is Integer range My_Array'Range;
>
> This suggests that arrays cannot have more elements than Integer'Last.
> Which cannot be true.
>
> What am I missing?

That the above was an example.  You could also do:

subtype My_Range is Interfaces.Integer_64 range My_Array'Range;
subtype My_Range is Interfaces.Unsigned_64 range My_Array'Range;

or any other integer or modular type that is used to define the index of  
My_Array.

- Ed



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-20 13:14   ` Maciej Sobczak
  2006-03-20 13:58     ` Ed Falis
@ 2006-03-20 14:20     ` Dmitry A. Kazakov
  2006-03-20 15:31     ` Robert A Duff
  2006-03-20 19:01     ` Martin Krischik
  3 siblings, 0 replies; 12+ messages in thread
From: Dmitry A. Kazakov @ 2006-03-20 14:20 UTC (permalink / raw)


On Mon, 20 Mar 2006 14:14:37 +0100, Maciej Sobczak wrote:

> subtype My_Range is Integer range My_Array'Range;
> 
> This suggests that arrays cannot have more elements than Integer'Last.
> Which cannot be true.
> 
> What am I missing?

A'Range is not a [sub]type, it is a value of some anonymous "range-type."
What you need is:

subtype My_Range is My_Array'Index range My_Array'Range;
   -- This is not Ada!

alas. This is also why you cannot:

generic
   type My_Array is array (<>) of Something; -- Not Ada!

Ada does not have array interfaces. So you have to specify the index type
instead of asking it from the array's instance:

generic
   type My_Index is ...;
   type My_Array is array (My_Index range <>) of Something;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-20 13:14   ` Maciej Sobczak
  2006-03-20 13:58     ` Ed Falis
  2006-03-20 14:20     ` Dmitry A. Kazakov
@ 2006-03-20 15:31     ` Robert A Duff
  2006-03-20 19:01     ` Martin Krischik
  3 siblings, 0 replies; 12+ messages in thread
From: Robert A Duff @ 2006-03-20 15:31 UTC (permalink / raw)


Maciej Sobczak <no.spam@no.spam.com> writes:

> What am I missing?

type T is array(1..10) of ...;

means the same as:

type T is array(Integer range 1..10) of ...;

which is a kludge.  I always write the latter (in the rare case where I
want Integer).  But you can say:

    type Index is range 1..Integer'Last + 1;
    type T is array(Index) of ...;
    X: T;

presuming System.Max_Int > Integer'Last,
and presuming you have enough memory to store the array.

- Bob



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-20 13:14   ` Maciej Sobczak
                       ` (2 preceding siblings ...)
  2006-03-20 15:31     ` Robert A Duff
@ 2006-03-20 19:01     ` Martin Krischik
  2006-03-20 20:20       ` Georg Bauhaus
  2006-03-20 21:51       ` Jeffrey R. Carter
  3 siblings, 2 replies; 12+ messages in thread
From: Martin Krischik @ 2006-03-20 19:01 UTC (permalink / raw)


Maciej Sobczak wrote:

> Martin Krischik wrote:
> 
>> Read:
>> 
>> http://en.wikibooks.org/wiki/Ada_Programming/Types/array#Array_Attributes
> 
> subtype My_Range is Integer range My_Array'Range;
> 
> This suggests that arrays cannot have more elements than Integer'Last.
> Which cannot be true.

You should have copied the original example: 

Hello_World  : const String := "Hello World!";
subtype Hello_World_Index is Integer range Hello_World'Range;

> What am I missing?

You are missing the definition of String which is part of Standard:

http://en.wikibooks.org/wiki/Ada_Programming/Libraries/Standard

and reads something like this (on GNAT):

 type Integer is range -(2 ** 31) .. +(2 ** 31 - 1);
 subtype Positive is Integer range 1 .. +(2 ** 31 - 1);

 type String is array (Positive range <>) of Character;
 pragma Pack (String);

And indeed the array in the example can not be larger then Integer'Last
because Strings are not larger then Integer'Last.

Of corse you could define:

 type Long_Long_Integer   is range -(2 ** 63) .. +(2 ** 63 - 1);
 subtype Long_Long_Positive is Integer range 1 .. +(2 ** 63 - 1);

 type Long_Long_String is array (Long_Long_Positive range <>) of Character;
 pragma Pack (Long_Long_String);

And then order additional memory for your computer to store
Long_Long_String's in  ;-)

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-20 19:01     ` Martin Krischik
@ 2006-03-20 20:20       ` Georg Bauhaus
  2006-03-20 21:51       ` Jeffrey R. Carter
  1 sibling, 0 replies; 12+ messages in thread
From: Georg Bauhaus @ 2006-03-20 20:20 UTC (permalink / raw)


Martin Krischik wrote:

>  type Long_Long_String is array (Long_Long_Positive range <>) of Character;
>  pragma Pack (Long_Long_String);
> 
> And then order additional memory for your computer to store
> Long_Long_String's in  ;-)


with Google;
package Big_Ones is

   type Huge is range 1 .. 10**66;

   type Long_Long_String is array(Huge range <>) of Character;
   type Text is access Long_Long_String;

   for Text'Storage_Pool use Google.Net;

end Big_Ones;


Given a suitable compiler for distributed sytems...  ;)


 Georg 



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-20 19:01     ` Martin Krischik
  2006-03-20 20:20       ` Georg Bauhaus
@ 2006-03-20 21:51       ` Jeffrey R. Carter
  2006-03-21 19:12         ` Martin Krischik
  1 sibling, 1 reply; 12+ messages in thread
From: Jeffrey R. Carter @ 2006-03-20 21:51 UTC (permalink / raw)


Martin Krischik wrote:
> 
>  subtype Positive is Integer range 1 .. +(2 ** 31 - 1);

The declaration of Positive is defined by ARM A.1, and so is the same for all 
implementations:

subtype Positive is Integer range 1 .. Integer'Last;

An implementation that used +(2 ** 31 - 1) instead of Integer'Last would not be Ada.

Of course, the meaning of Integer'Last depends on the definition of Integer, 
which is implementation defined, but the declaration of Positive is the same.

-- 
Jeff Carter
"From this day on, the official language of San Marcos will be Swedish."
Bananas
28



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: How to extract range from array
  2006-03-20 21:51       ` Jeffrey R. Carter
@ 2006-03-21 19:12         ` Martin Krischik
  0 siblings, 0 replies; 12+ messages in thread
From: Martin Krischik @ 2006-03-21 19:12 UTC (permalink / raw)


Jeffrey R. Carter wrote:

> Martin Krischik wrote:
>> 
>>  subtype Positive is Integer range 1 .. +(2 ** 31 - 1);
> 
> The declaration of Positive is defined by ARM A.1, and so is the same for
> all implementations:
> 
> subtype Positive is Integer range 1 .. Integer'Last;
> 
> An implementation that used +(2 ** 31 - 1) instead of Integer'Last would
> not be Ada.

No it's GNAT ;-) .

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2006-03-21 19:12 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-17 11:23 How to extract range from array Maciej Sobczak
2006-03-17 12:29 ` Ed Falis
2006-03-17 15:12 ` Martin Krischik
2006-03-20 10:04   ` Maciej Sobczak
2006-03-20 13:14   ` Maciej Sobczak
2006-03-20 13:58     ` Ed Falis
2006-03-20 14:20     ` Dmitry A. Kazakov
2006-03-20 15:31     ` Robert A Duff
2006-03-20 19:01     ` Martin Krischik
2006-03-20 20:20       ` Georg Bauhaus
2006-03-20 21:51       ` Jeffrey R. Carter
2006-03-21 19:12         ` Martin Krischik

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