comp.lang.ada
 help / color / mirror / Atom feed
* RE: Implementing an elegant range type.
@ 2001-03-20 20:17 Beard, Frank
  0 siblings, 0 replies; 12+ messages in thread
From: Beard, Frank @ 2001-03-20 20:17 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'


-----Original Message-----
From: Chad R. Meiners [mailto:crmeiners@hotmail.com]

>> Why not just write:
>>
>>     type Integer_Range is record
>>         First, Last : Integer;
>>     end record;
>>

> For example, the operator I want to use is "in" and not "Length".
> I want to be able to write "If x in Switches_To_Be_Reserved'Range then
..." 

Whether you use Mark's example above, or pass in the First and Last 
as parameters, you can still write the following:


   Assuming "Reserved_Range : in Integer_Range", as the formal argument
passed in.

   if x in Reserved_Range.First .. Reserved_Range.Last then ...


or, using Range_First and Range_Last as the formal parameters:

   Range_First : in integer;
   Range_Last  : in integer;

   if x in Range_First .. Range_Last then ...


I agree it might be slightly more elegant to pass a single parameter
so that the statement would be:

   if x in Reserved_Range then

but you're only saving one parameter, since "in" works for discrete types.

Frank




^ permalink raw reply	[flat|nested] 12+ messages in thread
* RE: Implementing an elegant range type.
@ 2001-03-21 13:00 Francisco Javier Loma Daza
  0 siblings, 0 replies; 12+ messages in thread
From: Francisco Javier Loma Daza @ 2001-03-21 13:00 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: 'comp.lang.ada@ada.eu.org'

El 20 Mar 2001 15:17:29 -0500, Beard, Frank escribi�:
> 
> -----Original Message-----
> From: Chad R. Meiners [mailto:crmeiners@hotmail.com]
> 
> >> Why not just write:
> >>
> >>     type Integer_Range is record
> >>         First, Last : Integer;
> >>     end record;
> >>
> 
> > For example, the operator I want to use is "in" and not "Length".
> > I want to be able to write "If x in Switches_To_Be_Reserved'Range then
> ..." 
> 
> Whether you use Mark's example above, or pass in the First and Last 
> as parameters, you can still write the following:
> 
> 
>    Assuming "Reserved_Range : in Integer_Range", as the formal argument
> passed in.
> 
>    if x in Reserved_Range.First .. Reserved_Range.Last then ...
> 
> 
> or, using Range_First and Range_Last as the formal parameters:
> 
>    Range_First : in integer;
>    Range_Last  : in integer;
> 
>    if x in Range_First .. Range_Last then ...
> 
> 
> I agree it might be slightly more elegant to pass a single parameter
> so that the statement would be:
> 
>    if x in Reserved_Range then
> 
> but you're only saving one parameter, since "in" works for discrete types.
> 
> Frank


    This is an interesting way to do range operations. I would like to
    do this one:

    function Iterator(this: Container.Object) return Container.Range;


    declare
        r: Container.Range := Iterator(list);
    begin
        for x in r'Range loop
            Process(Item(list, x));
        end loop;
    end;

    ...... even better

            Process(list(x)); -- :-)







^ permalink raw reply	[flat|nested] 12+ messages in thread
* Implementing an elegant range type.
@ 2001-03-19 17:45 Chad R. Meiners
  2001-03-19 18:38 ` Mark Lundquist
  2001-03-21 22:55 ` Chad R. Meiners
  0 siblings, 2 replies; 12+ messages in thread
From: Chad R. Meiners @ 2001-03-19 17:45 UTC (permalink / raw)


There are certain times that I have wanted to pass ranges as a
parameter to subroutines, for example being able to
write, "Register_Slots (75..4242);" which of course isn't directly
allowed.  The solution I came up with is to declare a null type that
doesn't take up any memory so that I can create array type of the
ranges I want as in the example below.

type Nothing is null record;
for  Nothing'Size use 0;

type Integer_Range_Type is array (Integer range <>) of Nothing;

Thus I could declare an array, "Integer_Range : Integer_Range_Type
(Integer);", and use it to pass array slice which would contain the
range data.  Example: "Register_Slots (Integer_Range(75..4242));"

The problem I am encountering is that when I declare a variable
with a range as large as Integer_Range's I get a
Storage_Error which doesn't make much sense since if Integer_Range's
range is something smaller than 2**20 elements, Integer_Range'Size
equals 8 for all cases which I expect should be true even for the
large cases since the elements of the array have no size.  I am
using GNAT 3.13p on WinNT.

Has anyone used a similar approach for representing ranges or have
any advice on getting around the Storage_Error for large ranges?

Thank you,
-Chad R. Meiners





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

end of thread, other threads:[~2001-03-23 22:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-20 20:17 Implementing an elegant range type Beard, Frank
  -- strict thread matches above, loose matches on Subject: below --
2001-03-21 13:00 Francisco Javier Loma Daza
2001-03-19 17:45 Chad R. Meiners
2001-03-19 18:38 ` Mark Lundquist
2001-03-19 20:50   ` Chad R. Meiners
2001-03-22 22:20     ` Nick Roberts
2001-03-23 22:29       ` Brian Rogoff
2001-03-20  4:48   ` Dr Adrian Wrigley
2001-03-20 15:31     ` Robert A Duff
2001-03-21  1:21       ` Dr Adrian Wrigley
2001-03-21  3:58         ` Brian Rogoff
2001-03-21 22:55 ` Chad R. Meiners

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