swarm-support
[Top][All Lists]
Advanced

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

Help on walking lists of Swarm Collection


From: William S. Shu
Subject: Help on walking lists of Swarm Collection
Date: Sat, 8 May 1999 12:59:31 +0100

A bit of assitance please:
I have been having very strange errors (segfaults) and my primary suspicion
now is the use of indexes (in collections) to walk a list of objects.  This
is because methods call methods which add/remove from the list via their own
created indices.  The manual clearly warns about this.  So I decide to use
atOffset:, and forEach: methods where possible.  I use atOffset like so:

  id <List> colonyList;
   ....
  int count, i;
  .....

  count = [colonyList getCount];
  for (i = 0; i < count; ++i) {
       cly = [colonyList atOffset: i];
       if (...condition ...) {      // remove base on certain condition
         ...
          [colonyList remove: cly];
       ...
       }
    ...
  }

My trouble is, the loop removes entries from colonyList, which makes my
value of count incorrect and (by the manual) triggers an exception.
Computing the count each time round the loop is NOT helpful as the list may
actually increase in size (via some other method invoked in the above code)
and hence not terminate when desired.  Any ideas?

Furthermore, it makes an assumption about the internal representation of the
list: all entries after a deleted element 'shift left' by 1 in their ordinal
possition in the list.  Is this the case?  If so, could it please form part
of the documentation of the next release of swarm?

I use forEach: like so:

{
...
  [colonyList forEach: M(dropDead)]; // destroy malaria population data (not
d.structure)
  [colonyList removeAll]; // remove all entries in list
  [colonyList drop];   // drop (delete) colonyList
  colonyList = nil;   // ensure no further use
...
}

Here, dropDead invokes methods that may remove entries from colonyList. (In
fact, each entry is supposed to remove itself from colonyList, then destroy
any pointers to the object that holds the colonyList)  What is not clear --
not in the manual -- is whether such changes might adversely affect the
forEach: message invoking dropDead.  In other words, the temporary index
used by forEach: is robust!

I will welcome any suggestions on how to handle the above.  One thing I was
thinking was why the List protocol (assumed to be like a singly-linked, or
perhaps doubly-linked, list) does not contain a 'next' and related methods
to walk a list.  This would be most natural for lists, and save one the
trouble of crashing indices!

Thanks

William.




                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

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