swarm-support
[Top][All Lists]
Advanced

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

Re: integer wrappers for Swarm Maps: did I miss something?


From: Marcus G. Daniels
Subject: Re: integer wrappers for Swarm Maps: did I miss something?
Date: 15 Sep 1999 10:18:49 -0700
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4

>>>>> "PJ" == Paul Johnson <address@hidden> writes:

PJ> Ok, so I'm writing more swarm user guide and I come to Maps.  It
PJ> seems to me that if you use an Integer wrapper class to put things
PJ> into a Map, you also need to maintain an Array of the Integer
PJ> objects so you can retrieve things from the Map when you want
PJ> them.  Not so?

Not so.  The notion of identity is defined by the comparison method or
function, so you can have different physical objects serving as the same key.

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 <simtools.h>
#import <collections.h>
#import <objectbase/SwarmObject.h>
#include <misc.h> // 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> map;
    id <Zone> 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:
*/

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