1994-11-04 16:29:24 +00:00
|
|
|
|
/* Implementation for Objective-C KeyedCollection collection object
|
1996-01-24 03:16:05 +00:00
|
|
|
|
Copyright (C) 1993,1994, 1996 Free Software Foundation, Inc.
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
|
|
Written by: R. Andrew McCallum <mccallum@gnu.ai.mit.edu>
|
|
|
|
|
Date: May 1993
|
|
|
|
|
|
1996-04-17 19:55:26 +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.
|
|
|
|
|
*/
|
|
|
|
|
|
1996-04-17 15:23:00 +00:00
|
|
|
|
#include <gnustep/base/KeyedCollection.h>
|
|
|
|
|
#include <gnustep/base/CollectionPrivate.h>
|
1994-11-04 16:29:24 +00:00
|
|
|
|
#include <stdio.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
|
#include <gnustep/base/Array.h>
|
|
|
|
|
#include <gnustep/base/NSString.h>
|
|
|
|
|
#include <gnustep/base/behavior.h>
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
@implementation KeyEnumerator
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-04-16 02:50:42 +00:00
|
|
|
|
- initWithCollection: coll
|
|
|
|
|
{
|
|
|
|
|
collection = [coll retain];
|
|
|
|
|
enum_state = [coll newEnumState];
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- nextObject
|
|
|
|
|
{
|
|
|
|
|
id k;
|
|
|
|
|
[collection nextObjectAndKey: &k withEnumState: &enum_state];
|
|
|
|
|
return k;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
[collection freeEnumState: &enum_state];
|
|
|
|
|
[collection release];
|
|
|
|
|
}
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
@end
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
@implementation ConstantKeyedCollection
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
|
1994-11-04 16:29:24 +00:00
|
|
|
|
// INITIALIZING;
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
/* This is the 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 subclassResponsibility: _cmd];
|
1996-03-18 13:51:14 +00:00
|
|
|
|
return nil;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
|
|
|
|
|
// GETTING ELEMENTS AND KEYS;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
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
|
|
|
|
[self subclassResponsibility: _cmd];
|
1996-03-18 13:51:14 +00:00
|
|
|
|
return nil;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- keyOfObject: aContentObject
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1996-03-18 13:51:14 +00:00
|
|
|
|
return nil;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
|
|
|
|
|
// TESTING;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
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 ([self objectAtKey: aKey] == NO_OBJECT)
|
|
|
|
|
return NO;
|
|
|
|
|
return YES;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
|
|
|
|
|
// ENUMERATIONS;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- (id <Enumerating>) keyEnumerator
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
return [[[KeyEnumerator alloc] initWithCollection: self]
|
|
|
|
|
autorelease];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-18 13:51:14 +00:00
|
|
|
|
- (void) withKeysInvoke: (id <Invoking>)anInvocation
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
id o, k;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
FOR_KEYED_COLLECTION(self, o, k)
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[anInvocation invokeWithObject: k];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
1996-02-22 15:18:57 +00:00
|
|
|
|
END_FOR_KEYED_COLLECTION(self);
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-03-18 13:51:14 +00:00
|
|
|
|
- (void) withKeysInvoke: (id <Invoking>)anInvocation
|
1996-02-22 15:18:57 +00:00
|
|
|
|
whileTrue: (BOOL *)flag
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
id o, k;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
FOR_KEYED_COLLECTION_WHILE_TRUE(self, o, k, *flag)
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[anInvocation invokeWithObject: k];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
1996-02-22 15:18:57 +00:00
|
|
|
|
END_FOR_KEYED_COLLECTION(self);
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
/* Override this Collection method */
|
|
|
|
|
- nextObjectWithEnumState: (void**)enumState
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
id k;
|
|
|
|
|
return [self nextObjectAndKey: &k withEnumState: enumState];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
|
|
|
|
|
// LOW-LEVEL ENUMERATING;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- nextObjectAndKey: (id*)keyPtr withEnumState: (void**)enumState
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1996-03-18 13:51:14 +00:00
|
|
|
|
return nil;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
|
1994-11-04 16:29:24 +00:00
|
|
|
|
// COPYING;
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- shallowCopyValuesAs: (Class)aConstantCollectingClass
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
int count = [self count];
|
|
|
|
|
id contents[count];
|
|
|
|
|
id k;
|
|
|
|
|
int i = 0;
|
|
|
|
|
id o;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
FOR_KEYED_COLLECTION(self, o, k)
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
contents[i++] = o;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
1996-02-22 15:18:57 +00:00
|
|
|
|
END_FOR_KEYED_COLLECTION(self);
|
|
|
|
|
return [[aConstantCollectingClass alloc]
|
|
|
|
|
initWithObjects: contents count: count];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- shallowCopyKeysAs: (Class)aCollectingClass;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
int count = [self count];
|
|
|
|
|
id contents[count];
|
|
|
|
|
id k;
|
|
|
|
|
int i = 0;
|
|
|
|
|
id o;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
FOR_KEYED_COLLECTION(self, o, k)
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
contents[i++] = k;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
1996-02-22 15:18:57 +00:00
|
|
|
|
END_FOR_KEYED_COLLECTION(self);
|
|
|
|
|
return [[aCollectingClass alloc]
|
|
|
|
|
initWithObjects: contents count: count];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- copyValuesAs: (Class)aCollectingClass
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[self notImplemented: _cmd];
|
1996-03-18 13:51:14 +00:00
|
|
|
|
return nil;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- copyKeysAs: (Class)aCollectingClass;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[self notImplemented: _cmd];
|
1996-03-18 13:51:14 +00:00
|
|
|
|
return nil;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
|
|
|
|
|
// ARCHIVING
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- (void) _encodeContentsWithCoder: (id <Encoding>)aCoder
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
unsigned int count = [self count];
|
|
|
|
|
id o, k;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[aCoder encodeValueOfCType: @encode(unsigned)
|
|
|
|
|
at: &count
|
|
|
|
|
withName: @"Collection content count"];
|
|
|
|
|
FOR_KEYED_COLLECTION(self, o, k)
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[aCoder encodeObject: k
|
|
|
|
|
withName: @"KeyedCollection key"];
|
|
|
|
|
[aCoder encodeObject: o
|
|
|
|
|
withName:@"KeyedCollection content"];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
1996-02-22 15:18:57 +00:00
|
|
|
|
END_FOR_KEYED_COLLECTION(self);
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- (void) _decodeContentsWithCoder: (id <Decoding>)aCoder
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
unsigned int count, i;
|
|
|
|
|
id *objs, *keys;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[aCoder decodeValueOfCType:@encode(unsigned)
|
|
|
|
|
at:&count
|
|
|
|
|
withName:NULL];
|
|
|
|
|
OBJC_MALLOC(objs, id, count);
|
|
|
|
|
OBJC_MALLOC(keys, id, count);
|
|
|
|
|
for (i = 0; i < count; i++)
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[aCoder decodeObjectAt: &(keys[i])
|
1996-03-22 01:46:30 +00:00
|
|
|
|
withName: NULL];
|
|
|
|
|
[aCoder decodeObjectAt: &(objs[i])
|
1996-02-22 15:18:57 +00:00
|
|
|
|
withName: NULL];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[self initWithObjects: objs forKeys: keys count: count];
|
|
|
|
|
OBJC_FREE(objs);
|
|
|
|
|
OBJC_FREE(keys);
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- (id <String>) description
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
id s = [NSMutableString new];
|
|
|
|
|
id o, k;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
FOR_KEYED_COLLECTION(self, o, k)
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[s appendFormat: @"(%@,%@) ", [k description], [o description]];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
1996-02-22 15:18:57 +00:00
|
|
|
|
END_FOR_KEYED_COLLECTION(self);
|
1996-03-03 00:35:28 +00:00
|
|
|
|
[s appendFormat: @" :%s\n", object_get_class_name (self)];
|
1996-02-22 15:18:57 +00:00
|
|
|
|
return [s autorelease];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
@end
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
|
|
|
|
|
@implementation KeyedCollection
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
+ (void) initialize
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
if (self == [KeyedCollection class])
|
|
|
|
|
class_add_behavior(self, [Collection class]);
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
// ADDING;
|
|
|
|
|
- (void) putObject: newContentObject atKey: aKey
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
|
|
|
|
|
// REPLACING AND SWAPPING;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- (void) replaceObjectAtKey: aKey with: newContentObject
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
- (void) swapObjectsAtKeys: key1 : key2
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
|
// REMOVING;
|
|
|
|
|
- (void) removeObjectAtKey: aKey
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-02-22 15:18:57 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
1996-02-22 15:18:57 +00:00
|
|
|
|
|