[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: SimpleSwarmBug3 code
From: |
Paul Johnson |
Subject: |
Re: SimpleSwarmBug3 code |
Date: |
Mon, 22 Apr 2002 08:14:19 -0500 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.9) Gecko/20020326 |
Stacy Oerder wrote:
Hi
I've been working through the tutorial on SimpleSwarmBug3 and am having
success but now I want to try something different with my reporterbug in
the code. As the program works I understand that the first bug is the
reporterbug and through some code varaiation I've managed to make all
bugs report on their food situation. Now I want to follow the progress
of one bug, but I specifically want say the third bug in the bugList.
> to get the third bug, do
Lists comply with the Offsets protocol, so you can ask for them by
number too.
thirdBug = [bugList atOffset: 2];
This does not remove the bug, it just gets their address.
Take a look in the Swarm Reference Guide, where it shows List adopts the
collection protocol, and that it means a List can do all the stuff
implied in the protocols that List adopts.
It is a little more interesting/difficult to remove and insert an item
in the middle of a List. Here is how I would do it.
The index object for a List can be used to remove and add objects. So
create the index, step to the one you want, then remove it, and then
when you want, you can stick it back in.
id index = [bugList begin: [self getZone]];
id anObject;
id removedObject;
[index next]; //step to bug at offset 0
[index next]; //step forward 1, to offset 1
anObject = [index next]; //step forward 1, return the object that is in
//that position, just in case you want to tell it to do something
//without removing it
removedObject = [index remove]; //remove object. Should be same thing as
//anObject.
do whatever you want to removedObject. Now you have to get it back into
the list, right? The docs say the index is now not pointing at a member
of the list, so you step it once to find one, and then use the special
addBefore method of the list index object:
[index next];
[index addBefore: removedObject];
[index drop];//drop index when you are finished. Otherwise you get a
memory leak.
I've a feeling that I need to use arrays but specifically how is the
question at hand.
You are right that if the bugList were set up as a Swarm Array, this
could be easier, but if you create it as an Array, you give up a lot of
freedom to do what you want, like "addLast".
pj
--
Paul E. Johnson email: address@hidden
Dept. of Political Science http://lark.cc.ku.edu/~pauljohn
University of Kansas Office: (785) 864-9086
Lawrence, Kansas 66045 FAX: (785) 864-5700
==================================
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.