Skip to content
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

CombinationsSequence: underestimatedCount == count #220

Merged
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
4 changes: 4 additions & 0 deletions Sources/Algorithms/Combinations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public struct CombinationsSequence<Base: Collection> {
binomial(n: n, k: $0)
}.reduce(0, +)
}

/// The total number of combinations.
@inlinable
public var underestimatedCount: Int { count }
Copy link

Choose a reason for hiding this comment

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

Doesn't this conflict with the complexity requirement of Sequence.underestimatedCount?

  /// - Complexity: O(1), except if the sequence also conforms to `Collection`.
  ///   In this case, see the documentation of `Collection.underestimatedCount`.
  var underestimatedCount: Int { get }

The requirement is relaxed for non-random access collections, so not a problem for #219.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hum, indeed. What is the complexity of your count? If it's log(N) IMO this is acceptable; complexity requirements are somewhat fungible (e.g. first on a lazy filtered collection is arguably not O(1)). Otherwise, I suppose not.

Oh, but they totally broke the complexity bound of underestimatedCount. It was O(n) before ea49459b7594. What a debacle.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, that O(1) constraint is meaningless as outlined here. You should ignore it.

}

extension CombinationsSequence: Sequence {
Expand Down
96 changes: 51 additions & 45 deletions Tests/SwiftAlgorithmsTests/CombinationsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,63 @@ import Algorithms
final class CombinationsTests: XCTestCase {
func testCount() {
let c = "ABCD"

let c0 = c.combinations(ofCount: 0).count
XCTAssertEqual(c0, 1)

let c1 = c.combinations(ofCount: 1).count
XCTAssertEqual(c1, 4)

let c2 = c.combinations(ofCount: 2).count
XCTAssertEqual(c2, 6)

let c3 = c.combinations(ofCount: 3).count
XCTAssertEqual(c3, 4)

let c4 = c.combinations(ofCount: 4).count
XCTAssertEqual(c4, 1)

let c5 = c.combinations(ofCount: 0...0).count
XCTAssertEqual(c5, 1)

let c6 = c.combinations(ofCount: 1...1).count
XCTAssertEqual(c6, 4)

let c7 = c.combinations(ofCount: 1...2).count
XCTAssertEqual(c7, 10)

let c8 = c.combinations(ofCount: 1...3).count
XCTAssertEqual(c8, 14)

let c9 = c.combinations(ofCount: 2...4).count
XCTAssertEqual(c9, 11)


/// XCTAsserts that `x`'s `count` and `underestimatedCount` are both `l` at
/// the given `file` and `line`.
func check(
_ x: CombinationsSequence<String>, countsAre l: Int,
file: StaticString, line: UInt)
{
XCTAssertEqual(x.count, l, "unexpected count", file: file, line: line)
XCTAssertEqual(
x.underestimatedCount, l, "unexpected underestimatedCount",
file: file, line: line)
}

/// XCTAsserts that the `count` and `underestimatedCount` of
/// `c.combinations(ofCount: l)` are both `n` at the given `file` and
/// `line`.
func check(
cHas n: Int,
combinationsOfLength l: Int,
file: StaticString = #filePath, line: UInt = #line)
{
check(c.combinations(ofCount: l), countsAre: n, file: file, line: line)
}

/// XCTAsserts that the `count` and `underestimatedCount` of
/// `c.combinations(ofCount: l)` are both `n` at the given `file` and
/// `line`.
func check<R: RangeExpression>(
cHas n: Int,
combinationsOfLengths l: R,
file: StaticString = #filePath, line: UInt = #line) where R.Bound == Int
{
check(c.combinations(ofCount: l), countsAre: n, file: file, line: line)
}

check(cHas: 1, combinationsOfLength: 0)
check(cHas: 4, combinationsOfLength: 1)
check(cHas: 6, combinationsOfLength: 2)
check(cHas: 1, combinationsOfLength: 4)

check(cHas: 1, combinationsOfLengths: 0...0)
check(cHas: 4, combinationsOfLengths: 1...1)
check(cHas: 10, combinationsOfLengths: 1...2)
check(cHas: 14, combinationsOfLengths: 1...3)
check(cHas: 11, combinationsOfLengths: 2...4)

// `k` greater than element count results in same number of combinations
let c10 = c.combinations(ofCount: 3...10).count
XCTAssertEqual(c10, 5)
check(cHas: 5, combinationsOfLengths: 3...10)

// `k` greater than element count results in same number of combinations
let c11 = c.combinations(ofCount: 4...10).count
XCTAssertEqual(c11, 1)
check(cHas: 1, combinationsOfLengths: 4...10)

// `k` entirely greater than element count results in no combinations
let c12 = c.combinations(ofCount: 5...10).count
XCTAssertEqual(c12, 0)

let c13 = c.combinations(ofCount: 0...).count
XCTAssertEqual(c13, 16)

let c14 = c.combinations(ofCount: ...3).count
XCTAssertEqual(c14, 15)
check(cHas: 0, combinationsOfLengths: 5...10)

let c15 = c.combinations(ofCount: 0...).count
XCTAssertEqual(c15, 16)
check(cHas: 16, combinationsOfLengths: 0...)
check(cHas: 15, combinationsOfLengths: ...3)
}

func testCombinations() {
Expand Down