swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using collections


From: glen e. p. ropella
Subject: Re: Using collections
Date: Thu, 16 Jan 1997 23:03:17 -0700

[sorry about that... to finish...]

> 2. I've discovered that a list index doesn't perform any
> bounds checking in the "next" method.  That is, if you call
> [index next] when you are at the "End" of the list, then you
> get a core dump.
>
>    However, [index getLoc] doesn't return "End" until after
> you moved past the last list element.  Therefore, when I'm
> traversing the list, I test two conditionals:
>      ([index getLoc] != End) and ([index next] != nil).
>
>    Have I overlooked an more direct way of stopping at the
> end of a list?  Is there a method to "peek" ahead without
> moving the index?  Perhaps the "getOffset" method can be
> used to accomplish this?

I'm not sure what the problem is, here.  The first statement I understand.
If one has a list:

            1     next      2     next      3      next    4
            --              --              --             --   
           |i |            |i |            |i |           |i |
           |--|            |--|            |--|           |--|
  Index => |j |            |j |            |j |           |j |
           |--|            |--|            |--|           |--|
           |k |   Index => |k |            |k |           |k |
            --              --              --             -- 
            End             End   Index =>  End            End
                                                 Index =>

                   ==> k           ==> End         ==> Core Dump


But, the test for having already retrieved the last member of the 
list can be done around the call to "next."  For example, from
the docs, a usual use of the "next" message might be:

  index = [aCollection begin: aZone];
  while ( (member = [index next]) ) {
    // do something with member ...
  }

In other words, the "next" transfer from state (2) to state (3)
above should return 0 or NULL, which would break out of the 
while loop in the code example.

But, more directly, you should be able to use:

   if ([list atOffset: [index getOffset] + 1]]) {
      [ ... do whatever ...]
   }

Howeve, I would avoid doing anything like this because
it relies on the constancy of the list (i.e. it hasn't 
been changed) and that the index hasn't been manually moved
around.

Let me know if you've done all this and you're still getting
that behavior!  And code would help, also.

glen



reply via email to

[Prev in Thread] Current Thread [Next in Thread]