Skip to content

Commit

Permalink
Implement NSArray index(es)Of* and enumerateObjectsAtIndexes methods (#…
Browse files Browse the repository at this point in the history
…2236)

fixes #2070
fixes #2072
  • Loading branch information
aballway authored Mar 23, 2017
1 parent 1903d21 commit cd1c143
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 64 deletions.
111 changes: 56 additions & 55 deletions Frameworks/Foundation/NSArray.mm
Original file line number Diff line number Diff line change
Expand Up @@ -208,23 +208,7 @@ - (NSUInteger)indexOfObject:(id)obj {
@Status Interoperable
*/
- (NSIndexSet*)indexesOfObjectsPassingTest:(BOOL (^)(id, NSUInteger, BOOL*))pred {
int count = [self count];

NSMutableIndexSet* ret = [NSMutableIndexSet indexSet];
for (int i = 0; i < count; i++) {
id value = [self objectAtIndex:i];
BOOL shouldStop = false;

if (pred(value, i, &shouldStop)) {
[ret addIndex:i];
}

if (shouldStop) {
break;
}
}

return ret;
return [self indexesOfObjectsWithOptions:0 passingTest:pred];
}

/**
Expand Down Expand Up @@ -269,15 +253,7 @@ - (NSUInteger)indexOfObject:(id)obj inRange:(NSRange)range {
@Status Interoperable
*/
- (NSUInteger)indexOfObjectIdenticalTo:(id)obj {
int count = [self count];

for (int i = 0; i < count; i++) {
if ([self objectAtIndex:i] == obj) {
return i;
}
}

return NSNotFound;
return [self indexOfObjectIdenticalTo:obj inRange:NSMakeRange(0, self.count)];
}

/**
Expand Down Expand Up @@ -888,7 +864,6 @@ - (NSArray*)objectsAtIndexes:(NSIndexSet*)indexes {

/**
@Status Stub
@Notes
*/
- (void)addObserver:(NSObject*)anObserver
toObjectsAtIndexes:(NSIndexSet*)indexes
Expand All @@ -900,23 +875,20 @@ - (void)addObserver:(NSObject*)anObserver

/**
@Status Stub
@Notes
*/
- (void)removeObserver:(NSObject*)anObserver fromObjectsAtIndexes:(NSIndexSet*)indexes forKeyPath:(NSString*)keyPath {
UNIMPLEMENTED();
}

/**
@Status Stub
@Notes
*/
- (void)removeObserver:(NSObject*)observer fromObjectsAtIndexes:(NSIndexSet*)indexes forKeyPath:(NSString*)keyPath context:(void*)context {
UNIMPLEMENTED();
}

/**
@Status Interoperable
@Notes
*/
+ (NSArray*)arrayWithContentsOfURL:(NSURL*)aURL {
return [[[self alloc] initWithContentsOfURL:aURL] autorelease];
Expand Down Expand Up @@ -988,38 +960,45 @@ - (NSString*)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
}

/**
@Status Stub
@Notes
@Status Interoperable
*/
- (void)enumerateObjectsAtIndexes:(NSIndexSet*)indexSet
options:(NSEnumerationOptions)opts
usingBlock:(void (^)(id, NSUInteger, BOOL*))block {
UNIMPLEMENTED();
[indexSet enumerateIndexesWithOptions:opts
usingBlock:^(NSUInteger index, BOOL* stop) {
block([self objectAtIndex:index], index, stop);
}];
}

/**
@Status Stub
@Notes
@Status Interoperable
*/
- (NSIndexSet*)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id, NSUInteger, BOOL*))predicate {
UNIMPLEMENTED();
return StubReturn();
NSIndexSet* fullRange = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.count)];
return [self indexesOfObjectsAtIndexes:fullRange options:opts passingTest:predicate];
}

/**
@Status Stub
@Notes
@Status Interoperable
*/
- (NSIndexSet*)indexesOfObjectsAtIndexes:(NSIndexSet*)indexSet
options:(NSEnumerationOptions)opts
passingTest:(BOOL (^)(id, NSUInteger, BOOL*))predicate {
UNIMPLEMENTED();
return StubReturn();
__block NSMutableIndexSet* ret = [NSMutableIndexSet indexSet];
[self enumerateObjectsAtIndexes:indexSet
options:opts
usingBlock:^(id element, NSUInteger index, BOOL* stop) {
if (predicate(element, index, stop)) {
[ret addIndex:index];
}
}];

return ret;
}

/**
@Status Interoperable
@Notes
*/
- (NSArray*)initWithContentsOfURL:(NSURL*)aURL {
NSData* data = [NSData dataWithContentsOfURL:aURL];
Expand Down Expand Up @@ -1056,40 +1035,62 @@ - (NSArray*)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSCompar

/**
@Status Stub
@Notes
*/
- (NSArray*)sortedArrayUsingFunction:(NSInteger (*)(id, id, void*))comparator context:(void*)context hint:(NSData*)hint {
UNIMPLEMENTED();
return StubReturn();
}

/**
@Status Stub
@Notes
@Status Interoperable
*/
- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range {
UNIMPLEMENTED();
return StubReturn();
if (NSMaxRange(range) > [self count]) {
[NSException raise:NSRangeException
format:@"-[%s %s]: range {%d, %d} extends beyond bounds [0 .. %d]",
sel_getName(_cmd),
class_getName([self class]),
range.location,
range.length,
[self count]];
}

id objects[range.length];
[self getObjects:objects range:range];
for (NSUInteger i = 0; i < range.length; ++i) {
if (objects[i] == anObject) {
return range.location + i;
}
}

return NSNotFound;
}

/**
@Status Stub
@Notes
@Status Interoperable
*/
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id, NSUInteger, BOOL*))predicate {
UNIMPLEMENTED();
return StubReturn();
NSIndexSet* fullRange = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.count)];
return [self indexOfObjectAtIndexes:fullRange options:opts passingTest:predicate];
}

/**
@Status Stub
@Notes
@Status Interoperable
*/
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet*)indexSet
options:(NSEnumerationOptions)opts
passingTest:(BOOL (^)(id, NSUInteger, BOOL*))predicate {
UNIMPLEMENTED();
return StubReturn();
NSIndexSet* matching = [self indexesOfObjectsAtIndexes:indexSet
options:opts
passingTest:^(id obj, NSUInteger index, BOOL* stop) {
BOOL ret = predicate(obj, index, stop);
if (ret == YES) {
*stop = YES;
}

return ret;
}];
return matching.firstIndex;
}

@end
14 changes: 6 additions & 8 deletions include/Foundation/NSArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,17 @@ FOUNDATION_EXPORT_CLASS
- (NSUInteger)indexOfObject:(ObjectType)anObject;
- (NSUInteger)indexOfObject:(ObjectType)anObject inRange:(NSRange)range;
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject;
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range STUB_METHOD;
- (NSUInteger)indexOfObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range;
- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(ObjectType, NSUInteger, BOOL*))predicate;
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts
passingTest:(BOOL (^)(ObjectType, NSUInteger, BOOL*))predicate STUB_METHOD;
- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType, NSUInteger, BOOL*))predicate;
- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet*)indexSet
options:(NSEnumerationOptions)opts
passingTest:(BOOL (^)(ObjectType, NSUInteger, BOOL*))predicate STUB_METHOD;
passingTest:(BOOL (^)(ObjectType, NSUInteger, BOOL*))predicate;
- (NSIndexSet*)indexesOfObjectsPassingTest:(BOOL (^)(ObjectType, NSUInteger, BOOL*))predicate;
- (NSIndexSet*)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts
passingTest:(BOOL (^)(ObjectType, NSUInteger, BOOL*))predicate STUB_METHOD;
- (NSIndexSet*)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(ObjectType, NSUInteger, BOOL*))predicate;
- (NSIndexSet*)indexesOfObjectsAtIndexes:(NSIndexSet*)indexSet
options:(NSEnumerationOptions)opts
passingTest:(BOOL (^)(ObjectType, NSUInteger, BOOL*))predicate STUB_METHOD;
passingTest:(BOOL (^)(ObjectType, NSUInteger, BOOL*))predicate;
- (NSUInteger)indexOfObject:(ObjectType)obj
inSortedRange:(NSRange)r
options:(NSBinarySearchingOptions)opts
Expand All @@ -89,7 +87,7 @@ FOUNDATION_EXPORT_CLASS
- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(ObjectType, NSUInteger, BOOL*))block;
- (void)enumerateObjectsAtIndexes:(NSIndexSet*)indexSet
options:(NSEnumerationOptions)opts
usingBlock:(void (^)(ObjectType, NSUInteger, BOOL*))block STUB_METHOD;
usingBlock:(void (^)(ObjectType, NSUInteger, BOOL*))block;
- (ObjectType)firstObjectCommonWithArray:(NSArray<ObjectType>*)otherArray;
- (BOOL)isEqualToArray:(NSArray<ObjectType>*)otherArray;
- (NSArray<ObjectType>*)arrayByAddingObject:(ObjectType)anObject;
Expand Down
Loading

0 comments on commit cd1c143

Please sign in to comment.