From 5c6139af075c66aaa2121fb409f85f73a78fd7c0 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Thu, 27 Jun 2019 13:56:27 +0200 Subject: [PATCH 01/10] Correct retain/release handling for GSIArray --- Source/GSOrderedSet.m | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Source/GSOrderedSet.m b/Source/GSOrderedSet.m index 77c331efa..907d4bc4a 100644 --- a/Source/GSOrderedSet.m +++ b/Source/GSOrderedSet.m @@ -40,14 +40,8 @@ #import "GSDispatch.h" #import "GSSorting.h" -#define GSI_ARRAY_TYPE NSRange -#define GSI_ARRAY_NO_RELEASE 0 -#define GSI_ARRAY_NO_RETAIN 0 #define GSI_ARRAY_TYPES GSUNION_OBJ -#define GSI_ARRAY_RELEASE(A, X) [(X).obj release] -#define GSI_ARRAY_RETAIN(A, X) [(X).obj retain] - #import "GNUstepBase/GSIArray.h" @interface GSOrderedSet : NSOrderedSet @@ -230,7 +224,6 @@ static Class mutableSetClass; if(![self containsObject: obj]) { GSIArrayAddItem(&array, item); - RETAIN(obj); } } return self; @@ -267,7 +260,6 @@ static Class mutableSetClass; { item.obj = object; GSIArrayInsertItem(&array, item, index); - RETAIN(object); _version++; } } From 5b169f55b5047809cf252733bfe05f3fb4f3a0ec Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Thu, 27 Jun 2019 13:56:27 +0200 Subject: [PATCH 02/10] Correct retain/release handling for GSIArray --- Source/GSOrderedSet.m | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Source/GSOrderedSet.m b/Source/GSOrderedSet.m index 77c331efa..907d4bc4a 100644 --- a/Source/GSOrderedSet.m +++ b/Source/GSOrderedSet.m @@ -40,14 +40,8 @@ #import "GSDispatch.h" #import "GSSorting.h" -#define GSI_ARRAY_TYPE NSRange -#define GSI_ARRAY_NO_RELEASE 0 -#define GSI_ARRAY_NO_RETAIN 0 #define GSI_ARRAY_TYPES GSUNION_OBJ -#define GSI_ARRAY_RELEASE(A, X) [(X).obj release] -#define GSI_ARRAY_RETAIN(A, X) [(X).obj retain] - #import "GNUstepBase/GSIArray.h" @interface GSOrderedSet : NSOrderedSet @@ -230,7 +224,6 @@ static Class mutableSetClass; if(![self containsObject: obj]) { GSIArrayAddItem(&array, item); - RETAIN(obj); } } return self; @@ -267,7 +260,6 @@ static Class mutableSetClass; { item.obj = object; GSIArrayInsertItem(&array, item, index); - RETAIN(object); _version++; } } From ce3c4ae88272df15f37c840ff2357fede3ea6055 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Thu, 27 Jun 2019 14:20:19 +0200 Subject: [PATCH 03/10] Format according to GNUstep coding style. --- Source/GSOrderedSet.m | 37 +++-- Source/NSOrderedSet.m | 370 ++++++++++++++++++++++-------------------- 2 files changed, 217 insertions(+), 190 deletions(-) diff --git a/Source/GSOrderedSet.m b/Source/GSOrderedSet.m index 907d4bc4a..8e21d672f 100644 --- a/Source/GSOrderedSet.m +++ b/Source/GSOrderedSet.m @@ -1,22 +1,22 @@ /** Concrete implementation of GSOrderedSet and GSMutableOrderedSet based on GNU NSOrderedSet and NSMutableOrderedSet classes Copyright (C) 2019 Free Software Foundation, Inc. - + Written by: Gregory Casamento 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, @@ -86,7 +86,7 @@ - (id) nextObject { - if(current < count) + if (current < count) { GSIArrayItem item = GSIArrayItemAtIndex(&set->array, current); current++; @@ -107,7 +107,7 @@ - (id) initWithOrderedSet: (GSOrderedSet*)d { self = [super initWithOrderedSet: d]; - if(self != nil) + if (self != nil) { current = GSIArrayCount(&set->array); } @@ -119,8 +119,10 @@ GSIArrayItem item; if (current == 0) - return nil; - + { + return nil; + } + item = GSIArrayItemAtIndex(&set->array, --current); return (id)(item.obj); } @@ -180,7 +182,7 @@ static Class mutableSetClass; NSUInteger count = [self count]; NSUInteger i = 0; - for(i = 0; i < count; i++) + for (i = 0; i < count; i++) { GSIArrayItem item = GSIArrayItemAtIndex(&array, i); size += [item.obj sizeInBytesExcluding: exclude]; @@ -212,7 +214,7 @@ static Class mutableSetClass; { id obj = objs[i]; GSIArrayItem item; - + if (objs[i] == nil) { DESTROY(self); @@ -221,7 +223,7 @@ static Class mutableSetClass; } item.obj = obj; - if(![self containsObject: obj]) + if (![self containsObject: obj]) { GSIArrayAddItem(&array, item); } @@ -249,14 +251,14 @@ static Class mutableSetClass; - (void) insertObject: (id)object atIndex: (NSUInteger)index { GSIArrayItem item; - if(object == nil) + if (object == nil) { [NSException raise: NSInvalidArgumentException format: @"Tried to add nil to set"]; } else { - if([self containsObject: object] == NO) + if ([self containsObject: object] == NO) { item.obj = object; GSIArrayInsertItem(&array, item, index); @@ -297,11 +299,11 @@ static Class mutableSetClass; // Init and fill set self = [self initWithCapacity: count]; - if(self != nil) + if (self != nil) { - for(i = 0; i < count; i++) + for (i = 0; i < count; i++) { - id anObject = objects[i]; + id anObject = objects[i]; [self addObject: anObject]; } } @@ -321,4 +323,3 @@ static Class mutableSetClass; } @end - diff --git a/Source/NSOrderedSet.m b/Source/NSOrderedSet.m index 1ce2fe6c9..7cd52a30a 100644 --- a/Source/NSOrderedSet.m +++ b/Source/NSOrderedSet.m @@ -86,7 +86,7 @@ static SEL rlSel; if (self == [NSOrderedSet class]) { [self setVersion: 1]; - + addSel = @selector(addObject:); countSel = @selector(count); eqSel = @selector(isEqual:); @@ -186,13 +186,13 @@ static SEL rlSel; /* HACK ... MacOS-X seems to code differently if the coder is an * actual instance of NSKeyedArchiver */ - + // Collect all objects... - while((obj = [en nextObject]) != nil) + while ((obj = [en nextObject]) != nil) { [array addObject: obj]; } - + if ([aCoder class] == [NSKeyedArchiver class]) { [(NSKeyedArchiver*)aCoder _encodeArrayOfObjects: array @@ -224,7 +224,7 @@ static SEL rlSel; { [aCoder encodeValueOfObjCType: @encode(id) at: &o]; } - } + } } - (id) copyWithZone: (NSZone*)zone @@ -239,7 +239,7 @@ static SEL rlSel; return [copy initWithOrderedSet: self copyItems: NO]; } -// NSFastEnumeration +// NSFastEnumeration - (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState *)state objects: (__unsafe_unretained id[])stackbuf count: (NSUInteger)len @@ -268,7 +268,7 @@ static SEL rlSel; // IMP imp = [self methodForSelector: @selector(objectAtIndex:)]; // int i; // int p = state->state; - + [self getObjects: stackbuf range: NSMakeRange(state->state, count)]; /*for (i = 0; i < count; i++, p++) @@ -291,15 +291,15 @@ static SEL rlSel; return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] init]); } -+ (instancetype) orderedSetWithArray:(NSArray *)objects ++ (instancetype) orderedSetWithArray: (NSArray *)objects { return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] initWithArray: objects]); } -+ (instancetype) orderedSetWithArray:(NSArray *)objects - range:(NSRange)range - copyItems:(BOOL)flag ++ (instancetype) orderedSetWithArray: (NSArray *)objects + range: (NSRange)range + copyItems: (BOOL)flag { return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] initWithArray: objects @@ -307,13 +307,13 @@ static SEL rlSel; copyItems: flag]); } -+ (instancetype) orderedSetWithObject:(id)anObject ++ (instancetype) orderedSetWithObject: (id)anObject { return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] initWithObject: anObject]); } -+ (instancetype) orderedSetWithObjects:(id)firstObject, ... ++ (instancetype) orderedSetWithObjects: (id)firstObject, ... { id set; GS_USEIDLIST(firstObject, @@ -322,28 +322,28 @@ static SEL rlSel; return AUTORELEASE(set); } -+ (instancetype) orderedSetWithObjects:(const id [])objects - count:(NSUInteger) count ++ (instancetype) orderedSetWithObjects: (const id [])objects + count: (NSUInteger) count { return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] initWithObjects: objects count: count]); } -+ (instancetype) orderedSetWithOrderedSet:(NSOrderedSet *)aSet ++ (instancetype) orderedSetWithOrderedSet: (NSOrderedSet *)aSet { return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] initWithOrderedSet: aSet]); } -+ (instancetype) orderedSetWithSet:(NSSet *)aSet ++ (instancetype) orderedSetWithSet: (NSSet *)aSet { return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] initWithSet: aSet]); } -+ (instancetype) orderedSetWithSet:(NSSet *)aSet - copyItems:(BOOL)flag ++ (instancetype) orderedSetWithSet: (NSSet *)aSet + copyItems: (BOOL)flag { return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] initWithSet: aSet @@ -351,7 +351,7 @@ static SEL rlSel; } // instance methods -- (instancetype) initWithArray:(NSArray *)other +- (instancetype) initWithArray: (NSArray *)other { unsigned count = [other count]; @@ -384,20 +384,20 @@ static SEL rlSel; return nil; } -- (instancetype) initWithArray:(NSArray *)other copyItems:(BOOL)flag +- (instancetype) initWithArray: (NSArray *)other copyItems: (BOOL)flag { unsigned count = [other count]; unsigned j = count; - + if (count == 0) { return [self init]; } - + GS_BEGINIDBUF(objs, count); { unsigned i; - + for (i = 0; i < count; i++) { if (flag == NO) @@ -410,10 +410,10 @@ static SEL rlSel; } } } - + self = [self initWithObjects: objs count: count]; - - if(flag == YES) + + if (flag == YES) { while(j--) { @@ -424,13 +424,13 @@ static SEL rlSel; return self; } -- (instancetype) initWithArray:(NSArray *)other - range:(NSRange)range - copyItems:(BOOL)flag +- (instancetype) initWithArray: (NSArray *)other + range: (NSRange)range + copyItems: (BOOL)flag { unsigned count = [other count]; unsigned i = 0, j = 0; - + if (count == 0) { return [self init]; @@ -440,13 +440,13 @@ static SEL rlSel; { unsigned loc = range.location; unsigned len = range.length; - + for (i = 0; i < count; i++) { - if(i >= loc && j < len) + if (i >= loc && j < len) { - if(flag == YES) - { + if (flag == YES) + { objs[i] = [[other objectAtIndex: i] copy]; } else @@ -455,8 +455,8 @@ static SEL rlSel; } j++; } - - if(j >= len) + + if (j >= len) { break; } @@ -464,7 +464,7 @@ static SEL rlSel; } self = [self initWithObjects: objs count: count]; - if(flag == YES) + if (flag == YES) { while(j--) { @@ -479,12 +479,12 @@ static SEL rlSel; - (instancetype) initWithObject: (id)obj { id objs[] = {obj}; - + self = [self initWithObjects: objs count: 1]; return self; } -- (instancetype) initWithObjects:(id)firstObject, ... +- (instancetype) initWithObjects: (id)firstObject, ... { GS_USEIDLIST(firstObject, self = [self initWithObjects: __objects count: __count]); @@ -497,20 +497,20 @@ static SEL rlSel; * and needs to be re-implemented in subclasses in order to have all * other initialisers work. */ -- (instancetype) initWithObjects:(const id [])objects // required override. - count:(NSUInteger)count +- (instancetype) initWithObjects: (const id [])objects // required override. + count: (NSUInteger)count { [self subclassResponsibility: _cmd]; return nil; } -- (instancetype) initWithOrderedSet:(NSOrderedSet *)aSet +- (instancetype) initWithOrderedSet: (NSOrderedSet *)aSet { return [self initWithOrderedSet: aSet copyItems: NO]; } -- (instancetype) initWithOrderedSet:(NSOrderedSet *)other - copyItems:(BOOL)flag +- (instancetype) initWithOrderedSet: (NSOrderedSet *)other + copyItems: (BOOL)flag { unsigned c = [other count]; id o, e = [other objectEnumerator]; @@ -520,9 +520,13 @@ static SEL rlSel; while ((o = [e nextObject])) { if (flag) - os[i] = [o copy]; + { + os[i] = [o copy]; + } else - os[i] = o; + { + os[i] = o; + } i++; } self = [self initWithObjects: os count: c]; @@ -537,9 +541,9 @@ static SEL rlSel; return self; } -- (instancetype) initWithOrderedSet:(NSOrderedSet *)other - range:(NSRange)range - copyItems:(BOOL)flag +- (instancetype) initWithOrderedSet: (NSOrderedSet *)other + range: (NSRange)range + copyItems: (BOOL)flag { unsigned c = [other count]; id o, e = [other objectEnumerator]; @@ -550,22 +554,26 @@ static SEL rlSel; while ((o = [e nextObject])) { - if(i >= loc && j < len) + if (i >= loc && j < len) { if (flag) - os[i] = [o copy]; + { + os[i] = [o copy]; + } else - os[i] = o; + { + os[i] = o; + } j++; } i++; - if(j >= len) + if (j >= len) { break; } } - + self = [self initWithObjects: os count: c]; if (flag) { @@ -578,14 +586,14 @@ static SEL rlSel; return self; } -- (instancetype) initWithSet:(NSSet *)aSet +- (instancetype) initWithSet: (NSSet *)aSet { return [self initWithSet: aSet copyItems: NO]; } -- (instancetype) initWithSet:(NSSet *)other copyItems:(BOOL)flag +- (instancetype) initWithSet: (NSSet *)other copyItems: (BOOL)flag { - unsigned c = [other count]; + unsigned c = [other count]; id o, e = [other objectEnumerator]; unsigned i = 0; GS_BEGINIDBUF(os, c); @@ -593,9 +601,13 @@ static SEL rlSel; while ((o = [e nextObject])) { if (flag) - os[i] = [o copy]; + { + os[i] = [o copy]; + } else - os[i] = o; + { + os[i] = o; + } i++; } self = [self initWithObjects: os count: c]; @@ -630,22 +642,22 @@ static SEL rlSel; } return YES; } - -- (void) enumerateObjectsAtIndexes:(NSIndexSet *)indexSet - options:(NSEnumerationOptions)opts - usingBlock:(GSEnumeratorBlock)aBlock + +- (void) enumerateObjectsAtIndexes: (NSIndexSet *)indexSet + options: (NSEnumerationOptions)opts + usingBlock: (GSEnumeratorBlock)aBlock { [[self objectsAtIndexes: indexSet] enumerateObjectsWithOptions: opts usingBlock: aBlock]; } -- (void) enumerateObjectsUsingBlock:(GSEnumeratorBlock)aBlock +- (void) enumerateObjectsUsingBlock: (GSEnumeratorBlock)aBlock { [self enumerateObjectsWithOptions: 0 usingBlock: aBlock]; } -- (void) enumerateObjectsWithOptions:(NSEnumerationOptions)opts - usingBlock:(GSEnumeratorBlock)aBlock +- (void) enumerateObjectsWithOptions: (NSEnumerationOptions)opts + usingBlock: (GSEnumeratorBlock)aBlock { NSUInteger count = 0; BLOCK_SCOPE BOOL shouldStop = NO; @@ -686,7 +698,9 @@ static SEL rlSel; { NSUInteger count = [self count]; if (count == 0) - return nil; + { + return nil; + } return [self objectAtIndex: 0]; } @@ -694,7 +708,9 @@ static SEL rlSel; { NSUInteger count = [self count]; if (count == 0) - return nil; + { + return nil; + } return [self objectAtIndex: count - 1]; } @@ -723,17 +739,17 @@ static SEL rlSel; return GS_IMMUTABLE(group); } -- (NSUInteger) indexOfObject:(id)anObject +- (NSUInteger) indexOfObject: (id)anObject { 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; @@ -827,22 +843,22 @@ static SEL rlSel; return NSNotFound; } -- (NSUInteger) indexOfObjectAtIndexes:(NSIndexSet *)indexSet - options:(NSEnumerationOptions)opts - passingTest:(GSPredicateBlock)predicate +- (NSUInteger) indexOfObjectAtIndexes: (NSIndexSet *)indexSet + options: (NSEnumerationOptions)opts + passingTest: (GSPredicateBlock)predicate { return [[self objectsAtIndexes: indexSet] indexOfObjectWithOptions: 0 passingTest: predicate]; } -- (NSUInteger) indexOfObjectPassingTest:(GSPredicateBlock)predicate +- (NSUInteger) indexOfObjectPassingTest: (GSPredicateBlock)predicate { return [self indexOfObjectWithOptions: 0 passingTest: predicate]; } -- (NSUInteger) indexOfObjectWithOptions:(NSEnumerationOptions)opts - passingTest:(GSPredicateBlock)predicate +- (NSUInteger) indexOfObjectWithOptions: (NSEnumerationOptions)opts + passingTest: (GSPredicateBlock)predicate { /* TODO: Concurrency. */ id enumerator = self; @@ -902,22 +918,22 @@ static SEL rlSel; return index; } -- (NSIndexSet *) indexesOfObjectsAtIndexes:(NSIndexSet *)indexSet - options:(NSEnumerationOptions)opts - passingTest:(GSPredicateBlock)predicate +- (NSIndexSet *) indexesOfObjectsAtIndexes: (NSIndexSet *)indexSet + options: (NSEnumerationOptions)opts + passingTest: (GSPredicateBlock)predicate { return [[self objectsAtIndexes: indexSet] indexesOfObjectsWithOptions: opts passingTest: predicate]; } -- (NSIndexSet *) indexesOfObjectsPassingTest:(GSPredicateBlock)predicate +- (NSIndexSet *) indexesOfObjectsPassingTest: (GSPredicateBlock)predicate { return [self indexesOfObjectsWithOptions: 0 passingTest: predicate]; } -- (NSIndexSet *) indexesOfObjectsWithOptions:(NSEnumerationOptions)opts - passingTest:(GSPredicateBlock)predicate +- (NSIndexSet *) indexesOfObjectsWithOptions: (NSEnumerationOptions)opts + passingTest: (GSPredicateBlock)predicate { /* TODO: Concurrency. */ NSMutableIndexSet *set = [NSMutableIndexSet indexSet]; @@ -981,11 +997,11 @@ static SEL rlSel; - (NSEnumerator *) reverseObjectEnumerator { - [self subclassResponsibility: _cmd]; - return nil; + [self subclassResponsibility: _cmd]; + return nil; } -- (NSOrderedSet *)reversedOrderedSet +- (NSOrderedSet *) reversedOrderedSet { NSEnumerator *e = [self reverseObjectEnumerator]; NSMutableArray *a = [NSMutableArray arrayWithCapacity: [self count]]; @@ -1014,10 +1030,10 @@ static SEL rlSel; // Key-Value Observing Support -- (void)addObserver: (NSObject *)observer - forKeyPath: (NSString *)keyPath - options: (NSKeyValueObservingOptions)options - context: (void *)context +- (void) addObserver: (NSObject *)observer + forKeyPath: (NSString *)keyPath + options: (NSKeyValueObservingOptions)options + context: (void *)context { NSException *exception = nil; NSString *reason = @"NSOrderedSet does not support KVO"; @@ -1027,8 +1043,8 @@ static SEL rlSel; [exception raise]; } -- (void)removeObserver: (NSObject *)observer - forKeyPath: (NSString *)keyPath +- (void) removeObserver: (NSObject *)observer + forKeyPath: (NSString *)keyPath { NSException *exception = nil; NSString *reason = @"NSOrderedSet does not support KVO"; @@ -1038,8 +1054,8 @@ static SEL rlSel; [exception raise]; } -- (void) removeObserver: (NSObject *)observer - forKeyPath: (NSString *)keyPath +- (void) removeObserver: (NSObject *)observer + forKeyPath: (NSString *)keyPath context: (void *)context { NSException *exception = nil; @@ -1055,8 +1071,8 @@ static SEL rlSel; { volatile id object = nil; NSEnumerator *e = [self objectEnumerator]; - - while((object = [e nextObject]) != nil) + + while ((object = [e nextObject]) != nil) { [object setValue: value forKey: key]; @@ -1074,7 +1090,9 @@ static SEL rlSel; id result = [object valueForKey: key]; if (result == nil) - continue; + { + continue; + } [results addObject: result]; } @@ -1087,10 +1105,10 @@ static SEL rlSel; if ([self count] == 0 && [aSet count] == 0) return YES; - + if (self == aSet) return YES; - + if ([self count] != [aSet count]) return NO; @@ -1101,7 +1119,10 @@ static SEL rlSel; - (BOOL) isEqual: (id)other { if ([other isKindOfClass: [NSOrderedSet class]]) - return [self isEqualToOrderedSet: other]; + { + return [self isEqualToOrderedSet: other]; + } + return NO; } @@ -1150,19 +1171,19 @@ static SEL rlSel; NSEnumerator *selfEnum = [self objectEnumerator]; NSEnumerator *otherEnum = [otherSet objectEnumerator]; NSUInteger l = [self count]; - + // -1. If this set is empty, this method should return YES. if (l == 0) { return YES; } - + // If count of set is more than otherSet it's not a subset if (l > [otherSet count]) { return NO; } - + so = [selfEnum nextObject]; // get first object in enum... while((oo = [otherEnum nextObject]) != nil) { @@ -1175,11 +1196,11 @@ static SEL rlSel; } } } - + return NO; // if all members are in set. } -- (BOOL) isSubsetOfSet:(NSSet *)otherSet +- (BOOL) isSubsetOfSet: (NSSet *)otherSet { id o = nil, e = nil; @@ -1198,12 +1219,12 @@ static SEL rlSel; } // Creating a Sorted Array -- (NSArray *) sortedArrayUsingDescriptors:(NSArray *)sortDescriptors +- (NSArray *) sortedArrayUsingDescriptors: (NSArray *)sortDescriptors { NSMutableArray *sortedArray = [NSMutableArray arrayWithArray: [self array]]; - + [sortedArray sortUsingDescriptors: sortDescriptors]; - + return GS_IMMUTABLE(sortedArray); } @@ -1215,14 +1236,14 @@ static SEL rlSel; - (NSArray *) sortedArrayWithOptions: (NSSortOptions)options - usingComparator: (NSComparator)comparator + usingComparator: (NSComparator)comparator { return [[self array] sortedArrayWithOptions: options usingComparator: comparator]; } // Filtering Ordered Sets -- (NSOrderedSet *)filteredOrderedSetUsingPredicate: (NSPredicate *)predicate +- (NSOrderedSet *) filteredOrderedSetUsingPredicate: (NSPredicate *)predicate { NSMutableOrderedSet *result = nil; NSEnumerator *e = [self objectEnumerator]; @@ -1261,12 +1282,12 @@ static SEL rlSel; NSEnumerator *en = [self objectEnumerator]; NSMutableArray *result = [NSMutableArray arrayWithCapacity: [self count]]; id o = nil; - + while((o = [en nextObject]) != nil) { [result addObject: o]; } - + return GS_IMMUTABLE(result); } @@ -1275,12 +1296,12 @@ static SEL rlSel; NSEnumerator *en = [self objectEnumerator]; NSMutableSet *result = [NSMutableSet setWithCapacity: [self count]]; id o = nil; - - while((o = [en nextObject]) != nil) + + while ((o = [en nextObject]) != nil) { [result addObject: o]; } - + return GS_IMMUTABLE(result); } @end @@ -1309,7 +1330,7 @@ static SEL rlSel; } } -+ (instancetype)orderedSetWithCapacity: (NSUInteger)capacity ++ (instancetype) orderedSetWithCapacity: (NSUInteger)capacity { return AUTORELEASE([[self allocWithZone: NSDefaultMallocZone()] initWithCapacity: capacity]); } @@ -1325,7 +1346,7 @@ static SEL rlSel; NSException *exception; NSString *reason; NSUInteger count = [self count]; - + info = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithUnsignedInteger: index], @"Index", [NSNumber numberWithUnsignedInteger: count], @"Count", @@ -1341,7 +1362,7 @@ static SEL rlSel; [exception raise]; } -- (instancetype)initWithCapacity: (NSUInteger)capacity +- (instancetype) initWithCapacity: (NSUInteger)capacity { self = [self init]; return self; @@ -1357,12 +1378,12 @@ static SEL rlSel; return self; } -- (void)addObject:(id)anObject +- (void) addObject: (id)anObject { [self subclassResponsibility: _cmd]; } -- (void)addObjects:(const id[])objects count:(NSUInteger)count +- (void) addObjects: (const id[])objects count: (NSUInteger)count { NSUInteger i = 0; for (i = 0; i < count; i++) @@ -1372,7 +1393,7 @@ static SEL rlSel; } } -- (void)addObjectsFromArray:(NSArray *)otherArray +- (void) addObjectsFromArray: (NSArray *)otherArray { NSEnumerator *en = [otherArray objectEnumerator]; id obj = nil; @@ -1382,12 +1403,12 @@ static SEL rlSel; } } -- (void)insertObject:(id)object atIndex:(NSUInteger)index // required override +- (void) insertObject: (id)object atIndex: (NSUInteger)index // required override { [self subclassResponsibility: _cmd]; } -- (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index +- (void) setObject: (id)object atIndexedSubscript: (NSUInteger)index { if ([self count] == index) { @@ -1399,12 +1420,12 @@ static SEL rlSel; } } -- (void)insertObjects:(NSArray *)array atIndexes:(NSIndexSet *)indexes +- (void) insertObjects: (NSArray *)array atIndexes: (NSIndexSet *)indexes { NSUInteger index = [indexes firstIndex]; NSEnumerator *enumerator = [array objectEnumerator]; id object = [enumerator nextObject]; - + while (object != nil && index != NSNotFound) { [self insertObject: object atIndex: index]; @@ -1413,7 +1434,7 @@ static SEL rlSel; } } -- (void)removeObject:(id)anObject +- (void) removeObject: (id)anObject { NSUInteger i; @@ -1457,7 +1478,7 @@ static SEL rlSel; } } -- (void)removeObjectAtIndex:(NSUInteger)index // required override +- (void) removeObjectAtIndex: (NSUInteger)index // required override { [self subclassResponsibility: _cmd]; } @@ -1515,7 +1536,7 @@ static SEL rlSel; } } -- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes +- (void) removeObjectsAtIndexes: (NSIndexSet *)indexes { NSUInteger count = [indexes count]; NSUInteger indexArray[count]; @@ -1528,7 +1549,7 @@ static SEL rlSel; numIndices: count]; } -- (void)removeObjectsInArray:(NSArray *)otherArray +- (void) removeObjectsInArray: (NSArray *)otherArray { NSUInteger c = [otherArray count]; @@ -1543,7 +1564,7 @@ static SEL rlSel; } } -- (void)removeObjectsInRange:(NSRange)aRange +- (void) removeObjectsInRange: (NSRange)aRange { NSUInteger i; NSUInteger s = aRange.location; @@ -1552,7 +1573,9 @@ static SEL rlSel; i = aRange.location + aRange.length; if (c < i) - i = c; + { + i = c; + } if (i > s) { @@ -1565,7 +1588,7 @@ static SEL rlSel; } } -- (void)removeAllObjects +- (void) removeAllObjects { NSUInteger c = [self count]; @@ -1575,11 +1598,11 @@ static SEL rlSel; { [self removeObjectAtIndex: 0]; } - } + } } -- (void)replaceObjectAtIndex:(NSUInteger)index // required override... - withObject:(id)object +- (void) replaceObjectAtIndex: (NSUInteger)index // required override... + withObject: (id)object { [self subclassResponsibility: _cmd]; } @@ -1592,7 +1615,7 @@ static SEL rlSel; // Remove the objects [self removeObjectsAtIndexes: indexes]; - + // Get the indexes [indexes getIndexes: indexArray maxCount: count @@ -1613,20 +1636,23 @@ static SEL rlSel; { id o = nil; NSUInteger i = count; - + if (count < (aRange.location + aRange.length)) - [NSException raise: NSRangeException - format: @"Replacing objects beyond end of const[] id."]; + { + [NSException raise: NSRangeException + format: @"Replacing objects beyond end of const[] id."]; + } + [self removeObjectsInRange: aRange]; while (i-- > 0) { o = objects[i]; - [self insertObject: o atIndex: aRange.location]; + [self insertObject: o atIndex: aRange.location]; } } -- (void)setObject:(id)anObject atIndex:(NSUInteger)anIndex +- (void) setObject: (id)anObject atIndex: (NSUInteger)anIndex { if ([self count] == anIndex) { @@ -1638,7 +1664,7 @@ static SEL rlSel; } } -- (void)moveObjectsAtIndexes:(NSIndexSet *)indexes toIndex:(NSUInteger)index +- (void) moveObjectsAtIndexes: (NSIndexSet *)indexes toIndex: (NSUInteger)index { NSUInteger count = [indexes count]; NSUInteger i = count; @@ -1646,7 +1672,7 @@ static SEL rlSel; NSMutableArray *tmpArray = [NSMutableArray arrayWithCapacity: count]; id o = nil; NSEnumerator *e = nil; - + [indexes getIndexes: indexArray maxCount: count inIndexRange: NULL]; @@ -1659,26 +1685,26 @@ static SEL rlSel; [tmpArray addObject: obj]; } - // Remove the originals... - for(i = 0; i < count; i++) + // Remove the originals... + for (i = 0; i < count; i++) { NSUInteger index = indexArray[i]; [self removeObjectAtIndex: index]; } - + // Move the objects e = [tmpArray objectEnumerator]; - while((o = [e nextObject]) != nil) + while ((o = [e nextObject]) != nil) { [self insertObject: o atIndex: index]; } } -- (void) exchangeObjectAtIndex:(NSUInteger)index - withObjectAtIndex:(NSUInteger)otherIndex +- (void) exchangeObjectAtIndex: (NSUInteger)index + withObjectAtIndex: (NSUInteger)otherIndex { NSUInteger count = [self count]; - + GS_BEGINIDBUF(objs, count); if (index >= count) { @@ -1692,26 +1718,26 @@ static SEL rlSel; { id tmp = nil; NSRange range = NSMakeRange(0,[self count]); - - [self getObjects: objs range: range]; + + [self getObjects: objs range: range]; tmp = objs[index]; objs[index] = objs[otherIndex]; objs[otherIndex] = tmp; - + [self removeAllObjects]; [self addObjects: objs count: count]; } GS_ENDIDBUF(); } -- (void)filterUsingPredicate:(NSPredicate *)predicate +- (void) filterUsingPredicate: (NSPredicate *)predicate { unsigned count = [self count]; while (count-- > 0) { id object = [self objectAtIndex: count]; - + if ([predicate evaluateWithObject: object] == NO) { [self removeObjectAtIndex: count]; @@ -1719,7 +1745,7 @@ static SEL rlSel; } } -- (void) sortUsingDescriptors:(NSArray *)descriptors +- (void) sortUsingDescriptors: (NSArray *)descriptors { NSArray *result = [[self array] sortedArrayUsingDescriptors: descriptors]; [self removeAllObjects]; @@ -1735,7 +1761,7 @@ static SEL rlSel; usingComparator: (NSComparator)comparator { NSUInteger count = [self count]; - + if ((1 < count) && (NULL != comparator)) { NSArray *res = nil; @@ -1747,7 +1773,7 @@ static SEL rlSel; { objects[i] = (*get)(self, oaiSel, i); } - + if (options & NSSortStable) { if (options & NSSortConcurrent) @@ -1780,16 +1806,16 @@ static SEL rlSel; // RELEASE(res); GS_ENDIDBUF(); - } + } } - (void) sortRange: (NSRange)range - options:(NSSortOptions)options + options: (NSSortOptions)options usingComparator: (NSComparator)comparator { } -- (void) intersectOrderedSet:(NSOrderedSet *)other +- (void) intersectOrderedSet: (NSOrderedSet *)other { if (other != self) { @@ -1806,11 +1832,11 @@ static SEL rlSel; } } -- (void) intersectSet:(NSSet *)other +- (void) intersectSet: (NSSet *)other { id keys = [self objectEnumerator]; id key; - + while ((key = [keys nextObject])) { if ([other containsObject: key] == NO) @@ -1820,9 +1846,9 @@ static SEL rlSel; } } -- (void) minusOrderedSet:(NSOrderedSet *)other +- (void) minusOrderedSet: (NSOrderedSet *)other { - if(other == self) + if (other == self) { [self removeAllObjects]; } @@ -1830,7 +1856,7 @@ static SEL rlSel; { id keys = [other objectEnumerator]; id key; - + while ((key = [keys nextObject])) { [self removeObject: key]; @@ -1838,18 +1864,18 @@ static SEL rlSel; } } -- (void) minusSet:(NSSet *)other +- (void) minusSet: (NSSet *)other { id keys = [other objectEnumerator]; id key; - + while ((key = [keys nextObject])) { [self removeObject: key]; } } -- (void) unionOrderedSet:(NSOrderedSet *)other +- (void) unionOrderedSet: (NSOrderedSet *)other { if (other != self) { @@ -1863,11 +1889,11 @@ static SEL rlSel; } } -- (void) unionSet:(NSSet *)other +- (void) unionSet: (NSSet *)other { id keys = [other objectEnumerator]; id key; - + while ((key = [keys nextObject])) { [self addObject: key]; From cbe54c4073e7fb57e8b8b4b0abccd18a67086f54 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Thu, 27 Jun 2019 14:29:01 +0200 Subject: [PATCH 04/10] Remove commented out code. --- Source/NSOrderedSet.m | 48 ++++--------------------------------------- 1 file changed, 4 insertions(+), 44 deletions(-) diff --git a/Source/NSOrderedSet.m b/Source/NSOrderedSet.m index 7cd52a30a..2ad5c3af7 100644 --- a/Source/NSOrderedSet.m +++ b/Source/NSOrderedSet.m @@ -108,23 +108,6 @@ static SEL rlSel; // NSCoding - (instancetype) initWithCoder: (NSCoder *)coder { - /* - 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 == NSMutableOrderedSet_abstract_class) - { - DESTROY(self); - self = [NSMutableOrderedSet_concrete_class allocWithZone: NSDefaultMallocZone()]; - return [self initWithCoder: coder]; - } - */ if ([coder allowsKeyedCoding]) { id array; @@ -265,16 +248,7 @@ static SEL rlSel; */ if (count > 0) { - // IMP imp = [self methodForSelector: @selector(objectAtIndex:)]; - // int i; - // int p = state->state; - - [self getObjects: stackbuf range: NSMakeRange(state->state, count)]; - /*for (i = 0; i < count; i++, p++) - { - stackbuf[i] = (*imp)(self, @selector(objectAtIndex:), p); - }*/ state->state += count; } else @@ -1446,7 +1420,6 @@ static SEL rlSel; 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]; @@ -1457,24 +1430,10 @@ static SEL rlSel; 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); + [self removeObjectAtIndex: i]; break; // since this is a set we should only have one copy... } } - //if (rem != 0) - // { - // RELEASE(anObject); - // } } } @@ -1804,7 +1763,7 @@ static SEL rlSel; [self removeAllObjects]; [self addObjectsFromArray: res]; - // RELEASE(res); + RELEASE(res); GS_ENDIDBUF(); } } @@ -1813,11 +1772,12 @@ static SEL rlSel; options: (NSSortOptions)options usingComparator: (NSComparator)comparator { + // FIXME: Implementation missing } - (void) intersectOrderedSet: (NSOrderedSet *)other { - if (other != self) + if (other != self) { id keys = [self objectEnumerator]; id key; From 3004e91330bfca063efe6aa41edf378a743f8ab9 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Thu, 27 Jun 2019 15:03:23 +0200 Subject: [PATCH 05/10] Move some more methods from the concrete to the abstract class. Move the _raiseRangeExceptionWithIndex:from: method the class it is declared on. Simplify the code. --- Source/GSOrderedSet.m | 12 --- Source/NSOrderedSet.m | 213 ++++++++++++++++-------------------------- 2 files changed, 80 insertions(+), 145 deletions(-) diff --git a/Source/GSOrderedSet.m b/Source/GSOrderedSet.m index 8e21d672f..e7e6513f0 100644 --- a/Source/GSOrderedSet.m +++ b/Source/GSOrderedSet.m @@ -243,11 +243,6 @@ static Class mutableSetClass; } } -- (void) addObject: (id)anObject -{ - [self insertObject: anObject atIndex: [self count]]; -} - - (void) insertObject: (id)object atIndex: (NSUInteger)index { GSIArrayItem item; @@ -273,13 +268,6 @@ static Class mutableSetClass; GSIArrayRemoveItemAtIndex(&array, index); } -- (void) replaceObjectAtIndex: (NSUInteger)index - withObject: (id)obj -{ - [self removeObjectAtIndex: index]; - [self insertObject: obj atIndex: index]; -} - - (id) init { return [self initWithCapacity: 0]; diff --git a/Source/NSOrderedSet.m b/Source/NSOrderedSet.m index 2ad5c3af7..0886eab2d 100644 --- a/Source/NSOrderedSet.m +++ b/Source/NSOrderedSet.m @@ -100,6 +100,29 @@ static SEL rlSel; } } + +- (void) _raiseRangeExceptionWithIndex: (NSUInteger)index from: (SEL)sel +{ + NSDictionary *info; + NSException *exception; + NSString *reason; + NSUInteger count = [self count]; + + info = [NSDictionary dictionaryWithObjectsAndKeys: + [NSNumber numberWithUnsignedInteger: index], @"Index", + [NSNumber numberWithUnsignedInteger: count], @"Count", + self, @"GSMutableSet", nil, nil]; + + reason = [NSString stringWithFormat: + @"Index %"PRIuPTR" is out of range %d (in '%@')", + index, count, NSStringFromSelector(sel)]; + + exception = [NSException exceptionWithName: NSRangeException + reason: reason + userInfo: info]; + [exception raise]; +} + - (Class) classForCoder { return NSOrderedSet_abstract_class; @@ -696,7 +719,7 @@ static SEL rlSel; - (id) objectAtIndexedSubscript: (NSUInteger)index { - return[self objectAtIndex: index]; + return [self objectAtIndex: index]; } - (NSArray *) objectsAtIndexes: (NSIndexSet *)indexes @@ -993,13 +1016,17 @@ static SEL rlSel; - (void) getObjects: (__unsafe_unretained id[])aBuffer range: (NSRange)aRange { - NSUInteger i, j = 0, c = [self count], e = aRange.location + aRange.length; + NSUInteger i, j = 0; + NSUInteger c = [self count]; + NSUInteger e = aRange.location + aRange.length; IMP get = [self methodForSelector: oaiSel]; GS_RANGE_CHECK(aRange, c); for (i = aRange.location; i < e; i++) - aBuffer[j++] = (*get)(self, oaiSel, i); + { + aBuffer[j++] = (*get)(self, oaiSel, i); + } } // Key-Value Observing Support @@ -1009,41 +1036,32 @@ static SEL rlSel; options: (NSKeyValueObservingOptions)options context: (void *)context { - NSException *exception = nil; - NSString *reason = @"NSOrderedSet does not support KVO"; - exception = [NSException exceptionWithName: NSGenericException - reason: reason - userInfo: nil]; - [exception raise]; + [[NSException exceptionWithName: NSGenericException + reason: @"NSOrderedSet does not support KVO" + userInfo: nil] raise]; } - (void) removeObserver: (NSObject *)observer forKeyPath: (NSString *)keyPath { - NSException *exception = nil; - NSString *reason = @"NSOrderedSet does not support KVO"; - exception = [NSException exceptionWithName: NSGenericException - reason: reason - userInfo: nil]; - [exception raise]; + [[NSException exceptionWithName: NSGenericException + reason: @"NSOrderedSet does not support KVO" + userInfo: nil] raise]; } - (void) removeObserver: (NSObject *)observer forKeyPath: (NSString *)keyPath context: (void *)context { - NSException *exception = nil; - NSString *reason = @"NSOrderedSet does not support KVO"; - exception = [NSException exceptionWithName: NSGenericException - reason: reason - userInfo: nil]; - [exception raise]; + [[NSException exceptionWithName: NSGenericException + reason: @"NSOrderedSet does not support KVO" + userInfo: nil] raise]; } // Key value coding support - (void) setValue: (id)value forKey: (NSString*)key { - volatile id object = nil; + id object; NSEnumerator *e = [self objectEnumerator]; while ((object = [e nextObject]) != nil) @@ -1056,7 +1074,7 @@ static SEL rlSel; - (id) valueForKey: (NSString*)key { NSEnumerator *e = [self objectEnumerator]; - id object = nil; + id object; NSMutableSet *results = [NSMutableSet setWithCapacity: [self count]]; while ((object = [e nextObject]) != nil) @@ -1103,8 +1121,8 @@ static SEL rlSel; // Set operations - (BOOL) intersectsOrderedSet: (NSOrderedSet *)otherSet { - id o = nil; - NSEnumerator *e = nil; + id o; + NSEnumerator *e; // -1. If this set is empty, this method should return NO. if ([self count] == 0) @@ -1314,28 +1332,6 @@ static SEL rlSel; return NSMutableOrderedSet_abstract_class; } -- (void) _raiseRangeExceptionWithIndex: (NSUInteger)index from: (SEL)sel -{ - NSDictionary *info; - NSException *exception; - NSString *reason; - NSUInteger count = [self count]; - - info = [NSDictionary dictionaryWithObjectsAndKeys: - [NSNumber numberWithUnsignedInteger: index], @"Index", - [NSNumber numberWithUnsignedInteger: count], @"Count", - self, @"GSMutableSet", nil, nil]; - - reason = [NSString stringWithFormat: - @"Index %"PRIuPTR" is out of range %d (in '%@')", - index, count, NSStringFromSelector(sel)]; - - exception = [NSException exceptionWithName: NSRangeException - reason: reason - userInfo: info]; - [exception raise]; -} - - (instancetype) initWithCapacity: (NSUInteger)capacity { self = [self init]; @@ -1354,7 +1350,7 @@ static SEL rlSel; - (void) addObject: (id)anObject { - [self subclassResponsibility: _cmd]; + [self insertObject: anObject atIndex: [self count]]; } - (void) addObjects: (const id[])objects count: (NSUInteger)count @@ -1410,30 +1406,18 @@ static SEL rlSel; - (void) removeObject: (id)anObject { - NSUInteger i; + NSUInteger index; if (anObject == nil) { NSWarnMLog(@"attempt to remove nil object"); return; } - i = [self count]; - if (i > 0) + + index = [self indexOfObject: anObject]; + if (NSNotFound != index) { - 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) - { - [self removeObjectAtIndex: i]; - break; // since this is a set we should only have one copy... - } - } + [self removeObjectAtIndex: index]; } } @@ -1442,59 +1426,6 @@ static SEL rlSel; [self subclassResponsibility: _cmd]; } -- (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(); - } -} - - (void) removeObjectsAtIndexes: (NSIndexSet *)indexes { NSUInteger count = [indexes count]; @@ -1504,8 +1435,15 @@ static SEL rlSel; maxCount: count inIndexRange: NULL]; - [self _removeObjectsFromIndices: indexArray - numIndices: count]; + if (count > 0) + { + IMP rem = [self methodForSelector: remSel]; + + while (count--) + { + (*rem)(self, remSel, indexArray[count]); + } + } } - (void) removeObjectsInArray: (NSArray *)otherArray @@ -1560,10 +1498,11 @@ static SEL rlSel; } } -- (void) replaceObjectAtIndex: (NSUInteger)index // required override... +- (void) replaceObjectAtIndex: (NSUInteger)index withObject: (id)object { - [self subclassResponsibility: _cmd]; + [self removeObjectAtIndex: index]; + [self insertObject: object atIndex: index]; } - (void) replaceObjectsAtIndexes: (NSIndexSet *)indexes @@ -1644,7 +1583,7 @@ static SEL rlSel; [tmpArray addObject: obj]; } - // Remove the originals... + // Remove the originals... for (i = 0; i < count; i++) { NSUInteger index = indexArray[i]; @@ -1664,7 +1603,6 @@ static SEL rlSel; { NSUInteger count = [self count]; - GS_BEGINIDBUF(objs, count); if (index >= count) { [self _raiseRangeExceptionWithIndex: index from: _cmd]; @@ -1675,18 +1613,27 @@ static SEL rlSel; } if (index != otherIndex) { - id tmp = nil; - NSRange range = NSMakeRange(0,[self count]); + NSUInteger low, high; + id obj1, obj2; - [self getObjects: objs range: range]; - tmp = objs[index]; - objs[index] = objs[otherIndex]; - objs[otherIndex] = tmp; + if (index > otherIndex) + { + high = index; + low = otherIndex; + } + else + { + high = otherIndex; + low = index; + } - [self removeAllObjects]; - [self addObjects: objs count: count]; + obj1 = [self objectAtIndex: low]; + obj2 = [self objectAtIndex: high]; + [self removeObjectAtIndex: high]; + [self removeObjectAtIndex: low]; + [self insertObject: obj2 atIndex: low]; + [self insertObject: obj1 atIndex: high]; } - GS_ENDIDBUF(); } - (void) filterUsingPredicate: (NSPredicate *)predicate From ff675b2e78d2d8afc10a5959aad8d39d43975cd0 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Thu, 27 Jun 2019 15:10:14 +0200 Subject: [PATCH 06/10] Format the test code. --- Tests/base/NSOrderedSet/basic.m | 172 ++++++++++++++++---------------- 1 file changed, 86 insertions(+), 86 deletions(-) diff --git a/Tests/base/NSOrderedSet/basic.m b/Tests/base/NSOrderedSet/basic.m index 03e9f2617..532e93e99 100644 --- a/Tests/base/NSOrderedSet/basic.m +++ b/Tests/base/NSOrderedSet/basic.m @@ -117,40 +117,40 @@ static NSString *stringData = @"" int main() { START_SET("NSOrderedSet base") - + NSOrderedSet *testObj, *testObj2; NSMutableOrderedSet *mutableTest1, *mutableTest2; NSMutableArray *testObjs = [NSMutableArray new]; NSData *data = [stringData dataUsingEncoding: NSUTF8StringEncoding]; - + testObj = [NSOrderedSet new]; [testObjs addObject: testObj]; PASS(testObj != nil && [testObj count] == 0, "can create an empty ordered set"); - + testObj = [NSOrderedSet orderedSetWithObject: @"Hello"]; [testObjs addObject: testObj]; PASS(testObj != nil && [testObj count] == 1, "can create an ordered set with one element"); - + id objs[] = {@"Hello", @"Hello1"}; testObj = [NSOrderedSet orderedSetWithObjects: objs count: 2]; [testObjs addObject: testObj]; PASS(testObj != nil && [testObj count] == 2, "can create an ordered set with multi element"); - + id objs1[] = {@"Hello", @"Hello"}; testObj = [NSOrderedSet orderedSetWithObjects: objs1 count: 2]; [testObjs addObject: testObj]; PASS(testObj != nil && [testObj count] == 1, "cannot create an ordered set with multiple like elements"); - + id objs2[] = {@"Hello"}; testObj = [NSOrderedSet orderedSetWithObjects: objs2 count: 2]; [testObjs addObject: testObj]; PASS(testObj != nil && [testObj count] == 1, "Does not throw exception when count != to number of elements"); - + NSMutableArray *arr = [NSMutableArray array]; [arr addObject: @"Hello"]; [arr addObject: @"World"]; @@ -158,7 +158,7 @@ int main() [testObjs addObject: testObj]; PASS(testObj != nil && [testObj count] == 2, "Is able to initialize with array"); - + id objs3[] = {@"Hello"}; id objc4[] = {@"World"}; testObj = [NSOrderedSet orderedSetWithObjects: objs3 count: 1]; @@ -168,7 +168,7 @@ int main() BOOL result = [testObj intersectsOrderedSet: testObj2]; PASS(result == NO, "Sets do not intersect!"); - + id objs5[] = {@"Hello"}; id objc6[] = {@"Hello"}; testObj = [NSOrderedSet orderedSetWithObjects: objs5 count: 1]; @@ -178,85 +178,85 @@ int main() BOOL result1 = [testObj intersectsOrderedSet: testObj2]; PASS(result1 == YES, "Sets do intersect!"); - + id o1 = @"Hello"; id o2 = @"World"; mutableTest1 = [NSMutableOrderedSet orderedSet]; - [mutableTest1 addObject:o1]; + [mutableTest1 addObject: o1]; [testObjs addObject: mutableTest1]; mutableTest2 = [NSMutableOrderedSet orderedSet]; - [mutableTest2 addObject:o2]; + [mutableTest2 addObject: o2]; [testObjs addObject: mutableTest2]; - [mutableTest1 unionOrderedSet:mutableTest2]; + [mutableTest1 unionOrderedSet: mutableTest2]; PASS(mutableTest1 != nil && mutableTest2 != nil && [mutableTest1 count] == 2, "mutableSets union properly"); - + id o3 = @"Hello"; id o4 = @"World"; mutableTest1 = [NSMutableOrderedSet orderedSet]; - [mutableTest1 addObject:o3]; + [mutableTest1 addObject: o3]; [testObjs addObject: mutableTest1]; mutableTest2 = [NSMutableOrderedSet orderedSet]; - [mutableTest2 addObject:o4]; + [mutableTest2 addObject: o4]; [testObjs addObject: mutableTest2]; - [mutableTest1 intersectOrderedSet:mutableTest2]; + [mutableTest1 intersectOrderedSet: mutableTest2]; PASS(mutableTest1 != nil && mutableTest2 != nil && [mutableTest1 count] == 0, "mutableSets do not intersect"); - + id o5 = @"Hello"; id o6 = @"Hello"; mutableTest1 = [NSMutableOrderedSet orderedSet]; - [mutableTest1 addObject:o5]; + [mutableTest1 addObject: o5]; [testObjs addObject: mutableTest1]; mutableTest2 = [NSMutableOrderedSet orderedSet]; - [mutableTest2 addObject:o6]; + [mutableTest2 addObject: o6]; [testObjs addObject: mutableTest2]; - [mutableTest1 intersectOrderedSet:mutableTest2]; + [mutableTest1 intersectOrderedSet: mutableTest2]; PASS(mutableTest1 != nil && mutableTest2 != nil && [mutableTest1 count] == 1, "mutableSets do intersect"); id o7 = @"Hello"; id o8 = @"World"; mutableTest1 = [NSMutableOrderedSet orderedSet]; - [mutableTest1 addObject:o7]; - [mutableTest1 addObject:o8]; + [mutableTest1 addObject: o7]; + [mutableTest1 addObject: o8]; [testObjs addObject: mutableTest1]; mutableTest2 = [NSMutableOrderedSet orderedSet]; - [mutableTest2 addObject:o7]; + [mutableTest2 addObject: o7]; [testObjs addObject: mutableTest2]; - BOOL isSubset = [mutableTest2 isSubsetOfOrderedSet:mutableTest1]; + BOOL isSubset = [mutableTest2 isSubsetOfOrderedSet: mutableTest1]; PASS(isSubset, "mutableTest2 is subset of mutableTest1"); - + id o9 = @"Hello"; id o10 = @"World"; id o11 = @"Ready"; mutableTest1 = [NSMutableOrderedSet orderedSet]; - [mutableTest1 addObject:o9]; + [mutableTest1 addObject: o9]; [testObjs addObject: mutableTest1]; mutableTest2 = [NSMutableOrderedSet orderedSet]; - [mutableTest2 addObject:o10]; - [mutableTest2 addObject:o9]; + [mutableTest2 addObject: o10]; + [mutableTest2 addObject: o9]; [testObjs addObject: mutableTest2]; - isSubset = [mutableTest2 isSubsetOfOrderedSet:mutableTest1]; + isSubset = [mutableTest2 isSubsetOfOrderedSet: mutableTest1]; PASS(isSubset == NO, "mutableTest2 is not subset of mutableTest1"); - + o9 = @"Hello"; o10 = @"World"; o11 = @"Ready"; id o12 = @"ToGo"; mutableTest1 = [NSMutableOrderedSet orderedSet]; - [mutableTest1 addObject:o9]; - [mutableTest1 addObject:o10]; - [mutableTest1 addObject:o12]; - [mutableTest1 addObject:o11]; + [mutableTest1 addObject: o9]; + [mutableTest1 addObject: o10]; + [mutableTest1 addObject: o12]; + [mutableTest1 addObject: o11]; [testObjs addObject: mutableTest1]; mutableTest2 = [NSMutableOrderedSet orderedSet]; - [mutableTest2 addObject:o9]; - [mutableTest2 addObject:o10]; + [mutableTest2 addObject: o9]; + [mutableTest2 addObject: o10]; [testObjs addObject: mutableTest2]; - isSubset = [mutableTest2 isSubsetOfOrderedSet:mutableTest1]; + isSubset = [mutableTest2 isSubsetOfOrderedSet: mutableTest1]; PASS(isSubset, "mutableTest2 is subset of mutableTest1"); @@ -265,12 +265,12 @@ int main() o11 = @"Ready"; o12 = @"ToGo"; mutableTest1 = [NSMutableOrderedSet orderedSet]; - [mutableTest1 addObject:o9]; - [mutableTest1 addObject:o10]; - [mutableTest1 addObject:o12]; - [mutableTest1 addObject:o11]; + [mutableTest1 addObject: o9]; + [mutableTest1 addObject: o10]; + [mutableTest1 addObject: o12]; + [mutableTest1 addObject: o11]; [testObjs addObject: mutableTest1]; - PASS([mutableTest1 isEqual:mutableTest1], + PASS([mutableTest1 isEqual: mutableTest1], "mutableTest1 is equal to itself"); o9 = @"Hello"; @@ -278,64 +278,64 @@ int main() o11 = @"Ready"; o12 = @"ToGo"; NSMutableOrderedSet *mutableTest3 = [NSMutableOrderedSet orderedSet]; - [mutableTest3 addObject:o9]; - [mutableTest3 addObject:o10]; - [mutableTest3 addObject:o12]; - [mutableTest3 addObject:o11]; - [mutableTest3 insertObject:@"Hello" atIndex:2]; + [mutableTest3 addObject: o9]; + [mutableTest3 addObject: o10]; + [mutableTest3 addObject: o12]; + [mutableTest3 addObject: o11]; + [mutableTest3 insertObject: @"Hello" atIndex: 2]; [testObjs addObject: mutableTest3]; - PASS([mutableTest3 isEqual:mutableTest1] == YES, + PASS([mutableTest3 isEqual: mutableTest1] == YES, "Insert at index does not replace existing object"); - + NSMutableOrderedSet *mutableTest4 = [NSMutableOrderedSet orderedSet]; - [mutableTest4 addObject:@"Now"]; - [mutableTest4 addObject:@"is"]; - [mutableTest4 addObject:@"the"]; - [mutableTest4 addObject:@"time"]; - [mutableTest4 addObject:@"for"]; - [mutableTest4 addObject:@"all"]; - [mutableTest4 addObject:@"Good"]; - [mutableTest4 addObject:@"men"]; - [mutableTest4 addObject:@"to"]; - [mutableTest4 addObject:@"come"]; - [mutableTest4 addObject:@"to the aid"]; - [mutableTest4 addObject:@"of their country"]; - [mutableTest4 moveObjectsAtIndexes:[NSIndexSet indexSetWithIndex:3] toIndex:10]; + [mutableTest4 addObject: @"Now"]; + [mutableTest4 addObject: @"is"]; + [mutableTest4 addObject: @"the"]; + [mutableTest4 addObject: @"time"]; + [mutableTest4 addObject: @"for"]; + [mutableTest4 addObject: @"all"]; + [mutableTest4 addObject: @"Good"]; + [mutableTest4 addObject: @"men"]; + [mutableTest4 addObject: @"to"]; + [mutableTest4 addObject: @"come"]; + [mutableTest4 addObject: @"to the aid"]; + [mutableTest4 addObject: @"of their country"]; + [mutableTest4 moveObjectsAtIndexes: [NSIndexSet indexSetWithIndex: 3] toIndex: 10]; [testObjs addObject: mutableTest4]; - PASS([[mutableTest4 objectAtIndex: 10] isEqual:@"time"] == YES, + PASS([[mutableTest4 objectAtIndex: 10] isEqual: @"time"] == YES, "Move to index moves to correct index"); NSMutableOrderedSet *mutableTest5 = [NSMutableOrderedSet orderedSet]; - [mutableTest5 addObject:@"Now"]; - [mutableTest5 addObject:@"is"]; - [mutableTest5 addObject:@"the"]; - [mutableTest5 exchangeObjectAtIndex:0 withObjectAtIndex:2]; + [mutableTest5 addObject: @"Now"]; + [mutableTest5 addObject: @"is"]; + [mutableTest5 addObject: @"the"]; + [mutableTest5 exchangeObjectAtIndex: 0 withObjectAtIndex: 2]; [testObjs addObject: mutableTest5]; - PASS([[mutableTest5 objectAtIndex: 0] isEqual:@"the"] == YES && - [[mutableTest5 objectAtIndex: 2] isEqual:@"Now"] == YES, + PASS([[mutableTest5 objectAtIndex: 0] isEqual: @"the"] == YES && + [[mutableTest5 objectAtIndex: 2] isEqual: @"Now"] == YES, "Exchanges indexes properly"); //NSLog(@"RESULT: %@",mutableTest4); mutableTest4 = [NSMutableOrderedSet orderedSet]; - [mutableTest4 addObject:@"Now"]; - [mutableTest4 addObject:@"is"]; - [mutableTest4 addObject:@"the"]; - [mutableTest4 addObject:@"time"]; - [mutableTest4 addObject:@"for"]; - [mutableTest4 addObject:@"all"]; - [mutableTest4 addObject:@"Good"]; - [mutableTest4 addObject:@"men"]; - [mutableTest4 addObject:@"to"]; - [mutableTest4 addObject:@"come to"]; - [mutableTest4 addObject:@"the aid"]; - [mutableTest4 addObject:@"of their country"]; - NSMutableIndexSet *is = [NSMutableIndexSet indexSetWithIndex:6]; + [mutableTest4 addObject: @"Now"]; + [mutableTest4 addObject: @"is"]; + [mutableTest4 addObject: @"the"]; + [mutableTest4 addObject: @"time"]; + [mutableTest4 addObject: @"for"]; + [mutableTest4 addObject: @"all"]; + [mutableTest4 addObject: @"Good"]; + [mutableTest4 addObject: @"men"]; + [mutableTest4 addObject: @"to"]; + [mutableTest4 addObject: @"come to"]; + [mutableTest4 addObject: @"the aid"]; + [mutableTest4 addObject: @"of their country"]; + NSMutableIndexSet *is = [NSMutableIndexSet indexSetWithIndex: 6]; [is addIndex: 9]; - NSMutableArray *array = [NSMutableArray arrayWithObjects:@"Horrible", @"Flee From", nil]; + NSMutableArray *array = [NSMutableArray arrayWithObjects: @"Horrible", @"Flee From", nil]; [mutableTest4 replaceObjectsAtIndexes: is withObjects: array]; [testObjs addObject: mutableTest4]; - PASS([[mutableTest4 objectAtIndex: 9] isEqual:@"Flee From"] == YES, + PASS([[mutableTest4 objectAtIndex: 9] isEqual: @"Flee From"] == YES, "replaceObjectsAtIndexes: adds to correct indexes"); id uobj = [NSKeyedUnarchiver unarchiveObjectWithData: data]; @@ -343,7 +343,7 @@ int main() [uobj isKindOfClass: [NSMutableOrderedSet class]] && [uobj containsObject: @"Now"]), "Object unarchives correctly from macOS archive") - + test_NSObject(@"NSOrderedSet", testObjs); test_NSCoding(testObjs); test_NSCopying(@"NSOrderedSet", @"NSMutableOrderedSet", testObjs, YES, NO); From eff6575d9fb5e89b8385bd863eb990598ee4a18b Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Thu, 27 Jun 2019 15:26:01 +0200 Subject: [PATCH 07/10] Add test for isSubsetOfSet: and correct implementation to fit the test. --- Source/NSOrderedSet.m | 38 ++++++++++++++++++++++----------- Tests/base/NSOrderedSet/basic.m | 23 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/Source/NSOrderedSet.m b/Source/NSOrderedSet.m index 0886eab2d..80cf064e8 100644 --- a/Source/NSOrderedSet.m +++ b/Source/NSOrderedSet.m @@ -1159,7 +1159,7 @@ static SEL rlSel; - (BOOL) isSubsetOfOrderedSet: (NSOrderedSet *)otherSet { - id so = nil, oo = nil; + id so, oo; NSEnumerator *selfEnum = [self objectEnumerator]; NSEnumerator *otherEnum = [otherSet objectEnumerator]; NSUInteger l = [self count]; @@ -1177,9 +1177,9 @@ static SEL rlSel; } so = [selfEnum nextObject]; // get first object in enum... - while((oo = [otherEnum nextObject]) != nil) + while ((oo = [otherEnum nextObject]) != nil) { - if([oo isEqual: so]) // if object is equal advance + if ([oo isEqual: so]) // if object is equal advance { so = [selfEnum nextObject]; if(so == nil) @@ -1194,18 +1194,30 @@ static SEL rlSel; - (BOOL) isSubsetOfSet: (NSSet *)otherSet { - id o = nil, e = nil; + id o, e; + NSUInteger l = [self count]; - // -1. If this set is empty, this method should return NO. - if ([self count] == 0) - return NO; - - // 0. Loop for all members in otherSet - e = [otherSet objectEnumerator]; - while ((o = [e nextObject])) // 1. pick a member from otherSet. + // -1. If this set is empty, this method should return YES. + if (l == 0) { - if ([self containsObject: o] == NO) // 2. check the member is in this set(self). - return NO; + return YES; + } + + // If count of set is more than otherSet it's not a subset + if (l > [otherSet count]) + { + return NO; + } + + // 0. Loop for all members in self + e = [self objectEnumerator]; + while ((o = [e nextObject])) // 1. pick a member from self. + { + if ([otherSet containsObject: o] == NO) // 2. check the member is in otherset. + { + NSLog(@"Object not contained %@", o); + return NO; + } } return YES; // if all members are in set. } diff --git a/Tests/base/NSOrderedSet/basic.m b/Tests/base/NSOrderedSet/basic.m index 532e93e99..28289cf57 100644 --- a/Tests/base/NSOrderedSet/basic.m +++ b/Tests/base/NSOrderedSet/basic.m @@ -122,6 +122,7 @@ int main() NSMutableOrderedSet *mutableTest1, *mutableTest2; NSMutableArray *testObjs = [NSMutableArray new]; NSData *data = [stringData dataUsingEncoding: NSUTF8StringEncoding]; + NSMutableSet *testSet; testObj = [NSOrderedSet new]; [testObjs addObject: testObj]; @@ -228,6 +229,13 @@ int main() PASS(isSubset, "mutableTest2 is subset of mutableTest1"); + testSet = [NSMutableSet set]; + [testSet addObject: o7]; + [testSet addObject: o8]; + isSubset = [mutableTest2 isSubsetOfSet: testSet]; + PASS(isSubset, + "mutableTest2 is subset of testSet"); + id o9 = @"Hello"; id o10 = @"World"; id o11 = @"Ready"; @@ -242,6 +250,12 @@ int main() PASS(isSubset == NO, "mutableTest2 is not subset of mutableTest1"); + testSet = [NSMutableSet set]; + [testSet addObject: o9]; + isSubset = [mutableTest2 isSubsetOfSet: testSet]; + PASS(isSubset == NO, + "mutableTest2 is not subset of testSet"); + o9 = @"Hello"; o10 = @"World"; o11 = @"Ready"; @@ -260,6 +274,15 @@ int main() PASS(isSubset, "mutableTest2 is subset of mutableTest1"); + testSet = [NSMutableSet set]; + [testSet addObject: o9]; + [testSet addObject: o10]; + [testSet addObject: o12]; + [testSet addObject: o11]; + isSubset = [mutableTest2 isSubsetOfSet: testSet]; + PASS(isSubset, + "mutableTest2 is subset of testSet"); + o9 = @"Hello"; o10 = @"World"; o11 = @"Ready"; From 8c10d1848e63536d1eba11ccefbab505b93142c8 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Thu, 27 Jun 2019 16:17:26 +0200 Subject: [PATCH 08/10] Remove log statement accidentialy left over. --- Source/NSOrderedSet.m | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/NSOrderedSet.m b/Source/NSOrderedSet.m index 80cf064e8..2eddb486a 100644 --- a/Source/NSOrderedSet.m +++ b/Source/NSOrderedSet.m @@ -1215,7 +1215,6 @@ static SEL rlSel; { if ([otherSet containsObject: o] == NO) // 2. check the member is in otherset. { - NSLog(@"Object not contained %@", o); return NO; } } From 7feb7be03a72958e72c72c3589971b93501070ba Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Thu, 27 Jun 2019 16:21:34 +0200 Subject: [PATCH 09/10] Remove unused variables. --- Source/NSOrderedSet.m | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Source/NSOrderedSet.m b/Source/NSOrderedSet.m index 2eddb486a..ae9a6b7f2 100644 --- a/Source/NSOrderedSet.m +++ b/Source/NSOrderedSet.m @@ -62,12 +62,9 @@ static Class NSMutableOrderedSet_abstract_class; static Class NSOrderedSet_concrete_class; static Class NSMutableOrderedSet_concrete_class; -static SEL addSel; -static SEL countSel; static SEL eqSel; static SEL oaiSel; static SEL remSel; -static SEL rlSel; + (id) allocWithZone: (NSZone*)z { @@ -87,12 +84,9 @@ static SEL rlSel; { [self setVersion: 1]; - addSel = @selector(addObject:); - countSel = @selector(count); eqSel = @selector(isEqual:); oaiSel = @selector(objectAtIndex:); remSel = @selector(removeObjectAtIndex:); - rlSel = @selector(removeLastObject); NSOrderedSet_abstract_class = self; NSOrderedSet_concrete_class = [GSOrderedSet class]; From 05233e64d6a84c6215b4191ff5e9b49eb5ad0ca0 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Thu, 27 Jun 2019 19:02:03 +0200 Subject: [PATCH 10/10] Remove broken test. This code gave a bad access exception when run on a Mac. --- Tests/base/NSOrderedSet/basic.m | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Tests/base/NSOrderedSet/basic.m b/Tests/base/NSOrderedSet/basic.m index 28289cf57..5351641f8 100644 --- a/Tests/base/NSOrderedSet/basic.m +++ b/Tests/base/NSOrderedSet/basic.m @@ -146,12 +146,6 @@ int main() PASS(testObj != nil && [testObj count] == 1, "cannot create an ordered set with multiple like elements"); - id objs2[] = {@"Hello"}; - testObj = [NSOrderedSet orderedSetWithObjects: objs2 count: 2]; - [testObjs addObject: testObj]; - PASS(testObj != nil && [testObj count] == 1, - "Does not throw exception when count != to number of elements"); - NSMutableArray *arr = [NSMutableArray array]; [arr addObject: @"Hello"]; [arr addObject: @"World"];