Commits

Tuukka Norri committed 29c4f72

Added HOM methods for collecting objects from an array or a set into a dictionary

Comments (0)

Files changed (2)

Sources/PGTSHOM.h

 - (id) PGTSDo;
 - (id) PGTSCollect;
 - (id) PGTSCollectReturning: (Class) aClass;
+- (id) PGTSKeyCollect;
 - (id) PGTSSelectFunction: (int (*)(id)) fptr;
 - (id) PGTSSelectFunction: (int (*)(id, void*)) fptr argument: (void *) arg;
 - (id) PGTSVisit: (id) visitor;
 - (id) PGTSDo;
 - (id) PGTSCollect;
 - (id) PGTSCollectReturning: (Class) aClass;
+- (id) PGTSKeyCollect;
 - (id) PGTSSelectFunction: (int (*)(id)) fptr;
 - (id) PGTSSelectFunction: (int (*)(id, void*)) fptr argument: (void *) arg;
 /**

Sources/PGTSHOM.m

 
 
 static void
+CollectAndPerformKeysSetArray (id self, id retval, NSInvocation* invocation)
+{
+	TSEnumerate (currentObject, e, [self objectEnumerator])
+	{
+		[invocation invokeWithTarget: currentObject];
+		id collected = nil;
+		[invocation getReturnValue: &collected];
+		if (! collected) collected = [NSNull null];
+		[retval setObject: collected forKey: currentObject];
+	}
+}
+
+
+static void
 Do (NSInvocation* invocation, NSEnumerator* enumerator)
 {
 	TSEnumerate (currentObject, e, enumerator)
 	return [self PGTSCollectReturning: [NSMutableSet class]];
 }
 
+- (id) PGTSKeyCollect
+{
+	return HOMTrampoline (self, @selector (PGTSKeyCollect:userInfo:), nil);
+}
+
 - (id) PGTSSelectFunction: (int (*)(id)) fptr
 {
 	id retval = [NSMutableSet setWithCapacity: [self count]];
 	CollectAndPerformSetArray (self, retval, invocation);
 }
 
+- (void) PGTSKeyCollect: (NSInvocation *) invocation userInfo: (id) ignored
+{
+	id retval = [[[NSMutableDictionary alloc] initWithCapacity: [self count]] autorelease];
+	CollectAndPerformKeysSetArray (self, retval, invocation);
+}
+
 - (void) PGTSDo: (NSInvocation *) invocation userInfo: (id) anObject
 {
 	Do (invocation, [self objectEnumerator]);
 	return [self PGTSCollectReturning: [NSMutableArray class]];
 }
 
+- (id) PGTSKeyCollect
+{
+	return HOMTrampoline (self, @selector (PGTSKeyCollect:userInfo:), nil);
+}
+
 - (id) PGTSCollectReturning: (Class) aClass
 {
 	return HOMTrampoline (self, @selector (PGTSCollect:userInfo:), aClass);
 	CollectAndPerformSetArray (self, retval, invocation);
 }
 
+- (void) PGTSKeyCollect: (NSInvocation *) invocation userInfo: (id) ignored
+{
+	id retval = [[[NSMutableDictionary alloc] initWithCapacity: [self count]] autorelease];
+	CollectAndPerformKeysSetArray (self, retval, invocation);
+}
+
 - (void) PGTSDo: (NSInvocation *) invocation userInfo: (id) userInfo
 {
 	Do (invocation, [self objectEnumerator]);