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,18 +283,43 @@ 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;
while ((k = [e nextObject])) if (shouldCopy)
{ {
ks[i] = k; NSZone *z = [self zone];
os[i] = [other objectForKey:k];
i++; 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 - 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

@ -42,14 +42,14 @@
@interface NSGCountedSet : NSCountedSet @interface NSGCountedSet : NSCountedSet
{ {
@public @public
FastMapTable_t map; FastMapTable_t map;
} }
@end @end
@interface NSGCountedSetEnumerator : NSEnumerator @interface NSGCountedSetEnumerator : NSEnumerator
{ {
NSGCountedSet *set; NSGCountedSet *set;
FastMapNode node; FastMapNode node;
} }
@end @end
@ -57,29 +57,31 @@
- initWithSet: (NSSet*)d - initWithSet: (NSSet*)d
{ {
self = [super init]; self = [super init];
if (self) { if (self)
set = [(NSGCountedSet*)d retain]; {
node = set->map.firstNode; set = [(NSGCountedSet*)d retain];
node = set->map.firstNode;
} }
return self; return self;
} }
- nextObject - nextObject
{ {
FastMapNode old = node; FastMapNode old = node;
if (node == 0) { if (node == 0)
return nil; {
return nil;
} }
node = node->nextInMap; node = node->nextInMap;
return old->key.o; return old->key.o;
} }
- (void) dealloc - (void) dealloc
{ {
[set release]; [set release];
[super dealloc]; [super dealloc];
} }
@end @end
@ -89,158 +91,182 @@
+ (void) initialize + (void) initialize
{ {
if (self == [NSGCountedSet class]) { if (self == [NSGCountedSet class])
class_add_behavior(self, [NSSetNonCore class]); {
class_add_behavior(self, [NSMutableSetNonCore class]); class_add_behavior(self, [NSSetNonCore class]);
class_add_behavior(self, [NSMutableSetNonCore class]);
} }
} }
- (void) dealloc - (void) dealloc
{ {
FastMapEmptyMap(&map); FastMapEmptyMap(&map);
[super dealloc]; [super dealloc];
} }
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
unsigned count = map.nodeCount; unsigned count = map.nodeCount;
FastMapNode node = map.firstNode; FastMapNode node = map.firstNode;
SEL sel1 = @selector(encodeObject:); SEL sel1 = @selector(encodeObject:);
IMP imp1 = [aCoder methodForSelector: sel1]; IMP imp1 = [aCoder methodForSelector: sel1];
SEL sel2 = @selector(encodeValueOfObjCType:at:); SEL sel2 = @selector(encodeValueOfObjCType:at:);
IMP imp2 = [aCoder methodForSelector: sel2]; IMP imp2 = [aCoder methodForSelector: sel2];
const char *type = @encode(unsigned); const char *type = @encode(unsigned);
(*imp2)(aCoder, sel2, type, &count); (*imp2)(aCoder, sel2, type, &count);
while (node != 0) { while (node != 0)
(*imp1)(aCoder, sel1, node->key.o); {
(*imp2)(aCoder, sel2, type, &node->value.I); (*imp1)(aCoder, sel1, node->key.o);
node = node->nextInMap; (*imp2)(aCoder, sel2, type, &node->value.I);
node = node->nextInMap;
} }
} }
- (id) initWithCoder: (NSCoder*)aCoder - (id) initWithCoder: (NSCoder*)aCoder
{ {
unsigned count; unsigned count;
id value; id value;
unsigned valcnt; unsigned valcnt;
SEL sel = @selector(decodeValueOfObjCType:at:); SEL sel = @selector(decodeValueOfObjCType:at:);
IMP imp = [aCoder methodForSelector: sel]; IMP imp = [aCoder methodForSelector: sel];
const char *utype = @encode(unsigned); const char *utype = @encode(unsigned);
const char *otype = @encode(id); const char *otype = @encode(id);
(*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, utype, &valcnt); (*imp)(aCoder, sel, otype, &value);
FastMapAddPairNoRetain(&map, (FastMapItem)value, (FastMapItem)valcnt); (*imp)(aCoder, sel, utype, &valcnt);
FastMapAddPairNoRetain(&map, (FastMapItem)value, (FastMapItem)valcnt);
} }
return self; return self;
} }
/* Designated initialiser */ /* Designated initialiser */
- (id) initWithCapacity: (unsigned)cap - (id) initWithCapacity: (unsigned)cap
{ {
FastMapInitWithZoneAndCapacity(&map, [self zone], cap); FastMapInitWithZoneAndCapacity(&map, [self zone], cap);
return self; return self;
} }
- (id) initWithObjects: (id*)objs count: (unsigned)c - (id) initWithObjects: (id*)objs count: (unsigned)c
{ {
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]; {
[NSException raise: NSInvalidArgumentException [self autorelease];
format: @"Tried to init counted set with nil value"]; [NSException raise: NSInvalidArgumentException
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++;
} }
} }
return self; return self;
} }
- (void) addObject: (NSObject*)anObject - (void) addObject: (NSObject*)anObject
{ {
FastMapNode node; FastMapNode node;
if (anObject == nil) { if (anObject == nil)
[NSException raise: NSInvalidArgumentException {
format: @"Tried to nil value to counted set"]; [NSException raise: NSInvalidArgumentException
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++;
} }
} }
- (unsigned) count - (unsigned) count
{ {
return map.nodeCount; return map.nodeCount;
} }
- (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;
} }
} }
return nil; return nil;
} }
- (NSEnumerator*) objectEnumerator - (NSEnumerator*) objectEnumerator
{ {
return [[[NSGCountedSetEnumerator alloc] initWithSet: self] autorelease]; return [[[NSGCountedSetEnumerator alloc] initWithSet: self] autorelease];
} }
- (void) removeObject: (NSObject*)anObject - (void) removeObject: (NSObject*)anObject
{ {
if (anObject) { if (anObject)
FastMapBucket bucket; {
FastMapBucket bucket;
bucket = FastMapBucketForKey(&map, (FastMapItem)anObject);
if (bucket) {
FastMapNode node;
node = FastMapNodeForKeyInBucket(bucket, (FastMapItem)anObject); bucket = FastMapBucketForKey(&map, (FastMapItem)anObject);
if (node) { if (bucket)
if (--node->value.I == 0) { {
FastMapRemoveNodeFromMap(&map, bucket, node); FastMapNode node;
FastMapFreeNode(&map, 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 - (void) removeAllObjects
{ {
FastMapCleanMap(&map); FastMapCleanMap(&map);
} }
@end @end

View file

@ -37,57 +37,63 @@
* instances of the string classes we know about! * instances of the string classes we know about!
*/ */
typedef struct { typedef struct {
Class *isa; Class *isa;
char *_contents_chars; char *_contents_chars;
int _count; int _count;
NSZone *_zone; NSZone *_zone;
unsigned _hash; unsigned _hash;
} *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));
} }
return ((dictAccessToStringHack)obj)->_hash; return ((dictAccessToStringHack)obj)->_hash;
} }
} }
return [obj hash]; return [obj hash];
} }
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);
} }
} }
return [self isEqual: other]; return [self isEqual: other];
} }
/* /*
@ -106,21 +112,21 @@ myEqual(NSObject *self, NSObject *other)
@interface NSGDictionary : NSDictionary @interface NSGDictionary : NSDictionary
{ {
@public @public
FastMapTable_t map; FastMapTable_t map;
} }
@end @end
@interface NSGMutableDictionary : NSMutableDictionary @interface NSGMutableDictionary : NSMutableDictionary
{ {
@public @public
FastMapTable_t map; FastMapTable_t map;
} }
@end @end
@interface NSGDictionaryKeyEnumerator : NSEnumerator @interface NSGDictionaryKeyEnumerator : NSEnumerator
{ {
NSGDictionary *dictionary; NSGDictionary *dictionary;
FastMapNode node; FastMapNode node;
} }
@end @end
@ -131,112 +137,182 @@ 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]);
} }
} }
- (unsigned) count - (unsigned) count
{ {
return map.nodeCount; return map.nodeCount;
} }
- (void) dealloc - (void) dealloc
{ {
FastMapEmptyMap(&map); FastMapEmptyMap(&map);
[super dealloc]; [super dealloc];
} }
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
unsigned count = map.nodeCount; unsigned count = map.nodeCount;
FastMapNode node = map.firstNode; FastMapNode node = map.firstNode;
SEL sel = @selector(encodeObject:); SEL sel = @selector(encodeObject:);
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->value.o); (*imp)(aCoder, sel, node->key.o);
node = node->nextInMap; (*imp)(aCoder, sel, node->value.o);
node = node->nextInMap;
} }
} }
- (unsigned) hash
{
return map.nodeCount;
}
- (id) initWithCoder: (NSCoder*)aCoder - (id) initWithCoder: (NSCoder*)aCoder
{ {
unsigned count; unsigned count;
id key; id key;
id value; id value;
SEL sel = @selector(decodeValueOfObjCType:at:); SEL sel = @selector(decodeValueOfObjCType:at:);
IMP imp = [aCoder methodForSelector: sel]; IMP imp = [aCoder methodForSelector: sel];
const char *type = @encode(id); const char *type = @encode(id);
[aCoder decodeValueOfObjCType: @encode(unsigned) [aCoder decodeValueOfObjCType: @encode(unsigned)
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, &value); (*imp)(aCoder, sel, type, &key);
FastMapAddPairNoRetain(&map, (FastMapItem)key, (FastMapItem)value); (*imp)(aCoder, sel, type, &value);
FastMapAddPairNoRetain(&map, (FastMapItem)key, (FastMapItem)value);
} }
return self; return self;
} }
/* 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);
for (i = 0; i < c; i++) {
FastMapNode node;
if (keys[i] == nil) { FastMapInitWithZoneAndCapacity(&map, fastZone(self), c);
[self autorelease]; for (i = 0; i < c; i++)
[NSException raise: NSInvalidArgumentException {
format: @"Tried to init dictionary with nil key"]; FastMapNode node;
if (keys[i] == nil)
{
[self autorelease];
[NSException raise: NSInvalidArgumentException
format: @"Tried to init dictionary with nil key"];
} }
if (objs[i] == nil) { if (objs[i] == nil)
[self autorelease]; {
[NSException raise: NSInvalidArgumentException [self autorelease];
format: @"Tried to init dictionary with nil value"]; [NSException raise: NSInvalidArgumentException
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]; {
[node->value.o release]; [objs[i] retain];
node->value.o = objs[i]; [node->value.o release];
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]
autorelease]; autorelease];
} }
- (NSEnumerator*) objectEnumerator - (NSEnumerator*) objectEnumerator
{ {
return [[[NSGDictionaryObjectEnumerator alloc] initWithDictionary: self] return [[[NSGDictionaryObjectEnumerator alloc] initWithDictionary: self]
autorelease]; autorelease];
} }
- (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;
} }
} }
return nil; return nil;
} }
@end @end
@ -245,51 +321,57 @@ 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, [NSGDictionary class]); behavior_class_add_class(self, [NSMutableDictionaryNonCore class]);
behavior_class_add_class(self, [NSGDictionary class]);
} }
} }
/* Designated initialiser */ /* Designated initialiser */
- (id) initWithCapacity: (unsigned)cap - (id) initWithCapacity: (unsigned)cap
{ {
FastMapInitWithZoneAndCapacity(&map, fastZone(self), cap); FastMapInitWithZoneAndCapacity(&map, fastZone(self), cap);
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 {
format: @"Tried to add nil key to dictionary"]; [NSException raise: NSInvalidArgumentException
format: @"Tried to add nil key to dictionary"];
} }
if (anObject == nil) { if (anObject == nil)
[NSException raise: NSInvalidArgumentException {
format: @"Tried to add nil value to dictionary"]; [NSException raise: NSInvalidArgumentException
format: @"Tried to add nil value to dictionary"];
} }
node = FastMapNodeForKey(&map, (FastMapItem)aKey); node = FastMapNodeForKey(&map, (FastMapItem)aKey);
if (node) { if (node)
[anObject retain]; {
[node->value.o release]; [anObject retain];
node->value.o = anObject; [node->value.o release];
node->value.o = anObject;
} }
else { else
FastMapAddPair(&map, (FastMapItem)aKey, (FastMapItem)anObject); {
FastMapAddPair(&map, (FastMapItem)aKey, (FastMapItem)anObject);
} }
} }
- (void) removeAllObjects - (void) removeAllObjects
{ {
FastMapCleanMap(&map); FastMapCleanMap(&map);
} }
- (void) removeObjectForKey: (NSObject *)aKey - (void) removeObjectForKey: (id)aKey
{ {
if (aKey) { if (aKey)
FastMapRemoveKey(&map, (FastMapItem)aKey); {
FastMapRemoveKey(&map, (FastMapItem)aKey);
} }
} }
@ -299,21 +381,22 @@ myEqual(NSObject *self, NSObject *other)
- (id) initWithDictionary: (NSDictionary*)d - (id) initWithDictionary: (NSDictionary*)d
{ {
[super init]; [super init];
dictionary = (NSGDictionary*)[d retain]; dictionary = (NSGDictionary*)[d retain];
node = dictionary->map.firstNode; node = dictionary->map.firstNode;
return self; return self;
} }
- nextObject - nextObject
{ {
FastMapNode old = node; FastMapNode old = node;
if (node == 0) { if (node == 0)
return nil; {
return nil;
} }
node = node->nextInMap; node = node->nextInMap;
return old->key.o; return old->key.o;
} }
- (void) dealloc - (void) dealloc
@ -328,13 +411,14 @@ myEqual(NSObject *self, NSObject *other)
- nextObject - nextObject
{ {
FastMapNode old = node; FastMapNode old = node;
if (node == 0) { if (node == 0)
return nil; {
return nil;
} }
node = node->nextInMap; node = node->nextInMap;
return old->value.o; return old->value.o;
} }
@end @end

View file

@ -41,21 +41,21 @@
@interface NSGSet : NSSet @interface NSGSet : NSSet
{ {
@public @public
FastMapTable_t map; FastMapTable_t map;
} }
@end @end
@interface NSGMutableSet : NSMutableSet @interface NSGMutableSet : NSMutableSet
{ {
@public @public
FastMapTable_t map; FastMapTable_t map;
} }
@end @end
@interface NSGSetEnumerator : NSEnumerator @interface NSGSetEnumerator : NSEnumerator
{ {
NSGSet *set; NSGSet *set;
FastMapNode node; FastMapNode node;
} }
@end @end
@ -63,27 +63,28 @@
- initWithSet: (NSSet*)d - initWithSet: (NSSet*)d
{ {
[super init]; [super init];
set = [(NSGSet*)d retain]; set = [(NSGSet*)d retain];
node = set->map.firstNode; node = set->map.firstNode;
return self; return self;
} }
- nextObject - nextObject
{ {
FastMapNode old = node; FastMapNode old = node;
if (node == 0) { if (node == 0)
return nil; {
return nil;
} }
node = node->nextInMap; node = node->nextInMap;
return old->key.o; return old->key.o;
} }
- (void) dealloc - (void) dealloc
{ {
[set release]; [set release];
[super dealloc]; [super dealloc];
} }
@end @end
@ -93,91 +94,105 @@
+ (void) initialize + (void) initialize
{ {
if (self == [NSGSet class]) { if (self == [NSGSet class])
class_add_behavior(self, [NSSetNonCore class]); {
class_add_behavior(self, [NSSetNonCore class]);
} }
} }
- (unsigned) count - (unsigned) count
{ {
return map.nodeCount; return map.nodeCount;
} }
- (void) dealloc - (void) dealloc
{ {
FastMapEmptyMap(&map); FastMapEmptyMap(&map);
[super dealloc]; [super dealloc];
} }
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
unsigned count = map.nodeCount; unsigned count = map.nodeCount;
FastMapNode node = map.firstNode; FastMapNode node = map.firstNode;
SEL sel = @selector(encodeObject:); SEL sel = @selector(encodeObject:);
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); {
node = node->nextInMap; (*imp)(aCoder, sel, node->key.o);
node = node->nextInMap;
} }
} }
- (unsigned) hash
{
return map.nodeCount;
}
- (id) initWithCoder: (NSCoder*)aCoder - (id) initWithCoder: (NSCoder*)aCoder
{ {
unsigned count; unsigned count;
id value; id value;
SEL sel = @selector(decodeValueOfObjCType:at:); SEL sel = @selector(decodeValueOfObjCType:at:);
IMP imp = [aCoder methodForSelector: sel]; IMP imp = [aCoder methodForSelector: sel];
const char *type = @encode(id); const char *type = @encode(id);
(*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); {
FastMapAddKeyNoRetain(&map, (FastMapItem)value); (*imp)(aCoder, sel, type, &value);
FastMapAddKeyNoRetain(&map, (FastMapItem)value);
} }
return self; return self;
} }
/* Designated initialiser */ /* Designated initialiser */
- (id) initWithObjects: (id*)objs count: (unsigned)c - (id) initWithObjects: (id*)objs count: (unsigned)c
{ {
int i; int i;
FastMapInitWithZoneAndCapacity(&map, [self zone], c);
for (i = 0; i < c; i++) {
FastMapNode node;
if (objs[i] == nil) { FastMapInitWithZoneAndCapacity(&map, [self zone], c);
[self autorelease]; for (i = 0; i < c; i++)
[NSException raise: NSInvalidArgumentException {
format: @"Tried to init set with nil value"]; 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]); node = FastMapNodeForKey(&map, (FastMapItem)objs[i]);
if (node == 0) { if (node == 0)
FastMapAddKey(&map, (FastMapItem)objs[i]); {
FastMapAddKey(&map, (FastMapItem)objs[i]);
} }
} }
return self; return self;
} }
- (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;
} }
} }
return nil; return nil;
} }
- (NSEnumerator*) objectEnumerator - (NSEnumerator*) objectEnumerator
{ {
return [[[NSGSetEnumerator alloc] initWithSet: self] autorelease]; return [[[NSGSetEnumerator alloc] initWithSet: self] autorelease];
} }
@end @end
@ -186,43 +201,47 @@
+ (void) initialize + (void) initialize
{ {
if (self == [NSGMutableSet class]) { if (self == [NSGMutableSet class])
class_add_behavior(self, [NSMutableSetNonCore class]); {
class_add_behavior(self, [NSGSet class]); class_add_behavior(self, [NSMutableSetNonCore class]);
class_add_behavior(self, [NSGSet class]);
} }
} }
/* Designated initialiser */ /* Designated initialiser */
- (id) initWithCapacity: (unsigned)cap - (id) initWithCapacity: (unsigned)cap
{ {
FastMapInitWithZoneAndCapacity(&map, [self zone], cap); FastMapInitWithZoneAndCapacity(&map, [self zone], cap);
return self; return self;
} }
- (void) addObject: (NSObject*)anObject - (void) addObject: (NSObject*)anObject
{ {
FastMapNode node; FastMapNode node;
if (anObject == nil) { if (anObject == nil)
[NSException raise: NSInvalidArgumentException {
format: @"Tried to add nil to set"]; [NSException raise: NSInvalidArgumentException
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);
} }
} }
- (void) removeAllObjects - (void) removeAllObjects
{ {
FastMapCleanMap(&map); FastMapCleanMap(&map);
} }
@end @end

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];