2019-05-18 00:37:19 +00:00
|
|
|
|
|
|
|
/** Interface for NSOrderedSet, NSMutableOrderedSet for GNUStep
|
|
|
|
Copyright (C) 2019 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Gregory John Casamento <greg.casamento@gmail.com>
|
|
|
|
Created: May 17 2019
|
|
|
|
|
|
|
|
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 Lesser 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 Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
#import "common.h"
|
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
#import "Foundation/NSAutoreleasePool.h"
|
|
|
|
#import "Foundation/NSOrderedSet.h"
|
|
|
|
#import "Foundation/NSCoder.h"
|
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
#import "Foundation/NSEnumerator.h"
|
|
|
|
#import "Foundation/NSKeyValueCoding.h"
|
|
|
|
#import "Foundation/NSValue.h"
|
|
|
|
#import "Foundation/NSException.h"
|
2019-05-30 11:46:54 +00:00
|
|
|
#import "Foundation/NSPredicate.h"
|
2019-05-20 08:01:20 +00:00
|
|
|
|
|
|
|
// #import "GNUstepBase/GNUstep.h"
|
2019-05-18 00:37:19 +00:00
|
|
|
// For private method _decodeArrayOfObjectsForKey:
|
2019-05-20 08:01:20 +00:00
|
|
|
|
2019-05-18 00:37:19 +00:00
|
|
|
#import "Foundation/NSKeyedArchiver.h"
|
|
|
|
#import "GSPrivate.h"
|
|
|
|
#import "GSFastEnumeration.h"
|
|
|
|
#import "GSDispatch.h"
|
|
|
|
|
2019-05-18 07:56:11 +00:00
|
|
|
@class GSOrderedSet;
|
|
|
|
@interface GSOrderedSet : NSObject // Help the compiler
|
|
|
|
@end
|
|
|
|
@class GSMutableOrderedSet;
|
|
|
|
@interface GSMutableOrderedSet : NSObject // Help the compiler
|
|
|
|
@end
|
|
|
|
|
2019-05-18 00:37:19 +00:00
|
|
|
@implementation NSOrderedSet
|
|
|
|
|
2019-05-18 07:56:11 +00:00
|
|
|
static Class NSOrderedSet_abstract_class;
|
|
|
|
static Class NSMutableOrderedSet_abstract_class;
|
|
|
|
static Class NSOrderedSet_concrete_class;
|
|
|
|
static Class NSMutableOrderedSet_concrete_class;
|
|
|
|
|
2019-05-28 08:49:59 +00:00
|
|
|
static SEL addSel;
|
|
|
|
static SEL appSel;
|
|
|
|
static SEL countSel;
|
|
|
|
static SEL eqSel;
|
|
|
|
static SEL oaiSel;
|
|
|
|
static SEL remSel;
|
|
|
|
static SEL rlSel;
|
|
|
|
|
2019-05-18 07:56:11 +00:00
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
if (self == NSOrderedSet_abstract_class)
|
|
|
|
{
|
|
|
|
return NSAllocateObject(NSOrderedSet_concrete_class, 0, z);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NSAllocateObject(self, 0, z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSOrderedSet class])
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
[self setVersion: 1];
|
|
|
|
|
|
|
|
addSel = @selector(addObject:);
|
|
|
|
appSel = @selector(appendString:);
|
|
|
|
countSel = @selector(count);
|
|
|
|
eqSel = @selector(isEqual:);
|
|
|
|
oaiSel = @selector(objectAtIndex:);
|
|
|
|
remSel = @selector(removeObjectAtIndex:);
|
|
|
|
rlSel = @selector(removeLastObject);
|
|
|
|
|
2019-05-18 07:56:11 +00:00
|
|
|
NSOrderedSet_abstract_class = self;
|
|
|
|
NSOrderedSet_concrete_class = [GSOrderedSet class];
|
|
|
|
[NSMutableSet class];
|
2019-05-30 13:03:34 +00:00
|
|
|
[self registerAtExit];
|
2019-05-18 07:56:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
- (Class) classForCoder
|
|
|
|
{
|
|
|
|
return NSOrderedSet_abstract_class;
|
|
|
|
}
|
|
|
|
|
2019-05-18 00:37:19 +00:00
|
|
|
// NSCoding
|
|
|
|
- (instancetype) initWithCoder: (NSCoder *)coder
|
|
|
|
{
|
2019-05-18 07:56:11 +00:00
|
|
|
Class c;
|
|
|
|
|
|
|
|
c = object_getClass(self);
|
|
|
|
if (c == NSOrderedSet_abstract_class)
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
self = [NSOrderedSet_concrete_class allocWithZone: NSDefaultMallocZone()];
|
|
|
|
return [self initWithCoder: coder];
|
|
|
|
}
|
|
|
|
else if (c == NSOrderedSet_abstract_class)
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
self = [NSOrderedSet_concrete_class allocWithZone: NSDefaultMallocZone()];
|
|
|
|
return [self initWithCoder: coder];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([coder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
id array;
|
|
|
|
|
|
|
|
array = [(NSKeyedUnarchiver*)coder _decodeArrayOfObjectsForKey:
|
|
|
|
@"NS.objects"];
|
|
|
|
if (array == nil)
|
|
|
|
{
|
|
|
|
unsigned i = 0;
|
|
|
|
NSString *key;
|
|
|
|
id val;
|
|
|
|
|
|
|
|
array = [NSMutableArray arrayWithCapacity: 2];
|
|
|
|
key = [NSString stringWithFormat: @"NS.object.%u", i];
|
|
|
|
val = [(NSKeyedUnarchiver*)coder decodeObjectForKey: key];
|
|
|
|
|
|
|
|
while (val != nil)
|
|
|
|
{
|
|
|
|
[array addObject: val];
|
|
|
|
i++;
|
|
|
|
key = [NSString stringWithFormat: @"NS.object.%u", i];
|
|
|
|
val = [(NSKeyedUnarchiver*)coder decodeObjectForKey: key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self = [self initWithArray: array];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned count;
|
|
|
|
|
|
|
|
[coder decodeValueOfObjCType: @encode(unsigned) at: &count];
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
GS_BEGINIDBUF(objs, count);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
[coder decodeValueOfObjCType: @encode(id) at: &objs[i]];
|
|
|
|
}
|
|
|
|
self = [self initWithObjects: objs count: count];
|
|
|
|
while (count-- > 0)
|
|
|
|
{
|
|
|
|
[objs[count] release];
|
|
|
|
}
|
|
|
|
GS_ENDIDBUF();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 07:56:11 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder *)aCoder
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-18 07:56:11 +00:00
|
|
|
if ([aCoder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
NSMutableArray *array = [NSMutableArray array];
|
|
|
|
NSEnumerator *en = [self objectEnumerator];
|
|
|
|
id obj = nil;
|
|
|
|
/* HACK ... MacOS-X seems to code differently if the coder is an
|
|
|
|
* actual instance of NSKeyedArchiver
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Collect all objects...
|
|
|
|
while((obj = [en nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[array addObject: obj];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([aCoder class] == [NSKeyedArchiver class])
|
|
|
|
{
|
|
|
|
[(NSKeyedArchiver*)aCoder _encodeArrayOfObjects: array
|
|
|
|
forKey: @"NS.objects"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned i = 0;
|
|
|
|
NSEnumerator *e = [self objectEnumerator];
|
|
|
|
id o;
|
|
|
|
|
|
|
|
while ((o = [e nextObject]) != nil)
|
|
|
|
{
|
|
|
|
NSString *key;
|
|
|
|
|
|
|
|
key = [NSString stringWithFormat: @"NS.object.%u", i++];
|
|
|
|
[(NSKeyedArchiver*)aCoder encodeObject: o forKey: key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)zone
|
|
|
|
{
|
2019-05-30 11:46:54 +00:00
|
|
|
NSOrderedSet *copy = [NSOrderedSet_concrete_class allocWithZone: zone];
|
|
|
|
|
|
|
|
return [copy initWithOrderedSet: self copyItems: YES];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)zone
|
|
|
|
{
|
2019-05-30 11:46:54 +00:00
|
|
|
NSMutableOrderedSet *copy = [NSMutableOrderedSet_concrete_class allocWithZone: zone];
|
|
|
|
|
|
|
|
return [copy initWithOrderedSet: self copyItems: NO];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NSFastEnumeration
|
|
|
|
- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState *)state
|
|
|
|
objects: (__unsafe_unretained id[])stackbuf
|
|
|
|
count: (NSUInteger)len
|
|
|
|
{
|
2019-05-30 13:56:19 +00:00
|
|
|
NSInteger count;
|
|
|
|
|
|
|
|
/* In a mutable subclass, the mutationsPtr should be set to point to a
|
|
|
|
* value (unsigned long) which will be changed (incremented) whenever
|
|
|
|
* the container is mutated (content added, removed, re-ordered).
|
|
|
|
* This is cached in the caller at the start and compared at each
|
|
|
|
* iteration. If it changes during the iteration then
|
|
|
|
* objc_enumerationMutation() will be called, throwing an exception.
|
|
|
|
* The abstract base class implementation points to a fixed value
|
|
|
|
* (the enumeration state pointer should exist and be unchanged for as
|
|
|
|
* long as the enumeration process runs), which is fine for enumerating
|
|
|
|
* an immutable array.
|
|
|
|
*/
|
|
|
|
state->mutationsPtr = (unsigned long *)&state->mutationsPtr;
|
|
|
|
count = MIN(len, [self count] - state->state);
|
|
|
|
/* If a mutation has occurred then it's possible that we are being asked to
|
|
|
|
* get objects from after the end of the array. Don't pass negative values
|
|
|
|
* to memcpy.
|
|
|
|
*/
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
IMP imp = [self methodForSelector: @selector(objectAtIndex:)];
|
|
|
|
int p = state->state;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++, p++)
|
|
|
|
{
|
|
|
|
stackbuf[i] = (*imp)(self, @selector(objectAtIndex:), p);
|
|
|
|
}
|
|
|
|
state->state += count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
state->itemsPtr = stackbuf;
|
|
|
|
return count;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// class methods
|
|
|
|
+ (instancetype) orderedSet
|
|
|
|
{
|
2019-05-18 07:56:11 +00:00
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] init]);
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype) orderedSetWithArray:(NSArray *)objects
|
|
|
|
{
|
2019-05-20 08:01:20 +00:00
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithArray: objects]);
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype) orderedSetWithArray:(NSArray *)objects
|
2019-05-20 08:01:20 +00:00
|
|
|
range:(NSRange)range
|
2019-05-18 00:37:19 +00:00
|
|
|
copyItems:(BOOL)flag
|
|
|
|
{
|
2019-05-20 08:01:20 +00:00
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithArray: objects
|
|
|
|
range: range
|
|
|
|
copyItems: flag]);
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
+ (instancetype) orderedSetWithObject:(id)anObject
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-20 08:01:20 +00:00
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithObject: anObject]);
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
+ (instancetype) orderedSetWithObjects:(id)firstObject, ...
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-20 08:01:20 +00:00
|
|
|
id set;
|
|
|
|
GS_USEIDLIST(firstObject,
|
|
|
|
set = [[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithObjects: __objects count: __count]);
|
|
|
|
return AUTORELEASE(set);
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
+ (instancetype) orderedSetWithObjects:(const id [])objects
|
2019-05-18 00:37:19 +00:00
|
|
|
count:(NSUInteger) count
|
|
|
|
{
|
2019-05-27 10:05:19 +00:00
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithObjects: objects count: count]);
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
+ (instancetype) orderedSetWithOrderedSet:(NSOrderedSet *)aSet
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 10:05:19 +00:00
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
2019-05-27 11:40:18 +00:00
|
|
|
initWithOrderedSet: aSet]);
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
+ (instancetype) orderedSetWithSet:(NSSet *)aSet
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 10:05:19 +00:00
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithSet: aSet]);
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
+ (instancetype) orderedSetWithSet:(NSSet *)aSet
|
2019-05-18 00:37:19 +00:00
|
|
|
copyItems:(BOOL)flag
|
|
|
|
{
|
2019-05-27 11:40:18 +00:00
|
|
|
return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()]
|
|
|
|
initWithSet: aSet
|
|
|
|
copyItems: flag]);
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// instance methods
|
|
|
|
- (instancetype) initWithArray:(NSArray *)other
|
|
|
|
{
|
2019-05-27 10:05:19 +00:00
|
|
|
unsigned count = [other count];
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
return [self init];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GS_BEGINIDBUF(objs, count);
|
|
|
|
|
|
|
|
if ([other isProxy])
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
objs[i] = [other objectAtIndex: i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[other getObjects: objs];
|
|
|
|
}
|
|
|
|
self = [self initWithObjects: objs count: count];
|
|
|
|
GS_ENDIDBUF();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-05-18 00:37:19 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype) initWithArray:(NSArray *)other copyItems:(BOOL)flag
|
|
|
|
{
|
2019-05-27 11:40:18 +00:00
|
|
|
unsigned count = [other count];
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
return [self init];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GS_BEGINIDBUF(objs, count);
|
|
|
|
|
|
|
|
if ([other isProxy])
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
objs[i] = [other objectAtIndex: i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[other getObjects: objs];
|
|
|
|
}
|
|
|
|
self = [self initWithObjects: objs count: count];
|
|
|
|
GS_ENDIDBUF();
|
|
|
|
return self;
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype) initWithArray:(NSArray *)other
|
|
|
|
range:(NSRange)range
|
|
|
|
copyItems:(BOOL)flag
|
|
|
|
{
|
2019-05-27 11:40:18 +00:00
|
|
|
unsigned count = [other count];
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
return [self init];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GS_BEGINIDBUF(objs, count);
|
|
|
|
|
|
|
|
if ([other isProxy])
|
|
|
|
{
|
|
|
|
unsigned i = 0;
|
|
|
|
unsigned loc = range.location;
|
|
|
|
unsigned len = range.length;
|
|
|
|
unsigned j = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
if(i >= loc && j < len)
|
|
|
|
{
|
|
|
|
if(flag == YES)
|
|
|
|
{
|
|
|
|
objs[i] = [[other objectAtIndex: i] copy];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
objs[i] = [other objectAtIndex: i];
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(j >= len)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[other getObjects: objs];
|
|
|
|
}
|
|
|
|
self = [self initWithObjects: objs count: count];
|
|
|
|
GS_ENDIDBUF();
|
|
|
|
return self;
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype) initWithObject:(id)object
|
|
|
|
{
|
2019-05-30 11:46:54 +00:00
|
|
|
self = [super init];
|
|
|
|
if(self != nil)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
return self;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
- (instancetype) initWithObjects:(id)firstObject, ...
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 10:05:19 +00:00
|
|
|
GS_USEIDLIST(firstObject,
|
2019-05-27 11:40:18 +00:00
|
|
|
self = [self initWithObjects: __objects count: __count]);
|
|
|
|
return self;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:40:18 +00:00
|
|
|
/** <init /> <override-subclass />
|
|
|
|
* Initialize to contain (unique elements of) objects.<br />
|
|
|
|
* Calls -init (which does nothing but maintain MacOS-X compatibility),
|
|
|
|
* and needs to be re-implemented in subclasses in order to have all
|
|
|
|
* other initialisers work.
|
|
|
|
*/
|
2019-05-30 11:46:54 +00:00
|
|
|
- (instancetype) initWithObjects:(const id [])objects // required override.
|
2019-05-18 00:37:19 +00:00
|
|
|
count:(NSUInteger)count
|
|
|
|
{
|
2019-05-27 11:40:18 +00:00
|
|
|
self = [self init];
|
2019-05-27 10:05:19 +00:00
|
|
|
return self;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
- (instancetype) initWithOrderedSet:(NSOrderedSet *)aSet
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 10:05:19 +00:00
|
|
|
return [self initWithOrderedSet: aSet copyItems: NO];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 10:05:19 +00:00
|
|
|
- (instancetype) initWithOrderedSet:(NSOrderedSet *)other
|
2019-05-18 00:37:19 +00:00
|
|
|
copyItems:(BOOL)flag
|
|
|
|
{
|
2019-05-27 10:05:19 +00:00
|
|
|
unsigned c = [other count];
|
|
|
|
id o, e = [other objectEnumerator];
|
|
|
|
unsigned i = 0;
|
|
|
|
GS_BEGINIDBUF(os, c);
|
|
|
|
|
|
|
|
while ((o = [e nextObject]))
|
|
|
|
{
|
|
|
|
if (flag)
|
|
|
|
os[i] = [o copy];
|
|
|
|
else
|
|
|
|
os[i] = o;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
self = [self initWithObjects: os count: c];
|
|
|
|
if (flag)
|
|
|
|
{
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
[os[i] release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GS_ENDIDBUF();
|
|
|
|
return self;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:17:30 +00:00
|
|
|
- (instancetype) initWithOrderedSet:(NSOrderedSet *)other
|
2019-05-20 08:01:20 +00:00
|
|
|
range:(NSRange)range
|
2019-05-18 00:37:19 +00:00
|
|
|
copyItems:(BOOL)flag
|
|
|
|
{
|
2019-05-27 11:17:30 +00:00
|
|
|
unsigned c = [other count];
|
|
|
|
id o, e = [other objectEnumerator];
|
|
|
|
unsigned i = 0, j = 0;
|
|
|
|
unsigned loc = range.location;
|
|
|
|
unsigned len = range.length;
|
|
|
|
GS_BEGINIDBUF(os, c);
|
|
|
|
|
|
|
|
while ((o = [e nextObject]))
|
|
|
|
{
|
|
|
|
if(i >= loc && j < len)
|
|
|
|
{
|
|
|
|
if (flag)
|
|
|
|
os[i] = [o copy];
|
|
|
|
else
|
|
|
|
os[i] = o;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
i++;
|
2019-05-27 11:40:18 +00:00
|
|
|
|
|
|
|
if(j >= len)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2019-05-27 11:17:30 +00:00
|
|
|
}
|
2019-05-27 11:40:18 +00:00
|
|
|
|
2019-05-27 11:17:30 +00:00
|
|
|
self = [self initWithObjects: os count: c];
|
|
|
|
if (flag)
|
|
|
|
{
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
[os[i] release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GS_ENDIDBUF();
|
|
|
|
return self;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
- (instancetype) initWithSet:(NSSet *)aSet
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 11:17:30 +00:00
|
|
|
return [self initWithSet: aSet copyItems: NO];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:17:30 +00:00
|
|
|
- (instancetype) initWithSet:(NSSet *)other copyItems:(BOOL)flag
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 11:17:30 +00:00
|
|
|
unsigned c = [other count];
|
|
|
|
id o, e = [other objectEnumerator];
|
|
|
|
unsigned i = 0;
|
|
|
|
GS_BEGINIDBUF(os, c);
|
|
|
|
|
|
|
|
while ((o = [e nextObject]))
|
|
|
|
{
|
|
|
|
if (flag)
|
|
|
|
os[i] = [o copy];
|
|
|
|
else
|
|
|
|
os[i] = o;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
self = [self initWithObjects: os count: c];
|
|
|
|
if (flag)
|
|
|
|
{
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
[os[i] release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GS_ENDIDBUF();
|
|
|
|
return self;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype) init
|
|
|
|
{
|
2019-05-27 11:17:30 +00:00
|
|
|
self = [super init];
|
|
|
|
if(self == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"NSOrderedSet not allocated.");
|
|
|
|
}
|
|
|
|
return self;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 13:56:19 +00:00
|
|
|
- (NSUInteger) count // required override
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 11:17:30 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
2019-05-18 00:37:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-30 13:56:19 +00:00
|
|
|
- (BOOL)containsObject:(id)anObject // required override
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 11:17:30 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) enumerateObjectsAtIndexes:(NSIndexSet *)indexSet
|
|
|
|
options:(NSEnumerationOptions)opts
|
|
|
|
usingBlock:(GSEnumeratorBlock)aBlock
|
|
|
|
{
|
2019-05-30 13:56:19 +00:00
|
|
|
[[self objectsAtIndexes: indexSet] enumerateObjectsWithOptions: opts
|
|
|
|
usingBlock: aBlock];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 08:01:20 +00:00
|
|
|
- (void) enumerateObjectsUsingBlock:(GSEnumeratorBlock)aBlock
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-30 13:03:34 +00:00
|
|
|
[self enumerateObjectsWithOptions: 0 usingBlock: aBlock];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) enumerateObjectsWithOptions:(NSEnumerationOptions)opts
|
|
|
|
usingBlock:(GSEnumeratorBlock)aBlock
|
|
|
|
{
|
2019-05-30 13:03:34 +00:00
|
|
|
NSUInteger count = 0;
|
|
|
|
BLOCK_SCOPE BOOL shouldStop = NO;
|
|
|
|
BOOL isReverse = (opts & NSEnumerationReverse);
|
|
|
|
id<NSFastEnumeration> enumerator = self;
|
|
|
|
|
|
|
|
/* If we are enumerating in reverse, use the reverse enumerator for fast
|
|
|
|
* enumeration. */
|
|
|
|
if (isReverse)
|
|
|
|
{
|
|
|
|
enumerator = [self reverseObjectEnumerator];
|
|
|
|
count = ([self count] - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
GS_DISPATCH_CREATE_QUEUE_AND_GROUP_FOR_ENUMERATION(enumQueue, opts)
|
|
|
|
FOR_IN (id, obj, enumerator)
|
|
|
|
GS_DISPATCH_SUBMIT_BLOCK(enumQueueGroup, enumQueue, if (YES == shouldStop) {return;}, return, aBlock, obj, count, &shouldStop);
|
|
|
|
if (isReverse)
|
|
|
|
{
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldStop)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
END_FOR_IN(enumerator)
|
|
|
|
GS_DISPATCH_TEARDOWN_QUEUE_AND_GROUP_FOR_ENUMERATION(enumQueue, opts)
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) firstObject
|
|
|
|
{
|
2019-05-30 13:03:34 +00:00
|
|
|
NSUInteger count = [self count];
|
|
|
|
if (count == 0)
|
|
|
|
return nil;
|
|
|
|
return [self objectAtIndex: 0];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) lastObject
|
|
|
|
{
|
2019-05-30 13:03:34 +00:00
|
|
|
NSUInteger count = [self count];
|
|
|
|
if (count == 0)
|
|
|
|
return nil;
|
|
|
|
return [self objectAtIndex: 0];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectAtIndex: (NSUInteger)index
|
|
|
|
{
|
2019-05-27 11:17:30 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
2019-05-18 00:37:19 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectAtIndexedSubscript: (NSUInteger)index
|
|
|
|
{
|
2019-05-27 11:17:30 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
2019-05-18 00:37:19 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) objectsAtIndexes: (NSIndexSet *)indexes
|
|
|
|
{
|
2019-05-27 11:17:30 +00:00
|
|
|
NSMutableArray *group = [NSMutableArray arrayWithCapacity: [indexes count]];
|
|
|
|
|
|
|
|
NSUInteger i = [indexes firstIndex];
|
|
|
|
while (i != NSNotFound)
|
|
|
|
{
|
|
|
|
[group addObject: [self objectAtIndex: i]];
|
|
|
|
i = [indexes indexGreaterThanIndex: i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return GS_IMMUTABLE(group);
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-30 13:56:19 +00:00
|
|
|
- (NSUInteger) indexOfObject:(id)anObject
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-30 13:56:19 +00:00
|
|
|
NSUInteger c = [self count];
|
|
|
|
|
|
|
|
if (c > 0 && anObject != nil)
|
|
|
|
{
|
|
|
|
NSUInteger i;
|
|
|
|
IMP get = [self methodForSelector: oaiSel];
|
|
|
|
BOOL (*eq)(id, SEL, id)
|
|
|
|
= (BOOL (*)(id, SEL, id))[anObject methodForSelector: eqSel];
|
|
|
|
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
if ((*eq)(anObject, eqSel, (*get)(self, oaiSel, i)) == YES)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return NSNotFound;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger) indexOfObject: (id)key
|
|
|
|
inSortedRange: (NSRange)range
|
|
|
|
options: (NSBinarySearchingOptions)options
|
|
|
|
usingComparator: (NSComparator)comparator
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger) indexOfObjectAtIndexes:(NSIndexSet *)indexSet
|
|
|
|
options:(NSEnumerationOptions)opts
|
|
|
|
passingTest:(GSPredicateBlock)predicate
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger) indexOfObjectPassingTest:(GSPredicateBlock)predicate
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUInteger) indexOfObjectWithOptions:(NSEnumerationOptions)opts
|
|
|
|
passingTest:(GSPredicateBlock)predicate
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSIndexSet *) indexesOfObjectsAtIndexes:(NSIndexSet *)indexSet
|
|
|
|
options:(NSEnumerationOptions)opts
|
|
|
|
passingTest:(GSPredicateBlock)predicate
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSIndexSet *)indexesOfObjectsPassingTest:(GSPredicateBlock)predicate
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSIndexSet *) indexesOfObjectWithOptions:(NSEnumerationOptions)opts
|
|
|
|
passingTest:(GSPredicateBlock)predicate
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSEnumerator *) objectEnumerator
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSEnumerator *) reverseObjectEnumerator
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSOrderedSet *)reversedOrderedSet
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) getObjects: (__unsafe_unretained id[])aBuffer
|
|
|
|
range: (NSRange)aRange
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key value coding support
|
|
|
|
- (void) setValue: (id)value forKey: (NSString*)key
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) valueForKey: (NSString*)key
|
|
|
|
{
|
2019-05-19 04:19:19 +00:00
|
|
|
NSEnumerator *e = [self objectEnumerator];
|
|
|
|
id object = nil;
|
|
|
|
NSMutableSet *results = [NSMutableSet setWithCapacity: [self count]];
|
|
|
|
|
|
|
|
while ((object = [e nextObject]) != nil)
|
|
|
|
{
|
|
|
|
id result = [object valueForKey: key];
|
|
|
|
|
|
|
|
if (result == nil)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
[results addObject: result];
|
|
|
|
}
|
|
|
|
return results;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Key-Value Observing Support
|
|
|
|
/*
|
|
|
|
- addObserver:forKeyPath:options:context:
|
|
|
|
- removeObserver:forKeyPath:
|
|
|
|
- removeObserver:forKeyPath:context:
|
|
|
|
*/
|
2019-05-19 04:19:19 +00:00
|
|
|
- (NSUInteger)_countForObject: (id)object
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-18 00:37:19 +00:00
|
|
|
// Comparing Sets
|
|
|
|
- (BOOL) isEqualToOrderedSet: (NSOrderedSet *)aSet
|
|
|
|
{
|
2019-05-19 04:19:19 +00:00
|
|
|
if ([self count] != [aSet count])
|
|
|
|
return NO;
|
|
|
|
else
|
|
|
|
{
|
2019-05-20 05:43:06 +00:00
|
|
|
id o, e = [self objectEnumerator];
|
2019-05-19 04:19:19 +00:00
|
|
|
|
|
|
|
while ((o = [e nextObject]))
|
|
|
|
{
|
2019-05-19 05:31:15 +00:00
|
|
|
if (![aSet containsObject: o])
|
2019-05-19 04:19:19 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ([self _countForObject: o] != [aSet _countForObject: o])
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)other
|
|
|
|
{
|
|
|
|
if ([other isKindOfClass: [NSOrderedSet class]])
|
|
|
|
return [self isEqualToOrderedSet: other];
|
2019-05-18 00:37:19 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2019-05-19 04:19:19 +00:00
|
|
|
|
2019-05-18 00:37:19 +00:00
|
|
|
// Set operations
|
|
|
|
- (BOOL) intersectsOrderedSet: (NSOrderedSet *)aSet
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:40:18 +00:00
|
|
|
- (BOOL) intersectsSet: (NSSet *)aSet
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isSubsetOfOrderedSet: (NSOrderedSet *)aSet
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:40:18 +00:00
|
|
|
- (BOOL) isSubsetOfSet:(NSSet *)aSet
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creating a Sorted Array
|
|
|
|
- (NSArray *) sortedArrayUsingDescriptors:(NSArray *)sortDescriptors
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *) sortedArrayUsingComparator: (NSComparator)comparator
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)
|
|
|
|
sortedArrayWithOptions: (NSSortOptions)options
|
|
|
|
usingComparator: (NSComparator)comparator
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filtering Ordered Sets
|
|
|
|
- (NSOrderedSet *)filteredOrderedSetUsingPredicate: (NSPredicate *)predicate
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Describing a set
|
|
|
|
- (NSString *) description
|
|
|
|
{
|
2019-05-19 04:19:19 +00:00
|
|
|
return [self descriptionWithLocale: nil];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) descriptionWithLocale: (NSLocale *)locale
|
2019-05-20 08:01:20 +00:00
|
|
|
{
|
|
|
|
NSArray *allObjects = [self sortedArrayUsingDescriptors: nil];
|
|
|
|
return [allObjects descriptionWithLocale: locale];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 04:19:19 +00:00
|
|
|
- (NSString*) descriptionWithLocale: (NSLocale *)locale indent: (BOOL)flag
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-19 04:19:19 +00:00
|
|
|
return [self descriptionWithLocale: locale];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
// Mutable Ordered Set
|
|
|
|
@implementation NSMutableOrderedSet
|
|
|
|
// Creating a Mutable Ordered Set
|
2019-05-18 08:01:11 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
2019-05-30 11:46:54 +00:00
|
|
|
if (self == [NSMutableOrderedSet class])
|
2019-05-18 08:01:11 +00:00
|
|
|
{
|
|
|
|
NSMutableOrderedSet_abstract_class = self;
|
|
|
|
NSMutableOrderedSet_concrete_class = [GSMutableOrderedSet class];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 00:37:19 +00:00
|
|
|
+ (instancetype)orderedSetWithCapacity: (NSUInteger)capacity
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2019-05-30 11:46:54 +00:00
|
|
|
- (Class) classForCoder
|
|
|
|
{
|
|
|
|
return NSMutableOrderedSet_abstract_class;
|
|
|
|
}
|
|
|
|
|
2019-05-18 00:37:19 +00:00
|
|
|
- (instancetype)initWithCapacity: (NSUInteger)capacity
|
|
|
|
{
|
2019-05-30 11:46:54 +00:00
|
|
|
self = [self init];
|
|
|
|
return self;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype) init
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
self = [super init];
|
|
|
|
if(self == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"Could not init class");
|
|
|
|
}
|
|
|
|
return self;
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addObject:(id)anObject
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addObjects:(const id[])objects count:(NSUInteger)count
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
NSUInteger i = 0;
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
id obj = objects[i];
|
|
|
|
[self addObject: obj];
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addObjectsFromArray:(NSArray *)otherArray
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
NSEnumerator *en = [otherArray objectEnumerator];
|
|
|
|
id obj = nil;
|
|
|
|
while((obj = [en nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[self addObject: obj];
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:46:08 +00:00
|
|
|
- (void)insertObject:(id)object atIndex:(NSUInteger)index // required override
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
if ([self count] == index)
|
|
|
|
{
|
|
|
|
[self addObject: object];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self replaceObjectAtIndex: index withObject: object];
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)insertObjects:(NSArray *)array atIndexes:(NSIndexSet *)indexes
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
NSUInteger index = [indexes firstIndex];
|
|
|
|
NSEnumerator *enumerator = [array objectEnumerator];
|
|
|
|
id object = [enumerator nextObject];
|
|
|
|
|
|
|
|
while (object != nil && index != NSNotFound)
|
|
|
|
{
|
|
|
|
[self insertObject: object atIndex: index];
|
|
|
|
object = [enumerator nextObject];
|
|
|
|
index = [indexes indexGreaterThanIndex: index];
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:09:13 +00:00
|
|
|
- (void)removeObject:(id)anObject
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-28 09:09:13 +00:00
|
|
|
NSUInteger i;
|
|
|
|
|
|
|
|
if (anObject == nil)
|
|
|
|
{
|
|
|
|
NSWarnMLog(@"attempt to remove nil object");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
i = [self count];
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
IMP rem = 0;
|
|
|
|
IMP get = [self methodForSelector: oaiSel];
|
|
|
|
BOOL (*eq)(id, SEL, id)
|
|
|
|
= (BOOL (*)(id, SEL, id))[anObject methodForSelector: eqSel];
|
|
|
|
|
|
|
|
while (i-- > 0)
|
|
|
|
{
|
|
|
|
id o = (*get)(self, oaiSel, i);
|
|
|
|
|
|
|
|
if (o == anObject || (*eq)(anObject, eqSel, o) == YES)
|
|
|
|
{
|
|
|
|
if (rem == 0)
|
|
|
|
{
|
|
|
|
rem = [self methodForSelector: remSel];
|
|
|
|
/*
|
|
|
|
* We need to retain the object so that when we remove the
|
|
|
|
* first equal object we don't get left with a bad object
|
|
|
|
* pointer for later comparisons.
|
|
|
|
*/
|
|
|
|
RETAIN(anObject);
|
|
|
|
}
|
|
|
|
(*rem)(self, remSel, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rem != 0)
|
|
|
|
{
|
|
|
|
RELEASE(anObject);
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:46:08 +00:00
|
|
|
- (void)removeObjectAtIndex:(NSUInteger)integer // required override
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:40:22 +00:00
|
|
|
- (void) _removeObjectsFromIndices: (NSUInteger*)indices
|
|
|
|
numIndices: (NSUInteger)count
|
|
|
|
{
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
NSUInteger to = 0;
|
|
|
|
NSUInteger from = 0;
|
|
|
|
NSUInteger i;
|
|
|
|
GS_BEGINITEMBUF(sorted, count, NSUInteger);
|
|
|
|
|
|
|
|
while (from < count)
|
|
|
|
{
|
|
|
|
NSUInteger val = indices[from++];
|
|
|
|
|
|
|
|
i = to;
|
|
|
|
while (i > 0 && sorted[i-1] > val)
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
if (i == to)
|
|
|
|
{
|
|
|
|
sorted[to++] = val;
|
|
|
|
}
|
|
|
|
else if (sorted[i] != val)
|
|
|
|
{
|
|
|
|
NSUInteger j = to++;
|
|
|
|
|
|
|
|
if (sorted[i] < val)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
while (j > i)
|
|
|
|
{
|
|
|
|
sorted[j] = sorted[j-1];
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
sorted[i] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to > 0)
|
|
|
|
{
|
|
|
|
IMP rem = [self methodForSelector: remSel];
|
|
|
|
|
|
|
|
while (to--)
|
|
|
|
{
|
|
|
|
(*rem)(self, remSel, sorted[to]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GS_ENDITEMBUF();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 00:37:19 +00:00
|
|
|
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes
|
|
|
|
{
|
2019-05-28 09:40:22 +00:00
|
|
|
NSUInteger count = [indexes count];
|
|
|
|
NSUInteger indexArray[count];
|
|
|
|
|
|
|
|
[indexes getIndexes: indexArray
|
|
|
|
maxCount: count
|
|
|
|
inIndexRange: NULL];
|
|
|
|
|
|
|
|
[self _removeObjectsFromIndices: indexArray
|
|
|
|
numIndices: count];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeObjectsInArray:(NSArray *)otherArray
|
|
|
|
{
|
2019-05-28 09:40:22 +00:00
|
|
|
NSUInteger c = [otherArray count];
|
|
|
|
|
|
|
|
if (c > 0)
|
|
|
|
{
|
|
|
|
NSUInteger i;
|
|
|
|
IMP get = [otherArray methodForSelector: oaiSel];
|
|
|
|
IMP rem = [self methodForSelector: @selector(removeObject:)];
|
|
|
|
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
(*rem)(self, @selector(removeObject:), (*get)(otherArray, oaiSel, i));
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 09:40:22 +00:00
|
|
|
- (void)removeObjectsInRange:(NSRange)aRange
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-28 09:40:22 +00:00
|
|
|
NSUInteger i;
|
|
|
|
NSUInteger s = aRange.location;
|
|
|
|
NSUInteger c = [self count];
|
|
|
|
|
|
|
|
i = aRange.location + aRange.length;
|
|
|
|
|
|
|
|
if (c < i)
|
|
|
|
i = c;
|
|
|
|
|
|
|
|
if (i > s)
|
|
|
|
{
|
|
|
|
IMP rem = [self methodForSelector: remSel];
|
|
|
|
|
|
|
|
while (i-- > s)
|
|
|
|
{
|
|
|
|
(*rem)(self, remSel, i);
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeAllObjects
|
|
|
|
{
|
2019-05-28 09:09:13 +00:00
|
|
|
NSUInteger c = [self count];
|
|
|
|
|
|
|
|
if (c > 0)
|
|
|
|
{
|
|
|
|
IMP remLast = [self methodForSelector: rlSel];
|
|
|
|
|
|
|
|
while (c--)
|
|
|
|
{
|
|
|
|
(*remLast)(self, rlSel);
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)replaceObjectAtIndex:(NSUInteger)index
|
|
|
|
withObject:(id)object
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) replaceObjectsAtIndexes: (NSIndexSet *)indexes
|
|
|
|
withObjects: (NSArray *)objects
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-28 09:46:08 +00:00
|
|
|
- (void) replaceObjectsInRange: (NSRange)range
|
|
|
|
withObjects: (const id[])objects
|
2019-05-18 00:37:19 +00:00
|
|
|
count: (NSUInteger)count
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-28 09:46:08 +00:00
|
|
|
- (void)setObject:(id)anObject atIndex:(NSUInteger)anIndex
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-28 09:46:08 +00:00
|
|
|
if ([self count] == anIndex)
|
|
|
|
{
|
|
|
|
[self addObject: anObject];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self replaceObjectAtIndex: anIndex withObject: anObject];
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)moveObjectsAtIndexes:(NSIndexSet *)indexes toIndex:(NSUInteger)index
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) exchangeObjectAtIndex:(NSUInteger)index withObjectAtIndex:(NSUInteger)otherIndex
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
id tmp = [self objectAtIndex: index];
|
|
|
|
|
|
|
|
RETAIN(tmp);
|
|
|
|
[self replaceObjectAtIndex: index withObject: [self objectAtIndex: otherIndex]];
|
|
|
|
[self replaceObjectAtIndex: otherIndex withObject: tmp];
|
|
|
|
RELEASE(tmp);
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)filterUsingPredicate:(NSPredicate *)predicate
|
|
|
|
{
|
2019-05-30 11:46:54 +00:00
|
|
|
unsigned count = [self count];
|
|
|
|
|
|
|
|
while (count-- > 0)
|
|
|
|
{
|
|
|
|
id object = [self objectAtIndex: count];
|
|
|
|
|
|
|
|
if ([predicate evaluateWithObject: object] == NO)
|
|
|
|
{
|
|
|
|
[self removeObjectAtIndex: count];
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) sortUsingDescriptors:(NSArray *)descriptors
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) sortUsingComparator: (NSComparator)comparator
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
[self sortWithOptions: 0 usingComparator: comparator];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) sortWithOptions: (NSSortOptions)options
|
|
|
|
usingComparator: (NSComparator)comparator
|
|
|
|
{
|
2019-05-28 08:49:59 +00:00
|
|
|
NSUInteger count = [self count];
|
|
|
|
|
|
|
|
if ((1 < count) && (NULL != comparator))
|
|
|
|
{
|
|
|
|
NSArray *res = nil;
|
|
|
|
NSUInteger i, c = [self count];
|
|
|
|
IMP get = [self methodForSelector: oaiSel];
|
|
|
|
NSEnumerator *en = nil;
|
|
|
|
id obj = nil;
|
|
|
|
|
|
|
|
GS_BEGINIDBUF(objects, count);
|
|
|
|
for (i = 0; i < c; i++)
|
|
|
|
{
|
|
|
|
objects[i] = (*get)(self, oaiSel, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options & NSSortStable)
|
|
|
|
{
|
|
|
|
if (options & NSSortConcurrent)
|
|
|
|
{
|
|
|
|
GSSortStableConcurrent(objects, NSMakeRange(0,count),
|
|
|
|
(id)comparator, GSComparisonTypeComparatorBlock, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GSSortStable(objects, NSMakeRange(0,count),
|
|
|
|
(id)comparator, GSComparisonTypeComparatorBlock, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (options & NSSortConcurrent)
|
|
|
|
{
|
|
|
|
GSSortUnstableConcurrent(objects, NSMakeRange(0,count),
|
|
|
|
(id)comparator, GSComparisonTypeComparatorBlock, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GSSortUnstable(objects, NSMakeRange(0,count),
|
|
|
|
(id)comparator, GSComparisonTypeComparatorBlock, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = [[NSArray alloc] initWithObjects: objects count: count];
|
|
|
|
[self removeAllObjects];
|
|
|
|
en = [res objectEnumerator];
|
|
|
|
while((obj = [en nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[self addObject: obj];
|
|
|
|
}
|
|
|
|
|
|
|
|
RELEASE(res);
|
|
|
|
GS_ENDIDBUF();
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) sortRange: (NSRange)range
|
|
|
|
options:(NSSortOptions)options
|
|
|
|
usingComparator: (NSComparator)comparator
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-27 11:50:11 +00:00
|
|
|
- (void) intersectOrderedSet:(NSOrderedSet *)other
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 11:50:11 +00:00
|
|
|
if (other != self)
|
|
|
|
{
|
|
|
|
id keys = [self objectEnumerator];
|
|
|
|
id key;
|
|
|
|
|
|
|
|
while ((key = [keys nextObject]))
|
|
|
|
{
|
|
|
|
if ([other containsObject: key] == NO)
|
|
|
|
{
|
|
|
|
[self removeObject: key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:50:11 +00:00
|
|
|
- (void) intersectSet:(NSSet *)other
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 11:50:11 +00:00
|
|
|
id keys = [self objectEnumerator];
|
|
|
|
id key;
|
|
|
|
|
|
|
|
while ((key = [keys nextObject]))
|
|
|
|
{
|
|
|
|
if ([other containsObject: key] == NO)
|
|
|
|
{
|
|
|
|
[self removeObject: key];
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:50:11 +00:00
|
|
|
- (void) minusOrderedSet:(NSOrderedSet *)other
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 11:50:11 +00:00
|
|
|
if(other != self)
|
|
|
|
{
|
|
|
|
[self removeAllObjects];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
id keys = [other objectEnumerator];
|
|
|
|
id key;
|
|
|
|
|
|
|
|
while ((key = [keys nextObject]))
|
|
|
|
{
|
|
|
|
[self removeObject: key];
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:50:11 +00:00
|
|
|
- (void) minusSet:(NSSet *)other
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 11:50:11 +00:00
|
|
|
id keys = [other objectEnumerator];
|
|
|
|
id key;
|
|
|
|
|
|
|
|
while ((key = [keys nextObject]))
|
|
|
|
{
|
|
|
|
[self removeObject: key];
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:50:11 +00:00
|
|
|
- (void) unionOrderedSet:(NSOrderedSet *)other
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 11:50:11 +00:00
|
|
|
if (other != self)
|
|
|
|
{
|
|
|
|
id keys = [other objectEnumerator];
|
|
|
|
id key;
|
|
|
|
|
|
|
|
while ((key = [keys nextObject]))
|
|
|
|
{
|
|
|
|
[self addObject: key];
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 11:50:11 +00:00
|
|
|
- (void) unionSet:(NSSet *)other
|
2019-05-18 00:37:19 +00:00
|
|
|
{
|
2019-05-27 11:50:11 +00:00
|
|
|
id keys = [other objectEnumerator];
|
|
|
|
id key;
|
|
|
|
|
|
|
|
while ((key = [keys nextObject]))
|
|
|
|
{
|
|
|
|
[self addObject: key];
|
|
|
|
}
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype) initWithCoder: (NSCoder *)coder
|
|
|
|
{
|
2019-05-27 11:50:11 +00:00
|
|
|
return [super initWithCoder: coder];
|
2019-05-18 00:37:19 +00:00
|
|
|
}
|
|
|
|
@end
|