Skip to content

[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

Merged
merged 2 commits into from
Apr 24, 2018

Conversation

natecook1000
Copy link
Member

@natecook1000 natecook1000 commented Dec 8, 2017

This implements the new last... 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.

@natecook1000 natecook1000 added the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label Dec 8, 2017
@natecook1000 natecook1000 force-pushed the nc-last branch 2 times, most recently from 319f46d to 8b5b102 Compare March 7, 2018 23:41
@natecook1000 natecook1000 force-pushed the nc-last branch 3 times, most recently from 565088f to 6670a60 Compare April 11, 2018 00:28
@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

@natecook1000
Copy link
Member Author

@swift-ci Please smoke test Linux platform

@natecook1000
Copy link
Member Author

@swift-ci Please smoke test macOS platform

@natecook1000 natecook1000 removed the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label Apr 19, 2018
@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

@tkremenek
Copy link
Member

@swift-ci test

@tkremenek
Copy link
Member

@natecook1000 can you resolve the merge conflicts?

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 6670a602a7e2c0561c145dc1cfdb860fbf3032e4

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 6670a602a7e2c0561c145dc1cfdb860fbf3032e4

@natecook1000 natecook1000 changed the title [stdlib] Implement sequence/collection methods for searching from the end [WIP] [stdlib] Implement sequence/collection methods for searching from the end Apr 20, 2018
@natecook1000
Copy link
Member Author

@swift-ci Please smoke test Linux platform

@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

@natecook1000
Copy link
Member Author

@swift-ci Please smoke test Linux platform

found,
stackTrace: SourceLocStack().with(test.loc))
expectEqual(
2,
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Member Author

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
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Member Author

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.


@inlinable
public func _customLastIndexOfEquatableElement(_ element: Bound) -> Index?? {
return _customIndexOfEquatableElement(element)
Copy link
Contributor

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 =)

@moiseev
Copy link
Contributor

moiseev commented Apr 23, 2018

Minor nitpicks, LGTM otherwise.

@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

@@ -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.
Copy link
Contributor

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!

@moiseev
Copy link
Contributor

moiseev commented Apr 23, 2018

:shipit:

@natecook1000 natecook1000 changed the title [WIP] [stdlib] Implement sequence/collection methods for searching from the end [stdlib] Implement sequence/collection methods for searching from the end Apr 24, 2018
@natecook1000 natecook1000 merged commit b1ab7d8 into swiftlang:master Apr 24, 2018
natecook1000 added a commit to natecook1000/swift that referenced this pull request Apr 24, 2018
… 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`.
natecook1000 added a commit that referenced this pull request Apr 26, 2018
* 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`.
@natecook1000 natecook1000 deleted the nc-last branch October 4, 2018 15:22
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.

4 participants