Corrected [-hash] implementations.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3123 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1998-10-27 08:12:49 +00:00
parent af846fdfd3
commit c1befc7b44
8 changed files with 502 additions and 324 deletions

View file

@ -30,10 +30,10 @@
@interface NSDictionary : NSObject <NSCoding, NSCopying, NSMutableCopying> @interface NSDictionary : NSObject <NSCoding, NSCopying, NSMutableCopying>
- initWithObjects: (id*)objects - initWithObjects: (id*)objects
forKeys: (NSObject**)keys forKeys: (id*)keys
count: (unsigned)count; count: (unsigned)count;
- (unsigned) count; - (unsigned) count;
- objectForKey: (NSObject*)aKey; - objectForKey: (id)aKey;
- (NSEnumerator*) keyEnumerator; - (NSEnumerator*) keyEnumerator;
- (NSEnumerator*) objectEnumerator; - (NSEnumerator*) objectEnumerator;
@end @end
@ -51,6 +51,8 @@
+ dictionaryWithObjectsAndKeys: (id)object, ...; + dictionaryWithObjectsAndKeys: (id)object, ...;
- initWithContentsOfFile: (NSString*)path; - initWithContentsOfFile: (NSString*)path;
- initWithDictionary: (NSDictionary*)otherDictionary; - initWithDictionary: (NSDictionary*)otherDictionary;
- initWithDictionary: (NSDictionary*)otherDictionary
copyItems: (BOOL)shouldCopy;
- initWithObjects: (NSArray*)objects forKeys: (NSArray*)keys; - initWithObjects: (NSArray*)objects forKeys: (NSArray*)keys;
- initWithObjectsAndKeys: (id)object, ...; - initWithObjectsAndKeys: (id)object, ...;
@ -74,8 +76,8 @@
@interface NSMutableDictionary: NSDictionary @interface NSMutableDictionary: NSDictionary
- initWithCapacity: (unsigned)numItems; - initWithCapacity: (unsigned)numItems;
- (void) setObject:anObject forKey:(NSObject *)aKey; - (void) setObject:anObject forKey:(id)aKey;
- (void) removeObjectForKey:(NSObject *)aKey; - (void) removeObjectForKey:(id)aKey;
@end @end
@interface NSMutableDictionary (NonCore) @interface NSMutableDictionary (NonCore)

View file

@ -368,6 +368,11 @@ static Class NSMutableArray_concrete_class;
aBuffer[j++] = [self objectAtIndex: i]; aBuffer[j++] = [self objectAtIndex: i];
} }
- (unsigned) hash
{
return [self count];
}
- (unsigned) indexOfObjectIdenticalTo:anObject - (unsigned) indexOfObjectIdenticalTo:anObject
{ {
unsigned i, c = [self count]; unsigned i, c = [self count];

View file

@ -30,7 +30,6 @@
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <Foundation/NSException.h> #include <Foundation/NSException.h>
#include <Foundation/NSAutoreleasePool.h> #include <Foundation/NSAutoreleasePool.h>
#include <assert.h>
@interface NSDictionaryNonCore : NSDictionary @interface NSDictionaryNonCore : NSDictionary
@end @end
@ -82,7 +81,7 @@ static Class NSMutableDictionary_concrete_class;
/* This is the designated initializer */ /* This is the designated initializer */
- initWithObjects: (id*)objects - initWithObjects: (id*)objects
forKeys: (NSObject**)keys forKeys: (id*)keys
count: (unsigned)count count: (unsigned)count
{ {
[self subclassResponsibility:_cmd]; [self subclassResponsibility:_cmd];
@ -95,7 +94,7 @@ static Class NSMutableDictionary_concrete_class;
return 0; return 0;
} }
- objectForKey: (NSObject*)aKey - objectForKey: (id)aKey
{ {
[self subclassResponsibility:_cmd]; [self subclassResponsibility:_cmd];
return 0; return 0;
@ -186,7 +185,7 @@ static Class NSMutableDictionary_concrete_class;
} }
+ dictionaryWithObjects: (id*)objects + dictionaryWithObjects: (id*)objects
forKeys: (NSObject**)keys forKeys: (id*)keys
count: (unsigned)count count: (unsigned)count
{ {
return [[[self alloc] initWithObjects:objects return [[[self alloc] initWithObjects:objects
@ -195,19 +194,25 @@ static Class NSMutableDictionary_concrete_class;
autorelease]; autorelease];
} }
- (unsigned) hash
{
return [self count];
}
- initWithObjects: (NSArray*)objects forKeys: (NSArray*)keys - initWithObjects: (NSArray*)objects forKeys: (NSArray*)keys
{ {
int c = [objects count]; int objectCount = [objects count];
id os[c], ks[c]; id os[objectCount], ks[objectCount];
int i; int i;
assert(c == [keys count]); /* Should be NSException instead */ if (objectCount != [keys count])
for (i = 0; i < c; i++)
{ {
os[i] = [objects objectAtIndex:i]; [NSException raise: NSInvalidArgumentException
ks[i] = [keys objectAtIndex:i]; 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, ... + dictionaryWithObjectsAndKeys: (id)firstObject, ...
@ -278,11 +283,35 @@ static Class NSMutableDictionary_concrete_class;
} }
- initWithDictionary: (NSDictionary*)other - initWithDictionary: (NSDictionary*)other
{
return [self initWithDictionary: other copyItems: NO];
}
- initWithDictionary: (NSDictionary*)other copyItems: (BOOL)shouldCopy
{ {
int c = [other count]; int c = [other count];
id os[c], ks[c], k, e = [other keyEnumerator]; id os[c], ks[c], k, e = [other keyEnumerator];
int i = 0; int i = 0;
if (shouldCopy)
{
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])) while ((k = [e nextObject]))
{ {
ks[i] = k; ks[i] = k;
@ -291,6 +320,7 @@ static Class NSMutableDictionary_concrete_class;
} }
return [self initWithObjects:os forKeys:ks count:c]; return [self initWithObjects:os forKeys:ks count:c];
} }
}
- initWithContentsOfFile: (NSString*)path - initWithContentsOfFile: (NSString*)path
{ {
@ -354,9 +384,9 @@ static Class NSMutableDictionary_concrete_class;
for (i = 0; i < c; i++) for (i = 0; i < c; i++)
{ {
k[i] = [e nextObject]; 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] return [[[NSArray alloc] initWithObjects:k count:c]
autorelease]; autorelease];
} }
@ -370,9 +400,9 @@ static Class NSMutableDictionary_concrete_class;
for (i = 0; i < c; i++) for (i = 0; i < c; i++)
{ {
k[i] = [e nextObject]; 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] return [[[NSArray alloc] initWithObjects:k count:c]
autorelease]; autorelease];
} }
@ -386,6 +416,8 @@ static Class NSMutableDictionary_concrete_class;
while ((k = [e nextObject])) while ((k = [e nextObject]))
if ([anObject isEqual: [self objectForKey: k]]) if ([anObject isEqual: [self objectForKey: k]])
a[c++] = k; a[c++] = k;
if (c == 0)
return nil;
return [[[NSArray alloc] initWithObjects: a count: c] return [[[NSArray alloc] initWithObjects: a count: c]
autorelease]; autorelease];
} }
@ -676,12 +708,12 @@ compareIt(id o1, id o2, void* context)
return 0; return 0;
} }
- (void) setObject:anObject forKey:(NSObject *)aKey - (void) setObject:anObject forKey:(id)aKey
{ {
[self subclassResponsibility:_cmd]; [self subclassResponsibility:_cmd];
} }
- (void) removeObjectForKey:(NSObject *)aKey - (void) removeObjectForKey:(id)aKey
{ {
[self subclassResponsibility:_cmd]; [self subclassResponsibility:_cmd];
} }
@ -698,7 +730,7 @@ compareIt(id o1, id o2, void* context)
/* Override superclass's designated initializer */ /* Override superclass's designated initializer */
- initWithObjects: (id*)objects - initWithObjects: (id*)objects
forKeys: (NSObject**)keys forKeys: (id*)keys
count: (unsigned)count count: (unsigned)count
{ {
[self initWithCapacity:count]; [self initWithCapacity:count];

View file

@ -140,6 +140,11 @@
return _count; return _count;
} }
- (unsigned) hash
{
return _count;
}
- (unsigned) indexOfObject: anObject - (unsigned) indexOfObject: anObject
{ {
unsigned hash = [anObject hash]; unsigned hash = [anObject hash];

View file

@ -58,7 +58,8 @@
- initWithSet: (NSSet*)d - initWithSet: (NSSet*)d
{ {
self = [super init]; self = [super init];
if (self) { if (self)
{
set = [(NSGCountedSet*)d retain]; set = [(NSGCountedSet*)d retain];
node = set->map.firstNode; node = set->map.firstNode;
} }
@ -69,7 +70,8 @@
{ {
FastMapNode old = node; FastMapNode old = node;
if (node == 0) { if (node == 0)
{
return nil; return nil;
} }
node = node->nextInMap; node = node->nextInMap;
@ -89,7 +91,8 @@
+ (void) initialize + (void) initialize
{ {
if (self == [NSGCountedSet class]) { if (self == [NSGCountedSet class])
{
class_add_behavior(self, [NSSetNonCore class]); class_add_behavior(self, [NSSetNonCore class]);
class_add_behavior(self, [NSMutableSetNonCore class]); class_add_behavior(self, [NSMutableSetNonCore class]);
} }
@ -113,7 +116,8 @@
(*imp2)(aCoder, sel2, type, &count); (*imp2)(aCoder, sel2, type, &count);
while (node != 0) { while (node != 0)
{
(*imp1)(aCoder, sel1, node->key.o); (*imp1)(aCoder, sel1, node->key.o);
(*imp2)(aCoder, sel2, type, &node->value.I); (*imp2)(aCoder, sel2, type, &node->value.I);
node = node->nextInMap; node = node->nextInMap;
@ -133,7 +137,8 @@
(*imp)(aCoder, sel, utype, &count); (*imp)(aCoder, sel, utype, &count);
FastMapInitWithZoneAndCapacity(&map, [self zone], count); FastMapInitWithZoneAndCapacity(&map, [self zone], count);
while (count-- > 0) { while (count-- > 0)
{
(*imp)(aCoder, sel, otype, &value); (*imp)(aCoder, sel, otype, &value);
(*imp)(aCoder, sel, utype, &valcnt); (*imp)(aCoder, sel, utype, &valcnt);
FastMapAddPairNoRetain(&map, (FastMapItem)value, (FastMapItem)valcnt); FastMapAddPairNoRetain(&map, (FastMapItem)value, (FastMapItem)valcnt);
@ -153,22 +158,27 @@
{ {
int i; int i;
if ([self initWithCapacity: c] == nil) { if ([self initWithCapacity: c] == nil)
{
return nil; return nil;
} }
for (i = 0; i < c; i++) { for (i = 0; i < c; i++)
{
FastMapNode node; FastMapNode node;
if (objs[i] == nil) { if (objs[i] == nil)
{
[self autorelease]; [self autorelease];
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to init counted set with nil value"]; format: @"Tried to init counted set with nil value"];
} }
node = FastMapNodeForKey(&map, (FastMapItem)objs[i]); node = FastMapNodeForKey(&map, (FastMapItem)objs[i]);
if (node == 0) { if (node == 0)
{
FastMapAddPair(&map,(FastMapItem)objs[i],(FastMapItem)(unsigned)1); FastMapAddPair(&map,(FastMapItem)objs[i],(FastMapItem)(unsigned)1);
} }
else { else
{
node->value.I++; node->value.I++;
} }
} }
@ -179,16 +189,19 @@
{ {
FastMapNode node; FastMapNode node;
if (anObject == nil) { if (anObject == nil)
{
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to nil value to counted set"]; format: @"Tried to nil value to counted set"];
} }
node = FastMapNodeForKey(&map, (FastMapItem)anObject); node = FastMapNodeForKey(&map, (FastMapItem)anObject);
if (node == 0) { if (node == 0)
{
FastMapAddPair(&map,(FastMapItem)anObject,(FastMapItem)(unsigned)1); FastMapAddPair(&map,(FastMapItem)anObject,(FastMapItem)(unsigned)1);
} }
else { else
{
node->value.I++; node->value.I++;
} }
} }
@ -200,22 +213,31 @@
- (unsigned) countForObject: (id)anObject - (unsigned) countForObject: (id)anObject
{ {
if (anObject) { if (anObject)
{
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject); FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject);
if (node) { if (node)
{
return node->value.I; return node->value.I;
} }
} }
return 0; return 0;
} }
- (unsigned) hash
{
return map.nodeCount;
}
- (id) member: (id)anObject - (id) member: (id)anObject
{ {
if (anObject) { if (anObject)
{
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject); FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject);
if (node) { if (node)
{
return node->key.o; return node->key.o;
} }
} }
@ -229,16 +251,20 @@
- (void) removeObject: (NSObject*)anObject - (void) removeObject: (NSObject*)anObject
{ {
if (anObject) { if (anObject)
{
FastMapBucket bucket; FastMapBucket bucket;
bucket = FastMapBucketForKey(&map, (FastMapItem)anObject); bucket = FastMapBucketForKey(&map, (FastMapItem)anObject);
if (bucket) { if (bucket)
{
FastMapNode node; FastMapNode node;
node = FastMapNodeForKeyInBucket(bucket, (FastMapItem)anObject); node = FastMapNodeForKeyInBucket(bucket, (FastMapItem)anObject);
if (node) { if (node)
if (--node->value.I == 0) { {
if (--node->value.I == 0)
{
FastMapRemoveNodeFromMap(&map, bucket, node); FastMapRemoveNodeFromMap(&map, bucket, node);
FastMapFreeNode(&map, node); FastMapFreeNode(&map, node);
} }

View file

@ -45,18 +45,20 @@ typedef struct {
} *dictAccessToStringHack; } *dictAccessToStringHack;
static INLINE unsigned static INLINE unsigned
myHash(NSObject *obj) myHash(id obj)
{
if (fastIsInstance(obj))
{ {
if (fastIsInstance(obj)) {
Class c = fastClass(obj); Class c = fastClass(obj);
if (c == _fastCls._NXConstantString || if (c == _fastCls._NXConstantString ||
c == _fastCls._NSGCString || c == _fastCls._NSGCString ||
c == _fastCls._NSGMutableCString || c == _fastCls._NSGMutableCString ||
c == _fastCls._NSGString || c == _fastCls._NSGString ||
c == _fastCls._NSGMutableString) { c == _fastCls._NSGMutableString)
{
if (((dictAccessToStringHack)obj)->_hash == 0) { if (((dictAccessToStringHack)obj)->_hash == 0)
{
((dictAccessToStringHack)obj)->_hash = ((dictAccessToStringHack)obj)->_hash =
_fastImp._NSString_hash(obj, @selector(hash)); _fastImp._NSString_hash(obj, @selector(hash));
} }
@ -67,22 +69,26 @@ myHash(NSObject *obj)
} }
static INLINE BOOL static INLINE BOOL
myEqual(NSObject *self, NSObject *other) myEqual(id self, id other)
{
if (self == other)
{ {
if (self == other) {
return YES; return YES;
} }
if (fastIsInstance(self)) { if (fastIsInstance(self))
{
Class c = fastClass(self); Class c = fastClass(self);
if (c == _fastCls._NXConstantString || if (c == _fastCls._NXConstantString ||
c == _fastCls._NSGCString || c == _fastCls._NSGCString ||
c == _fastCls._NSGMutableCString) { c == _fastCls._NSGMutableCString)
{
return _fastImp._NSGCString_isEqual_(self, return _fastImp._NSGCString_isEqual_(self,
@selector(isEqual:), other); @selector(isEqual:), other);
} }
if (c == _fastCls._NSGString || if (c == _fastCls._NSGString ||
c == _fastCls._NSGMutableString) { c == _fastCls._NSGMutableString)
{
return _fastImp._NSGString_isEqual_(self, return _fastImp._NSGString_isEqual_(self,
@selector(isEqual:), other); @selector(isEqual:), other);
} }
@ -131,7 +137,8 @@ myEqual(NSObject *self, NSObject *other)
+ (void) initialize + (void) initialize
{ {
if (self == [NSGDictionary class]) { if (self == [NSGDictionary class])
{
behavior_class_add_class(self, [NSDictionaryNonCore class]); behavior_class_add_class(self, [NSDictionaryNonCore class]);
} }
} }
@ -155,13 +162,19 @@ myEqual(NSObject *self, NSObject *other)
IMP imp = [aCoder methodForSelector: sel]; IMP imp = [aCoder methodForSelector: sel];
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &count]; [aCoder encodeValueOfObjCType: @encode(unsigned) at: &count];
while (node != 0) { while (node != 0)
{
(*imp)(aCoder, sel, node->key.o); (*imp)(aCoder, sel, node->key.o);
(*imp)(aCoder, sel, node->value.o); (*imp)(aCoder, sel, node->value.o);
node = node->nextInMap; node = node->nextInMap;
} }
} }
- (unsigned) hash
{
return map.nodeCount;
}
- (id) initWithCoder: (NSCoder*)aCoder - (id) initWithCoder: (NSCoder*)aCoder
{ {
unsigned count; unsigned count;
@ -175,7 +188,8 @@ myEqual(NSObject *self, NSObject *other)
at: &count]; at: &count];
FastMapInitWithZoneAndCapacity(&map, fastZone(self), count); FastMapInitWithZoneAndCapacity(&map, fastZone(self), count);
while (count-- > 0) { while (count-- > 0)
{
(*imp)(aCoder, sel, type, &key); (*imp)(aCoder, sel, type, &key);
(*imp)(aCoder, sel, type, &value); (*imp)(aCoder, sel, type, &value);
FastMapAddPairNoRetain(&map, (FastMapItem)key, (FastMapItem)value); FastMapAddPairNoRetain(&map, (FastMapItem)key, (FastMapItem)value);
@ -184,37 +198,97 @@ myEqual(NSObject *self, NSObject *other)
} }
/* Designated initialiser */ /* Designated initialiser */
- (id) initWithObjects: (id*)objs forKeys: (NSObject**)keys count: (unsigned)c - (id) initWithObjects: (id*)objs forKeys: (id*)keys count: (unsigned)c
{ {
int i; int i;
FastMapInitWithZoneAndCapacity(&map, fastZone(self), c); FastMapInitWithZoneAndCapacity(&map, fastZone(self), c);
for (i = 0; i < c; i++) { for (i = 0; i < c; i++)
{
FastMapNode node; FastMapNode node;
if (keys[i] == nil) { if (keys[i] == nil)
{
[self autorelease]; [self autorelease];
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to init dictionary with nil key"]; format: @"Tried to init dictionary with nil key"];
} }
if (objs[i] == nil) { if (objs[i] == nil)
{
[self autorelease]; [self autorelease];
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to init dictionary with nil value"]; format: @"Tried to init dictionary with nil value"];
} }
node = FastMapNodeForKey(&map, (FastMapItem)keys[i]); node = FastMapNodeForKey(&map, (FastMapItem)keys[i]);
if (node) { if (node)
{
[objs[i] retain]; [objs[i] retain];
[node->value.o release]; [node->value.o release];
node->value.o = objs[i]; node->value.o = objs[i];
} }
else { else
{
FastMapAddPair(&map, (FastMapItem)keys[i], (FastMapItem)objs[i]); 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 - (NSEnumerator*) keyEnumerator
{ {
return [[[NSGDictionaryKeyEnumerator alloc] initWithDictionary: self] return [[[NSGDictionaryKeyEnumerator alloc] initWithDictionary: self]
@ -229,10 +303,12 @@ myEqual(NSObject *self, NSObject *other)
- (id) objectForKey: aKey - (id) objectForKey: aKey
{ {
if (aKey != nil) { if (aKey != nil)
{
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)aKey); FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)aKey);
if (node) { if (node)
{
return node->value.o; return node->value.o;
} }
} }
@ -245,7 +321,8 @@ myEqual(NSObject *self, NSObject *other)
+ (void) initialize + (void) initialize
{ {
if (self == [NSGMutableDictionary class]) { if (self == [NSGMutableDictionary class])
{
behavior_class_add_class(self, [NSMutableDictionaryNonCore class]); behavior_class_add_class(self, [NSMutableDictionaryNonCore class]);
behavior_class_add_class(self, [NSGDictionary class]); behavior_class_add_class(self, [NSGDictionary class]);
} }
@ -258,25 +335,29 @@ myEqual(NSObject *self, NSObject *other)
return self; return self;
} }
- (void) setObject: (NSObject*)anObject forKey: (NSObject *)aKey - (void) setObject: (id)anObject forKey: (id)aKey
{ {
FastMapNode node; FastMapNode node;
if (aKey == nil) { if (aKey == nil)
{
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to add nil key to dictionary"]; format: @"Tried to add nil key to dictionary"];
} }
if (anObject == nil) { if (anObject == nil)
{
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to add nil value to dictionary"]; format: @"Tried to add nil value to dictionary"];
} }
node = FastMapNodeForKey(&map, (FastMapItem)aKey); node = FastMapNodeForKey(&map, (FastMapItem)aKey);
if (node) { if (node)
{
[anObject retain]; [anObject retain];
[node->value.o release]; [node->value.o release];
node->value.o = anObject; node->value.o = anObject;
} }
else { else
{
FastMapAddPair(&map, (FastMapItem)aKey, (FastMapItem)anObject); FastMapAddPair(&map, (FastMapItem)aKey, (FastMapItem)anObject);
} }
} }
@ -286,9 +367,10 @@ myEqual(NSObject *self, NSObject *other)
FastMapCleanMap(&map); FastMapCleanMap(&map);
} }
- (void) removeObjectForKey: (NSObject *)aKey - (void) removeObjectForKey: (id)aKey
{
if (aKey)
{ {
if (aKey) {
FastMapRemoveKey(&map, (FastMapItem)aKey); FastMapRemoveKey(&map, (FastMapItem)aKey);
} }
} }
@ -309,7 +391,8 @@ myEqual(NSObject *self, NSObject *other)
{ {
FastMapNode old = node; FastMapNode old = node;
if (node == 0) { if (node == 0)
{
return nil; return nil;
} }
node = node->nextInMap; node = node->nextInMap;
@ -330,7 +413,8 @@ myEqual(NSObject *self, NSObject *other)
{ {
FastMapNode old = node; FastMapNode old = node;
if (node == 0) { if (node == 0)
{
return nil; return nil;
} }
node = node->nextInMap; node = node->nextInMap;

View file

@ -73,7 +73,8 @@
{ {
FastMapNode old = node; FastMapNode old = node;
if (node == 0) { if (node == 0)
{
return nil; return nil;
} }
node = node->nextInMap; node = node->nextInMap;
@ -93,7 +94,8 @@
+ (void) initialize + (void) initialize
{ {
if (self == [NSGSet class]) { if (self == [NSGSet class])
{
class_add_behavior(self, [NSSetNonCore class]); class_add_behavior(self, [NSSetNonCore class]);
} }
} }
@ -117,12 +119,18 @@
IMP imp = [aCoder methodForSelector: sel]; IMP imp = [aCoder methodForSelector: sel];
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &count]; [aCoder encodeValueOfObjCType: @encode(unsigned) at: &count];
while (node != 0) { while (node != 0)
{
(*imp)(aCoder, sel, node->key.o); (*imp)(aCoder, sel, node->key.o);
node = node->nextInMap; node = node->nextInMap;
} }
} }
- (unsigned) hash
{
return map.nodeCount;
}
- (id) initWithCoder: (NSCoder*)aCoder - (id) initWithCoder: (NSCoder*)aCoder
{ {
unsigned count; unsigned count;
@ -134,7 +142,8 @@
(*imp)(aCoder, sel, @encode(unsigned), &count); (*imp)(aCoder, sel, @encode(unsigned), &count);
FastMapInitWithZoneAndCapacity(&map, [self zone], count); FastMapInitWithZoneAndCapacity(&map, [self zone], count);
while (count-- > 0) { while (count-- > 0)
{
(*imp)(aCoder, sel, type, &value); (*imp)(aCoder, sel, type, &value);
FastMapAddKeyNoRetain(&map, (FastMapItem)value); FastMapAddKeyNoRetain(&map, (FastMapItem)value);
} }
@ -146,17 +155,21 @@
- (id) initWithObjects: (id*)objs count: (unsigned)c - (id) initWithObjects: (id*)objs count: (unsigned)c
{ {
int i; int i;
FastMapInitWithZoneAndCapacity(&map, [self zone], c); FastMapInitWithZoneAndCapacity(&map, [self zone], c);
for (i = 0; i < c; i++) { for (i = 0; i < c; i++)
{
FastMapNode node; FastMapNode node;
if (objs[i] == nil) { if (objs[i] == nil)
{
[self autorelease]; [self autorelease];
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to init set with nil value"]; format: @"Tried to init set with nil value"];
} }
node = FastMapNodeForKey(&map, (FastMapItem)objs[i]); node = FastMapNodeForKey(&map, (FastMapItem)objs[i]);
if (node == 0) { if (node == 0)
{
FastMapAddKey(&map, (FastMapItem)objs[i]); FastMapAddKey(&map, (FastMapItem)objs[i]);
} }
} }
@ -165,10 +178,12 @@
- (id) member: (id)anObject - (id) member: (id)anObject
{ {
if (anObject) { if (anObject)
{
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject); FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject);
if (node) { if (node)
{
return node->key.o; return node->key.o;
} }
} }
@ -186,7 +201,8 @@
+ (void) initialize + (void) initialize
{ {
if (self == [NSGMutableSet class]) { if (self == [NSGMutableSet class])
{
class_add_behavior(self, [NSMutableSetNonCore class]); class_add_behavior(self, [NSMutableSetNonCore class]);
class_add_behavior(self, [NSGSet class]); class_add_behavior(self, [NSGSet class]);
} }
@ -203,19 +219,22 @@
{ {
FastMapNode node; FastMapNode node;
if (anObject == nil) { if (anObject == nil)
{
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"Tried to add nil to set"]; format: @"Tried to add nil to set"];
} }
node = FastMapNodeForKey(&map, (FastMapItem)anObject); node = FastMapNodeForKey(&map, (FastMapItem)anObject);
if (node == 0) { if (node == 0)
{
FastMapAddKey(&map, (FastMapItem)anObject); FastMapAddKey(&map, (FastMapItem)anObject);
} }
} }
- (void) removeObject: (NSObject *)anObject - (void) removeObject: (NSObject *)anObject
{ {
if (anObject) { if (anObject)
{
FastMapRemoveKey(&map, (FastMapItem)anObject); FastMapRemoveKey(&map, (FastMapItem)anObject);
} }
} }

View file

@ -328,6 +328,11 @@ static Class NSMutableSet_concrete_class;
return (([self member:anObject]) ? YES : NO); return (([self member:anObject]) ? YES : NO);
} }
- (unsigned) hash
{
return [self count];
}
- (void) makeObjectsPerform: (SEL)aSelector - (void) makeObjectsPerform: (SEL)aSelector
{ {
id o, e = [self objectEnumerator]; id o, e = [self objectEnumerator];