diff --git a/Headers/gnustep/base/NSDictionary.h b/Headers/gnustep/base/NSDictionary.h index 5c47d9dab..b6bee1531 100644 --- a/Headers/gnustep/base/NSDictionary.h +++ b/Headers/gnustep/base/NSDictionary.h @@ -30,10 +30,10 @@ @interface NSDictionary : NSObject - initWithObjects: (id*)objects - forKeys: (NSObject**)keys + forKeys: (id*)keys count: (unsigned)count; - (unsigned) count; -- objectForKey: (NSObject*)aKey; +- objectForKey: (id)aKey; - (NSEnumerator*) keyEnumerator; - (NSEnumerator*) objectEnumerator; @end @@ -51,6 +51,8 @@ + dictionaryWithObjectsAndKeys: (id)object, ...; - initWithContentsOfFile: (NSString*)path; - initWithDictionary: (NSDictionary*)otherDictionary; +- initWithDictionary: (NSDictionary*)otherDictionary + copyItems: (BOOL)shouldCopy; - initWithObjects: (NSArray*)objects forKeys: (NSArray*)keys; - initWithObjectsAndKeys: (id)object, ...; @@ -74,8 +76,8 @@ @interface NSMutableDictionary: NSDictionary - initWithCapacity: (unsigned)numItems; -- (void) setObject:anObject forKey:(NSObject *)aKey; -- (void) removeObjectForKey:(NSObject *)aKey; +- (void) setObject:anObject forKey:(id)aKey; +- (void) removeObjectForKey:(id)aKey; @end @interface NSMutableDictionary (NonCore) diff --git a/Source/NSArray.m b/Source/NSArray.m index 5374b9a8c..9b272d559 100644 --- a/Source/NSArray.m +++ b/Source/NSArray.m @@ -368,6 +368,11 @@ static Class NSMutableArray_concrete_class; aBuffer[j++] = [self objectAtIndex: i]; } +- (unsigned) hash +{ + return [self count]; +} + - (unsigned) indexOfObjectIdenticalTo:anObject { unsigned i, c = [self count]; diff --git a/Source/NSDictionary.m b/Source/NSDictionary.m index 5d2f2a6cc..c9893572f 100644 --- a/Source/NSDictionary.m +++ b/Source/NSDictionary.m @@ -30,7 +30,6 @@ #include #include #include -#include @interface NSDictionaryNonCore : NSDictionary @end @@ -82,7 +81,7 @@ static Class NSMutableDictionary_concrete_class; /* This is the designated initializer */ - initWithObjects: (id*)objects - forKeys: (NSObject**)keys + forKeys: (id*)keys count: (unsigned)count { [self subclassResponsibility:_cmd]; @@ -95,7 +94,7 @@ static Class NSMutableDictionary_concrete_class; return 0; } -- objectForKey: (NSObject*)aKey +- objectForKey: (id)aKey { [self subclassResponsibility:_cmd]; return 0; @@ -186,7 +185,7 @@ static Class NSMutableDictionary_concrete_class; } + dictionaryWithObjects: (id*)objects - forKeys: (NSObject**)keys + forKeys: (id*)keys count: (unsigned)count { return [[[self alloc] initWithObjects:objects @@ -195,19 +194,25 @@ static Class NSMutableDictionary_concrete_class; autorelease]; } +- (unsigned) hash +{ + return [self count]; +} + - initWithObjects: (NSArray*)objects forKeys: (NSArray*)keys { - int c = [objects count]; - id os[c], ks[c]; + int objectCount = [objects count]; + id os[objectCount], ks[objectCount]; int i; - assert(c == [keys count]); /* Should be NSException instead */ - for (i = 0; i < c; i++) + if (objectCount != [keys count]) { - os[i] = [objects objectAtIndex:i]; - ks[i] = [keys objectAtIndex:i]; + [NSException raise: NSInvalidArgumentException + format: @"init with obj and key arrays of different sizes"]; } - return [self initWithObjects:os forKeys:ks count:c]; + [objects getObjects: os]; + [keys getObjects: ks]; + return [self initWithObjects:os forKeys:ks count:objectCount]; } + dictionaryWithObjectsAndKeys: (id)firstObject, ... @@ -278,18 +283,43 @@ static Class NSMutableDictionary_concrete_class; } - initWithDictionary: (NSDictionary*)other +{ + return [self initWithDictionary: other copyItems: NO]; +} + +- initWithDictionary: (NSDictionary*)other copyItems: (BOOL)shouldCopy { int c = [other count]; id os[c], ks[c], k, e = [other keyEnumerator]; int i = 0; - while ((k = [e nextObject])) + if (shouldCopy) { - ks[i] = k; - os[i] = [other objectForKey:k]; - i++; + NSZone *z = [self zone]; + + while ((k = [e nextObject])) + { + ks[i] = k; + os[i] = [[other objectForKey: k] copyWithZone: z]; + i++; + } + self = [self initWithObjects: os forKeys: ks count: i]; + while (i > 0) + { + [os[--i] release]; + } + return self; + } + else + { + while ((k = [e nextObject])) + { + ks[i] = k; + os[i] = [other objectForKey:k]; + i++; + } + return [self initWithObjects:os forKeys:ks count:c]; } - return [self initWithObjects:os forKeys:ks count:c]; } - initWithContentsOfFile: (NSString*)path @@ -354,9 +384,9 @@ static Class NSMutableDictionary_concrete_class; for (i = 0; i < c; i++) { k[i] = [e nextObject]; - assert(k[i]); + NSAssert (k[i], NSInternalInconsistencyException); } - assert(![e nextObject]); + NSAssert (![e nextObject], NSInternalInconsistencyException); return [[[NSArray alloc] initWithObjects:k count:c] autorelease]; } @@ -370,9 +400,9 @@ static Class NSMutableDictionary_concrete_class; for (i = 0; i < c; i++) { k[i] = [e nextObject]; - assert(k[i]); + NSAssert (k[i], NSInternalInconsistencyException); } - assert(![e nextObject]); + NSAssert (![e nextObject], NSInternalInconsistencyException); return [[[NSArray alloc] initWithObjects:k count:c] autorelease]; } @@ -386,6 +416,8 @@ static Class NSMutableDictionary_concrete_class; while ((k = [e nextObject])) if ([anObject isEqual: [self objectForKey: k]]) a[c++] = k; + if (c == 0) + return nil; return [[[NSArray alloc] initWithObjects: a count: c] autorelease]; } @@ -676,12 +708,12 @@ compareIt(id o1, id o2, void* context) return 0; } -- (void) setObject:anObject forKey:(NSObject *)aKey +- (void) setObject:anObject forKey:(id)aKey { [self subclassResponsibility:_cmd]; } -- (void) removeObjectForKey:(NSObject *)aKey +- (void) removeObjectForKey:(id)aKey { [self subclassResponsibility:_cmd]; } @@ -698,7 +730,7 @@ compareIt(id o1, id o2, void* context) /* Override superclass's designated initializer */ - initWithObjects: (id*)objects - forKeys: (NSObject**)keys + forKeys: (id*)keys count: (unsigned)count { [self initWithCapacity:count]; diff --git a/Source/NSGArray.m b/Source/NSGArray.m index 2b182d885..0f99f52bd 100644 --- a/Source/NSGArray.m +++ b/Source/NSGArray.m @@ -140,6 +140,11 @@ return _count; } +- (unsigned) hash +{ + return _count; +} + - (unsigned) indexOfObject: anObject { unsigned hash = [anObject hash]; diff --git a/Source/NSGCountedSet.m b/Source/NSGCountedSet.m index 0801a8081..d374ed88a 100644 --- a/Source/NSGCountedSet.m +++ b/Source/NSGCountedSet.m @@ -42,14 +42,14 @@ @interface NSGCountedSet : NSCountedSet { @public - FastMapTable_t map; + FastMapTable_t map; } @end @interface NSGCountedSetEnumerator : NSEnumerator { - NSGCountedSet *set; - FastMapNode node; + NSGCountedSet *set; + FastMapNode node; } @end @@ -57,29 +57,31 @@ - initWithSet: (NSSet*)d { - self = [super init]; - if (self) { - set = [(NSGCountedSet*)d retain]; - node = set->map.firstNode; + self = [super init]; + if (self) + { + set = [(NSGCountedSet*)d retain]; + node = set->map.firstNode; } - return self; + return self; } - nextObject { - FastMapNode old = node; + FastMapNode old = node; - if (node == 0) { - return nil; + if (node == 0) + { + return nil; } - node = node->nextInMap; - return old->key.o; + node = node->nextInMap; + return old->key.o; } - (void) dealloc { - [set release]; - [super dealloc]; + [set release]; + [super dealloc]; } @end @@ -89,158 +91,182 @@ + (void) initialize { - if (self == [NSGCountedSet class]) { - class_add_behavior(self, [NSSetNonCore class]); - class_add_behavior(self, [NSMutableSetNonCore class]); + if (self == [NSGCountedSet class]) + { + class_add_behavior(self, [NSSetNonCore class]); + class_add_behavior(self, [NSMutableSetNonCore class]); } } - (void) dealloc { - FastMapEmptyMap(&map); - [super dealloc]; + FastMapEmptyMap(&map); + [super dealloc]; } - (void) encodeWithCoder: (NSCoder*)aCoder { - unsigned count = map.nodeCount; - FastMapNode node = map.firstNode; - SEL sel1 = @selector(encodeObject:); - IMP imp1 = [aCoder methodForSelector: sel1]; - SEL sel2 = @selector(encodeValueOfObjCType:at:); - IMP imp2 = [aCoder methodForSelector: sel2]; - const char *type = @encode(unsigned); + unsigned count = map.nodeCount; + FastMapNode node = map.firstNode; + SEL sel1 = @selector(encodeObject:); + IMP imp1 = [aCoder methodForSelector: sel1]; + SEL sel2 = @selector(encodeValueOfObjCType:at:); + IMP imp2 = [aCoder methodForSelector: sel2]; + const char *type = @encode(unsigned); - (*imp2)(aCoder, sel2, type, &count); + (*imp2)(aCoder, sel2, type, &count); - while (node != 0) { - (*imp1)(aCoder, sel1, node->key.o); - (*imp2)(aCoder, sel2, type, &node->value.I); - node = node->nextInMap; + while (node != 0) + { + (*imp1)(aCoder, sel1, node->key.o); + (*imp2)(aCoder, sel2, type, &node->value.I); + node = node->nextInMap; } } - (id) initWithCoder: (NSCoder*)aCoder { - unsigned count; - id value; - unsigned valcnt; - SEL sel = @selector(decodeValueOfObjCType:at:); - IMP imp = [aCoder methodForSelector: sel]; - const char *utype = @encode(unsigned); - const char *otype = @encode(id); + unsigned count; + id value; + unsigned valcnt; + SEL sel = @selector(decodeValueOfObjCType:at:); + IMP imp = [aCoder methodForSelector: sel]; + const char *utype = @encode(unsigned); + const char *otype = @encode(id); - (*imp)(aCoder, sel, utype, &count); + (*imp)(aCoder, sel, utype, &count); - FastMapInitWithZoneAndCapacity(&map, [self zone], count); - while (count-- > 0) { - (*imp)(aCoder, sel, otype, &value); - (*imp)(aCoder, sel, utype, &valcnt); - FastMapAddPairNoRetain(&map, (FastMapItem)value, (FastMapItem)valcnt); + FastMapInitWithZoneAndCapacity(&map, [self zone], count); + while (count-- > 0) + { + (*imp)(aCoder, sel, otype, &value); + (*imp)(aCoder, sel, utype, &valcnt); + FastMapAddPairNoRetain(&map, (FastMapItem)value, (FastMapItem)valcnt); } - return self; + return self; } /* Designated initialiser */ - (id) initWithCapacity: (unsigned)cap { - FastMapInitWithZoneAndCapacity(&map, [self zone], cap); - return self; + FastMapInitWithZoneAndCapacity(&map, [self zone], cap); + return self; } - (id) initWithObjects: (id*)objs count: (unsigned)c { - int i; + int i; - if ([self initWithCapacity: c] == nil) { - return nil; + if ([self initWithCapacity: c] == nil) + { + return nil; } - for (i = 0; i < c; i++) { - FastMapNode node; + for (i = 0; i < c; i++) + { + FastMapNode node; - if (objs[i] == nil) { - [self autorelease]; - [NSException raise: NSInvalidArgumentException - format: @"Tried to init counted set with nil value"]; + if (objs[i] == nil) + { + [self autorelease]; + [NSException raise: NSInvalidArgumentException + format: @"Tried to init counted set with nil value"]; } - node = FastMapNodeForKey(&map, (FastMapItem)objs[i]); - if (node == 0) { - FastMapAddPair(&map,(FastMapItem)objs[i],(FastMapItem)(unsigned)1); + node = FastMapNodeForKey(&map, (FastMapItem)objs[i]); + if (node == 0) + { + FastMapAddPair(&map,(FastMapItem)objs[i],(FastMapItem)(unsigned)1); } - else { - node->value.I++; + else + { + node->value.I++; } } - return self; + return self; } - (void) addObject: (NSObject*)anObject { - FastMapNode node; + FastMapNode node; - if (anObject == nil) { - [NSException raise: NSInvalidArgumentException - format: @"Tried to nil value to counted set"]; + if (anObject == nil) + { + [NSException raise: NSInvalidArgumentException + format: @"Tried to nil value to counted set"]; } - node = FastMapNodeForKey(&map, (FastMapItem)anObject); - if (node == 0) { - FastMapAddPair(&map,(FastMapItem)anObject,(FastMapItem)(unsigned)1); + node = FastMapNodeForKey(&map, (FastMapItem)anObject); + if (node == 0) + { + FastMapAddPair(&map,(FastMapItem)anObject,(FastMapItem)(unsigned)1); } - else { - node->value.I++; + else + { + node->value.I++; } } - (unsigned) count { - return map.nodeCount; + return map.nodeCount; } - (unsigned) countForObject: (id)anObject { - if (anObject) { - FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject); + if (anObject) + { + FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject); - if (node) { - return node->value.I; + if (node) + { + return node->value.I; } } - return 0; + return 0; +} + +- (unsigned) hash +{ + return map.nodeCount; } - (id) member: (id)anObject { - if (anObject) { - FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject); + if (anObject) + { + FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject); - if (node) { - return node->key.o; + if (node) + { + return node->key.o; } } - return nil; + return nil; } - (NSEnumerator*) objectEnumerator { - return [[[NSGCountedSetEnumerator alloc] initWithSet: self] autorelease]; + return [[[NSGCountedSetEnumerator alloc] initWithSet: self] autorelease]; } - (void) removeObject: (NSObject*)anObject { - if (anObject) { - FastMapBucket bucket; - - bucket = FastMapBucketForKey(&map, (FastMapItem)anObject); - if (bucket) { - FastMapNode node; + if (anObject) + { + FastMapBucket bucket; - node = FastMapNodeForKeyInBucket(bucket, (FastMapItem)anObject); - if (node) { - if (--node->value.I == 0) { - FastMapRemoveNodeFromMap(&map, bucket, node); - FastMapFreeNode(&map, node); + bucket = FastMapBucketForKey(&map, (FastMapItem)anObject); + if (bucket) + { + FastMapNode node; + + node = FastMapNodeForKeyInBucket(bucket, (FastMapItem)anObject); + if (node) + { + if (--node->value.I == 0) + { + FastMapRemoveNodeFromMap(&map, bucket, node); + FastMapFreeNode(&map, node); } } } @@ -249,7 +275,7 @@ - (void) removeAllObjects { - FastMapCleanMap(&map); + FastMapCleanMap(&map); } @end diff --git a/Source/NSGDictionary.m b/Source/NSGDictionary.m index 42992311f..9ebca07de 100644 --- a/Source/NSGDictionary.m +++ b/Source/NSGDictionary.m @@ -37,57 +37,63 @@ * instances of the string classes we know about! */ typedef struct { - Class *isa; - char *_contents_chars; - int _count; - NSZone *_zone; - unsigned _hash; + Class *isa; + char *_contents_chars; + int _count; + NSZone *_zone; + unsigned _hash; } *dictAccessToStringHack; static INLINE unsigned -myHash(NSObject *obj) +myHash(id obj) { - if (fastIsInstance(obj)) { - Class c = fastClass(obj); + if (fastIsInstance(obj)) + { + Class c = fastClass(obj); - if (c == _fastCls._NXConstantString || - c == _fastCls._NSGCString || - c == _fastCls._NSGMutableCString || - c == _fastCls._NSGString || - c == _fastCls._NSGMutableString) { - - if (((dictAccessToStringHack)obj)->_hash == 0) { - ((dictAccessToStringHack)obj)->_hash = + if (c == _fastCls._NXConstantString || + c == _fastCls._NSGCString || + c == _fastCls._NSGMutableCString || + c == _fastCls._NSGString || + c == _fastCls._NSGMutableString) + { + if (((dictAccessToStringHack)obj)->_hash == 0) + { + ((dictAccessToStringHack)obj)->_hash = _fastImp._NSString_hash(obj, @selector(hash)); } - return ((dictAccessToStringHack)obj)->_hash; + return ((dictAccessToStringHack)obj)->_hash; } } - return [obj hash]; + return [obj hash]; } static INLINE BOOL -myEqual(NSObject *self, NSObject *other) +myEqual(id self, id other) { - if (self == other) { - return YES; + if (self == other) + { + return YES; } - if (fastIsInstance(self)) { - Class c = fastClass(self); + if (fastIsInstance(self)) + { + Class c = fastClass(self); - if (c == _fastCls._NXConstantString || - c == _fastCls._NSGCString || - c == _fastCls._NSGMutableCString) { - return _fastImp._NSGCString_isEqual_(self, + if (c == _fastCls._NXConstantString || + c == _fastCls._NSGCString || + c == _fastCls._NSGMutableCString) + { + return _fastImp._NSGCString_isEqual_(self, @selector(isEqual:), other); } - if (c == _fastCls._NSGString || - c == _fastCls._NSGMutableString) { - return _fastImp._NSGString_isEqual_(self, + if (c == _fastCls._NSGString || + c == _fastCls._NSGMutableString) + { + return _fastImp._NSGString_isEqual_(self, @selector(isEqual:), other); } } - return [self isEqual: other]; + return [self isEqual: other]; } /* @@ -106,21 +112,21 @@ myEqual(NSObject *self, NSObject *other) @interface NSGDictionary : NSDictionary { @public - FastMapTable_t map; + FastMapTable_t map; } @end @interface NSGMutableDictionary : NSMutableDictionary { @public - FastMapTable_t map; + FastMapTable_t map; } @end @interface NSGDictionaryKeyEnumerator : NSEnumerator { - NSGDictionary *dictionary; - FastMapNode node; + NSGDictionary *dictionary; + FastMapNode node; } @end @@ -131,112 +137,182 @@ myEqual(NSObject *self, NSObject *other) + (void) initialize { - if (self == [NSGDictionary class]) { - behavior_class_add_class(self, [NSDictionaryNonCore class]); + if (self == [NSGDictionary class]) + { + behavior_class_add_class(self, [NSDictionaryNonCore class]); } } - (unsigned) count { - return map.nodeCount; + return map.nodeCount; } - (void) dealloc { - FastMapEmptyMap(&map); - [super dealloc]; + FastMapEmptyMap(&map); + [super dealloc]; } - (void) encodeWithCoder: (NSCoder*)aCoder { - unsigned count = map.nodeCount; - FastMapNode node = map.firstNode; - SEL sel = @selector(encodeObject:); - IMP imp = [aCoder methodForSelector: sel]; + unsigned count = map.nodeCount; + FastMapNode node = map.firstNode; + SEL sel = @selector(encodeObject:); + IMP imp = [aCoder methodForSelector: sel]; - [aCoder encodeValueOfObjCType: @encode(unsigned) at: &count]; - while (node != 0) { - (*imp)(aCoder, sel, node->key.o); - (*imp)(aCoder, sel, node->value.o); - node = node->nextInMap; + [aCoder encodeValueOfObjCType: @encode(unsigned) at: &count]; + while (node != 0) + { + (*imp)(aCoder, sel, node->key.o); + (*imp)(aCoder, sel, node->value.o); + node = node->nextInMap; } } +- (unsigned) hash +{ + return map.nodeCount; +} + - (id) initWithCoder: (NSCoder*)aCoder { - unsigned count; - id key; - id value; - SEL sel = @selector(decodeValueOfObjCType:at:); - IMP imp = [aCoder methodForSelector: sel]; - const char *type = @encode(id); + unsigned count; + id key; + id value; + SEL sel = @selector(decodeValueOfObjCType:at:); + IMP imp = [aCoder methodForSelector: sel]; + const char *type = @encode(id); - [aCoder decodeValueOfObjCType: @encode(unsigned) - at: &count]; + [aCoder decodeValueOfObjCType: @encode(unsigned) + at: &count]; - FastMapInitWithZoneAndCapacity(&map, fastZone(self), count); - while (count-- > 0) { - (*imp)(aCoder, sel, type, &key); - (*imp)(aCoder, sel, type, &value); - FastMapAddPairNoRetain(&map, (FastMapItem)key, (FastMapItem)value); + FastMapInitWithZoneAndCapacity(&map, fastZone(self), count); + while (count-- > 0) + { + (*imp)(aCoder, sel, type, &key); + (*imp)(aCoder, sel, type, &value); + FastMapAddPairNoRetain(&map, (FastMapItem)key, (FastMapItem)value); } - return self; + return self; } /* Designated initialiser */ -- (id) initWithObjects: (id*)objs forKeys: (NSObject**)keys count: (unsigned)c +- (id) initWithObjects: (id*)objs forKeys: (id*)keys count: (unsigned)c { - int i; - FastMapInitWithZoneAndCapacity(&map, fastZone(self), c); - for (i = 0; i < c; i++) { - FastMapNode node; + int i; - if (keys[i] == nil) { - [self autorelease]; - [NSException raise: NSInvalidArgumentException - format: @"Tried to init dictionary with nil key"]; + FastMapInitWithZoneAndCapacity(&map, fastZone(self), c); + for (i = 0; i < c; i++) + { + FastMapNode node; + + if (keys[i] == nil) + { + [self autorelease]; + [NSException raise: NSInvalidArgumentException + format: @"Tried to init dictionary with nil key"]; } - if (objs[i] == nil) { - [self autorelease]; - [NSException raise: NSInvalidArgumentException - format: @"Tried to init dictionary with nil value"]; + if (objs[i] == nil) + { + [self autorelease]; + [NSException raise: NSInvalidArgumentException + format: @"Tried to init dictionary with nil value"]; } - node = FastMapNodeForKey(&map, (FastMapItem)keys[i]); - if (node) { - [objs[i] retain]; - [node->value.o release]; - node->value.o = objs[i]; + node = FastMapNodeForKey(&map, (FastMapItem)keys[i]); + if (node) + { + [objs[i] retain]; + [node->value.o release]; + node->value.o = objs[i]; } - else { - FastMapAddPair(&map, (FastMapItem)keys[i], (FastMapItem)objs[i]); + else + { + FastMapAddPair(&map, (FastMapItem)keys[i], (FastMapItem)objs[i]); } } - return self; + return self; +} + +/* + * This avoids using the designated initialiser for performance reasons. + */ +- (id) initWithDictionary: (NSDictionary*)other + copyItems: (BOOL)shouldCopy +{ + NSEnumerator *e = [other keyEnumerator]; + NSZone *z = fastZone(self); + unsigned c = [other count]; + unsigned i; + + FastMapInitWithZoneAndCapacity(&map, z, c); + for (i = 0; i < c; i++) + { + FastMapNode node; + id k = [e nextObject]; + id o = [other objectForKey: k]; + + k = [k copyWithZone: z]; + if (k == nil) + { + [self autorelease]; + [NSException raise: NSInvalidArgumentException + format: @"Tried to init dictionary with nil key"]; + } + if (shouldCopy) + { + o = [o copyWithZone: z]; + } + else + { + o = [o retain]; + } + if (o == nil) + { + [self autorelease]; + [NSException raise: NSInvalidArgumentException + format: @"Tried to init dictionary with nil value"]; + } + + node = FastMapNodeForKey(&map, (FastMapItem)k); + if (node) + { + [node->value.o release]; + node->value.o = o; + } + else + { + FastMapAddPairNoRetain(&map, (FastMapItem)k, (FastMapItem)o); + } + } + return self; } - (NSEnumerator*) keyEnumerator { - return [[[NSGDictionaryKeyEnumerator alloc] initWithDictionary: self] + return [[[NSGDictionaryKeyEnumerator alloc] initWithDictionary: self] autorelease]; } - (NSEnumerator*) objectEnumerator { - return [[[NSGDictionaryObjectEnumerator alloc] initWithDictionary: self] + return [[[NSGDictionaryObjectEnumerator alloc] initWithDictionary: self] autorelease]; } - (id) objectForKey: aKey { - if (aKey != nil) { - FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)aKey); + if (aKey != nil) + { + FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)aKey); - if (node) { - return node->value.o; + if (node) + { + return node->value.o; } } - return nil; + return nil; } @end @@ -245,51 +321,57 @@ myEqual(NSObject *self, NSObject *other) + (void) initialize { - if (self == [NSGMutableDictionary class]) { - behavior_class_add_class(self, [NSMutableDictionaryNonCore class]); - behavior_class_add_class(self, [NSGDictionary class]); + if (self == [NSGMutableDictionary class]) + { + behavior_class_add_class(self, [NSMutableDictionaryNonCore class]); + behavior_class_add_class(self, [NSGDictionary class]); } } /* Designated initialiser */ - (id) initWithCapacity: (unsigned)cap { - FastMapInitWithZoneAndCapacity(&map, fastZone(self), cap); - return self; + FastMapInitWithZoneAndCapacity(&map, fastZone(self), cap); + return self; } -- (void) setObject: (NSObject*)anObject forKey: (NSObject *)aKey +- (void) setObject: (id)anObject forKey: (id)aKey { - FastMapNode node; + FastMapNode node; - if (aKey == nil) { - [NSException raise: NSInvalidArgumentException - format: @"Tried to add nil key to dictionary"]; + if (aKey == nil) + { + [NSException raise: NSInvalidArgumentException + format: @"Tried to add nil key to dictionary"]; } - if (anObject == nil) { - [NSException raise: NSInvalidArgumentException - format: @"Tried to add nil value to dictionary"]; + if (anObject == nil) + { + [NSException raise: NSInvalidArgumentException + format: @"Tried to add nil value to dictionary"]; } - node = FastMapNodeForKey(&map, (FastMapItem)aKey); - if (node) { - [anObject retain]; - [node->value.o release]; - node->value.o = anObject; + node = FastMapNodeForKey(&map, (FastMapItem)aKey); + if (node) + { + [anObject retain]; + [node->value.o release]; + node->value.o = anObject; } - else { - FastMapAddPair(&map, (FastMapItem)aKey, (FastMapItem)anObject); + else + { + FastMapAddPair(&map, (FastMapItem)aKey, (FastMapItem)anObject); } } - (void) removeAllObjects { - FastMapCleanMap(&map); + FastMapCleanMap(&map); } -- (void) removeObjectForKey: (NSObject *)aKey +- (void) removeObjectForKey: (id)aKey { - if (aKey) { - FastMapRemoveKey(&map, (FastMapItem)aKey); + if (aKey) + { + FastMapRemoveKey(&map, (FastMapItem)aKey); } } @@ -299,21 +381,22 @@ myEqual(NSObject *self, NSObject *other) - (id) initWithDictionary: (NSDictionary*)d { - [super init]; - dictionary = (NSGDictionary*)[d retain]; - node = dictionary->map.firstNode; - return self; + [super init]; + dictionary = (NSGDictionary*)[d retain]; + node = dictionary->map.firstNode; + return self; } - nextObject { - FastMapNode old = node; + FastMapNode old = node; - if (node == 0) { - return nil; + if (node == 0) + { + return nil; } - node = node->nextInMap; - return old->key.o; + node = node->nextInMap; + return old->key.o; } - (void) dealloc @@ -328,13 +411,14 @@ myEqual(NSObject *self, NSObject *other) - nextObject { - FastMapNode old = node; + FastMapNode old = node; - if (node == 0) { - return nil; + if (node == 0) + { + return nil; } - node = node->nextInMap; - return old->value.o; + node = node->nextInMap; + return old->value.o; } @end diff --git a/Source/NSGSet.m b/Source/NSGSet.m index 31cbe9124..3268ca6e1 100644 --- a/Source/NSGSet.m +++ b/Source/NSGSet.m @@ -41,21 +41,21 @@ @interface NSGSet : NSSet { @public - FastMapTable_t map; + FastMapTable_t map; } @end @interface NSGMutableSet : NSMutableSet { @public - FastMapTable_t map; + FastMapTable_t map; } @end @interface NSGSetEnumerator : NSEnumerator { - NSGSet *set; - FastMapNode node; + NSGSet *set; + FastMapNode node; } @end @@ -63,27 +63,28 @@ - initWithSet: (NSSet*)d { - [super init]; - set = [(NSGSet*)d retain]; - node = set->map.firstNode; - return self; + [super init]; + set = [(NSGSet*)d retain]; + node = set->map.firstNode; + return self; } - nextObject { - FastMapNode old = node; + FastMapNode old = node; - if (node == 0) { - return nil; + if (node == 0) + { + return nil; } - node = node->nextInMap; - return old->key.o; + node = node->nextInMap; + return old->key.o; } - (void) dealloc { - [set release]; - [super dealloc]; + [set release]; + [super dealloc]; } @end @@ -93,91 +94,105 @@ + (void) initialize { - if (self == [NSGSet class]) { - class_add_behavior(self, [NSSetNonCore class]); + if (self == [NSGSet class]) + { + class_add_behavior(self, [NSSetNonCore class]); } } - (unsigned) count { - return map.nodeCount; + return map.nodeCount; } - (void) dealloc { - FastMapEmptyMap(&map); - [super dealloc]; + FastMapEmptyMap(&map); + [super dealloc]; } - (void) encodeWithCoder: (NSCoder*)aCoder { - unsigned count = map.nodeCount; - FastMapNode node = map.firstNode; - SEL sel = @selector(encodeObject:); - IMP imp = [aCoder methodForSelector: sel]; + unsigned count = map.nodeCount; + FastMapNode node = map.firstNode; + SEL sel = @selector(encodeObject:); + IMP imp = [aCoder methodForSelector: sel]; - [aCoder encodeValueOfObjCType: @encode(unsigned) at: &count]; - while (node != 0) { - (*imp)(aCoder, sel, node->key.o); - node = node->nextInMap; + [aCoder encodeValueOfObjCType: @encode(unsigned) at: &count]; + while (node != 0) + { + (*imp)(aCoder, sel, node->key.o); + node = node->nextInMap; } } +- (unsigned) hash +{ + return map.nodeCount; +} + - (id) initWithCoder: (NSCoder*)aCoder { - unsigned count; - id value; - SEL sel = @selector(decodeValueOfObjCType:at:); - IMP imp = [aCoder methodForSelector: sel]; - const char *type = @encode(id); + unsigned count; + id value; + SEL sel = @selector(decodeValueOfObjCType:at:); + IMP imp = [aCoder methodForSelector: sel]; + const char *type = @encode(id); - (*imp)(aCoder, sel, @encode(unsigned), &count); + (*imp)(aCoder, sel, @encode(unsigned), &count); - FastMapInitWithZoneAndCapacity(&map, [self zone], count); - while (count-- > 0) { - (*imp)(aCoder, sel, type, &value); - FastMapAddKeyNoRetain(&map, (FastMapItem)value); + FastMapInitWithZoneAndCapacity(&map, [self zone], count); + while (count-- > 0) + { + (*imp)(aCoder, sel, type, &value); + FastMapAddKeyNoRetain(&map, (FastMapItem)value); } - return self; + return self; } /* Designated initialiser */ - (id) initWithObjects: (id*)objs count: (unsigned)c { - int i; - FastMapInitWithZoneAndCapacity(&map, [self zone], c); - for (i = 0; i < c; i++) { - FastMapNode node; + int i; - if (objs[i] == nil) { - [self autorelease]; - [NSException raise: NSInvalidArgumentException - format: @"Tried to init set with nil value"]; + FastMapInitWithZoneAndCapacity(&map, [self zone], c); + for (i = 0; i < c; i++) + { + FastMapNode node; + + if (objs[i] == nil) + { + [self autorelease]; + [NSException raise: NSInvalidArgumentException + format: @"Tried to init set with nil value"]; } - node = FastMapNodeForKey(&map, (FastMapItem)objs[i]); - if (node == 0) { - FastMapAddKey(&map, (FastMapItem)objs[i]); + node = FastMapNodeForKey(&map, (FastMapItem)objs[i]); + if (node == 0) + { + FastMapAddKey(&map, (FastMapItem)objs[i]); } } - return self; + return self; } - (id) member: (id)anObject { - if (anObject) { - FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject); + if (anObject) + { + FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject); - if (node) { - return node->key.o; + if (node) + { + return node->key.o; } } - return nil; + return nil; } - (NSEnumerator*) objectEnumerator { - return [[[NSGSetEnumerator alloc] initWithSet: self] autorelease]; + return [[[NSGSetEnumerator alloc] initWithSet: self] autorelease]; } @end @@ -186,43 +201,47 @@ + (void) initialize { - if (self == [NSGMutableSet class]) { - class_add_behavior(self, [NSMutableSetNonCore class]); - class_add_behavior(self, [NSGSet class]); + if (self == [NSGMutableSet class]) + { + class_add_behavior(self, [NSMutableSetNonCore class]); + class_add_behavior(self, [NSGSet class]); } } /* Designated initialiser */ - (id) initWithCapacity: (unsigned)cap { - FastMapInitWithZoneAndCapacity(&map, [self zone], cap); - return self; + FastMapInitWithZoneAndCapacity(&map, [self zone], cap); + return self; } - (void) addObject: (NSObject*)anObject { - FastMapNode node; + FastMapNode node; - if (anObject == nil) { - [NSException raise: NSInvalidArgumentException - format: @"Tried to add nil to set"]; + if (anObject == nil) + { + [NSException raise: NSInvalidArgumentException + format: @"Tried to add nil to set"]; } - node = FastMapNodeForKey(&map, (FastMapItem)anObject); - if (node == 0) { - FastMapAddKey(&map, (FastMapItem)anObject); + node = FastMapNodeForKey(&map, (FastMapItem)anObject); + if (node == 0) + { + FastMapAddKey(&map, (FastMapItem)anObject); } } - (void) removeObject: (NSObject *)anObject { - if (anObject) { - FastMapRemoveKey(&map, (FastMapItem)anObject); + if (anObject) + { + FastMapRemoveKey(&map, (FastMapItem)anObject); } } - (void) removeAllObjects { - FastMapCleanMap(&map); + FastMapCleanMap(&map); } @end diff --git a/Source/NSSet.m b/Source/NSSet.m index 52d1cd835..663cf048f 100644 --- a/Source/NSSet.m +++ b/Source/NSSet.m @@ -328,6 +328,11 @@ static Class NSMutableSet_concrete_class; return (([self member:anObject]) ? YES : NO); } +- (unsigned) hash +{ + return [self count]; +} + - (void) makeObjectsPerform: (SEL)aSelector { id o, e = [self objectEnumerator];