-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] Implement sequence/collection methods for searching from the end #13337
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
Conversation
319f46d
to
8b5b102
Compare
565088f
to
6670a60
Compare
@swift-ci Please smoke test |
@swift-ci Please smoke test Linux platform |
@swift-ci Please smoke test macOS platform |
@swift-ci Please smoke test |
@swift-ci test |
@natecook1000 can you resolve the merge conflicts? |
Build failed |
Build failed |
@swift-ci Please smoke test Linux platform |
@swift-ci Please smoke test |
e68bb24
to
ead2de7
Compare
@swift-ci Please smoke test |
@swift-ci Please smoke test Linux platform |
found, | ||
stackTrace: SourceLocStack().with(test.loc)) | ||
expectEqual( | ||
2, |
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.
It is not immediately clear where this hardcoded 2
is coming from, and it will break if, for whatever reason, the lastPerformanceTest
changes. Maybe it's worth just getting this value by a very dumb and obviously correct code, like a for loop over the test.sequence
.
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 maybe add another field to FindTest
.
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.
👍 I pulled these out into their own FindLastTest
type, which let me combine the semantics and performance tests.
Optional<CollectionWithEquatableElement.Index>.self, | ||
&result) | ||
let zeroBasedIndex = result.map { | ||
numericCast(c.distance(from: c.startIndex, to: $0)) as Int |
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 is just a long version of saying Int(c.distance(from: c.startIndex, to: $0))
, isn't it?
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.
... and it should be Int
anyway, since the deprecation of Collection.IndexDistance
.
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 is pervasive in these files, from when IndexDistance
was a thing. I'll fix 'em in a follow-up.
stdlib/public/core/ClosedRange.swift
Outdated
|
||
@inlinable | ||
public func _customLastIndexOfEquatableElement(_ element: Bound) -> Index?? { | ||
return _customIndexOfEquatableElement(element) |
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.
Took me some time to convince myself it's correct =)
Minor nitpicks, LGTM otherwise. |
@swift-ci Please smoke test |
stdlib/public/core/Dictionary.swift
Outdated
@@ -1264,6 +1264,7 @@ extension Dictionary { | |||
|
|||
@inlinable // FIXME(sil-serialize-all) | |||
public func _customLastIndexOfEquatableElement(_ element: Element) -> Index?? { | |||
// The first and last elements are the same because each element is unique. |
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! This is the right kind of comment. Thanks!
|
… end (swiftlang#13337) This implements the new last(where:), and lastIndex(of/where:) methods as extensions on `BidirectionalCollection`, which partially implements SE-204. The protocol requirements for `Sequence` and `Collection` as described in the proposal need to wait until there's a solution for picking up the specialized versions in types that conditionally conform to `BidirectionalCollection`.
* Merge pull request #16089 from natecook1000/nc-firstindex [stdlib] Rename index(...) methods to firstIndex(...) * [stdlib] Implement sequence/collection methods for searching from the end (#13337) This implements the new last(where:), and lastIndex(of/where:) methods as extensions on `BidirectionalCollection`, which partially implements SE-204. The protocol requirements for `Sequence` and `Collection` as described in the proposal need to wait until there's a solution for picking up the specialized versions in types that conditionally conform to `BidirectionalCollection`.
This implements the new
last...
methods as extensions onBidirectionalCollection
, which partially implements SE-204. The protocol requirements forSequence
andCollection
as described in the proposal need to wait until there's a solution for picking up the specialized versions in types that conditionally conform toBidirectionalCollection
.