2001-12-17 14:31:42 +00:00
|
|
|
/** Concrete implementation of NSCountedSet based on GNU Set class
|
2000-12-07 22:57:56 +00:00
|
|
|
Copyright (C) 1998,2000 Free Software Foundation, Inc.
|
1995-10-30 01:00:39 +00:00
|
|
|
|
1999-04-12 12:54:14 +00:00
|
|
|
Written by: Richard frith-Macdonald <richard@brainstorm.co.uk>
|
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
|
1999-09-09 02:56:20 +00:00
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
1995-10-30 01:00:39 +00:00
|
|
|
*/
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1998-10-08 13:46:53 +00:00
|
|
|
#include <Foundation/NSSet.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>
|
1999-11-07 14:50:30 +00:00
|
|
|
#include <Foundation/NSDebug.h>
|
1998-10-08 13:46:53 +00:00
|
|
|
|
|
|
|
|
2002-01-24 17:03:04 +00:00
|
|
|
#define GSI_MAP_RETAIN_VAL(M, X)
|
|
|
|
#define GSI_MAP_RELEASE_VAL(M, X)
|
1999-06-21 08:30:26 +00:00
|
|
|
#define GSI_MAP_KTYPES GSUNION_OBJ
|
|
|
|
#define GSI_MAP_VTYPES GSUNION_INT
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1999-06-21 08:30:26 +00:00
|
|
|
#include <base/GSIMap.h>
|
1998-10-08 13:46:53 +00:00
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@interface GSCountedSet : NSCountedSet
|
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-30 01:00:39 +00:00
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@interface GSCountedSetEnumerator : NSEnumerator
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
2000-12-21 10:31:33 +00:00
|
|
|
GSCountedSet *set;
|
|
|
|
GSIMapEnumerator_t enumerator;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@implementation GSCountedSetEnumerator
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-12-07 22:57:56 +00:00
|
|
|
RELEASE(set);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
1995-10-30 01:00:39 +00:00
|
|
|
|
1999-09-28 08:48:27 +00:00
|
|
|
- (id) initWithSet: (NSSet*)d
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
self = [super init];
|
2000-12-21 10:31:33 +00:00
|
|
|
if (self != nil)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
set = RETAIN((GSCountedSet*)d);
|
2000-12-21 10:31:33 +00:00
|
|
|
enumerator = GSIMapEnumeratorForMap(&set->map);
|
1998-10-17 06:47:46 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
return self;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1999-09-28 08:48:27 +00:00
|
|
|
- (id) nextObject
|
1995-10-30 01:00:39 +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-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@implementation GSCountedSet
|
1995-10-30 01:00:39 +00:00
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
if (self == [GSCountedSet class])
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
|
|
|
* Adds an object to the set. If the set already contains an object
|
|
|
|
* equal to the specified object (as determined by the [-isEqual:]
|
|
|
|
* method) then the count for that object is incremented rather
|
|
|
|
* than the new object being added.
|
|
|
|
*/
|
2000-12-07 22:57:56 +00:00
|
|
|
- (void) addObject: (NSObject*)anObject
|
|
|
|
{
|
|
|
|
GSIMapNode node;
|
|
|
|
|
|
|
|
if (anObject == nil)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to nil value to counted set"];
|
|
|
|
}
|
|
|
|
|
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
|
|
|
|
if (node == 0)
|
|
|
|
{
|
|
|
|
GSIMapAddPair(&map,(GSIMapKey)anObject,(GSIMapVal)(unsigned)1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->value.uint++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned) count
|
|
|
|
{
|
|
|
|
return map.nodeCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned) countForObject: (id)anObject
|
|
|
|
{
|
|
|
|
if (anObject)
|
|
|
|
{
|
|
|
|
GSIMapNode node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
|
|
|
|
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
return node->value.uint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
GSIMapEmptyMap(&map);
|
1998-10-27 08:12:49 +00:00
|
|
|
[super dealloc];
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
unsigned count = map.nodeCount;
|
|
|
|
SEL sel1 = @selector(encodeObject:);
|
|
|
|
IMP imp1 = [aCoder methodForSelector: sel1];
|
|
|
|
SEL sel2 = @selector(encodeValueOfObjCType:at:);
|
|
|
|
IMP imp2 = [aCoder methodForSelector: sel2];
|
|
|
|
const char *type = @encode(unsigned);
|
2000-12-21 10:31:33 +00:00
|
|
|
GSIMapEnumerator_t enumerator = GSIMapEnumeratorForMap(&map);
|
|
|
|
GSIMapNode node = GSIMapEnumeratorNextNode(&enumerator);
|
1998-10-27 08:12:49 +00:00
|
|
|
|
|
|
|
(*imp2)(aCoder, sel2, type, &count);
|
|
|
|
|
|
|
|
while (node != 0)
|
|
|
|
{
|
1999-04-12 12:54:14 +00:00
|
|
|
(*imp1)(aCoder, sel1, node->key.obj);
|
|
|
|
(*imp2)(aCoder, sel2, type, &node->value.uint);
|
2000-12-21 10:31:33 +00:00
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
- (unsigned) hash
|
|
|
|
{
|
|
|
|
return map.nodeCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Designated initialiser */
|
|
|
|
- (id) initWithCapacity: (unsigned)cap
|
|
|
|
{
|
|
|
|
GSIMapInitWithZoneAndCapacity(&map, [self zone], cap);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
1998-10-27 08:12:49 +00:00
|
|
|
unsigned count;
|
|
|
|
id value;
|
|
|
|
unsigned valcnt;
|
|
|
|
SEL sel = @selector(decodeValueOfObjCType:at:);
|
|
|
|
IMP imp = [aCoder methodForSelector: sel];
|
|
|
|
const char *utype = @encode(unsigned);
|
|
|
|
const char *otype = @encode(id);
|
|
|
|
|
|
|
|
(*imp)(aCoder, sel, utype, &count);
|
|
|
|
|
1999-06-21 08:30:26 +00:00
|
|
|
GSIMapInitWithZoneAndCapacity(&map, [self zone], count);
|
1998-10-27 08:12:49 +00:00
|
|
|
while (count-- > 0)
|
|
|
|
{
|
|
|
|
(*imp)(aCoder, sel, otype, &value);
|
|
|
|
(*imp)(aCoder, sel, utype, &valcnt);
|
1999-06-21 08:30:26 +00:00
|
|
|
GSIMapAddPairNoRetain(&map, (GSIMapKey)value, (GSIMapVal)valcnt);
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
1998-10-27 08:12:49 +00:00
|
|
|
return self;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithObjects: (id*)objs count: (unsigned)c
|
|
|
|
{
|
2003-01-03 20:14:47 +00:00
|
|
|
unsigned int i;
|
1998-10-08 13:46:53 +00:00
|
|
|
|
2001-01-26 12:09:35 +00:00
|
|
|
self = [self initWithCapacity: c];
|
|
|
|
if (self == nil)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
|
|
|
return nil;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
GSIMapNode node;
|
1998-10-27 08:12:49 +00:00
|
|
|
|
|
|
|
if (objs[i] == nil)
|
|
|
|
{
|
1999-09-28 19:35:09 +00:00
|
|
|
IF_NO_GC(AUTORELEASE(self));
|
1998-10-27 08:12:49 +00:00
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to init counted set with nil value"];
|
1998-10-17 06:47:46 +00:00
|
|
|
}
|
1999-06-21 08:30:26 +00:00
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)objs[i]);
|
1998-10-27 08:12:49 +00:00
|
|
|
if (node == 0)
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
GSIMapAddPair(&map,(GSIMapKey)objs[i],(GSIMapVal)(unsigned)1);
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
else
|
|
|
|
{
|
1999-04-12 12:54:14 +00:00
|
|
|
node->value.uint++;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
return self;
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) member: (id)anObject
|
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
if (anObject != nil)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
GSIMapNode node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
|
1998-10-08 13:46:53 +00:00
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
if (node != 0)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
1999-04-12 12:54:14 +00:00
|
|
|
return node->key.obj;
|
1998-10-17 06:47:46 +00:00
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
1998-10-27 08:12:49 +00:00
|
|
|
return nil;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSEnumerator*) objectEnumerator
|
|
|
|
{
|
2000-12-07 22:57:56 +00:00
|
|
|
return AUTORELEASE([[GSCountedSetEnumerator allocWithZone:
|
1999-07-03 19:59:44 +00:00
|
|
|
NSDefaultMallocZone()] initWithSet: self]);
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
2002-05-28 11:30:15 +00:00
|
|
|
/**
|
|
|
|
* Removes all objcts which have not been added more than level times
|
|
|
|
* from the counted set.<br />
|
|
|
|
* Note to GNUstep maintainers ... this method depends on the characteristic
|
|
|
|
* of the GSIMap enumeration that, once enumerated, an object can be removed
|
|
|
|
* from the map. If GSIMap ever loses that characterstic, this will break.
|
|
|
|
*/
|
2000-04-18 09:02:38 +00:00
|
|
|
- (void) purge: (int)level
|
|
|
|
{
|
|
|
|
if (level > 0)
|
|
|
|
{
|
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-04-18 09:02:38 +00:00
|
|
|
|
|
|
|
while (node != 0)
|
|
|
|
{
|
2003-01-03 20:14:47 +00:00
|
|
|
if (node->value.uint <= (unsigned int)level)
|
2000-04-18 09:02:38 +00:00
|
|
|
{
|
|
|
|
GSIMapRemoveNodeFromMap(&map, bucket, node);
|
|
|
|
GSIMapFreeNode(&map, node);
|
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
bucket = GSIMapEnumeratorBucket(&enumerator);
|
|
|
|
node = GSIMapEnumeratorNextNode(&enumerator);
|
2000-04-18 09:02:38 +00:00
|
|
|
}
|
2002-05-28 11:30:15 +00:00
|
|
|
GSIMapEndEnumerator(&enumerator);
|
2000-04-18 09:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
- (void) removeAllObjects
|
|
|
|
{
|
|
|
|
GSIMapCleanMap(&map);
|
|
|
|
}
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
|
|
|
* Decrements the count of the number of times that the specified
|
2002-08-20 15:07:58 +00:00
|
|
|
* object (or an object equal to it as determined by the
|
2002-08-20 10:22:05 +00:00
|
|
|
* [-isEqual:] method) has been added to the set. If the count
|
|
|
|
* becomes zero, the object is removed from the set.
|
|
|
|
*/
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) removeObject: (NSObject*)anObject
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1999-11-04 10:42:20 +00:00
|
|
|
GSIMapBucket bucket;
|
|
|
|
|
|
|
|
if (anObject == nil)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
2001-05-22 09:30:07 +00:00
|
|
|
NSWarnMLog(@"attempt to remove nil object");
|
1999-11-04 10:42:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bucket = GSIMapBucketForKey(&map, (GSIMapKey)anObject);
|
|
|
|
if (bucket != 0)
|
|
|
|
{
|
|
|
|
GSIMapNode node;
|
1998-10-27 08:12:49 +00:00
|
|
|
|
2002-01-24 17:03:04 +00:00
|
|
|
node = GSIMapNodeForKeyInBucket(&map, bucket, (GSIMapKey)anObject);
|
1999-11-04 10:42:20 +00:00
|
|
|
if (node != 0)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
1999-11-04 10:42:20 +00:00
|
|
|
if (--node->value.uint == 0)
|
1998-10-27 08:12:49 +00:00
|
|
|
{
|
1999-11-04 10:42:20 +00:00
|
|
|
GSIMapRemoveNodeFromMap(&map, bucket, node);
|
|
|
|
GSIMapFreeNode(&map, node);
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
2000-04-18 09:02:38 +00:00
|
|
|
- (id) unique: (id)anObject
|
|
|
|
{
|
|
|
|
GSIMapNode node;
|
|
|
|
id result;
|
|
|
|
|
|
|
|
if (anObject == nil)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"Tried to unique nil value in counted set"];
|
|
|
|
}
|
|
|
|
|
|
|
|
node = GSIMapNodeForKey(&map, (GSIMapKey)anObject);
|
|
|
|
if (node == 0)
|
|
|
|
{
|
|
|
|
result = anObject;
|
2000-04-19 12:29:17 +00:00
|
|
|
GSIMapAddPair(&map,(GSIMapKey)anObject,(GSIMapVal)(unsigned)1);
|
2000-04-18 09:02:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = node->key.obj;
|
|
|
|
node->value.uint++;
|
|
|
|
#if !GS_WITH_GC
|
|
|
|
if (result != anObject)
|
|
|
|
{
|
|
|
|
[anObject release];
|
|
|
|
[result retain];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
1995-10-30 01:00:39 +00:00
|
|
|
@end
|