comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: Wanted: Ada STL.  Reward: Ada's Future
Date: 1999/02/04
Date: 1999-02-04T00:00:00+00:00	[thread overview]
Message-ID: <m3k8xy0y4f.fsf@mheaney.ni.net> (raw)
In-Reply-To: u7lty3wpx.fsf@gsfc.nasa.gov

Stephen Leake <Stephen.Leake@gsfc.nasa.gov> writes:

> I thought Matthew just did that, but you haven't answered the question
> "why does the STL allow iteration past the end of the array". This
> bugs me too. Is there a real reason here, or is it an accident of the
> implementation?

To terminate an iteration, you always have to "fall off the end" of the
thing you're iterating over.  This is true of array iteration, loop
iteration, stream iteration, etc:

  for (i = 0; i < len; i++ { 
    ... a [i] ...
  }

The iteration terminates when i falls off the end the array.

  declare
     Node : Node_Access := List;
  begin
     while Node /= null loop
        <process Node.Item>
        Node := Node.Next;
     end loop;
  end;

This loop terminates when Node falls off the end of the list.

  loop
     Get (N);
     exit when N = 0;
     <process N>
  end loop;


This loop terminates when N falls off the end of the stream (eos being
indicated by a sentinal, the value 0).

Now what Ada does, is do this falling off the end behind the scenes, so
that you the programmer don't have to worry about whether your index has
an extra value.  However, you can use the low level approach (necessary
once in a while), by using the technique I showed in my earlier post.

I do this in the implementation of an iterator over a bounded data
structure implemented as an array:

type Stack_Iterator is
  record
    Index : Natural;  -- note that this is Natural, not Positive
    Stack : Stack_Access;
  end record;

function Initialize 
  (Stack : Stack_Type) return Stack_Iterator is
begin
  return (Index => Stack.Top, Stack => Stack'Access);
end;

function Is_Done (Iterator : Stack_Iterator) return Boolean is
begin
   return Iterator.Index = 0;  
   -- the value 0 is outside range of index subtype (Positive)
end;

procedure Advance (Iterator : Stack_Iterator) is
begin
  Iterator.Index := Iterator.Index - 1;
  -- this might case Index to fall off the end of the array
end;


Iteration would look like this:

declare
   Iter : Stack_Iterator := Initialize (Stack);
begin
   while not Is_Done (Iterator) loop
     <process Get_Item (Iter)>
     Advance (Iter);
   end loop;
end;


This loop terminates because Iter.Index falls of the end of the array
(in this case, the beginning of the array).

Iteration past the end of the array isn't an STL "problem," it is just
the nature of iteration, irrespective of language.  (Although in Ada's
case, this falling off the end is often hidden from the programmer.)





  parent reply	other threads:[~1999-02-04  0:00 UTC|newest]

Thread overview: 102+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-01-31  0:00 Wanted: Ada STL. Reward: Ada's Future Alexy V Khrabrov
1999-01-31  0:00 ` Simon Wright
1999-02-01  0:00 ` Stanley R. Allen
1999-02-01  0:00 ` Jerry van Dijk
1999-02-01  0:00   ` Marin David Condic
1999-02-01  0:00 ` dennison
1999-02-01  0:00 ` Brian Rogoff
1999-02-01  0:00   ` Ehud Lamm
1999-02-02  0:00     ` Pointer Arithmetic (was: Wanted: Ada STL....) adam
1999-02-02  0:00       ` William Clodius
1999-02-03  0:00         ` adam
1999-02-03  0:00           ` William Clodius
1999-02-03  0:00           ` Nick Roberts
1999-02-03  0:00           ` robert_dewar
1999-02-03  0:00             ` Jean-Pierre Rosen
1999-02-03  0:00       ` Nick Roberts
1999-02-03  0:00         ` robert_dewar
1999-02-03  0:00           ` Robert A Duff
1999-02-03  0:00       ` robert_dewar
1999-02-02  0:00     ` Wanted: Ada STL. Reward: Ada's Future Brian Rogoff
1999-02-02  0:00       ` robert_dewar
1999-02-04  0:00         ` Ehud Lamm
1999-02-03  0:00       ` John English
1999-02-03  0:00         ` Matthew Heaney
1999-02-03  0:00           ` Brian Rogoff
1999-02-04  0:00             ` Stephen Leake
1999-02-04  0:00               ` Hyman Rosen
1999-02-05  0:00                 ` Stephen Leake
1999-02-05  0:00                   ` Hyman Rosen
1999-02-04  0:00               ` Matthew Heaney [this message]
1999-02-04  0:00               ` Brian Rogoff
1999-02-05  0:00                 ` Stephen Leake
1999-02-05  0:00                   ` Brian Rogoff
1999-02-05  0:00               ` John English
1999-02-05  0:00                 ` Tucker Taft
1999-02-05  0:00                   ` Brian Rogoff
1999-02-05  0:00                   ` Richard D Riehle
1999-02-05  0:00                 ` Brian Rogoff
1999-02-06  0:00                   ` Matthew Heaney
1999-02-05  0:00           ` John English
1999-02-05  0:00           ` Nick Roberts
1999-02-04  0:00         ` Ehud Lamm
1999-02-04  0:00           ` Pat Rogers
1999-02-04  0:00             ` Larry Kilgallen
1999-02-04  0:00               ` Pat Rogers
1999-02-04  0:00                 ` Larry Kilgallen
1999-02-05  0:00                   ` robert_dewar
1999-02-05  0:00                     ` Larry Kilgallen
1999-02-05  0:00                     ` Tom Moran
1999-02-05  0:00                       ` dewar
1999-02-05  0:00                         ` Tom Moran
1999-02-05  0:00                           ` dewar
1999-02-05  0:00                             ` dennison
1999-02-06  0:00                               ` dewar
1999-02-08  0:00                                 ` dennison
1999-02-08  0:00                                   ` robert_dewar
1999-02-08  0:00                                     ` dennison
1999-02-09  0:00                                       ` robert_dewar
1999-02-09  0:00                                         ` dennison
1999-02-09  0:00                                     ` Nick Roberts
1999-02-07  0:00                               ` Simon Wright
1999-02-08  0:00                               ` Corey Minyard
1999-02-08  0:00                                 ` Open Source Licensing (was: Wanted: Ada STL. Reward: Ada's Future) dennison
1999-02-08  0:00                                   ` Corey Minyard
1999-02-09  0:00                                     ` robert_dewar
1999-02-09  0:00                                       ` Corey Minyard
1999-02-09  0:00                                     ` dennison
1999-02-09  0:00                                       ` Corey Minyard
1999-02-09  0:00                                 ` Wanted: Ada STL. Reward: Ada's Future robert_dewar
1999-02-09  0:00                                   ` dennison
1999-02-10  0:00                                     ` robert_dewar
1999-02-07  0:00                           ` Simon Wright
     [not found]                     ` <36ba730b.35540068@ <79fmg1$fn0$1@nnrp1.dejanews.com>
1999-02-06  0:00                       ` Larry Kilgallen
1999-02-05  0:00                 ` robert_dewar
1999-02-05  0:00               ` robert_dewar
1999-02-05  0:00                 ` Larry Kilgallen
1999-02-05  0:00                   ` robert_dewar
     [not found]                     ` <79f24e$t14 <36BB4162.52FC6D9F@averstar.com>
1999-02-05  0:00                       ` robert_dewar
1999-02-05  0:00                       ` dennison
1999-02-05  0:00                     ` Tucker Taft
1999-02-05  0:00                     ` dennison
1999-02-05  0:00                       ` robert_dewar
1999-02-05  0:00                         ` dennison
1999-02-06  0:00                       ` Nick Roberts
1999-02-04  0:00           ` Al Christians
1999-02-04  0:00           ` Brian Rogoff
1999-02-05  0:00             ` Matthew Heaney
1999-02-05  0:00               ` Brian Rogoff
1999-02-08  0:00                 ` John English
1999-02-05  0:00           ` John English
1999-02-09  0:00             ` micro_ada
1999-02-05  0:00         ` Nick Roberts
1999-02-08  0:00           ` John English
1999-02-02  0:00     ` Richard D Riehle
1999-02-03  0:00       ` robert_dewar
1999-02-01  0:00 ` Matthew Heaney
1999-02-01  0:00   ` Alexy V Khrabrov
1999-02-01  0:00     ` Matthew Heaney
1999-02-01  0:00       ` Jeff Carter
1999-02-01  0:00 ` Jeff Carter
1999-02-08  0:00   ` Michael F Brenner
1999-02-05  0:00 ` Corey Minyard
replies disabled

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