//Marcus Daniels, swarm-support, Sept. 15, 1999 /*The last clause (with the KEY and PRINTKEY macros) shows how you can re-use / dereference-into the same key for lookups, avoiding gratuitous allocation and deallocation. */ #import #import #import #include // printf @interface Integer: SwarmObject { int value; } - setValue: (int)value; - (int)getValue; @end @implementation Integer - setValue: (int)theValue { value = theValue; return self; } - (int)getValue { return value; } @end #define INTEGER(value) \ [[[Integer createBegin: scratchZone] setValue: value] createEnd] int compareIntegerObjects (id obj1, id obj2) { return ((Integer *) obj1)->value - ((Integer *) obj2)->value; } int main (int argc, const char **argv) { initSwarmBatch (argc, argv); { id map; id aZone = [Zone create: globalZone]; map = [[[Map createBegin: aZone] setCompareFunction: compareIntegerObjects] createEnd]; [map at: INTEGER (10) insert: INTEGER (100)]; [map at: INTEGER (20) insert: INTEGER (200)]; { id mi, member, key; mi = [map begin: scratchZone]; while ((member = [mi next: (id *) &key])) printf ("%d -> %d\n", [key getValue], [member getValue]); [mi drop]; } { Integer *key = INTEGER (0); #define KEY(theValue) ((key)->value = (theValue), key) #define PRINTKEY(theKey) \ printf ("find: %d -> %d\n", (theKey), [[map at: KEY (theKey)] getValue]) PRINTKEY (10); PRINTKEY (20); #undef KEY #undef PRINTKEY [key drop]; } } } /* Local Variables: compile-command: "$SWARMHOME/bin/libtool-swarm --mode=link gcc -o integermap -g -Wno-import -I$SWARMHOME/include -L$SWARMHOME/lib integermap.m -lswarm -lobjc" End: */