swarm-support
[Top][All Lists]
Advanced

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

Re: removing elements while looping with an index, and return valuesfrom


From: Rick Riolo
Subject: Re: removing elements while looping with an index, and return valuesfrom Set add
Date: Tue, 19 Mar 2002 08:58:13 -0500 (EST)

oh, yea, i didn't look close enough at your example.

you want to send the remove message to the index,
not to the list.
see the Collections->Index doc page:

- remove
The remove message removes the member at the current location of an index, 
and returns the member value removed. The index position is set to a 
special position between the members which previously preceded and 
followed the removed member. If there is no preceding or following member, 
the index is set to the special location before the start or after the end 
of all members. After a current member is removed, there is no member at 
the current index location, but a subsequent next or prev message will 
continue with the same member that would have been accessed had the 
current member not been removed. An InvalidIndexLoc error is raised if the 
index is not positioned at a current member.


-r 

-- 
Rick Riolo                           address@hidden
Center for Study of Complex Systems (CSCS)
4477 Randall Lab                
University of Michigan         Ann Arbor MI 48109-1120
Phone: 734 763 3323                  Fax: 734 763 9267
http://www.pscs.umich.edu/PEOPLE/rlr-home.html

On Tue, 19 Mar 2002, Gary Polhill wrote:

> Date: Tue, 19 Mar 2002 12:56:03 +0000
> From: Gary Polhill <address@hidden>
> Reply-To: address@hidden
> To: address@hidden
> Subject: Re: removing elements while looping with an index,
>      and return valuesfrom Set add
> 
> I think the problem is that you can't remove items from a list while using an 
> index as this upsets the index -- the for/while encodings are pretty much 
> equivalent. If you want to remove elements from a list, you are probably 
> better off using removeFirst and addLast into a dummy list and then copying 
> the dummy list back. So something like this:
> 
> -(void)removeFromListUnlessCondition: (id <List>) aList {
>   id <List> dummyList = [List create: scratchZone];
>   id elem;
> 
>   while([aList getCount] > 0) {
>     elem = [aList removeFirst];
>                       // You might be able to get away with
>                       // while(elem = [aList removeFirst]) {
>     if(condition) {
>       [dummyList addLast: elem];
>     }
>   }
>   while([dummyList getCount] > 0) {
>     [aList addLast: [dummyList removeFirst]];
>   }
>   [dummyList drop];
> }
> 
> Gary
> 
> >>> Rick Riolo <address@hidden> 19/03/02 12:25:13 >>>
> 
> I don't know what's wrong with what you are doing with your
> list (eg i can't recall what [index getLoc] returns under
> various cases), but i would  suggest first trying it with the form
> given in the Users Guide:
> 
> id aCollection;
> id <Index> index;
> aCollection = [List create: [self getZone]];
> index =  [aCollection begin: [self getZone]];
> 
> while( (anObject=[index next]) != nil )
> {
>   //write code that does something to anObject
> }
> 
> Also, are you sure its crashing in this loop?
> i'd put an fprintf(stderr,... before, in and after that loop.
> (Eg maybe its crashing somewhere else because you are removing
> objects from under a list of agents in the middle of
> a schedule working its way down the list).
> if all else fails, just to clarify things, 
> i'd separate out the remove and add,
> and be sure its the remove causing the crash.
> 
> As for the second, BOOL returns YES or NO as I recall,
> so first i'd try it with the explicit test, and not
> depend on the YES or NO being 0 or not 0.
> 
> - r
> 
> 
> 


                  ==================================
   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]