[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: nested Maps?
From: |
Marcus G. Daniels |
Subject: |
Re: nested Maps? |
Date: |
30 Jul 1999 19:25:04 -0700 |
User-agent: |
Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4 |
>>>>> "DC" == Daniel Calhoun <address@hidden> writes:
DC> any community lore on the advisability/inadvisability of this approach?
Just fine. But depending on your application, it may be easier to
understand the code if you use composite keys instead of key that
gives you a value that contains more keys. It depends whether or not
you need to be able to look at a chunk at a time (without applying a filter).
DC> Are there any publicly available examples of code for nested Maps
DC> (or nested Lists)?
The addAgent:X:Y: method is an example of how you might update
such a data structure. The data structure is a Map of Maps of Lists.
Something like this might be useful if you had a very sparse
space and you wanted to be able to look at the particular agents
in a given row.
(Below, ignore the code inside USE_HDF5 ifdefs unless you want to be
able to store the output to a file. If you do set USE_HDF5, you can
look at the output with the `h5dump' program that is installed with
the HDF5 distribution. -isClass is defined due to a bug in Swarm 1.4.1.)
#import <simtools.h> // initSwarm
#import <collections.h> // Map, List
#import <random.h> // uniformIntRand
#import <defobj/Create.h> // CreateDrop
@interface Agent: CreateDrop
{
unsigned serialNumber;
}
- setSerialNumber: (unsigned)serialNumber;
@end
@implementation Agent
- setSerialNumber: (unsigned)theSerialNumber
{
serialNumber = theSerialNumber;
return self;
}
@end
@interface SparseSpace: CreateDrop
{
id <Map> ymap;
}
+ createBegin: aZone;
#ifdef USE_HDF5
- updateArchiver: archiver;
#endif
@end
static id
createIntMap (id <Zone> aZone)
{
return [[[Map createBegin: aZone]
setCompareFunction: compareIntegers]
createEnd];
}
@implementation SparseSpace
+ createBegin: aZone
{
SparseSpace *obj = [super createBegin: aZone];
obj->ymap = createIntMap (aZone);
return obj;
}
- (void)addAgent: agent X: (unsigned)x Y: (unsigned)y
{
id <Map> xmap = [ymap at: (id) y];
id <List> agentList;
if (!xmap)
{
xmap = createIntMap ([self getZone]);
[ymap at: (id) y insert: xmap];
}
agentList = [xmap at: (id) x];
if (!agentList)
{
agentList = [List create: [self getZone]];
[xmap at: (id) x insert: agentList];
}
[agentList addLast: agent];
}
#ifdef USE_HDF5
- updateArchiver: archiver
{
[archiver putDeep: "ymap" object: ymap];
return self;
}
- (BOOL)isClass
{
return NO;
}
#endif
@end
int
main (int argc, const char **argv)
{
unsigned i;
id sparseSpace;
initSwarm (argc, argv);
sparseSpace = [SparseSpace create: globalZone];
for (i = 0; i < 10; i++)
{
unsigned x, y;
id agent = [[[Agent createBegin: globalZone]
setSerialNumber: i]
createEnd];
x = [uniformIntRand getIntegerWithMin: 0 withMax: 100000];
y = [uniformIntRand getIntegerWithMin: 0 withMax: 100000];
[sparseSpace addAgent: agent X: x Y: y];
}
#ifdef USE_HDF5
[hdf5Archiver registerClient: sparseSpace];
[hdf5Archiver save];
#endif
}
/*
Local Variables:
compile-command: "/opt/gnu/bin/gcc -o nestedmaps -g -DUSE_HDF5 -Wno-import
-L/opt/SUNWtcl/8.0/sun4/lib -R/opt/SUNWtcl/8.0/sun4/lib -L/opt/SDGblt/2.4g/lib
-R/opt/SDGblt/2.4g/lib -L/opt/SDGlibffi/1.20/lib -R/opt/SDGlibffi/1.20/lib
-L/opt/SDGswarm/1.4.1/lib -L/opt/SDGzlib/1.1.3/lib -L/usr/local/X11/lib
-R/usr/local/X11/lib -L/usr/openwin/lib -R/usr/openwin/lib
-L/opt/SDGhdf5/1.0.1/lib -I/opt/SDGswarm/1.4.1/include nestedmaps.m -lanalysis
-lsimtools -lsimtoolsgui -lactivity -ltkobjc -lrandom -lobjectbase -ldefobj
-lcollections -lmisc -ltclobjc -ltk8.0 -ltcl8.0 -lBLT -lsocket -ldl -lnsl
-L/usr/openwin/lib -lhdf5 -lpng -lz -lXpm -lX11 -lffi -lm -lobjc -lpthread
-lposix4"
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.
- nested Maps?, Daniel Calhoun, 1999/07/30
- Re: nested Maps?,
Marcus G. Daniels <=