comp.lang.ada
 help / color / mirror / Atom feed
From: stt@houdini.camb.inmet.com (Tucker Taft)
Subject: Re: Curious Question
Date: 1997/12/05
Date: 1997-12-05T00:00:00+00:00	[thread overview]
Message-ID: <EKq77F.MJJ.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: 66757i$mk5$1@swlab1.msd.ray.com


Chad Reichenbach {81726} (car@swl.msd.ray.com) wrote:

: I have a question I can'f figure out.  If I have the following code:

:   type My_Type is (obj1, obj2 obj3, obj4, obj5, obj6);
:   subtype My_Subtype is My_Type range obj1 .. obj4;

: My question is, I need a new subtype that ranges from obj4 to obj1.
: Will the declaration

:   subtype Second_Subtype is My_Subtype
:           range My_Subtype'last .. My_Subtype'first

: do that?  

No, this would declare a subtype with a null range, having
no values at all.

: ... If not is there a way to do it without defining a new
: enumeration?

You can iterate backwards through a range via:
   for I in reverse My_Subtype loop

But it sounds like you might want to iterate around in a circle,
going obj4, obj5, obj6, obj1.  Is that right?  If so, then
you might want to declare a modular type, which has wrap-around
semantics for S'Succ:

   type Wrap_Around is mod My_Type'Pos(obj6)+1;

and then you could iterate as follows:

    declare
        I : Wrap_Around := My_Type'Pos(Obj4);
    begin
        loop
            declare
                J : My_Type := My_Type'Val(I);
            begin
                -- use "J" as necessary
                exit when J = Obj1;
            end;
            I := Wrap_Around'Succ(I);
        end loop;
    end;
 
Note that a "for" loop over the Wrap_Around would still not work,
because a for loop is not executed at all if the starting value is
greater than the ending value, even for a modular type.

So this would *not* work as desired:

    for I in Wrap_Around range My_Type'Pos(Obj4) .. My_Type'Pos(Obj1) loop
       -- Will never get here
    end loop

Of course, simpler than all the above would be to simply write
a loop as follows:

    declare
        J : My_Type := Obj4;
    begin
        loop
            -- use "J" as necessary
            exit when J = Obj1;
            if J = Obj6 then
                J := Obj1;
            else
                J := My_Type'Succ(J);
            end if;
        end loop;
    end;

: The type would be used in a loop that loops through the enumeration
: objects using S'succ(S).

: TIA,
: chad


--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




  reply	other threads:[~1997-12-05  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-12-04  0:00 Curious Question Chad Reichenbach {81726}
1997-12-05  0:00 ` Tucker Taft [this message]
1997-12-09  0:00 ` Anonymous
replies disabled

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