Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Implement NSArray index(es)Of* and enumerateObjectsAtIndexes methods#2236

Merged
aballway merged 8 commits into
microsoft:developfrom
aballway:nsarray
Mar 23, 2017
Merged

Implement NSArray index(es)Of* and enumerateObjectsAtIndexes methods#2236
aballway merged 8 commits into
microsoft:developfrom
aballway:nsarray

Conversation

@aballway

Copy link
Copy Markdown
Contributor

fixes #2070
fixes #2072

Comment thread Frameworks/Foundation/NSArray.mm Outdated
/**
@Status Stub
@Status Interoperable
@Notes

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove empty notes. <ESC>:g/@Notes$/d

Comment thread Frameworks/Foundation/NSArray.mm Outdated
return StubReturn();
if (range.location + range.length > [self count]) {
[NSException raise:NSRangeException
format:@"Range {%d, %d} larger than array of size %d.", range.location, range.length, [self count]];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't match the other exception formats NSArray uses

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference platform throws an NSRangeException for invalid ranges, should I opt for consistency here?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant the wording was inconsistent :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to reflect exception generated on reference platform

Comment thread Frameworks/Foundation/NSArray.mm Outdated
format:@"Range {%d, %d} larger than array of size %d.", range.location, range.length, [self count]];
}

NSUInteger end = NSMaxRange(range);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this above 1035 and use end in the if(...)

Comment thread Frameworks/Foundation/NSArray.mm Outdated

NSUInteger end = NSMaxRange(range);
for (NSUInteger i = range.location; i < end; ++i) {
if ([self objectAtIndex:i] == anObject) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is curious; if you prefer getObjects:range:, a "fast" NSArray implementation can turn that into an n-wide buffer copy into your provided buffer. Right now, _NSCFArray doesn't do that... but it could!

Arguably, it should, for optimization purposes. It would call CFArrayGetValues under the hood.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can apply anywhere you iterate manually over a range.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should I make that part of these changes? I'm wary of changing too much in NSArray for possibly unnecessary optimization

Comment thread Frameworks/Foundation/NSArray.mm Outdated
passingTest:(BOOL (^)(id, NSUInteger, BOOL*))predicate {
UNIMPLEMENTED();
return StubReturn();
NSIndexSet* matching = [self indexesOfObjectsAtIndexes:indexSet options:opts passingTest:predicate];

@DHowett-MSFT DHowett-MSFT Mar 15, 2017

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimization:
If you add another level of block here, you can stop enumeration on the first hit instead of continuing to aggregate every index and then discarding the rest of them. #ByDesign

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference platform will blow up on invalid ranges, even if a valid element is found, and this gives us that.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fail to see what that has to do with this; using *stop = YES; to abort the iteration inside the block still has us calling indexesOfObjects... and getting the blowing-up behaviour.

@aballway aballway changed the base branch from packaging to develop March 16, 2017 15:51
Comment thread Frameworks/Foundation/NSArray.mm Outdated

id objects[range.length];
[self getObjects:objects range:range];
for (NSUInteger i = range.location; i < end; ++i) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why not i=0, < range.length?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considered it but this was the path of least change, didn't think it made a substantial difference in readability or perf

Comment thread Frameworks/Foundation/NSArray.mm Outdated
NSUInteger end = NSMaxRange(range);
if (end > [self count]) {
[NSException raise:NSRangeException
format:@"[%s %s]: range {%d, %d} extends beyond bounds [ 0 .. %d]",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing - before [%s

Comment thread Frameworks/Foundation/NSArray.mm Outdated
passingTest:(BOOL (^)(id, NSUInteger, BOOL*))predicate {
UNIMPLEMENTED();
return StubReturn();
NSIndexSet* matching = [self indexesOfObjectsAtIndexes:indexSet options:opts passingTest:predicate];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fail to see what that has to do with this; using *stop = YES; to abort the iteration inside the block still has us calling indexesOfObjects... and getting the blowing-up behaviour.

@aballway aballway merged commit cd1c143 into microsoft:develop Mar 23, 2017
@aballway aballway deleted the nsarray branch April 25, 2017 16:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement NSArray enumerateObjectWithIndexes:options:usingBlock: finish NSArray index* methods that are stubbed out

4 participants