2005-02-22 11:22:44 +00:00
|
|
|
/** NSCountedSet - CountedSet object
|
1996-03-18 13:55:09 +00:00
|
|
|
Copyright (C) 1995, 1996 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>
|
1995-10-30 01:00:39 +00:00
|
|
|
Created: Sep 1995
|
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-30 01:00:39 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1995-10-30 01:00:39 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2005-02-22 11:22:44 +00:00
|
|
|
|
1995-10-30 01:00:39 +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
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1995-10-30 01:00:39 +00:00
|
|
|
License along with this library; if not, write to the Free
|
2006-10-20 10:56:27 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSCountedSet class reference</title>
|
|
|
|
$Date$ $Revision$
|
1995-10-30 01:00:39 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2007-11-29 20:53:26 +00:00
|
|
|
#import "GNUstepBase/GSLock.h"
|
|
|
|
#import "Foundation/NSEnumerator.h"
|
|
|
|
#import "Foundation/NSSet.h"
|
|
|
|
#import "Foundation/NSCoder.h"
|
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
#import "Foundation/NSLock.h"
|
|
|
|
#import "Foundation/NSNotification.h"
|
|
|
|
#import "Foundation/NSThread.h"
|
1995-10-30 01:00:39 +00:00
|
|
|
|
2000-12-07 22:57:56 +00:00
|
|
|
@class GSCountedSet;
|
2005-07-08 11:48:37 +00:00
|
|
|
@interface GSCountedSet : NSObject // Help the compiler
|
|
|
|
@end
|
1998-10-08 13:46:53 +00:00
|
|
|
|
2000-04-18 09:02:38 +00:00
|
|
|
/*
|
|
|
|
* Class variables for uniquing objects;
|
|
|
|
*/
|
2018-04-10 13:59:35 +00:00
|
|
|
static NSRecursiveLock *uniqueLock = nil;
|
2000-04-18 09:02:38 +00:00
|
|
|
static NSCountedSet *uniqueSet = nil;
|
|
|
|
static IMP uniqueImp = 0;
|
|
|
|
static IMP lockImp = 0;
|
|
|
|
static IMP unlockImp = 0;
|
|
|
|
static BOOL uniquing = NO;
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
|
|
|
* <p>
|
2004-06-22 22:40:40 +00:00
|
|
|
* The <code>NSCountedSet</code> class is used to maintain a set of objects
|
|
|
|
* where the number of times each object has been added (without a
|
2002-08-20 10:22:05 +00:00
|
|
|
* corresponding removal) is kept track of.
|
|
|
|
* </p>
|
|
|
|
* <p>
|
2004-06-22 22:40:40 +00:00
|
|
|
* In GNUstep, the <code>purge</code> and <code>unique</code> methods
|
|
|
|
* are provided to make use of a counted set for <em>uniquing</em> objects
|
|
|
|
* easier.
|
2002-08-20 10:22:05 +00:00
|
|
|
* </p>
|
|
|
|
*/
|
2005-02-22 11:22:44 +00:00
|
|
|
@implementation NSCountedSet
|
1995-10-30 01:00:39 +00:00
|
|
|
|
1999-09-28 08:48:27 +00:00
|
|
|
static Class NSCountedSet_abstract_class;
|
1995-10-30 01:00:39 +00:00
|
|
|
static Class NSCountedSet_concrete_class;
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
1999-09-28 08:48:27 +00:00
|
|
|
if (self == [NSCountedSet class])
|
|
|
|
{
|
|
|
|
NSCountedSet_abstract_class = self;
|
2000-12-07 22:57:56 +00:00
|
|
|
NSCountedSet_concrete_class = [GSCountedSet class];
|
2018-04-10 13:59:35 +00:00
|
|
|
uniqueLock = [NSRecursiveLock new];
|
2013-08-22 15:44:54 +00:00
|
|
|
[[NSObject leakAt: &uniqueLock] release];
|
2004-02-08 09:42:38 +00:00
|
|
|
lockImp = [uniqueLock methodForSelector: @selector(lock)];
|
|
|
|
unlockImp = [uniqueLock methodForSelector: @selector(unlock)];
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-09-28 08:48:27 +00:00
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1999-09-28 08:48:27 +00:00
|
|
|
if (self == NSCountedSet_abstract_class)
|
2000-08-07 22:00:31 +00:00
|
|
|
{
|
|
|
|
return NSAllocateObject(NSCountedSet_concrete_class, 0, z);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NSAllocateObject(self, 0, z);
|
|
|
|
}
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 08:03:05 +00:00
|
|
|
- (NSUInteger) _countForObject: (id)anObject
|
|
|
|
{
|
|
|
|
return [self countForObject: anObject];
|
|
|
|
}
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of times that an object that is equal to the
|
2004-06-22 22:40:40 +00:00
|
|
|
* specified object (as determined by the [-isEqual:] method) has
|
2002-08-20 10:22:05 +00:00
|
|
|
* been added to the set and not removed from it.
|
|
|
|
*/
|
2009-02-23 20:42:32 +00:00
|
|
|
- (NSUInteger) countForObject: (id)anObject
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1999-04-12 12:53:30 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1998-10-08 13:46:53 +00:00
|
|
|
return 0;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1999-09-28 08:48:27 +00:00
|
|
|
- (id) copyWithZone: (NSZone*)z
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1999-04-12 12:53:30 +00:00
|
|
|
return [[[self class] allocWithZone: z] initWithSet: self copyItems: YES];
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1999-09-28 08:48:27 +00:00
|
|
|
- (id) mutableCopyWithZone: (NSZone*)z
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1999-04-12 12:53:30 +00:00
|
|
|
return [[[self class] allocWithZone: z] initWithSet: self copyItems: NO];
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1999-09-28 08:48:27 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
unsigned count;
|
2010-02-22 10:13:20 +00:00
|
|
|
Class c = object_getClass(self);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
|
|
|
if (c == NSCountedSet_abstract_class)
|
|
|
|
{
|
2010-02-25 18:49:31 +00:00
|
|
|
DESTROY(self);
|
2000-08-07 22:00:31 +00:00
|
|
|
self = [NSCountedSet_concrete_class allocWithZone: NSDefaultMallocZone()];
|
|
|
|
return [self initWithCoder: aCoder];
|
|
|
|
}
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(unsigned) at: &count];
|
|
|
|
{
|
|
|
|
id objs[count];
|
|
|
|
unsigned refs[count];
|
|
|
|
unsigned i;
|
|
|
|
IMP addImp = [self methodForSelector: @selector(addObject:)];
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(id) at: &objs[i]];
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(unsigned) at: &refs[i]];
|
|
|
|
}
|
|
|
|
self = [self initWithObjects: objs count: count];
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
unsigned j = refs[i];
|
|
|
|
|
|
|
|
while (j-- > 1)
|
|
|
|
{
|
|
|
|
(*addImp)(self, @selector(addObject:), objs[i]);
|
|
|
|
}
|
2004-02-11 16:51:00 +00:00
|
|
|
RELEASE(objs[i]);
|
2000-08-07 22:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Class) classForCoder
|
|
|
|
{
|
|
|
|
return NSCountedSet_abstract_class;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1999-09-28 08:48:27 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
unsigned count = [self count];
|
|
|
|
NSEnumerator *e = [self objectEnumerator];
|
|
|
|
id o;
|
|
|
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &count];
|
|
|
|
while ((o = [e nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(id) at: &o];
|
|
|
|
count = [self countForObject: o];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned) at: &count];
|
|
|
|
}
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1999-09-28 08:48:27 +00:00
|
|
|
- (id) initWithSet: (NSSet*)other copyItems: (BOOL)flag
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1999-04-12 12:53:30 +00:00
|
|
|
unsigned c = [other count];
|
|
|
|
id os[c], o, e = [other objectEnumerator];
|
|
|
|
unsigned i = 0;
|
|
|
|
NSZone *z = [self zone];
|
1999-09-28 08:48:27 +00:00
|
|
|
IMP next = [e methodForSelector: @selector(nextObject)];
|
1995-10-30 01:00:39 +00:00
|
|
|
|
1999-09-28 08:48:27 +00:00
|
|
|
while ((o = (*next)(e, @selector(nextObject))) != nil)
|
1998-10-08 13:46:53 +00:00
|
|
|
{
|
|
|
|
if (flag)
|
1999-04-12 12:53:30 +00:00
|
|
|
os[i] = [o copyWithZone: z];
|
1998-10-08 13:46:53 +00:00
|
|
|
else
|
|
|
|
os[i] = o;
|
|
|
|
i++;
|
|
|
|
}
|
1999-04-12 12:53:30 +00:00
|
|
|
self = [self initWithObjects: os count: c];
|
1999-09-28 08:48:27 +00:00
|
|
|
if ([other isKindOfClass: NSCountedSet_abstract_class])
|
1998-10-08 13:46:53 +00:00
|
|
|
{
|
1999-04-12 12:53:30 +00:00
|
|
|
unsigned j;
|
1999-09-28 08:48:27 +00:00
|
|
|
IMP addImp = [self methodForSelector: @selector(addObject:)];
|
1998-10-08 13:46:53 +00:00
|
|
|
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
{
|
|
|
|
unsigned extra = [(NSCountedSet*)other countForObject: os[j]];
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
while (extra-- > 1)
|
|
|
|
(*addImp)(self, @selector(addObject:), os[j]);
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flag)
|
1999-04-12 12:53:30 +00:00
|
|
|
while (i--)
|
1998-10-08 13:46:53 +00:00
|
|
|
[os[i] release];
|
|
|
|
return self;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
- (void) purge: (NSInteger)level
|
2000-04-18 09:02:38 +00:00
|
|
|
{
|
|
|
|
if (level > 0)
|
|
|
|
{
|
|
|
|
NSEnumerator *enumerator = [self objectEnumerator];
|
|
|
|
|
|
|
|
if (enumerator != nil)
|
|
|
|
{
|
|
|
|
id obj;
|
|
|
|
id (*nImp)(NSEnumerator*, SEL);
|
|
|
|
unsigned (*cImp)(NSCountedSet*, SEL, id);
|
|
|
|
void (*rImp)(NSCountedSet*, SEL, id);
|
|
|
|
|
|
|
|
nImp = (id (*)(NSEnumerator*, SEL))
|
|
|
|
[enumerator methodForSelector: @selector(nextObject)];
|
|
|
|
cImp = (unsigned (*)(NSCountedSet*, SEL, id))
|
|
|
|
[self methodForSelector: @selector(countForObject:)];
|
|
|
|
rImp = (void (*)(NSCountedSet*, SEL, id))
|
|
|
|
[self methodForSelector: @selector(removeObject:)];
|
|
|
|
while ((obj = (*nImp)(enumerator, @selector(nextObject))) != nil)
|
|
|
|
{
|
|
|
|
unsigned c = (*cImp)(self, @selector(countForObject:), obj);
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
if (c <= (NSUInteger)level)
|
2000-04-18 09:02:38 +00:00
|
|
|
{
|
|
|
|
while (c-- > 0)
|
|
|
|
{
|
|
|
|
(*rImp)(self, @selector(removeObject:), obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) unique: (id)anObject
|
|
|
|
{
|
|
|
|
id o = [self member: anObject];
|
|
|
|
|
|
|
|
[self addObject: anObject];
|
2000-04-19 12:29:17 +00:00
|
|
|
if (o == nil)
|
|
|
|
{
|
|
|
|
o = anObject;
|
|
|
|
}
|
2000-04-18 09:02:38 +00:00
|
|
|
if (o != anObject)
|
|
|
|
{
|
|
|
|
[anObject release];
|
|
|
|
[o retain];
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
2004-06-22 22:40:40 +00:00
|
|
|
* This function purges the global [NSCountedSet] object used for
|
2002-08-20 10:22:05 +00:00
|
|
|
* uniquing. It handles locking as necessary. It can be used to
|
|
|
|
* purge the set even when uniquing is turned off.
|
|
|
|
*/
|
2000-04-18 09:02:38 +00:00
|
|
|
void
|
2009-02-23 20:42:32 +00:00
|
|
|
GSUPurge(NSUInteger count)
|
2000-04-18 09:02:38 +00:00
|
|
|
{
|
|
|
|
if (uniqueLock != nil)
|
|
|
|
{
|
|
|
|
(*lockImp)(uniqueLock, @selector(lock));
|
|
|
|
}
|
2002-10-05 17:47:54 +00:00
|
|
|
[uniqueSet purge: count];
|
2000-04-18 09:02:38 +00:00
|
|
|
if (uniqueLock != nil)
|
|
|
|
{
|
|
|
|
(*unlockImp)(uniqueLock, @selector(unlock));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
|
|
|
* This function sets the count for the specified object. If the
|
|
|
|
* count for the object is set to zero then the object is removed
|
|
|
|
* from the global uniquing set. The object is added to the set
|
|
|
|
* if necessary. The object returned is the one stored in the set.
|
|
|
|
* The function handles locking as necessary. It can be used to
|
|
|
|
* alter the set even when uniquing is turned off.
|
|
|
|
*/
|
2000-04-18 09:37:06 +00:00
|
|
|
id
|
2009-02-23 20:42:32 +00:00
|
|
|
GSUSet(id anObject, NSUInteger count)
|
2000-04-18 09:27:06 +00:00
|
|
|
{
|
|
|
|
id found;
|
2009-02-23 20:42:32 +00:00
|
|
|
NSUInteger i;
|
2000-04-18 09:27:06 +00:00
|
|
|
|
|
|
|
if (uniqueLock != nil)
|
|
|
|
{
|
|
|
|
(*lockImp)(uniqueLock, @selector(lock));
|
|
|
|
}
|
2002-10-05 17:47:54 +00:00
|
|
|
found = [uniqueSet member: anObject];
|
2000-04-18 09:27:06 +00:00
|
|
|
if (found == nil)
|
|
|
|
{
|
2002-10-05 17:47:54 +00:00
|
|
|
found = anObject;
|
|
|
|
for (i = 0; i < count; i++)
|
2000-04-18 09:27:06 +00:00
|
|
|
{
|
2002-10-05 17:47:54 +00:00
|
|
|
[uniqueSet addObject: anObject];
|
2000-04-18 09:27:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-04-18 09:37:06 +00:00
|
|
|
i = [uniqueSet countForObject: found];
|
2002-10-05 17:47:54 +00:00
|
|
|
if (i < count)
|
2000-04-18 09:27:06 +00:00
|
|
|
{
|
2002-10-05 17:47:54 +00:00
|
|
|
while (i < count)
|
2000-04-18 09:27:06 +00:00
|
|
|
{
|
2000-04-18 09:37:06 +00:00
|
|
|
[uniqueSet addObject: found];
|
2000-04-18 09:27:06 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2002-10-05 17:47:54 +00:00
|
|
|
else if (i > count)
|
2000-04-18 09:27:06 +00:00
|
|
|
{
|
2002-10-05 17:47:54 +00:00
|
|
|
while (i > count)
|
2000-04-18 09:27:06 +00:00
|
|
|
{
|
2000-04-18 09:37:06 +00:00
|
|
|
[uniqueSet removeObject: found];
|
2000-04-18 09:27:06 +00:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uniqueLock != nil)
|
|
|
|
{
|
|
|
|
(*unlockImp)(uniqueLock, @selector(unlock));
|
|
|
|
}
|
2000-04-18 09:37:06 +00:00
|
|
|
return found;
|
2000-04-18 09:27:06 +00:00
|
|
|
}
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
|
|
|
* This function <em>uniques</em> the supplied argument, returning
|
|
|
|
* the result. It works by using the [-unique:] method of a global
|
|
|
|
* NSCountedSet object. It handles locking as necessary.
|
|
|
|
* If uniquing is turned off, it simply returns its argument.
|
|
|
|
*/
|
2000-04-18 09:02:38 +00:00
|
|
|
id
|
2002-10-05 17:47:54 +00:00
|
|
|
GSUnique(id anObject)
|
2000-04-18 09:02:38 +00:00
|
|
|
{
|
|
|
|
if (uniquing == YES)
|
|
|
|
{
|
|
|
|
if (uniqueLock != nil)
|
|
|
|
{
|
|
|
|
(*lockImp)(uniqueLock, @selector(lock));
|
|
|
|
}
|
2002-10-05 17:47:54 +00:00
|
|
|
anObject = (*uniqueImp)(uniqueSet, @selector(unique:), anObject);
|
2000-04-18 09:02:38 +00:00
|
|
|
if (uniqueLock != nil)
|
|
|
|
{
|
|
|
|
(*unlockImp)(uniqueLock, @selector(unlock));
|
|
|
|
}
|
|
|
|
}
|
2002-10-05 17:47:54 +00:00
|
|
|
return anObject;
|
2000-04-18 09:02:38 +00:00
|
|
|
}
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
|
|
|
* This function sets the state of a flag that determines the
|
|
|
|
* behavior of the GSUnique() function. If the flag is on,
|
|
|
|
* uniquing is performed, if it is off the function has no effect.
|
|
|
|
* The default is for uniquing to be turned off.
|
|
|
|
*/
|
2000-04-18 09:02:38 +00:00
|
|
|
void
|
|
|
|
GSUniquing(BOOL flag)
|
|
|
|
{
|
|
|
|
if (uniqueSet == nil)
|
|
|
|
{
|
|
|
|
uniqueSet = [NSCountedSet new];
|
|
|
|
uniqueImp = [uniqueSet methodForSelector: @selector(unique:)];
|
|
|
|
}
|
|
|
|
uniquing = flag;
|
|
|
|
}
|
|
|
|
|