1995-10-30 01:00:39 +00:00
|
|
|
/* NSCountedSet - CountedSet object
|
1996-03-18 13:55:09 +00:00
|
|
|
Copyright (C) 1995, 1996 Free Software Foundation, Inc.
|
1995-10-30 01:00:39 +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
|
|
|
|
|
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 <gnustep/base/behavior.h>
|
1995-10-30 01:00:39 +00:00
|
|
|
#include <Foundation/NSSet.h>
|
|
|
|
#include <Foundation/NSGSet.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSUtilities.h>
|
1998-10-08 13:46:53 +00:00
|
|
|
#include <Foundation/NSString.h>
|
1995-10-30 01:00:39 +00:00
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
@class NSSetNonCore;
|
|
|
|
@class NSMutableSetNonCore;
|
|
|
|
|
1995-10-30 01:00:39 +00:00
|
|
|
@implementation NSCountedSet
|
|
|
|
|
|
|
|
static Class NSCountedSet_concrete_class;
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSCountedSet class]) {
|
|
|
|
NSCountedSet_concrete_class = [NSGCountedSet class];
|
|
|
|
behavior_class_add_class(self, [NSMutableSetNonCore class]);
|
|
|
|
behavior_class_add_class(self, [NSSetNonCore class]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-10-30 01:00:39 +00:00
|
|
|
+ (void) _CountedSetConcreteClass: (Class)c
|
|
|
|
{
|
|
|
|
NSCountedSet_concrete_class = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (Class) _concreteClass
|
|
|
|
{
|
|
|
|
return NSCountedSet_concrete_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ allocWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
return NSAllocateObject([self _concreteClass], 0, z);
|
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (unsigned int) countForObject: anObject
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
return 0;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- copyWithZone: (NSZone*)z
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
NSSet *newSet;
|
|
|
|
int count = [self count];
|
|
|
|
id objects[count];
|
|
|
|
id enumerator = [self objectEnumerator];
|
|
|
|
id o;
|
|
|
|
int i;
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
for (i = 0; (o = [enumerator nextObject]); i++)
|
|
|
|
objects[i] = [o copyWithZone:z];
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
newSet = [[[self class] allocWithZone: z] initWithObjects: objects
|
|
|
|
count: count];
|
1998-10-08 13:46:53 +00:00
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
for (i = 0; (o = [enumerator nextObject]); i++) {
|
|
|
|
unsigned extra;
|
|
|
|
|
|
|
|
extra = [self countForObject: o];
|
|
|
|
|
|
|
|
if (extra > 1) {
|
|
|
|
while (--extra) {
|
|
|
|
[newSet addObject: o];
|
|
|
|
}
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
[o release];
|
1998-10-08 13:46:53 +00:00
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
|
|
|
|
return newSet;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- mutableCopyWithZone: (NSZone*)z
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
return [self copyWithZone: z];
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- initWithCoder: aCoder
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
|
|
|
[self subclassResponsibility:_cmd];
|
1998-10-08 13:46:53 +00:00
|
|
|
return nil;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- (void) encodeWithCoder: aCoder
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
|
|
|
[self subclassResponsibility:_cmd];
|
|
|
|
}
|
|
|
|
|
1998-10-08 13:46:53 +00:00
|
|
|
- initWithSet: (NSSet*)other copyItems: (BOOL)flag
|
1995-10-30 01:00:39 +00:00
|
|
|
{
|
1998-10-08 13:46:53 +00:00
|
|
|
int c = [other count];
|
|
|
|
id os[c], o, e = [other objectEnumerator];
|
|
|
|
int i = 0;
|
1995-10-30 01:00:39 +00:00
|
|
|
|
|
|
|
while ((o = [e nextObject]))
|
1998-10-08 13:46:53 +00:00
|
|
|
{
|
|
|
|
if (flag)
|
|
|
|
os[i] = [o copy];
|
|
|
|
else
|
|
|
|
os[i] = o;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
self = [self initWithObjects:os count:c];
|
|
|
|
if ([other isKindOfClass: [NSCountedSet class]])
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
{
|
|
|
|
unsigned extra = [(NSCountedSet*)other countForObject: os[j]];
|
|
|
|
|
|
|
|
if (extra > 1)
|
|
|
|
while (--extra)
|
|
|
|
[self addObject: os[j]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flag)
|
|
|
|
while (--i)
|
|
|
|
[os[i] release];
|
|
|
|
return self;
|
1995-10-30 01:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|