/* Concrete implementation of NSSet based on GNU Set class Copyright (C) 1995, 1996, 1998 Free Software Foundation, Inc. Written by: Andrew Kachites McCallum Created: September 1995 Rewrite by: Richard frith-Macdonald This file is part of the GNUstep Base Library. 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. */ #include #include //#include #include #include #include #include #include #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 @interface NSGSetEnumerator : NSEnumerator { NSGSet *set; FastMapNode node; } @end @implementation NSGSetEnumerator - initWithSet: (NSSet*)d { [super init]; set = [(NSGSet*)d retain]; node = set->map.firstNode; return self; } - nextObject { FastMapNode old = node; if (node == 0) { return nil; } node = node->nextInMap; return old->key.o; } - (void) dealloc { [set release]; [super dealloc]; } @end @implementation NSGSet + (void) initialize { if (self == [NSGSet class]) { class_add_behavior(self, [NSSetNonCore class]); } } - (unsigned) count { return map.nodeCount; } - (void) dealloc { FastMapEmptyMap(&map); [super dealloc]; } - (void) encodeWithCoder: (NSCoder*)aCoder { unsigned count = map.nodeCount; FastMapNode node = map.firstNode; [(id)aCoder encodeValueOfCType: @encode(unsigned) at: &count withName: @"Set content count"]; if ([aCoder isKindOfClass: [NSPortCoder class]] && [(NSPortCoder*)aCoder isBycopy]) { while (node != 0) { [(id)aCoder encodeBycopyObject: node->key.o withName: @"Set content"]; node = node->nextInMap; } } else { while (node != 0) { [(id)aCoder encodeObject: node->key.o withName: @"Set content"]; node = node->nextInMap; } } } - (id) initWithCoder: (NSCoder*)aCoder { unsigned count; id value; [(id)aCoder decodeValueOfCType: @encode(unsigned) at: &count withName: NULL]; FastMapInitWithZoneAndCapacity(&map, [self zone], count); while (count-- > 0) { [(id)aCoder decodeObjectAt: &value withName: NULL]; FastMapAddKeyNoRetain(&map, (FastMapItem)value); } return self; } /* Designated initialiser */ - (id) initWithObjects: (id*)objs count: (unsigned)c { 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; } - (NSEnumerator*) objectEnumerator { return [[[NSGSetEnumerator alloc] initWithSet: self] autorelease]; } @end @implementation NSGMutableSet + (void) initialize { if (self == [NSGMutableSet class]) { class_add_behavior(self, [NSMutableSetNonCore class]); class_add_behavior(self, [NSGSet class]); } } /* Designated initialiser */ - (id) initWithCapacity: (unsigned)cap { FastMapInitWithZoneAndCapacity(&map, [self zone], cap); return self; } - (void) addObject: (NSObject*)anObject { FastMapNode node = FastMapNodeForKey(&map, (FastMapItem)anObject); if (node == 0) { FastMapAddKey(&map, (FastMapItem)anObject); } } - (void) removeObject: (NSObject *)anObject { FastMapRemoveKey(&map, (FastMapItem)anObject); } - (void) removeAllObjects { FastMapCleanMap(&map); } @end