swarm-support
[Top][All Lists]
Advanced

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

Re: [Swarm-Support] How to prevent sending a message to a dropped object


From: Paul Johnson
Subject: Re: [Swarm-Support] How to prevent sending a message to a dropped object?
Date: Wed, 03 Aug 2005 14:01:26 -0500
User-agent: Mozilla Thunderbird 1.0.6-1.1.fc4 (X11/20050720)

martijn cox wrote:
Well, that would indeed work, but the thing is: I don't have the pointer to
the object available. All I can do is loop over a List of agents like so:

 while( (agent = [index next]) != nil )
    {
      if ([agent getBirthAction])
       [lifeMutationSchedule remove: [agent getBirthAction]];
      [_agentList remove: agent];
      [agent drop];
      agent = nil; // ***
    }


remove returns a pointer to the removed object. But I don't think you should remove items this way.

FIrst, don't use the while construct, use for. If your while finds a nil in your list, the iterating will stop prematurely. Second, you are wrecking the index by removing a member while iterating. That's bad.

I can give you a much better algorithm for doiing this that fixes all of these problems. Then it will be much cleaner & faster to remove with the index.

id agent, index;

index = [agentList begin: [self getZone]];

for(agent = [index next]; [index getLoc]==Member; agent = [index next])
{

   if ([agent getBirthAction])
        {
           id theAgent; 
           [lifeMutationSchedule remove: [agent getBirthAction]];
           theAgent = [index remove];
           [theAgent drop];
           theAgent = nil;
         }
}

Actions on indexes to remove items are
1. safe to the index and
2. Much Much faster than searching through a collection to find something and remove it.



--
Paul E. Johnson                       email: address@hidden
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
1541 Lilac Lane, Rm 504
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66044-3177           FAX: (785) 864-5700


reply via email to

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