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: Gary Polhill
Subject: Re: removing elements while looping with an index, and return valuesfrom Set add
Date: Tue, 19 Mar 2002 12:56:03 +0000

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


-- 
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, Ken Gosier wrote:

> Date: Tue, 19 Mar 2002 07:08:55 -0500 (EST)
> From: Ken Gosier <address@hidden>
> Reply-To: address@hidden 
> To: address@hidden 
> Subject: removing elements while looping with an index,
>      and return values from Set add
> 
> 2 questions about a simple sim I'm coding up:
> 
> I'm sure the first question is an RTFM question, so if I can get a pointer
> to the proper FM to R, I'd be most grateful. :) Anyway, I'm using an index
> to loop over a List, and I want to remove elements which test positive for
> a certain condition:
> 
> id <List> newList = [List create: self];
> id <Index> index = [oldList begin: self];
> id thisElem;
> 
> for (thisElem=[index next]; [index getLoc]==Member; thisElem=[index next])
> {
>    if (  condition  )
>    {
>       [newList addLast: [oldList remove: thisElem]];
>    }
> }
> [index drop];
> 
> So basically, b/c I'm removing some of the elements of oldList while I'm
> traversing it with the index, I wind up with seg faults.
> - One discussion in the archives suggests it'd be best to just save the
> offsets of those elements that satisfy the condition, then remove them
> manually in a separate loop.
> - Also, I searched through Paul Johnson's FAQ, and found an item for
> working with Lists, but not the answer to this specific question.
> 
> Many thanks for any help or pointers. It seems like this is a
> commonly-faced problem.
> 
> 
> 2nd question: I'm adding elements into a set with an error check like so:
> 
> if (  ! [mySet add: thisElem]  )
> {
>    fprintf(stderr, "Failed to add element\n");
> }
> 
> This always prints out the error message. But then when I loop over the
> set to print out the elements, it's always been added. The ref guide says
> a BOOL is passed back from add. Help in figuring out this behavior?
> 
> 
> Many thanks for any help,
> 
> 


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


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