-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
[stdlib] Implementing O(1) methods on CountableClosedRange
#2993
Conversation
98c3593
to
2fc3ccd
Compare
CountableClosedRange
CountableClosedRange
@swift-ci Please test |
return 1 + limit.distance(to: right) | ||
case (.pastEnd, .pastEnd): | ||
// end <--> end | ||
return limit.distance(to: limit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bound.Stride
conforms to Integer
, so this can be 0
I guess?
Thanks @austinzheng for doing this. Please let me know once you've updated the tests and I'll be happy to merge it in. |
I also have another PR, whenever you or Dmitri have time: #2940 |
2fc3ccd
to
52d61ec
Compare
I need to rewrite a bunch of these tests. I'll ping you when I have the updated code ready. |
3c6aa17
to
49241da
Compare
I fixed the issues raised in your comments, addressed some additional bugs, and rewrote some of the test suites I added since they were not properly implemented before. The tests in question pass; I'm running the entire test suite on OS X right now and will comment again when it finishes. Otherwise, it's ready. |
All tests pass on my OS X machine. |
e0d164e
to
c8e5228
Compare
} | ||
previousValue = range[idx].value | ||
} | ||
// Iterate once more to get to the 'before start' position |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not look right. Past-end is a valid index, before-start is not. Valid indices can only be in the following range [startIndex, endIndex).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. In addition to the comment being misleading, this specific test is really confusingly written and I will redo it to make the intent clearer.
I looked into that test more closely. The test was correct, but written in a very confusing way, so I changed it around and made the preconditions it tests explicit in a comment. The "before start index" comment was simply wrong and I've removed it. I'm happy to make any other changes you'd like. Also let me know if you want me to squash the commit. @moiseev |
df1da50
to
5d955f1
Compare
Changes: - Implemented O(1) `index(_:offsetBy:)` and `distance(from:to:)` methods on `CountableClosedRange` - Fixed some unit tests - Added many more unit tests for Countable*Range index APIs
5d955f1
to
59789d3
Compare
@swift-ci Please test |
Looks like everything's in working order. |
@@ -47,26 +47,64 @@ public struct ClosedRangeIndex<Bound> : Comparable | |||
} | |||
} | |||
|
|||
internal func _predecessor(upperBound limit: Bound) -> ClosedRangeIndex { | |||
internal func _predecessor( | |||
lowerBound: Bound, upperBound limit: Bound |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point, now that you are passing both bounds, I think it would make sense to just move this code into its only callsite.
Ooops. Sorry, @gribozavr, I merged it before seeing your comments. |
NP. |
|
||
/// Return the index `leftIndex` steps away from `start`, or the collection's | ||
/// `endIndex` if `leftIndex` is `nil`. | ||
func leftIndex<C: RandomAccessCollection>(forCollection c: C) -> C.Index { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use just for:
for the label (Collection
is redundant).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even of:
.
@austinzheng Great work! 👍 |
Thanks! I'll open a new PR to address these comments soon.
|
Changes: - Moved only-used-once helper methods into call sites - Parameterized tests that might expect a crash - Fixed some comments and formatting; renamed some test helper APIs
What's in this pull request?
Functional changes and additional unit test coverage for
CountableRange
andCountableClosedRange
.All tests pass on OS X.
@gribozavr
Before merging this pull request to apple/swift repository:
Triggering Swift CI
The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:
Smoke Testing
Validation Testing
Lint Testing
Note: Only members of the Apple organization can trigger swift-ci.
Changes:
index(_:offsetBy:)
anddistance(from:to:)
methods onCountableClosedRange