Skip to content

Fix Product2's indexing logic #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 15, 2020
Merged

Conversation

timvermeulen
Copy link
Contributor

This PR changes the way Product2.endIndex is represented: instead of (base1.endIndex, base2.endIndex), it is now (base1.endIndex, base2.startIndex).

The latter turns out to be a more natural way of representing the "past the end" index because it is exactly the index after the last valid index (lastValidIndexOfBase1, lastValidIndexOfBase2) according to the existing mechanism of advancing any other index of Product2. As a result, the methods index(before:), index(after:), and distance(from:to:) don't need any custom logic to deal with endIndex.

endIndex wasn't handled correctly everywhere so this also ends up fixing bugs in index(before:) and distance(from:to:).

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

Copy link

@kylemacomber kylemacomber left a comment

Choose a reason for hiding this comment

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

This looks like a great simplification. Nice work @timvermeulen!

}

public var endIndex: Index {
Index(i1: base1.endIndex, i2: base2.endIndex)
// this representation makes index calculations simpler

Choose a reason for hiding this comment

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

I'm not sure we need this comment to make it into the final patch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback, the only reason for putting it there was to indicate to someone reading the code that this is not a typo – any idea how else we could convey this?

Choose a reason for hiding this comment

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

I'll defer to @natecook1000 on whether we should keep it, I guess I generally don't assume there are typos in the code, and if I'm surprised I'll read the surrounding code closer.

That being said, I can understand wanting to draw attention to i2 being base.startIndex, to prevent the reader from glossing over it. For example:

// `base2.startIndex` simplifies index calculations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that's indeed what I was going for. I'll change it to that for now and then leave it up to Nate whether to remove it completely.

@natecook1000
Copy link
Member

Thanks, @timvermeulen! :shipit:

@natecook1000 natecook1000 merged commit fb69aac into apple:main Oct 15, 2020
@timvermeulen timvermeulen deleted the product-index branch October 15, 2020 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants