[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Example of sets and indexing
From: |
Marcus G. Daniels |
Subject: |
Re: Example of sets and indexing |
Date: |
24 Jul 1999 18:13:50 -0700 |
User-agent: |
Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4 |
>>>>> "AV" == Ajoy Victor <address@hidden> writes:
AV> How is this used?
AV> createIndex: aZone fromMember: anObject
It can be used in the situation where you have a collection that
has the notion of keys and values.
See below for an example of createIndex:fromMember: and its use with
Lists where members objects store the links (thus ensuring unique
members and the notion of a `set'). Compare the two code sections
within the "#if 1" area -- they are functionally equivalent.
AV> What messages are commonly used between createBegin and CreateEnd?
Generally, any message that describes structural characteristics
of the object you are creating. For example, a Map *must* be able to
compare one key with another, or it has no meaningful semantics
(-setCompareFunction:).
AV> What kind of messages are commonly used to manipulate sets?
add:, replace:, contains:, or getCount:.
AV> I think I'm going to need a set of objects where each contains a
AV> 2D grid Is that possible?
How about using the Discrete2d interface in the space library?
AV> I can't even program this: (What is wrong?)
Remember to include simtools.h. and call initSwarm.
A couple portability suggestions: 1) include misc.h instead of stdio.h.
Directly including a system header will make your model less portable.
2) note that `unsigned char' is not the same size as an `id'.
I suggest a double cast so that it is clear in the code that this
was not an oversight.
[s add: (id) (unsigned) i];
You're right about count vs. getCount; getCount should have worked;
that was a bug in 1.4.1.
#import <simtools.h>
#import <collections.h>
#import <defobj/Create.h>
#import <collections/Collection.h>
@interface Integer: CreateDrop
{
int value;
member_t link;
}
- setValue: (int)value;
- (int)getValue;
@end
@implementation Integer
- setValue: (int)theValue
{
value = theValue;
return self;
}
- (int)getValue
{
return value;
}
- (void)describe: outputCharStream
{
char buf[DSIZE (int) + 1 + 1];
sprintf (buf, "%d\n", value);
[outputCharStream catC: buf];
}
@end
#define MAKEINTEGER(aZone,value) \
[[[Integer createBegin: aZone] setValue: value] createEnd]
int
main (int argc, const char **argv)
{
initSwarmBatch (argc, argv);
{
id <Zone> aZone = [Zone create: globalZone];
id member;
id <Index> index;
#if 1
id l;
l = [List createBegin: aZone];
[l setIndexFromMemberLoc: offsetof (Integer, link)];
l = [l createEnd];
[l addLast: MAKEINTEGER (aZone, 10)];
member = MAKEINTEGER (aZone, 20);
[l addLast: member];
index = [l createIndex: aZone fromMember: member];
[l addLast: MAKEINTEGER (aZone, 30)];
#else
id s;
s = [OrderedSet createBegin: aZone];
[s setIndexFromMemberLoc: offsetof (Integer, link)];
s = [s createEnd];
[s add: MAKEINTEGER (aZone, 10)];
member = MAKEINTEGER (aZone, 20);
[s add: member];
index = [s createIndex: aZone fromMember: member];
[s add: MAKEINTEGER (aZone, 30)];
#endif
do xprint (member); while (member = [index next]);
}
}
/*
Local Variables:
compile-command: "/opt/src/mgd/packages/swarm/swarm/bin/libtool-swarm
--mode=link /opt/src/mgd/packages/development/egcs/bin/gcc -o memberloc -g
-Wno-import -I/opt/src/mgd/packages/swarm/swarm/include
-L/opt/src/mgd/packages/swarm/swarm/lib memberloc.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.