1995-10-26 01:16:38 +00:00
|
|
|
/* Concrete implementation of NSSet based on GNU Set class
|
1998-10-08 13:46:53 +00:00
|
|
|
Copyright (C) 1995, 1996, 1998 Free Software Foundation, Inc.
|
1995-10-26 01:16:38 +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: September 1995
|
1998-10-08 13:46:53 +00:00
|
|
|
Rewrite by: Richard frith-Macdonald <richard@brainstorm.co.uk>
|
1995-10-26 01:16:38 +00:00
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1995-10-26 01:16:38 +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>
|
1998-10-08 13:46:53 +00:00
|
|
|
#include <Foundation/NSSet.h>
|
|
|
|
//#include <Foundation/NSGSet.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
#include <gnustep/base/behavior.h>
|
1995-10-26 01:16:38 +00:00
|
|
|
#include <Foundation/NSUtilities.h>
|
|
|
|
#include <Foundation/NSString.h>
|
1998-10-08 13:46:53 +00:00
|
|
|
#include <Foundation/NSPortCoder.h>
|
|
|
|
#include <gnustep/base/Coding.h>
|
|
|
|
|
|
|
|
#define FAST_MAP_HAS_VALUE 0
|
|
|
|
|
|
|
|
#include "FastMap.x"
|
|
|
|
|
|
|
|
@class NSSetNonCore;
|
|
|
|
@class NSMutableSetNonCore;
|
|
|
|
|
|
|
|
@interface NSGSet : NSSet
|
|
|
|
{
|
|
|
|
@public
|
|
|
|
FastMapTable_t map;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSGMutableSet : NSMutableSet
|
|
|
|
{
|
|
|
|
@public
|
|
|
|
FastMapTable_t map;
|
|
|
|
}
|
|
|
|
@end
|
1995-10-26 01:16:38 +00:00
|
|
|
|
|
|
|
@interface NSGSetEnumerator : NSEnumerator
|
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
NSGSet *set;
|
|
|
|
FastMapNode node;
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSGSetEnumerator
|
|
|
|
|
|
|
|
- initWithSet: (NSSet*)d
|
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
[super init];
|
|
|
|
set = [(NSGSet*)d retain];
|
|
|
|
node = set->map.firstNode;
|
|
|
|
return self;
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- nextObject
|
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
FastMapNode old = node;
|
|
|
|
|
|
|
|
if (node == 0) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
node = node->nextInMap;
|
|
|
|
return old->key.o;
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
[set release];
|
|
|
|
[super dealloc];
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NSGSet
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
if (self == [NSGSet class]) {
|
|
|
|
class_add_behavior(self, [NSSetNonCore class]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned) count
|
|
|
|
{
|
|
|
|
return map.nodeCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
FastMapEmptyMap(&map);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
1995-10-26 01:16:38 +00:00
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
unsigned count = map.nodeCount;
|
|
|
|
FastMapNode node = map.firstNode;
|
|
|
|
|
|
|
|
[(id<Encoding>)aCoder encodeValueOfCType: @encode(unsigned)
|
|
|
|
at: &count
|
|
|
|
withName: @"Set content count"];
|
|
|
|
|
|
|
|
if ([aCoder isKindOfClass: [NSPortCoder class]] &&
|
|
|
|
[(NSPortCoder*)aCoder isBycopy]) {
|
|
|
|
while (node != 0) {
|
|
|
|
[(id<Encoding>)aCoder encodeBycopyObject: node->key.o
|
|
|
|
withName: @"Set content"];
|
|
|
|
node = node->nextInMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (node != 0) {
|
|
|
|
[(id<Encoding>)aCoder encodeObject: node->key.o
|
|
|
|
withName: @"Set content"];
|
|
|
|
node = node->nextInMap;
|
|
|
|
}
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
unsigned count;
|
|
|
|
id value;
|
1995-10-26 01:16:38 +00:00
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
[(id<Decoding>)aCoder decodeValueOfCType: @encode(unsigned)
|
|
|
|
at: &count
|
|
|
|
withName: NULL];
|
1995-10-26 01:16:38 +00:00
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
FastMapInitWithZoneAndCapacity(&map, [self zone], count);
|
|
|
|
while (count-- > 0) {
|
|
|
|
[(id<Decoding>)aCoder decodeObjectAt: &value withName: NULL];
|
|
|
|
FastMapAddKeyNoRetain(&map, (FastMapItem)value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Designated initialiser */
|
|
|
|
- (id) initWithObjects: (id*)objs count: (unsigned)c
|
1995-10-26 01:16:38 +00:00
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
int i;
|
|
|
|
FastMapInitWithZoneAndCapacity(&map, [self zone], c);
|
|
|
|
for (i = 0; i < c; i++) {
|
|
|
|
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)objs[i]);
|
|
|
|
|
|
|
|
if (node == 0) {
|
|
|
|
FastMapAddKey(&map, (FastMapItem)objs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) member: (id)anObject
|
|
|
|
{
|
|
|
|
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject);
|
|
|
|
|
|
|
|
if (node == 0) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return node->key.o;
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (NSEnumerator*) objectEnumerator
|
1995-10-30 01:51:46 +00:00
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
return [[[NSGSetEnumerator alloc] initWithSet: self] autorelease];
|
1995-10-30 01:51:46 +00:00
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1995-10-26 01:16:38 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSGMutableSet
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
if (self == [NSGMutableSet class]) {
|
|
|
|
class_add_behavior(self, [NSMutableSetNonCore class]);
|
|
|
|
class_add_behavior(self, [NSGSet class]);
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
/* Designated initialiser */
|
|
|
|
- (id) initWithCapacity: (unsigned)cap
|
|
|
|
{
|
|
|
|
FastMapInitWithZoneAndCapacity(&map, [self zone], cap);
|
|
|
|
return self;
|
|
|
|
}
|
1995-10-26 01:16:38 +00:00
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) addObject: (NSObject*)anObject
|
|
|
|
{
|
|
|
|
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject);
|
1995-10-26 01:16:38 +00:00
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
if (node == 0) {
|
|
|
|
FastMapAddKey(&map, (FastMapItem)anObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeObject: (NSObject *)anObject
|
1995-10-26 01:16:38 +00:00
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
FastMapRemoveKey(&map, (FastMapItem)anObject);
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) removeAllObjects
|
1995-10-30 01:51:46 +00:00
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
FastMapCleanMap(&map);
|
1995-10-30 01:51:46 +00:00
|
|
|
}
|
|
|
|
|
1995-10-26 01:16:38 +00:00
|
|
|
@end
|