2001-12-17 14:31:42 +00:00
|
|
|
|
/** Interface to concrete implementation of NSDictionary
|
1998-10-01 16:06:24 +00:00
|
|
|
|
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
|
1999-09-09 02:56:20 +00:00
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
1995-05-05 18:27:56 +00:00
|
|
|
|
*/
|
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "Foundation/NSDictionary.h"
|
|
|
|
|
#include "Foundation/NSAutoreleasePool.h"
|
|
|
|
|
#include "Foundation/NSUtilities.h"
|
|
|
|
|
#include "Foundation/NSString.h"
|
|
|
|
|
#include "Foundation/NSException.h"
|
|
|
|
|
#include "Foundation/NSPortCoder.h"
|
|
|
|
|
#include "Foundation/NSDebug.h"
|
2004-01-27 21:51:33 +00:00
|
|
|
|
// For private method _decodeArrayOfObjectsForKey:
|
|
|
|
|
#include "Foundation/NSKeyedArchiver.h"
|
1995-05-05 18:27:56 +00:00
|
|
|
|
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/GSObjCRuntime.h"
|
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.
|
|
|
|
|
*/
|
1999-06-21 08:30:26 +00:00
|
|
|
|
#define GSI_MAP_KTYPES GSUNION_OBJ
|
|
|
|
|
#define GSI_MAP_VTYPES GSUNION_OBJ
|
2002-01-24 17:03:04 +00:00
|
|
|
|
#define GSI_MAP_HASH(M, X) [X.obj hash]
|
|
|
|
|
#define GSI_MAP_EQUAL(M, X,Y) [X.obj isEqual: Y.obj]
|
2004-02-04 00:14:00 +00:00
|
|
|
|
#define GSI_MAP_RETAIN_KEY(M, X) ((X).obj) = \
|
2002-01-24 17:03:04 +00:00
|
|
|
|
[((id)(X).obj) copyWithZone: map->zone]
|
1998-10-06 15:11:27 +00:00
|
|
|
|
|
2003-07-31 23:49:32 +00:00
|
|
|
|
#include "GNUstepBase/GSIMap.h"
|
1998-10-01 16:06:24 +00:00
|
|
|
|
|
2001-01-09 09:17:31 +00:00
|
|
|
|
@interface GSDictionary : NSDictionary
|
1995-05-05 18:27:56 +00:00
|
|
|
|
{
|
1998-10-01 16:06:24 +00:00
|
|
|
|
@public
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GSIMapTable_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
|
|
|
|
|
2001-01-09 09:17:31 +00:00
|
|
|
|
@interface GSMutableDictionary : NSMutableDictionary
|
1995-05-05 18:27:56 +00:00
|
|
|
|
{
|
1998-10-01 16:06:24 +00:00
|
|
|
|
@public
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GSIMapTable_t map;
|
1995-05-05 18:27:56 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2001-01-09 09:17:31 +00:00
|
|
|
|
@interface GSDictionaryKeyEnumerator : NSEnumerator
|
1998-10-08 13:46:53 +00:00
|
|
|
|
{
|
2001-01-09 09:17:31 +00:00
|
|
|
|
GSDictionary *dictionary;
|
2000-12-21 10:31:33 +00:00
|
|
|
|
GSIMapEnumerator_t enumerator;
|
1998-10-08 13:46:53 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2001-01-09 09:17:31 +00:00
|
|
|
|
@interface GSDictionaryObjectEnumerator : GSDictionaryKeyEnumerator
|
1998-10-08 13:46:53 +00:00
|
|
|
|
@end
|
|
|
|
|
|
2001-01-09 09:17:31 +00:00
|
|
|
|
@implementation GSDictionary
|
1995-05-05 18:27:56 +00:00
|
|
|
|
|
2000-10-30 18:00:27 +00:00
|
|
|
|
static SEL nxtSel;
|
|
|
|
|
static SEL objSel;
|
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
2001-01-09 09:17:31 +00:00
|
|
|
|
if (self == [GSDictionary class])
|
1998-10-27 08:12:49 +00:00
|
|
|
|
{
|
2000-10-30 18:00:27 +00:00
|
|
|
|
nxtSel = @selector(nextObject);
|
|
|
|
|
objSel = @selector(objectForKey:);
|
1998-10-01 16:06:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1995-05-05 18:27:56 +00:00
|
|
|
|
|
2003-01-20 17:18:06 +00:00
|
|
|
|
- (id) copyWithZone: (NSZone*)zone
|
|
|
|
|
{
|
|
|
|
|
return RETAIN(self);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GSIMapEmptyMap(&map);
|
1998-10-27 08:12:49 +00:00
|
|
|
|
[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
|
|
|
|
|
{
|
2004-01-28 07:33:20 +00:00
|
|
|
|
if ([aCoder allowsKeyedCoding])
|
1998-10-27 08:12:49 +00:00
|
|
|
|
{
|
2004-04-06 10:39:26 +00:00
|
|
|
|
[super encodeWithCoder: aCoder];
|
2004-01-28 07:33:20 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned count = map.nodeCount;
|
|
|
|
|
SEL sel = @selector(encodeObject:);
|
|
|
|
|
IMP imp = [aCoder methodForSelector: sel];
|
|
|
|
|
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
|
|
|
|
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &count];
|
|
|
|
|
while (node != 0)
|
|
|
|
|
{
|
|
|
|
|
(*imp)(aCoder, sel, node->key.obj);
|
|
|
|
|
(*imp)(aCoder, sel, node->value.obj);
|
|
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
|
|
|
|
}
|
|
|
|
|
GSIMapEndEnumerator(&enumerator);
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-07 09:22:10 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
return [self initWithObjects: 0 forKeys: 0 count: 0];
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
2004-01-27 21:51:33 +00:00
|
|
|
|
if ([aCoder allowsKeyedCoding])
|
|
|
|
|
{
|
2004-01-28 07:33:20 +00:00
|
|
|
|
self = [super initWithCoder: aCoder];
|
2004-01-27 21:51:33 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
1998-10-27 08:12:49 +00:00
|
|
|
|
{
|
2004-01-27 21:51:33 +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];
|
|
|
|
|
|
|
|
|
|
GSIMapInitWithZoneAndCapacity(&map, GSObjCZone(self), count);
|
|
|
|
|
while (count-- > 0)
|
|
|
|
|
{
|
|
|
|
|
(*imp)(aCoder, sel, type, &key);
|
|
|
|
|
(*imp)(aCoder, sel, type, &value);
|
|
|
|
|
GSIMapAddPairNoRetain(&map, (GSIMapKey)key, (GSIMapVal)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
|
|
|
|
|
{
|
2003-01-03 20:14:47 +00:00
|
|
|
|
unsigned int i;
|
1998-10-27 08:12:49 +00:00
|
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
|
GSIMapInitWithZoneAndCapacity(&map, GSObjCZone(self), c);
|
1998-10-27 08:12:49 +00:00
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GSIMapNode node;
|
1998-10-27 08:12:49 +00:00
|
|
|
|
|
|
|
|
|
if (keys[i] == nil)
|
|
|
|
|
{
|
1999-09-28 19:35:09 +00:00
|
|
|
|
IF_NO_GC(AUTORELEASE(self));
|
1998-10-27 08:12:49 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Tried to init dictionary with nil key"];
|
|
|
|
|
}
|
|
|
|
|
if (objs[i] == nil)
|
|
|
|
|
{
|
1999-09-28 19:35:09 +00:00
|
|
|
|
IF_NO_GC(AUTORELEASE(self));
|
1998-10-27 08:12:49 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Tried to init dictionary with nil value"];
|
|
|
|
|
}
|
|
|
|
|
|
1999-06-21 08:30:26 +00:00
|
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)keys[i]);
|
1998-10-27 08:12:49 +00:00
|
|
|
|
if (node)
|
|
|
|
|
{
|
1999-09-28 19:35:09 +00:00
|
|
|
|
IF_NO_GC(RETAIN(objs[i]));
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(node->value.obj);
|
1999-04-12 12:53:30 +00:00
|
|
|
|
node->value.obj = objs[i];
|
1998-10-27 08:12:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GSIMapAddPair(&map, (GSIMapKey)keys[i], (GSIMapVal)objs[i]);
|
1998-10-27 08:12:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2000-10-31 16:17:33 +00:00
|
|
|
|
NSZone *z = GSObjCZone(self);
|
1998-10-27 08:12:49 +00:00
|
|
|
|
unsigned c = [other count];
|
|
|
|
|
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GSIMapInitWithZoneAndCapacity(&map, z, c);
|
1998-10-27 08:12:49 +00:00
|
|
|
|
|
1999-09-12 19:03:09 +00:00
|
|
|
|
if (c > 0)
|
|
|
|
|
{
|
|
|
|
|
NSEnumerator *e = [other keyEnumerator];
|
|
|
|
|
IMP nxtObj = [e methodForSelector: nxtSel];
|
|
|
|
|
IMP otherObj = [other methodForSelector: objSel];
|
2004-03-15 18:01:53 +00:00
|
|
|
|
BOOL isProxy = [other isProxy];
|
1999-09-12 19:03:09 +00:00
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < c; i++)
|
1998-10-27 08:12:49 +00:00
|
|
|
|
{
|
1999-09-12 19:03:09 +00:00
|
|
|
|
GSIMapNode node;
|
2004-03-15 18:01:53 +00:00
|
|
|
|
id k;
|
|
|
|
|
id o;
|
1998-10-17 05:38:46 +00:00
|
|
|
|
|
2004-03-15 18:01:53 +00:00
|
|
|
|
if (isProxy == YES)
|
|
|
|
|
{
|
|
|
|
|
k = [e nextObject];
|
|
|
|
|
o = [other objectForKey: k];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
k = (*nxtObj)(e, nxtSel);
|
|
|
|
|
o = (*otherObj)(other, objSel, k);
|
|
|
|
|
}
|
1999-09-12 19:03:09 +00:00
|
|
|
|
k = [k copyWithZone: z];
|
|
|
|
|
if (k == nil)
|
|
|
|
|
{
|
1999-09-28 19:35:09 +00:00
|
|
|
|
IF_NO_GC(AUTORELEASE(self));
|
1999-09-12 19:03:09 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Tried to init dictionary with nil key"];
|
|
|
|
|
}
|
|
|
|
|
if (shouldCopy)
|
|
|
|
|
{
|
|
|
|
|
o = [o copyWithZone: z];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
o = RETAIN(o);
|
|
|
|
|
}
|
|
|
|
|
if (o == nil)
|
|
|
|
|
{
|
1999-09-28 19:35:09 +00:00
|
|
|
|
IF_NO_GC(AUTORELEASE(self));
|
1999-09-12 19:03:09 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Tried to init dictionary with nil value"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)k);
|
|
|
|
|
if (node)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(node->value.obj);
|
|
|
|
|
node->value.obj = o;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GSIMapAddPairNoRetain(&map, (GSIMapKey)k, (GSIMapVal)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
|
|
|
|
}
|
|
|
|
|
|
2003-02-16 11:31:28 +00:00
|
|
|
|
- (BOOL) isEqualToDictionary: (NSDictionary*)other
|
|
|
|
|
{
|
|
|
|
|
unsigned count;
|
|
|
|
|
|
|
|
|
|
if (other == self)
|
|
|
|
|
{
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
count = map.nodeCount;
|
|
|
|
|
if (count == [other count])
|
|
|
|
|
{
|
|
|
|
|
if (count > 0)
|
|
|
|
|
{
|
|
|
|
|
GSIMapEnumerator_t enumerator;
|
|
|
|
|
GSIMapNode node;
|
|
|
|
|
IMP otherObj = [other methodForSelector: objSel];
|
|
|
|
|
|
|
|
|
|
enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
|
while ((node = GSIMapEnumeratorNextNode(&enumerator)) != 0)
|
|
|
|
|
{
|
|
|
|
|
id o1 = node->value.obj;
|
|
|
|
|
id o2 = (*otherObj)(other, objSel, node->key.obj);
|
|
|
|
|
|
|
|
|
|
if (o1 != o2 && [o1 isEqual: o2] == NO)
|
|
|
|
|
{
|
|
|
|
|
GSIMapEndEnumerator(&enumerator);
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
GSIMapEndEnumerator(&enumerator);
|
|
|
|
|
}
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
|
- (NSEnumerator*) keyEnumerator
|
1996-04-16 02:52:13 +00:00
|
|
|
|
{
|
2001-01-09 09:17:31 +00:00
|
|
|
|
return AUTORELEASE([[GSDictionaryKeyEnumerator allocWithZone:
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSDefaultMallocZone()] initWithDictionary: self]);
|
1995-05-05 18:27:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-01 16:06:24 +00:00
|
|
|
|
- (NSEnumerator*) objectEnumerator
|
|
|
|
|
{
|
2001-01-09 09:17:31 +00:00
|
|
|
|
return AUTORELEASE([[GSDictionaryObjectEnumerator allocWithZone:
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSDefaultMallocZone()] initWithDictionary: self]);
|
1998-10-01 16:06:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) objectForKey: aKey
|
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
|
if (aKey != nil)
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GSIMapNode node = GSIMapNodeForKey(&map, (GSIMapKey)aKey);
|
1998-10-01 16:06:24 +00:00
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
|
if (node)
|
|
|
|
|
{
|
1999-04-12 12:53:30 +00:00
|
|
|
|
return node->value.obj;
|
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
|
|
|
|
|
|
2001-01-09 09:17:31 +00:00
|
|
|
|
@implementation GSMutableDictionary
|
1995-05-05 18:27:56 +00:00
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
2001-01-09 09:17:31 +00:00
|
|
|
|
if (self == [GSMutableDictionary class])
|
1998-10-27 08:12:49 +00:00
|
|
|
|
{
|
2002-11-27 12:52:29 +00:00
|
|
|
|
GSObjCAddClassBehavior(self, [GSDictionary class]);
|
1997-10-16 23:56:27 +00:00
|
|
|
|
}
|
1995-05-05 18:27:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-20 17:18:06 +00:00
|
|
|
|
- (id) copyWithZone: (NSZone*)zone
|
|
|
|
|
{
|
|
|
|
|
NSDictionary *copy = [GSDictionary allocWithZone: zone];
|
|
|
|
|
|
|
|
|
|
return [copy initWithDictionary: self copyItems: NO];
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-07 09:22:10 +00:00
|
|
|
|
- (id) init
|
|
|
|
|
{
|
|
|
|
|
return [self initWithCapacity: 0];
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
|
/* Designated initialiser */
|
1998-10-01 16:06:24 +00:00
|
|
|
|
- (id) initWithCapacity: (unsigned)cap
|
|
|
|
|
{
|
2000-10-31 16:17:33 +00:00
|
|
|
|
GSIMapInitWithZoneAndCapacity(&map, GSObjCZone(self), cap);
|
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
|
|
|
|
|
2002-10-14 08:52:39 +00:00
|
|
|
|
- (id) makeImmutableCopyOnFail: (BOOL)force
|
|
|
|
|
{
|
2002-11-10 13:56:34 +00:00
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
GSDebugAllocationRemove(isa, self);
|
|
|
|
|
#endif
|
2002-10-14 08:52:39 +00:00
|
|
|
|
isa = [GSDictionary class];
|
2002-11-10 13:56:34 +00:00
|
|
|
|
#ifndef NDEBUG
|
|
|
|
|
GSDebugAllocationAdd(isa, self);
|
|
|
|
|
#endif
|
2002-10-14 08:52:39 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
|
- (void) setObject: (id)anObject forKey: (id)aKey
|
1995-05-05 18:27:56 +00:00
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GSIMapNode node;
|
1998-10-01 16:06:24 +00:00
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
|
if (aKey == nil)
|
|
|
|
|
{
|
2004-06-05 06:17:10 +00:00
|
|
|
|
NSException *e;
|
|
|
|
|
|
|
|
|
|
e = [NSException exceptionWithName: NSInvalidArgumentException
|
|
|
|
|
reason: @"Tried to add nil key to dictionary"
|
|
|
|
|
userInfo: self];
|
|
|
|
|
[e raise];
|
1998-10-17 05:38:46 +00:00
|
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
|
if (anObject == nil)
|
|
|
|
|
{
|
2004-06-05 06:17:10 +00:00
|
|
|
|
NSException *e;
|
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
|
|
s = [NSString stringWithFormat:
|
|
|
|
|
@"Tried to add nil value for key '%@' to dictionary", aKey];
|
|
|
|
|
e = [NSException exceptionWithName: NSInvalidArgumentException
|
|
|
|
|
reason: s
|
|
|
|
|
userInfo: self];
|
|
|
|
|
[e raise];
|
1998-10-17 05:38:46 +00:00
|
|
|
|
}
|
1999-06-21 08:30:26 +00:00
|
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)aKey);
|
1998-10-27 08:12:49 +00:00
|
|
|
|
if (node)
|
|
|
|
|
{
|
1999-09-28 19:35:09 +00:00
|
|
|
|
IF_NO_GC(RETAIN(anObject));
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(node->value.obj);
|
1999-04-12 12:53:30 +00:00
|
|
|
|
node->value.obj = anObject;
|
1998-10-01 16:06:24 +00:00
|
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GSIMapAddPair(&map, (GSIMapKey)aKey, (GSIMapVal)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
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GSIMapCleanMap(&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
|
|
|
|
{
|
1999-11-04 10:42:20 +00:00
|
|
|
|
if (aKey == nil)
|
1998-10-27 08:12:49 +00:00
|
|
|
|
{
|
2004-06-05 06:17:10 +00:00
|
|
|
|
NSWarnMLog(@"attempt to remove nil key from dictionary %@", self);
|
1999-11-04 10:42:20 +00:00
|
|
|
|
return;
|
1998-10-17 05:38:46 +00:00
|
|
|
|
}
|
1999-11-04 10:42:20 +00:00
|
|
|
|
GSIMapRemoveKey(&map, (GSIMapKey)aKey);
|
1998-10-01 16:06:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2001-01-09 09:17:31 +00:00
|
|
|
|
@implementation GSDictionaryKeyEnumerator
|
1998-10-01 16:06:24 +00:00
|
|
|
|
|
|
|
|
|
- (id) initWithDictionary: (NSDictionary*)d
|
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
|
[super init];
|
2001-01-09 09:17:31 +00:00
|
|
|
|
dictionary = (GSDictionary*)RETAIN(d);
|
2000-12-21 10:31:33 +00:00
|
|
|
|
enumerator = GSIMapEnumeratorForMap(&dictionary->map);
|
1998-10-27 08:12:49 +00:00
|
|
|
|
return self;
|
1998-10-01 16:06:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
|
- (id) nextObject
|
1998-10-01 16:06:24 +00:00
|
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
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
|
|
|
|
}
|
2000-12-21 10:31:33 +00:00
|
|
|
|
return node->key.obj;
|
1998-10-01 16:06:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2002-05-28 11:30:15 +00:00
|
|
|
|
GSIMapEndEnumerator(&enumerator);
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(dictionary);
|
1998-10-01 16:06:24 +00:00
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2001-01-09 09:17:31 +00:00
|
|
|
|
@implementation GSDictionaryObjectEnumerator
|
1998-10-01 16:06:24 +00:00
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
|
- (id) nextObject
|
1998-10-01 16:06:24 +00:00
|
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
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
|
|
|
|
}
|
2000-12-21 10:31:33 +00:00
|
|
|
|
return node->value.obj;
|
1995-05-05 18:27:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
2001-01-09 09:17:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@interface NSGDictionary : NSDictionary
|
|
|
|
|
@end
|
|
|
|
|
@implementation NSGDictionary
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Warning - decoding archive containing obsolete %@ object - please delete/replace this archive", NSStringFromClass([self class]));
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
self = (id)NSAllocateObject([GSDictionary class], 0, NSDefaultMallocZone());
|
|
|
|
|
self = [self initWithCoder: aCoder];
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface NSGMutableDictionary : NSMutableDictionary
|
|
|
|
|
@end
|
|
|
|
|
@implementation NSGMutableDictionary
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Warning - decoding archive containing obsolete %@ object - please delete/replace this archive", NSStringFromClass([self class]));
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
self = (id)NSAllocateObject([GSMutableDictionary class], 0, NSDefaultMallocZone());
|
|
|
|
|
self = [self initWithCoder: aCoder];
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|