1994-11-04 16:29:24 +00:00
|
|
|
/* Implementation for Objective-C Dictionary collection object
|
1996-02-22 15:18:57 +00:00
|
|
|
Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
|
1994-11-04 16:29:24 +00:00
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1996-02-22 15:18:57 +00:00
|
|
|
Created: May 1993
|
1994-11-04 16:29:24 +00:00
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1994-11-04 16:29:24 +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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
#include <gnustep/base/Dictionary.h>
|
|
|
|
#include <gnustep/base/CollectionPrivate.h>
|
1997-09-18 14:56:47 +00:00
|
|
|
#include <Foundation/NSCharacterSet.h>
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
#define DEFAULT_DICTIONARY_CAPACITY 32
|
|
|
|
|
|
|
|
@implementation Dictionary
|
|
|
|
|
|
|
|
// MANAGING CAPACITY;
|
|
|
|
|
|
|
|
/* Eventually we will want to have better capacity management,
|
|
|
|
potentially keep default capacity as a class variable. */
|
|
|
|
|
|
|
|
+ (unsigned) defaultCapacity
|
|
|
|
{
|
|
|
|
return DEFAULT_DICTIONARY_CAPACITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// INITIALIZING;
|
|
|
|
|
|
|
|
/* This is the designated initializer of this class */
|
1996-02-22 15:18:57 +00:00
|
|
|
- initWithCapacity: (unsigned)cap
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
_contents_hash = NSCreateMapTable (NSObjectMapKeyCallBacks,
|
|
|
|
NSObjectMapValueCallBacks,
|
|
|
|
cap);
|
1994-11-04 16:29:24 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
/* Override the KeyedCollection designated initializer */
|
|
|
|
- initWithObjects: (id*)objects forKeys: (id*)keys count: (unsigned)c
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
[self initWithCapacity: c];
|
|
|
|
while (c--)
|
|
|
|
[self putObject: objects[c] atKey: keys[c]];
|
1994-11-04 16:29:24 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- init
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
return [self initWithCapacity: DEFAULT_DICTIONARY_CAPACITY];
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
/* Archiving must mimic the above designated initializer */
|
|
|
|
|
|
|
|
- _initCollectionWithCoder: (id <Decoding>)coder
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
_contents_hash = NSCreateMapTable (NSObjectMapKeyCallBacks,
|
|
|
|
NSObjectMapValueCallBacks,
|
|
|
|
0);
|
1994-11-04 16:29:24 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty copy must empty an allocCopy'ed version of self */
|
|
|
|
|
|
|
|
- emptyCopy
|
|
|
|
{
|
|
|
|
Dictionary *copy = [super emptyCopy];
|
1996-02-22 15:18:57 +00:00
|
|
|
copy->_contents_hash = NSCreateMapTable (NSObjectMapKeyCallBacks,
|
|
|
|
NSObjectMapValueCallBacks,
|
|
|
|
0);
|
1994-11-04 16:29:24 +00:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
- (void) _collectionReleaseContents
|
|
|
|
{
|
|
|
|
if (_contents_hash) {
|
|
|
|
NSFreeMapTable (_contents_hash);
|
|
|
|
_contents_hash = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) dealloc
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
[self _collectionReleaseContents];
|
|
|
|
[super dealloc];
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This must work without sending any messages to content objects */
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) _collectionEmpty
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
NSResetMapTable (_contents_hash);
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ADDING OR REPLACING;
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) addObject: newObject
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
[self shouldNotImplement: _cmd];
|
1994-11-04 16:29:24 +00:00
|
|
|
/* or should I make up some default behavior here?
|
|
|
|
Base it on object conforming to <Associating> protocol, perhaps */
|
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) putObject: newObject atKey: aKey
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
NSMapInsert (_contents_hash, aKey, newObject);
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// REMOVING;
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) removeObjectAtKey: aKey
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
NSMapRemove (_contents_hash, aKey);
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) removeObject: oldObject
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-08-30 21:11:15 +00:00
|
|
|
/* xxx Could be more efficient! */
|
1996-08-30 21:00:25 +00:00
|
|
|
int count = [self count];
|
|
|
|
id keys_to_remove[count];
|
|
|
|
int num_keys_to_remove = 0;
|
|
|
|
id o, k;
|
|
|
|
NSMapEnumerator me = NSEnumerateMapTable (_contents_hash);
|
|
|
|
|
|
|
|
/* Find all the keys with corresponding objects that equal oldObject. */
|
|
|
|
while (NSNextMapEnumeratorPair (&me, (void**)&k, (void**)&o))
|
|
|
|
if ([oldObject isEqual: o])
|
|
|
|
keys_to_remove[num_keys_to_remove++] = k;
|
|
|
|
/* Remove them. */
|
|
|
|
while (num_keys_to_remove--)
|
|
|
|
[self removeObjectAtKey: keys_to_remove[num_keys_to_remove]];
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GETTING ELEMENTS;
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
- (NSArray*) allKeys
|
|
|
|
{
|
|
|
|
return NSAllMapTableKeys(_contents_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray*) allValues
|
|
|
|
{
|
|
|
|
return NSAllMapTableValues(_contents_hash);
|
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- objectAtKey: aKey
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
return NSMapGet (_contents_hash, aKey);
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TESTING;
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (BOOL) containsKey: aKey
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
if (NSMapGet (_contents_hash, aKey))
|
1994-11-04 16:29:24 +00:00
|
|
|
return YES;
|
|
|
|
else
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (unsigned) count
|
|
|
|
{
|
|
|
|
return NSCountMapTable (_contents_hash);
|
|
|
|
}
|
|
|
|
|
1994-11-04 16:29:24 +00:00
|
|
|
// ENUMERATIONS;
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- nextObjectAndKey: (id*)aKeyPtr withEnumState: (void**)enumState
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
id o;
|
|
|
|
if (!NSNextMapEnumeratorPair (*enumState, (void**)aKeyPtr, (void**)&o))
|
|
|
|
return NO_OBJECT;
|
|
|
|
return o;
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void*) newEnumState
|
|
|
|
{
|
1996-03-18 13:44:52 +00:00
|
|
|
void *me;
|
|
|
|
|
|
|
|
OBJC_MALLOC (me, NSMapEnumerator, 1);
|
|
|
|
*((NSMapEnumerator*)me) = NSEnumerateMapTable (_contents_hash);
|
|
|
|
return me;
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) freeEnumState: (void**)enumState
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
OBJC_FREE (*enumState);
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
1997-09-18 14:56:47 +00:00
|
|
|
- (NSString*) descriptionWithIndent: (unsigned)level
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-18 14:56:47 +00:00
|
|
|
int dict_count;
|
1997-09-01 21:59:51 +00:00
|
|
|
id desc;
|
|
|
|
id keyenum = [self keyEnumerator];
|
|
|
|
id key;
|
1997-09-18 14:56:47 +00:00
|
|
|
NSCharacterSet *quotables;
|
|
|
|
|
|
|
|
quotables = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1997-09-18 14:56:47 +00:00
|
|
|
dict_count = [self count];
|
1997-09-01 21:59:51 +00:00
|
|
|
desc = [NSMutableString stringWithCapacity: 2];
|
|
|
|
[desc appendString: @"{"];
|
1997-09-18 14:56:47 +00:00
|
|
|
if (dict_count > 0)
|
|
|
|
[desc appendString: @"\n"];
|
1997-10-31 16:26:44 +00:00
|
|
|
|
|
|
|
level += 2;
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
while ((key = [keyenum nextObject]))
|
|
|
|
{
|
1997-09-18 14:56:47 +00:00
|
|
|
NSString *string;
|
|
|
|
id object;
|
|
|
|
string = [key description];
|
1997-10-31 16:26:44 +00:00
|
|
|
|
1997-09-18 14:56:47 +00:00
|
|
|
if ([string rangeOfCharacterFromSet: quotables].length > 0)
|
1997-10-31 16:26:44 +00:00
|
|
|
[desc appendFormat: @"%*s%s = ", level, "", [string quotedCString]];
|
1997-09-18 14:56:47 +00:00
|
|
|
else
|
1997-10-31 16:26:44 +00:00
|
|
|
[desc appendFormat: @"%*s%s = ", level, "", [string cStringNoCopy]];
|
1997-09-18 14:56:47 +00:00
|
|
|
object = [self objectAtKey: key];
|
|
|
|
if ([object respondsToSelector: @selector(descriptionWithIndent:)])
|
|
|
|
{
|
|
|
|
/* This a dictionary or array, so don't quote it */
|
1997-10-31 16:26:44 +00:00
|
|
|
string = [object descriptionWithIndent: level];
|
1997-09-18 14:56:47 +00:00
|
|
|
[desc appendFormat: @"%@;\n", string];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This should be a string or number, so decide if we need to
|
|
|
|
quote it */
|
|
|
|
string = [object description];
|
|
|
|
if ([string rangeOfCharacterFromSet: quotables].length > 0)
|
1997-10-31 16:26:44 +00:00
|
|
|
[desc appendFormat: @"%s;\n", [string quotedCString]];
|
1997-09-18 14:56:47 +00:00
|
|
|
else
|
1997-10-31 16:26:44 +00:00
|
|
|
[desc appendFormat: @"%s;\n", [string cStringNoCopy]];
|
1997-09-18 14:56:47 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
1997-09-18 14:56:47 +00:00
|
|
|
if (dict_count == 0)
|
|
|
|
level = 0;
|
1997-10-31 16:26:44 +00:00
|
|
|
else
|
|
|
|
level -= 2;
|
1997-09-18 14:56:47 +00:00
|
|
|
[desc appendFormat: @"%*s}", level, ""];
|
1997-09-01 21:59:51 +00:00
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
1997-09-18 14:56:47 +00:00
|
|
|
- (NSString*) description
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-18 14:56:47 +00:00
|
|
|
return [self descriptionWithIndent: 0];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1994-11-04 16:29:24 +00:00
|
|
|
@end
|