Skip to content

Clarify endIndex behavior of partitioningIndex(where:) in documentation (Resolves #170) #171

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
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Sources/Algorithms/Partition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ extension MutableCollection where Self: BidirectionalCollection {
//===----------------------------------------------------------------------===//

extension Collection {
/// Returns the index of the first element in the collection that matches
/// the predicate.
/// Returns the start index of the partition of a collection that matches
/// the given predicate.
///
/// The collection must already be partitioned according to the predicate.
/// That is, there should be an index `i` where for every element in
Expand All @@ -179,7 +179,8 @@ extension Collection {
/// - Parameter belongsInSecondPartition: A predicate that partitions the
/// collection.
/// - Returns: The index of the first element in the collection for which
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’m still a bit unsure about this sentence. It almost sounds like the first time that predicate is evaluated and returns true, execution stops and that index is returned, like firstIndex(where:).

/// `predicate` returns `true`.
/// `predicate` returns `true`, or `endIndex` if there are no elements
/// for which `predicate` returns `true`.
///
/// - Complexity: O(log *n*), where *n* is the length of this collection if
/// the collection conforms to `RandomAccessCollection`, otherwise O(*n*).
Expand Down
20 changes: 20 additions & 0 deletions Tests/SwiftAlgorithmsTests/PartitionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ final class PartitionTests: XCTestCase {
}
}

func testPartitioningIndexWithEmptyInput() {
let input: [Int] = []

let a = input.partitioningIndex(where: { _ in return true })
XCTAssertEqual(a, input.startIndex)

let b = input.partitioningIndex(where: { _ in return false })
XCTAssertEqual(b, input.endIndex)
}

func testPartitioningIndexWithOneEmptyPartition() {
let input: Range<Int> = (0 ..< 10)

let a = input.partitioningIndex(where: { $0 > 10 })
XCTAssertEqual(a, input.endIndex)

let b = input.partitioningIndex(where: { $0 >= 0 })
XCTAssertEqual(b, input.startIndex)
}

func testPartitionWithSubrangeBidirectionalCollection() {
for length in 10...20 {
let a = Array(0..<length)
Expand Down