New implementation.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3010 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1998-10-01 16:06:24 +00:00
parent e63d3f7c30
commit 11cab3d661

View file

@ -1,84 +1,67 @@
/* Concrete implementation of NSDictionary based on GNU Dictionary class /* Interface to concrete implementation of NSDictionary
Copyright (C) 1995, 1996 Free Software Foundation, Inc. Copyright (C) 1998 Free Software Foundation, Inc.
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu> Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
Created: April 1995 Date: September 1998
This file is part of the GNUstep Base Library. This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details. Library General Public License for more details.
You should have received a copy of the GNU Library General Public You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#include <config.h> #include <config.h>
#include <Foundation/NSGDictionary.h> #include <Foundation/NSDictionary.h>
#include <gnustep/base/NSDictionary.h>
#include <gnustep/base/behavior.h>
#include <gnustep/base/Dictionary.h>
#include <Foundation/NSUtilities.h> #include <Foundation/NSUtilities.h>
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <gnustep/base/Coding.h>
@class NSDictionaryNonCore; /*
@class NSMutableDictionaryNonCore; * The 'Fastmap' stuff provides an inline implementation of a mapping
* table - for maximum performance.
*/
#include "FastMap.x"
@class NSDictionaryNonCore;
@class NSMutableDictionaryNonCore;
@class NSGDictionary;
@class NSGMutableDictionary;
@interface NSGDictionaryKeyEnumerator : NSEnumerator @interface NSGDictionaryKeyEnumerator : NSEnumerator
{ {
NSDictionary *dictionary; NSGDictionary *dictionary;
void *enum_state; FastMapNode node;
} }
@end @end
@implementation NSGDictionaryKeyEnumerator
- initWithDictionary: (NSDictionary*)d
{
[super init];
dictionary = d;
[dictionary retain];
enum_state = 0;
return self;
}
- nextObject
{
id k;
[dictionary nextObjectAndKey: &k withEnumState: &enum_state];
return k;
}
- (void) dealloc
{
[dictionary release];
[super dealloc];
}
@end
@interface NSGDictionaryObjectEnumerator : NSGDictionaryKeyEnumerator @interface NSGDictionaryObjectEnumerator : NSGDictionaryKeyEnumerator
@end @end
@implementation NSGDictionaryObjectEnumerator @interface NSGDictionary : NSDictionary
- nextObject
{ {
id k; @public
[dictionary nextObjectAndKey: &k withEnumState: &enum_state]; FastMapTable_t map;
return k;
} }
@end @end
@interface NSGMutableDictionary : NSMutableDictionary
{
@public
FastMapTable_t map;
}
@end
@implementation NSGDictionary @implementation NSGDictionary
@ -87,26 +70,97 @@
if (self == [NSGDictionary class]) if (self == [NSGDictionary class])
{ {
behavior_class_add_class (self, [NSDictionaryNonCore class]); behavior_class_add_class (self, [NSDictionaryNonCore class]);
behavior_class_add_class (self, [Dictionary class]);
} }
} }
- objectForKey: aKey - (unsigned) count
{ {
/* xxx Should I change the method name in Dictionary? return map.nodeCount;
I don't really want to; I think "at" is better. */
return [self objectAtKey: aKey];
} }
/* - (void) dealloc
Comes from Dictionary.m {
- initWithObjects: (id*)objects FastMapEmptyMap(&map);
forKeys: (NSObject**)keys [super dealloc];
count: (unsigned)count }
- (unsigned) count
- (NSEnumerator*) keyEnumerator - (void) encodeWithCoder: (NSCoder*)aCoder
- (NSEnumerator*) objectEnumerator {
*/ unsigned count = map.nodeCount;
FastMapNode node = map.firstNode;
[(id<Encoding>)aCoder encodeValueOfCType: @encode(unsigned)
at: &count
withName: @"Dictionary content count"];
while (node != 0) {
[(id<Encoding>)aCoder encodeObject: node->key
withName: @"Dictionary key"];
[(id<Encoding>)aCoder encodeObject: node->value
withName: @"Dictionary content"];
node = node->nextInMap;
}
}
- (id) initWithCoder: (NSCoder*)aCoder
{
unsigned count;
id key;
id value;
[(id<Decoding>)aCoder decodeValueOfCType: @encode(unsigned)
at: &count
withName: NULL];
FastMapInitWithZoneAndCapacity(&map, [self zone], count);
while (count-- > 0) {
[(id<Decoding>)aCoder decodeObjectAt: &key withName: NULL];
[(id<Decoding>)aCoder decodeObjectAt: &value withName: NULL];
FastMapAddPairNoRetain(&map, key, value);
}
return self;
}
- (id) initWithObjects: (id*)objs forKeys: (NSObject**)keys count: (unsigned)c
{
int i;
FastMapInitWithZoneAndCapacity(&map, [self zone], c);
for (i = 0; i < c; i++) {
FastMapNode node = FastMapNodeForKey(&map, keys[i]);
if (node) {
[objs[i] retain];
[node->value release];
node->value = objs[i];
}
else {
FastMapAddPair(&map, keys[i], objs[i]);
}
}
return self;
}
- (NSEnumerator*) keyEnumerator
{
return [[[NSGDictionaryKeyEnumerator alloc] initWithDictionary: self]
autorelease];
}
- (NSEnumerator*) objectEnumerator
{
return [[[NSGDictionaryObjectEnumerator alloc] initWithDictionary: self]
autorelease];
}
- (id) objectForKey: aKey
{
FastMapNode node = FastMapNodeForKey(&map, aKey);
if (node)
return node->value;
return nil;
}
@end @end
@ -118,24 +172,76 @@
{ {
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]);
behavior_class_add_class (self, [Dictionary class]);
} }
} }
- (id) initWithCapacity: (unsigned)cap
/* This is the designated initializer */ {
/* Comes from Dictionary.m FastMapInitWithZoneAndCapacity(&map, [self zone], cap);
- initWithCapacity: (unsigned)numItems return self;
*/ }
- (void) setObject:anObject forKey:(NSObject *)aKey - (void) setObject:anObject forKey:(NSObject *)aKey
{ {
[self putObject: anObject atKey: aKey]; FastMapNode node = FastMapNodeForKey(&map, aKey);
if (node) {
[anObject retain];
[node->value release];
node->value = anObject;
}
else {
FastMapAddPair(&map, aKey, anObject);
}
} }
- (void) removeObjectForKey:(NSObject *)aKey - (void) removeObjectForKey:(NSObject *)aKey
{ {
[self removeObjectAtKey: aKey]; FastMapRemoveKey(&map, aKey);
}
@end
@implementation NSGDictionaryKeyEnumerator
- (id) initWithDictionary: (NSDictionary*)d
{
[super init];
dictionary = (NSGDictionary*)[d retain];
node = dictionary->map.firstNode;
return self;
}
- nextObject
{
FastMapNode old = node;
if (node == 0) {
return nil;
}
node = node->nextInMap;
return old->key;
}
- (void) dealloc
{
[dictionary release];
[super dealloc];
}
@end
@implementation NSGDictionaryObjectEnumerator
- nextObject
{
FastMapNode old = node;
if (node == 0) {
return nil;
}
node = node->nextInMap;
return old->value;
} }
@end @end