swarm-support
[Top][All Lists]
Advanced

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

Using indexes [Solaris 2.6, Swarm 1.2]


From: Gary Polhill
Subject: Using indexes [Solaris 2.6, Swarm 1.2]
Date: Thu, 22 Oct 1998 12:18:00 +0100

Here is an excerpt from the documentation for the Index class,
illustrating how to make a loop that will traverse a collection.

Following is a simple loop which illustrates such usage:

      index = [aCollection begin: aZone];
       do {
         member = [index next];
         // do something with member ...
       } while ( [index getLoc] == Member );

Foolishly I uncritically used this loop to traverse my list objects,
and, of course, when I got to the end of the list, the "do something
with member ..." bit went wrong. However, I also got a wierd bug in
that [index next] was not returning a pointer to the member of the
list. Here is an editted highlight of a gdb session:

The players:

tempZone, a Zone (declared as type id, and created earlier in the
    method using tempZone = [Zone create: scratchZone];)
lpl3, a List (declared as type id)
lp3i, an Index (declared as type id)
lp3, an item of class LandParcel (declared as type LandParcel *)
    The LandParcel class has a method getAttributes, which returns
    a BitString object. The BitString object has a method print,
    which writes the bit string to stdout. All LandParcel objects,
    when created, are given attributes with a bit string of
    length 20.

273 lp3i = [lpl3 begin: tempZone];
(gdb) n
274 do {
(gdb) n
275   lp3 = [lp3i next];
(gdb) n
276 if(lp3 == nil) {
(gdb) call [[lp3 getAttributes] print]
$22 = 0
(gdb) call [[[lpl3 atOffset: 0] getAttributes] print]
00101000001110000101
(gdb) call [[[lp3i get] getAttributes] print]
00101000001110000101

So it would seem that the index is pointing to a LandParcel object,
but the -next method is not returning it. Am I making a mistake in
statically typing lp3 to LandParcel? Does anyone have any thoughts
on what the best way to traverse a list is? Or any thoughts on which
is better (in terms of efficiency/elegance/readability) of the
following:

for(i = 0; i < [aList getCount]; i++) {
    member = [aList atOffset: i];

    // do your stuff
}

for(anIndex = [aList begin: aZone], [anIndex next];
    [anIndex getLoc] == Member;
    [anIndex next])
{
    member = [anIndex get];

    // do your stuff
}

Of course, if there is no bug in -next, then you could do:

for(anIndex = [aList begin: aZone], member = [anIndex next];
    [anIndex getLoc == Member;
    member = [anIndex next])
{
    // do your stuff
}

Is there an "official" view on how to use Lists? If there is, then
it'd be handy if this was in the documentation. (The same applies
to other classes.)

Thanks,
    Gary

--

Macaulay Land Use Research Institute, Craigiebuckler, Aberdeen. AB15 8QH
Tel: +44 (0) 1224 318611               Email: address@hidden




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