comp.lang.ada
 help / color / mirror / Atom feed
* Curious Question
@ 1997-12-04  0:00 Chad Reichenbach {81726}
  1997-12-05  0:00 ` Tucker Taft
  1997-12-09  0:00 ` Anonymous
  0 siblings, 2 replies; 3+ messages in thread
From: Chad Reichenbach {81726} @ 1997-12-04  0:00 UTC (permalink / raw)



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?  If not is there a way to do it without defining a new
enumeration?

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

TIA,
chad





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

* Re: Curious Question
  1997-12-04  0:00 Curious Question Chad Reichenbach {81726}
@ 1997-12-05  0:00 ` Tucker Taft
  1997-12-09  0:00 ` Anonymous
  1 sibling, 0 replies; 3+ messages in thread
From: Tucker Taft @ 1997-12-05  0:00 UTC (permalink / raw)



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




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

* Re: Curious Question
  1997-12-04  0:00 Curious Question Chad Reichenbach {81726}
  1997-12-05  0:00 ` Tucker Taft
@ 1997-12-09  0:00 ` Anonymous
  1 sibling, 0 replies; 3+ messages in thread
From: Anonymous @ 1997-12-09  0:00 UTC (permalink / raw)



On 4 Dec 1997 20:53:06 GMT, car@swl.msd.ray.com (Chad Reichenbach
{81726}) 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?  If not is there a way to do it without defining a new
> enumeration?
> 
> The type would be used in a loop that loops through the enumeration
> objects using S'succ(S).
> 

Not necessary:

   for S in reverse My_Subtype loop

The first time through the loop, S = My_Subtype'Last; the next time, S =
My_Subtype'Pred (My_Subtype'Last); ...; the last time, S =
My_Subtype"First.

If you need to manually work through the loop, use 'Pred instead of
'Succ:

   S := My_Subtype'Last;

   loop
      -- Do stuff with S

      exit when S = My_Subtype'First;

      S := My_Subtype'Pred (S);
   end loop;

Jeff Carter  PGP:1024/440FBE21
My real e-mail address: ( carter @ innocon . com )
"We burst our pimples at you."
Monty Python & the Holy Grail

Posted with Spam Hater - see
http://www.compulink.co.uk/~net-services/spam/




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

end of thread, other threads:[~1997-12-09  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-12-04  0:00 Curious Question Chad Reichenbach {81726}
1997-12-05  0:00 ` Tucker Taft
1997-12-09  0:00 ` Anonymous

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