1998-10-01 16:06:24 +00:00
|
|
|
/* Interface to concrete implementation of NSDictionary
|
|
|
|
Copyright (C) 1998 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
Date: September 1998
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1998-10-01 16:06:24 +00:00
|
|
|
|
1995-05-05 18:27:56 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
1998-10-01 16:06:24 +00:00
|
|
|
|
1995-05-05 18:27:56 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
1998-10-01 16:06:24 +00:00
|
|
|
|
1995-05-05 18:27:56 +00:00
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1998-10-01 16:06:24 +00:00
|
|
|
#include <Foundation/NSDictionary.h>
|
1998-10-17 06:47:46 +00:00
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
1995-05-05 18:27:56 +00:00
|
|
|
#include <Foundation/NSUtilities.h>
|
|
|
|
#include <Foundation/NSString.h>
|
1998-10-17 05:38:46 +00:00
|
|
|
#include <Foundation/NSException.h>
|
1998-10-08 03:03:25 +00:00
|
|
|
#include <Foundation/NSPortCoder.h>
|
1995-05-05 18:27:56 +00:00
|
|
|
|
1998-10-06 15:11:27 +00:00
|
|
|
#include <gnustep/base/fast.x>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Evil hack - this structure MUST correspond to the layout of all
|
|
|
|
* instances of the string classes we know about!
|
|
|
|
*/
|
|
|
|
typedef struct {
|
1998-10-27 08:12:49 +00:00
|
|
|
Class *isa;
|
|
|
|
char *_contents_chars;
|
|
|
|
int _count;
|
|
|
|
NSZone *_zone;
|
|
|
|
unsigned _hash;
|
1998-10-06 15:11:27 +00:00
|
|
|
} *dictAccessToStringHack;
|
|
|
|
|
|
|
|
static INLINE unsigned
|
1998-10-27 08:12:49 +00:00
|
|
|
myHash(id obj)
|
1998-10-06 15:11:27 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
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 =
|
1998-10-09 04:24:56 +00:00
|
|
|
_fastImp._NSString_hash(obj, @selector(hash));
|
1998-10-06 15:11:27 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
return ((dictAccessToStringHack)obj)->_hash;
|
1998-10-06 15:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
return [obj hash];
|
1998-10-06 15:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static INLINE BOOL
|
1998-10-27 08:12:49 +00:00
|
|
|
myEqual(id self, id other)
|
1998-10-06 15:11:27 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
if (self == other)
|
|
|
|
{
|
|
|
|
return YES;
|
1998-10-06 15:11:27 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
if (fastIsInstance(self))
|
|
|
|
{
|
|
|
|
Class c = fastClass(self);
|
|
|
|
|
|
|
|
if (c == _fastCls._NXConstantString ||
|
|
|
|
c == _fastCls._NSGCString ||
|
|
|
|
c == _fastCls._NSGMutableCString)
|
|
|
|
{
|
|
|
|
return _fastImp._NSGCString_isEqual_(self,
|
1998-10-06 15:11:27 +00:00
|
|
|
@selector(isEqual:), other);
|
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
if (c == _fastCls._NSGString ||
|
|
|
|
c == _fastCls._NSGMutableString)
|
|
|
|
{
|
|
|
|
return _fastImp._NSGString_isEqual_(self,
|
1998-10-06 15:11:27 +00:00
|
|
|
@selector(isEqual:), other);
|
|
|
|
}
|
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
return [self isEqual: other];
|
1998-10-06 15:11:27 +00:00
|
|
|
}
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
/*
|
|
|
|
* The 'Fastmap' stuff provides an inline implementation of a mapping
|
|
|
|
* table - for maximum performance.
|
|
|
|
*/
|
1998-10-06 15:11:27 +00:00
|
|
|
#define FAST_MAP_HASH(X) myHash(X.o)
|
|
|
|
#define FAST_MAP_EQUAL(X,Y) myEqual(X.o,Y.o)
|
1998-10-26 20:49:57 +00:00
|
|
|
#define FAST_MAP_RETAIN_KEY(X) [((id)(X).o) copyWithZone: map->zone]
|
1998-10-06 15:11:27 +00:00
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
#include "FastMap.x"
|
|
|
|
|
|
|
|
@class NSDictionaryNonCore;
|
|
|
|
@class NSMutableDictionaryNonCore;
|
1995-05-05 18:27:56 +00:00
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
@interface NSGDictionary : NSDictionary
|
1995-05-05 18:27:56 +00:00
|
|
|
{
|
1998-10-01 16:06:24 +00:00
|
|
|
@public
|
1998-10-27 08:12:49 +00:00
|
|
|
FastMapTable_t map;
|
1995-05-05 18:27:56 +00:00
|
|
|
}
|
1998-10-01 16:06:24 +00:00
|
|
|
@end
|
1995-05-05 18:27:56 +00:00
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
@interface NSGMutableDictionary : NSMutableDictionary
|
1995-05-05 18:27:56 +00:00
|
|
|
{
|
1998-10-01 16:06:24 +00:00
|
|
|
@public
|
1998-10-27 08:12:49 +00:00
|
|
|
FastMapTable_t map;
|
1995-05-05 18:27:56 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
@interface NSGDictionaryKeyEnumerator : NSEnumerator
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
NSGDictionary *dictionary;
|
|
|
|
FastMapNode node;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSGDictionaryObjectEnumerator : NSGDictionaryKeyEnumerator
|
|
|
|
@end
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
@implementation NSGDictionary
|
1995-05-05 18:27:56 +00:00
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
if (self == [NSGDictionary class])
|
|
|
|
{
|
|
|
|
behavior_class_add_class(self, [NSDictionaryNonCore class]);
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
1995-05-05 18:27:56 +00:00
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
- (unsigned) count
|
1995-05-05 18:27:56 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
return map.nodeCount;
|
1995-05-05 18:27:56 +00:00
|
|
|
}
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
FastMapEmptyMap(&map);
|
|
|
|
[super dealloc];
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1995-05-05 18:27:56 +00:00
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
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;
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
|
|
|
}
|
1995-05-05 18:27:56 +00:00
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
- (unsigned) hash
|
|
|
|
{
|
|
|
|
return map.nodeCount;
|
|
|
|
}
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
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];
|
|
|
|
|
|
|
|
FastMapInitWithZoneAndCapacity(&map, fastZone(self), count);
|
|
|
|
while (count-- > 0)
|
|
|
|
{
|
|
|
|
(*imp)(aCoder, sel, type, &key);
|
|
|
|
(*imp)(aCoder, sel, type, &value);
|
|
|
|
FastMapAddPairNoRetain(&map, (FastMapItem)key, (FastMapItem)value);
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
return self;
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1995-05-05 18:27:56 +00:00
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
/* Designated initialiser */
|
1998-10-27 08:12:49 +00:00
|
|
|
- (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;
|
|
|
|
|
|
|
|
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"];
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This avoids using the designated initialiser for performance reasons.
|
|
|
|
*/
|
|
|
|
- (id) initWithDictionary: (NSDictionary*)other
|
|
|
|
copyItems: (BOOL)shouldCopy
|
1995-05-05 18:27:56 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
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];
|
1998-10-17 05:38:46 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
if (o == nil)
|
|
|
|
{
|
|
|
|
[self autorelease];
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to init dictionary with nil value"];
|
1998-10-17 05:38:46 +00:00
|
|
|
}
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
node = FastMapNodeForKey(&map, (FastMapItem)k);
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
[node->value.o release];
|
|
|
|
node->value.o = o;
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
FastMapAddPairNoRetain(&map, (FastMapItem)k, (FastMapItem)o);
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1997-10-16 23:56:27 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
return self;
|
1996-04-16 02:52:13 +00:00
|
|
|
}
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
- (NSEnumerator*) keyEnumerator
|
1996-04-16 02:52:13 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
return [[[NSGDictionaryKeyEnumerator alloc] initWithDictionary: self]
|
1998-10-01 16:06:24 +00:00
|
|
|
autorelease];
|
1995-05-05 18:27:56 +00:00
|
|
|
}
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
- (NSEnumerator*) objectEnumerator
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
return [[[NSGDictionaryObjectEnumerator alloc] initWithDictionary: self]
|
1998-10-01 16:06:24 +00:00
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectForKey: aKey
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
if (aKey != nil)
|
|
|
|
{
|
|
|
|
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)aKey);
|
1998-10-01 16:06:24 +00:00
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
return node->value.o;
|
1998-10-17 06:47:46 +00:00
|
|
|
}
|
1998-10-17 05:38:46 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
return nil;
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1995-05-05 18:27:56 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSGMutableDictionary
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
if (self == [NSGMutableDictionary class])
|
|
|
|
{
|
|
|
|
behavior_class_add_class(self, [NSMutableDictionaryNonCore class]);
|
|
|
|
behavior_class_add_class(self, [NSGDictionary class]);
|
1997-10-16 23:56:27 +00:00
|
|
|
}
|
1995-05-05 18:27:56 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
/* Designated initialiser */
|
1998-10-01 16:06:24 +00:00
|
|
|
- (id) initWithCapacity: (unsigned)cap
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
FastMapInitWithZoneAndCapacity(&map, fastZone(self), cap);
|
|
|
|
return self;
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1995-05-05 18:27:56 +00:00
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
- (void) setObject: (id)anObject forKey: (id)aKey
|
1995-05-05 18:27:56 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
FastMapNode node;
|
1998-10-01 16:06:24 +00:00
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
if (aKey == nil)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to add nil key to dictionary"];
|
1998-10-17 05:38:46 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
if (anObject == nil)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to add nil value to dictionary"];
|
1998-10-17 05:38:46 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
node = FastMapNodeForKey(&map, (FastMapItem)aKey);
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
[anObject retain];
|
|
|
|
[node->value.o release];
|
|
|
|
node->value.o = anObject;
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
FastMapAddPair(&map, (FastMapItem)aKey, (FastMapItem)anObject);
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1995-05-05 18:27:56 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) removeAllObjects
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
FastMapCleanMap(&map);
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
- (void) removeObjectForKey: (id)aKey
|
1995-05-05 18:27:56 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
if (aKey)
|
|
|
|
{
|
|
|
|
FastMapRemoveKey(&map, (FastMapItem)aKey);
|
1998-10-17 05:38:46 +00:00
|
|
|
}
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSGDictionaryKeyEnumerator
|
|
|
|
|
|
|
|
- (id) initWithDictionary: (NSDictionary*)d
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
[super init];
|
|
|
|
dictionary = (NSGDictionary*)[d retain];
|
|
|
|
node = dictionary->map.firstNode;
|
|
|
|
return self;
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- nextObject
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
FastMapNode old = node;
|
1998-10-01 16:06:24 +00:00
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
if (node == 0)
|
|
|
|
{
|
|
|
|
return nil;
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
node = node->nextInMap;
|
|
|
|
return old->key.o;
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[dictionary release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSGDictionaryObjectEnumerator
|
|
|
|
|
|
|
|
- nextObject
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
FastMapNode old = node;
|
1998-10-01 16:06:24 +00:00
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
if (node == 0)
|
|
|
|
{
|
|
|
|
return nil;
|
1998-10-01 16:06:24 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
node = node->nextInMap;
|
|
|
|
return old->value.o;
|
1995-05-05 18:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|