2001-12-17 14:31:42 +00:00
|
|
|
/** Concrete implementation of NSSet based on GNU Set class
|
2000-12-07 22:57:56 +00:00
|
|
|
Copyright (C) 1995, 1996, 1998, 2000 Free Software Foundation, Inc.
|
2005-02-22 11:22:44 +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
|
2000-12-07 22:57:56 +00:00
|
|
|
Rewrite by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
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.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1995-10-26 01:16:38 +00:00
|
|
|
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.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1995-10-26 01:16:38 +00:00
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
2005-05-22 03:32:16 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
|
1995-10-26 01:16:38 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "Foundation/NSSet.h"
|
2003-07-31 23:49:32 +00:00
|
|
|
#include "GNUstepBase/GSObjCRuntime.h"
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "Foundation/NSAutoreleasePool.h"
|
|
|
|
#include "Foundation/NSArray.h"
|
|
|
|
#include "Foundation/NSException.h"
|
|
|
|
#include "Foundation/NSUtilities.h"
|
|
|
|
#include "Foundation/NSString.h"
|
|
|
|
#include "Foundation/NSPortCoder.h"
|
|
|
|
#include "Foundation/NSDebug.h"
|
|
|
|
#include "Foundation/NSObjCRuntime.h"
|
2004-01-27 21:51:33 +00:00
|
|
|
// For private method _decodeArrayOfObjectsForKey:
|
|
|
|
#include "Foundation/NSKeyedArchiver.h"
|
2004-09-07 11:08:46 +00:00
|
|
|
#include "GSPrivate.h"
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1999-06-21 08:30:26 +00:00
|
|
|
#define GSI_MAP_HAS_VALUE 0
|
|
|
|
#define GSI_MAP_KTYPES GSUNION_OBJ
|
1998-10-08 13:46:53 +00:00
|
|
|
|
2003-07-31 23:49:32 +00:00
|
|
|
#include "GNUstepBase/GSIMap.h"
|
1998-10-08 13:46:53 +00:00
|
|
|
|
2003-02-16 11:31:28 +00:00
|
|
|
static SEL memberSel;
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@interface GSSet : NSSet
|
1998-10-08 13:46:53 +00:00
|
|
|
{
|
|
|
|
@public
|
1999-06-21 08:30:26 +00:00
|
|
|
GSIMapTable_t map;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@interface GSMutableSet : NSMutableSet
|
1998-10-08 13:46:53 +00:00
|
|
|
{
|
|
|
|
@public
|
1999-06-21 08:30:26 +00:00
|
|
|
GSIMapTable_t map;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
@end
|
1995-10-26 01:16:38 +00:00
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@interface GSSetEnumerator : NSEnumerator
|
1995-10-26 01:16:38 +00:00
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSSet *set;
|
|
|
|
GSIMapEnumerator_t enumerator;
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@implementation GSSetEnumerator
|
1995-10-26 01:16:38 +00:00
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
- (id) initWithSet: (NSSet*)d
|
1995-10-26 01:16:38 +00:00
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
self = [super init];
|
|
|
|
if (self != nil)
|
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
set = (GSSet*)RETAIN(d);
|
2000-12-21 10:31:33 +00:00
|
|
|
enumerator = GSIMapEnumeratorForMap(&set->map);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
return self;
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
- (id) nextObject
|
1995-10-26 01:16:38 +00:00
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
if (node == 0)
|
|
|
|
{
|
|
|
|
return nil;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
2000-12-21 10:31:33 +00:00
|
|
|
return node->key.obj;
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
1999-07-03 19:59:44 +00:00
|
|
|
RELEASE(set);
|
1998-10-27 08:12:49 +00:00
|
|
|
[super dealloc];
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@implementation GSSet
|
1995-10-26 01:16:38 +00:00
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
static Class arrayClass;
|
|
|
|
static Class setClass;
|
|
|
|
static Class mutableSetClass;
|
|
|
|
|
1995-10-26 01:16:38 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
if (self == [GSSet class])
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
arrayClass = [NSArray class];
|
2000-12-07 22:57:56 +00:00
|
|
|
setClass = [GSSet class];
|
|
|
|
mutableSetClass = [GSMutableSet class];
|
2003-02-16 11:31:28 +00:00
|
|
|
memberSel = @selector(member:);
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
- (NSArray*) allObjects
|
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
|
|
|
unsigned i = 0;
|
2004-09-07 11:08:46 +00:00
|
|
|
NSArray *result;
|
|
|
|
GS_BEGINIDBUF(objects, map.nodeCount);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
while (node != 0)
|
|
|
|
{
|
2004-09-07 11:08:46 +00:00
|
|
|
objects[i++] = node->key.obj;
|
2000-12-21 10:31:33 +00:00
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2004-09-07 11:08:46 +00:00
|
|
|
result = AUTORELEASE([[arrayClass allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithObjects: objects count: i]);
|
|
|
|
GS_ENDIDBUF();
|
|
|
|
return result;
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) anyObject
|
|
|
|
{
|
|
|
|
if (map.nodeCount > 0)
|
2000-12-07 22:57:56 +00:00
|
|
|
{
|
2002-05-28 05:23:36 +00:00
|
|
|
GSIMapBucket bucket = map.buckets;
|
2004-09-07 11:08:46 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (bucket->firstNode)
|
|
|
|
{
|
|
|
|
return bucket->firstNode->key.obj;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bucket++;
|
|
|
|
}
|
|
|
|
}
|
2000-12-07 22:57:56 +00:00
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
else
|
2000-12-07 22:57:56 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-20 17:18:06 +00:00
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
return RETAIN(self);
|
|
|
|
}
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
- (unsigned) count
|
|
|
|
{
|
|
|
|
return map.nodeCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
GSIMapEmptyMap(&map);
|
|
|
|
[super dealloc];
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
2004-01-28 07:33:20 +00:00
|
|
|
if ([aCoder allowsKeyedCoding])
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2004-01-28 07:33:20 +00:00
|
|
|
[super encodeWithCoder: aCoder];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned count = map.nodeCount;
|
|
|
|
SEL sel = @selector(encodeObject:);
|
|
|
|
IMP imp = [aCoder methodForSelector: sel];
|
|
|
|
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
|
|
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &count];
|
|
|
|
while (node != 0)
|
|
|
|
{
|
|
|
|
(*imp)(aCoder, sel, node->key.obj);
|
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
|
|
|
}
|
|
|
|
GSIMapEndEnumerator(&enumerator);
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
- (unsigned) hash
|
|
|
|
{
|
|
|
|
return map.nodeCount;
|
|
|
|
}
|
|
|
|
|
2004-09-07 09:22:10 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
return [self initWithObjects: 0 count: 0];
|
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
2004-01-27 21:51:33 +00:00
|
|
|
if ([aCoder allowsKeyedCoding])
|
|
|
|
{
|
2004-01-28 07:33:20 +00:00
|
|
|
self = [super initWithCoder: aCoder];
|
2004-01-27 21:51:33 +00:00
|
|
|
}
|
|
|
|
else
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2004-01-27 21:51:33 +00:00
|
|
|
unsigned count;
|
|
|
|
id value;
|
|
|
|
SEL sel = @selector(decodeValueOfObjCType:at:);
|
|
|
|
IMP imp = [aCoder methodForSelector: sel];
|
|
|
|
const char *type = @encode(id);
|
2005-02-22 11:22:44 +00:00
|
|
|
|
2004-01-27 21:51:33 +00:00
|
|
|
(*imp)(aCoder, sel, @encode(unsigned), &count);
|
|
|
|
|
|
|
|
GSIMapInitWithZoneAndCapacity(&map, [self zone], count);
|
|
|
|
while (count-- > 0)
|
|
|
|
{
|
|
|
|
(*imp)(aCoder, sel, type, &value);
|
|
|
|
GSIMapAddKeyNoRetain(&map, (GSIMapKey)value);
|
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
2004-01-28 07:33:20 +00:00
|
|
|
return self;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
/* Designated initialiser */
|
|
|
|
- (id) initWithObjects: (id*)objs count: (unsigned)c
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
GSIMapInitWithZoneAndCapacity(&map, [self zone], c);
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
{
|
|
|
|
GSIMapNode node;
|
|
|
|
|
|
|
|
if (objs[i] == nil)
|
|
|
|
{
|
|
|
|
IF_NO_GC(AUTORELEASE(self));
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to init set with nil value"];
|
|
|
|
}
|
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)objs[i]);
|
|
|
|
if (node == 0)
|
|
|
|
{
|
|
|
|
GSIMapAddKey(&map, (GSIMapKey)objs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
- (BOOL) intersectsSet: (NSSet*) otherSet
|
1995-10-26 01:16:38 +00:00
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
Class c;
|
1998-10-27 08:12:49 +00:00
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
/*
|
|
|
|
* If this set is empty, or the other is nil, this method should return NO.
|
|
|
|
*/
|
|
|
|
if (map.nodeCount == 0)
|
2000-12-07 22:57:56 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
if (otherSet == nil)
|
2000-12-07 22:57:56 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
// Loop for all members in otherSet
|
2000-10-31 16:17:33 +00:00
|
|
|
c = GSObjCClass(otherSet);
|
2000-08-07 22:00:31 +00:00
|
|
|
if (c == setClass || c == mutableSetClass)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapTable m = &((GSSet*)otherSet)->map;
|
|
|
|
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(m);
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
1998-10-17 06:47:46 +00:00
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
while (node != 0)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
if (GSIMapNodeForKey(&map, node->key) != 0)
|
|
|
|
{
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
return YES;
|
|
|
|
}
|
2000-12-21 10:31:33 +00:00
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
1998-10-17 06:47:46 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSEnumerator *e;
|
|
|
|
id o;
|
|
|
|
|
|
|
|
e = [otherSet objectEnumerator];
|
|
|
|
while ((o = [e nextObject])) // 1. pick a member from otherSet.
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
if (GSIMapNodeForKey(&map, (GSIMapKey)o) != 0)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
return NO;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
- (BOOL) isSubsetOfSet: (NSSet*) otherSet
|
1998-10-08 13:46:53 +00:00
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapEnumerator_t enumerator;
|
|
|
|
GSIMapNode node;
|
2003-02-16 11:31:28 +00:00
|
|
|
IMP imp;
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
// -1. members of this set(self) <= that of otherSet
|
|
|
|
if (map.nodeCount > [otherSet count])
|
2000-12-07 22:57:56 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2003-02-16 11:31:28 +00:00
|
|
|
if (map.nodeCount == 0)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
|
2003-02-16 11:31:28 +00:00
|
|
|
imp = [otherSet methodForSelector: memberSel];
|
2000-12-21 10:31:33 +00:00
|
|
|
enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
// 0. Loop for all members in this set(self).
|
|
|
|
while (node != 0)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
// 1. check the member is in the otherSet.
|
2003-02-16 11:31:28 +00:00
|
|
|
if ((*imp)(otherSet, memberSel, node->key.obj) != nil)
|
2000-12-21 10:31:33 +00:00
|
|
|
{
|
|
|
|
// 1.1 if true -> continue, try to check the next member.
|
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
else
|
2000-12-21 10:31:33 +00:00
|
|
|
{
|
|
|
|
// 1.2 if false -> return NO;
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-12-21 10:31:33 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
// 2. return YES; all members in this set are also in the otherSet.
|
|
|
|
return YES;
|
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
- (BOOL) isEqualToSet: (NSSet*)other
|
|
|
|
{
|
|
|
|
if (other == nil)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
else if (other == self)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-10-31 16:17:33 +00:00
|
|
|
Class c = GSObjCClass(other);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
if (c == setClass || c == mutableSetClass)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
if (map.nodeCount != ((GSSet*)other)->map.nodeCount)
|
2000-08-07 22:00:31 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2003-02-16 11:31:28 +00:00
|
|
|
else if (map.nodeCount == 0)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
else
|
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapEnumerator_t enumerator;
|
|
|
|
GSIMapNode node;
|
|
|
|
|
|
|
|
enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
while (node != 0)
|
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
if (GSIMapNodeForKey(&(((GSSet*)other)->map), node->key) == 0)
|
2000-08-07 22:00:31 +00:00
|
|
|
{
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2000-12-21 10:31:33 +00:00
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
1998-10-17 06:47:46 +00:00
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (map.nodeCount != [other count])
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2003-02-16 11:31:28 +00:00
|
|
|
else if (map.nodeCount == 0)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
2000-08-07 22:00:31 +00:00
|
|
|
else
|
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapEnumerator_t enumerator;
|
|
|
|
GSIMapNode node;
|
2003-02-16 11:31:28 +00:00
|
|
|
IMP imp;
|
2000-12-21 10:31:33 +00:00
|
|
|
|
2003-02-16 11:31:28 +00:00
|
|
|
imp = [other methodForSelector: memberSel];
|
2000-12-21 10:31:33 +00:00
|
|
|
enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
while (node != 0)
|
|
|
|
{
|
2003-02-16 11:31:28 +00:00
|
|
|
if ((*imp)(other, memberSel, node->key.obj) == nil)
|
2000-08-07 22:00:31 +00:00
|
|
|
{
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2000-12-21 10:31:33 +00:00
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return YES;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
- (void) makeObjectsPerform: (SEL)aSelector
|
1995-10-30 01:51:46 +00:00
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
while (node != 0)
|
|
|
|
{
|
|
|
|
[node->key.obj performSelector: aSelector];
|
2000-12-21 10:31:33 +00:00
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) makeObjectsPerformSelector: (SEL)aSelector
|
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
while (node != 0)
|
|
|
|
{
|
|
|
|
[node->key.obj performSelector: aSelector];
|
2000-12-21 10:31:33 +00:00
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) makeObjectsPerformSelector: (SEL)aSelector withObject: argument
|
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
while (node != 0)
|
|
|
|
{
|
|
|
|
[node->key.obj performSelector: aSelector withObject: argument];
|
2000-12-21 10:31:33 +00:00
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) makeObjectsPerform: (SEL)aSelector withObject: argument
|
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
while (node != 0)
|
|
|
|
{
|
|
|
|
[node->key.obj performSelector: aSelector withObject: argument];
|
2000-12-21 10:31:33 +00:00
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
1995-10-30 01:51:46 +00:00
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
- (id) member: (id)anObject
|
1995-10-26 01:16:38 +00:00
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
if (anObject != nil)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
GSIMapNode node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
|
|
|
|
|
|
|
|
if (node != 0)
|
|
|
|
{
|
|
|
|
return node->key.obj;
|
|
|
|
}
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
2000-12-07 22:57:56 +00:00
|
|
|
return nil;
|
1995-10-26 01:16:38 +00:00
|
|
|
}
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
- (NSEnumerator*) objectEnumerator
|
1998-10-08 13:46:53 +00:00
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
return AUTORELEASE([[GSSetEnumerator alloc] initWithSet: self]);
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
1995-10-26 01:16:38 +00:00
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@end
|
2000-08-07 22:00:31 +00:00
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@implementation GSMutableSet
|
2000-08-07 22:00:31 +00:00
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [GSMutableSet class])
|
|
|
|
{
|
2002-11-27 12:52:29 +00:00
|
|
|
GSObjCAddClassBehavior(self, [GSSet class]);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) addObject: (NSObject*)anObject
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
GSIMapNode node;
|
1995-10-26 01:16:38 +00:00
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
if (anObject == nil)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
2000-08-07 22:00:31 +00:00
|
|
|
format: @"Tried to add nil to set"];
|
1998-10-17 06:47:46 +00:00
|
|
|
}
|
1999-06-21 08:30:26 +00:00
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
|
1998-10-27 08:12:49 +00:00
|
|
|
if (node == 0)
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
GSIMapAddKey(&map, (GSIMapKey)anObject);
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
- (void) addObjectsFromArray: (NSArray*)array
|
|
|
|
{
|
|
|
|
unsigned count = [array count];
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
while (count-- > 0)
|
2000-08-07 22:00:31 +00:00
|
|
|
{
|
|
|
|
id anObject = [array objectAtIndex: count];
|
|
|
|
|
|
|
|
if (anObject == nil)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to add nil to set"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GSIMapNode node;
|
|
|
|
|
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
|
|
|
|
if (node == 0)
|
|
|
|
{
|
|
|
|
GSIMapAddKey(&map, (GSIMapKey)anObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-20 17:18:06 +00:00
|
|
|
/* Override version from GSSet */
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
NSSet *copy = [setClass allocWithZone: z];
|
|
|
|
|
|
|
|
return [copy initWithSet: self copyItems: NO];
|
|
|
|
}
|
|
|
|
|
2004-09-07 09:22:10 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
return [self initWithCapacity: 0];
|
|
|
|
}
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
/* Designated initialiser */
|
|
|
|
- (id) initWithCapacity: (unsigned)cap
|
2000-08-07 22:00:31 +00:00
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
GSIMapInitWithZoneAndCapacity(&map, [self zone], cap);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithObjects: (id*)objects
|
|
|
|
count: (unsigned)count
|
|
|
|
{
|
|
|
|
self = [self initWithCapacity: count];
|
|
|
|
|
|
|
|
while (count--)
|
2000-08-07 22:00:31 +00:00
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
id anObject = objects[count];
|
2000-08-07 22:00:31 +00:00
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
if (anObject == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"Tried to init a set with a nil object");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
2000-08-07 22:00:31 +00:00
|
|
|
{
|
|
|
|
GSIMapNode node;
|
|
|
|
|
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
|
|
|
|
if (node == 0)
|
|
|
|
{
|
|
|
|
GSIMapAddKey(&map, (GSIMapKey)anObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-12-07 22:57:56 +00:00
|
|
|
return self;
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) intersectSet: (NSSet*) other
|
|
|
|
{
|
|
|
|
if (other != self)
|
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapBucket bucket = GSIMapEnumeratorBucket(&enumerator);
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
while (node != 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ([other containsObject: node->key.obj] == NO)
|
|
|
|
{
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapRemoveNodeFromMap(&map, bucket, node);
|
|
|
|
GSIMapFreeNode(&map, node);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
bucket = GSIMapEnumeratorBucket(&enumerator);
|
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-14 08:52:39 +00:00
|
|
|
- (id) makeImmutableCopyOnFail: (BOOL)force
|
|
|
|
{
|
2002-11-10 13:56:34 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
GSDebugAllocationRemove(isa, self);
|
|
|
|
#endif
|
2002-10-14 08:52:39 +00:00
|
|
|
isa = [GSSet class];
|
2002-11-10 13:56:34 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
GSDebugAllocationAdd(isa, self);
|
|
|
|
#endif
|
2002-10-14 08:52:39 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
- (void) minusSet: (NSSet*) other
|
|
|
|
{
|
|
|
|
if (other == self)
|
|
|
|
{
|
|
|
|
GSIMapCleanMap(&map);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSEnumerator *e = [other objectEnumerator];
|
|
|
|
id anObject;
|
|
|
|
|
|
|
|
while ((anObject = [e nextObject]) != nil)
|
|
|
|
{
|
|
|
|
GSIMapRemoveKey(&map, (GSIMapKey)anObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
- (void) removeAllObjects
|
|
|
|
{
|
|
|
|
GSIMapCleanMap(&map);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeObject: (NSObject *)anObject
|
|
|
|
{
|
|
|
|
if (anObject == nil)
|
|
|
|
{
|
2001-05-22 09:30:07 +00:00
|
|
|
NSWarnMLog(@"attempt to remove nil object");
|
2000-12-07 22:57:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
GSIMapRemoveKey(&map, (GSIMapKey)anObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) unionSet: (NSSet*) other
|
|
|
|
{
|
|
|
|
if (other != self)
|
|
|
|
{
|
|
|
|
NSEnumerator *e = [other objectEnumerator];
|
|
|
|
|
2001-05-22 09:30:07 +00:00
|
|
|
if (e != nil)
|
2000-12-07 22:57:56 +00:00
|
|
|
{
|
2001-05-22 09:30:07 +00:00
|
|
|
id anObject;
|
|
|
|
SEL sel = @selector(nextObject);
|
|
|
|
IMP imp = [e methodForSelector: sel];
|
2000-12-07 22:57:56 +00:00
|
|
|
|
2001-05-22 09:30:07 +00:00
|
|
|
while ((anObject = (*imp)(e, sel)) != nil)
|
2000-12-07 22:57:56 +00:00
|
|
|
{
|
2001-05-22 09:30:07 +00:00
|
|
|
GSIMapNode node;
|
|
|
|
|
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
|
|
|
|
if (node == 0)
|
|
|
|
{
|
|
|
|
GSIMapAddKey(&map, (GSIMapKey)anObject);
|
|
|
|
}
|
2000-12-07 22:57:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
@end
|
2000-12-07 22:57:56 +00:00
|
|
|
|
2001-01-08 23:44:25 +00:00
|
|
|
@interface NSGSet : NSSet
|
|
|
|
@end
|
|
|
|
@implementation NSGSet
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
NSLog(@"Warning - decoding archive containing obsolete %@ object - please delete/replace this archive", NSStringFromClass([self class]));
|
|
|
|
RELEASE(self);
|
|
|
|
self = (id)NSAllocateObject([GSSet class], 0, NSDefaultMallocZone());
|
|
|
|
self = [self initWithCoder: aCoder];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSGMutableSet : NSMutableSet
|
|
|
|
@end
|
|
|
|
@implementation NSGMutableSet
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
NSLog(@"Warning - decoding archive containing obsolete %@ object - please delete/replace this archive", NSStringFromClass([self class]));
|
|
|
|
RELEASE(self);
|
|
|
|
self = (id)NSAllocateObject([GSMutableSet class], 0, NSDefaultMallocZone());
|
|
|
|
self = [self initWithCoder: aCoder];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|