1998-10-08 13:46:53 +00:00
|
|
|
/* Concrete implementation of NSSet based on GNU Set class
|
|
|
|
Copyright (C) 1998 Free Software Foundation, Inc.
|
1995-10-30 01:00:39 +00:00
|
|
|
|
1998-10-20 14:42:18 +00:00
|
|
|
Written by: Richard frith-Macdonald <richard@brainstorm.co.Ik>
|
1998-10-08 13:46:53 +00:00
|
|
|
Created: October 1998
|
1995-10-30 01:00:39 +00:00
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1995-10-30 01:00:39 +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>
|
1996-04-17 15:23:00 +00:00
|
|
|
#include <gnustep/base/behavior.h>
|
1998-10-17 06:47:46 +00:00
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
|
|
|
#include <Foundation/NSException.h>
|
1995-10-30 01:00:39 +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_RETAIN_VAL(X) X
|
|
|
|
#define FAST_MAP_RELEASE_VAL(X)
|
|
|
|
|
|
|
|
#include "FastMap.x"
|
|
|
|
|
|
|
|
@class NSSetNonCore;
|
|
|
|
@class NSMutableSetNonCore;
|
|
|
|
|
|
|
|
@interface NSGCountedSet : NSCountedSet
|
|
|
|
{
|
|
|
|
@public
|
|
|
|
FastMapTable_t map;
|
|
|
|
}
|
|
|
|
@end
|
1995-10-30 01:00:39 +00:00
|
|
|
|
|
|
|
@interface NSGCountedSetEnumerator : NSEnumerator
|
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
NSGCountedSet *set;
|
|
|
|
FastMapNode node;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSGCountedSetEnumerator
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- initWithSet: (NSSet*)d
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1998-10-17 06:47:46 +00:00
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
set = [(NSGCountedSet*)d retain];
|
|
|
|
node = set->map.firstNode;
|
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
return self;
|
1995-10-30 01:00:39 +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-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
[set release];
|
|
|
|
[super dealloc];
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NSGCountedSet
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
if (self == [NSGCountedSet class]) {
|
|
|
|
class_add_behavior(self, [NSSetNonCore class]);
|
|
|
|
class_add_behavior(self, [NSMutableSetNonCore class]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
FastMapEmptyMap(&map);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
unsigned count = map.nodeCount;
|
|
|
|
FastMapNode node = map.firstNode;
|
|
|
|
|
|
|
|
[(id<Encoding>)aCoder encodeValueOfCType: @encode(unsigned)
|
|
|
|
at: &count
|
|
|
|
withName: @"Set content count"];
|
1995-10-30 01:00:39 +00:00
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
if ([aCoder isKindOfClass: [NSPortCoder class]] &&
|
|
|
|
[(NSPortCoder*)aCoder isBycopy]) {
|
|
|
|
while (node != 0) {
|
|
|
|
[(id<Encoding>)aCoder encodeBycopyObject: node->key.o
|
|
|
|
withName: @"Set value"];
|
|
|
|
[(id<Encoding>)aCoder encodeValueOfCType: @encode(unsigned)
|
1998-10-20 14:42:18 +00:00
|
|
|
at: &node->value.I
|
1998-10-08 13:46:53 +00:00
|
|
|
withName: @"Set value count"];
|
|
|
|
node = node->nextInMap;
|
|
|
|
}
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
else {
|
|
|
|
while (node != 0) {
|
|
|
|
[(id<Encoding>)aCoder encodeObject: node->key.o
|
|
|
|
withName: @"Set content"];
|
|
|
|
[(id<Encoding>)aCoder encodeValueOfCType: @encode(unsigned)
|
1998-10-20 14:42:18 +00:00
|
|
|
at: &node->value.I
|
1998-10-08 13:46:53 +00:00
|
|
|
withName: @"Set value count"];
|
|
|
|
node = node->nextInMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
unsigned count;
|
|
|
|
id value;
|
|
|
|
unsigned valcnt;
|
|
|
|
|
|
|
|
[(id<Decoding>)aCoder decodeValueOfCType: @encode(unsigned)
|
|
|
|
at: &count
|
|
|
|
withName: NULL];
|
|
|
|
|
|
|
|
FastMapInitWithZoneAndCapacity(&map, [self zone], count);
|
|
|
|
while (count-- > 0) {
|
|
|
|
[(id<Decoding>)aCoder decodeObjectAt: &value withName: NULL];
|
|
|
|
[(id<Decoding>)aCoder decodeValueOfCType: @encode(unsigned)
|
|
|
|
at: &valcnt
|
|
|
|
withName: NULL];
|
|
|
|
FastMapAddPairNoRetain(&map, (FastMapItem)value, (FastMapItem)valcnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Designated initialiser */
|
|
|
|
- (id) initWithCapacity: (unsigned)cap
|
|
|
|
{
|
|
|
|
FastMapInitWithZoneAndCapacity(&map, [self zone], cap);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithObjects: (id*)objs count: (unsigned)c
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ([self initWithCapacity: c] == nil) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
for (i = 0; i < c; i++) {
|
1998-10-17 06:47:46 +00:00
|
|
|
FastMapNode node;
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1998-10-17 06:47:46 +00:00
|
|
|
if (objs[i] == nil) {
|
|
|
|
[self autorelease];
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to init counted set with nil value"];
|
|
|
|
}
|
|
|
|
node = FastMapNodeForKey(&map, (FastMapItem)objs[i]);
|
1998-10-08 13:46:53 +00:00
|
|
|
if (node == 0) {
|
|
|
|
FastMapAddPair(&map,(FastMapItem)objs[i],(FastMapItem)(unsigned)1);
|
|
|
|
}
|
|
|
|
else {
|
1998-10-20 14:42:18 +00:00
|
|
|
node->value.I++;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addObject: (NSObject*)anObject
|
|
|
|
{
|
1998-10-17 06:47:46 +00:00
|
|
|
FastMapNode node;
|
|
|
|
|
|
|
|
if (anObject == nil) {
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to nil value to counted set"];
|
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1998-10-17 06:47:46 +00:00
|
|
|
node = FastMapNodeForKey(&map, (FastMapItem)anObject);
|
1998-10-08 13:46:53 +00:00
|
|
|
if (node == 0) {
|
|
|
|
FastMapAddPair(&map,(FastMapItem)anObject,(FastMapItem)(unsigned)1);
|
|
|
|
}
|
|
|
|
else {
|
1998-10-20 14:42:18 +00:00
|
|
|
node->value.I++;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned) count
|
|
|
|
{
|
|
|
|
return map.nodeCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned) countForObject: (id)anObject
|
|
|
|
{
|
1998-10-17 06:47:46 +00:00
|
|
|
if (anObject) {
|
|
|
|
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject);
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1998-10-17 06:47:46 +00:00
|
|
|
if (node) {
|
1998-10-20 14:42:18 +00:00
|
|
|
return node->value.I;
|
1998-10-17 06:47:46 +00:00
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
1998-10-17 06:47:46 +00:00
|
|
|
return 0;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) member: (id)anObject
|
|
|
|
{
|
1998-10-17 06:47:46 +00:00
|
|
|
if (anObject) {
|
|
|
|
FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject);
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1998-10-17 06:47:46 +00:00
|
|
|
if (node) {
|
|
|
|
return node->key.o;
|
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
1998-10-17 06:47:46 +00:00
|
|
|
return nil;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSEnumerator*) objectEnumerator
|
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
return [[[NSGCountedSetEnumerator alloc] initWithSet: self] autorelease];
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) removeObject: (NSObject*)anObject
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1998-10-17 06:47:46 +00:00
|
|
|
if (anObject) {
|
|
|
|
FastMapBucket bucket;
|
|
|
|
|
|
|
|
bucket = FastMapBucketForKey(&map, (FastMapItem)anObject);
|
|
|
|
if (bucket) {
|
|
|
|
FastMapNode node;
|
|
|
|
|
|
|
|
node = FastMapNodeForKeyInBucket(bucket, (FastMapItem)anObject);
|
|
|
|
if (node) {
|
1998-10-20 14:42:18 +00:00
|
|
|
if (--node->value.I == 0) {
|
1998-10-17 06:47:46 +00:00
|
|
|
FastMapRemoveNodeFromMap(&map, bucket, node);
|
|
|
|
FastMapFreeNode(&map, node);
|
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) removeAllObjects
|
1995-10-30 01:51:17 +00:00
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
FastMapCleanMap(&map);
|
1995-10-30 01:51:17 +00:00
|
|
|
}
|
|
|
|
|
1995-10-30 01:00:39 +00:00
|
|
|
@end
|