From fb69aacc5dfae83cafa346043d80d8233f0383b9 Mon Sep 17 00:00:00 2001 From: Tim Vermeulen Date: Thu, 15 Oct 2020 17:41:33 +0200 Subject: [PATCH] Fix `Product2`'s indexing logic (#26) * Change representation of `Product2`'s `endIndex` * Fix `Product2.index(before:)` * Fix `Product2.distance(from:to:)` * Make comment more specific --- Sources/Algorithms/Product.swift | 33 ++++++++----------- Tests/SwiftAlgorithmsTests/ProductTests.swift | 15 +++++++++ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Sources/Algorithms/Product.swift b/Sources/Algorithms/Product.swift index c5e106e..c34618b 100644 --- a/Sources/Algorithms/Product.swift +++ b/Sources/Algorithms/Product.swift @@ -91,27 +91,22 @@ extension Product2: Collection where Base1: Collection { } public var startIndex: Index { - base1.isEmpty || base2.isEmpty - ? endIndex - : Index(i1: base1.startIndex, i2: base2.startIndex) + Index( + i1: base2.isEmpty ? base1.endIndex : base1.startIndex, + i2: base2.startIndex) } public var endIndex: Index { - Index(i1: base1.endIndex, i2: base2.endIndex) + // `base2.startIndex` simplifies index calculations. + Index(i1: base1.endIndex, i2: base2.startIndex) } public func index(after i: Index) -> Index { - precondition(i.i1 != base1.endIndex && i.i2 != base2.endIndex, - "Can't advance past endIndex") + precondition(i.i1 != base1.endIndex, "Can't advance past endIndex") let newIndex2 = base2.index(after: i.i2) - if newIndex2 < base2.endIndex { - return Index(i1: i.i1, i2: newIndex2) - } - - let newIndex1 = base1.index(after: i.i1) - return newIndex1 == base1.endIndex - ? endIndex - : Index(i1: newIndex1, i2: base2.startIndex) + return newIndex2 == base2.endIndex + ? Index(i1: base1.index(after: i.i1), i2: base2.startIndex) + : Index(i1: i.i1, i2: newIndex2) } // TODO: Implement index(_:offsetBy:) and index(_:offsetBy:limitedBy:) @@ -125,7 +120,7 @@ extension Product2: Collection where Base1: Collection { } return base2[start.i2...].count + base2[.. (Base1.Element, Base2.Element) { @@ -140,12 +135,12 @@ extension Product2: BidirectionalCollection precondition(i != startIndex, "Can't move before startIndex") if i.i2 == base2.startIndex { + return Index( + i1: base1.index(before: i.i1), + i2: base2.index(before: base2.endIndex)) + } else { return Index(i1: i.i1, i2: base2.index(before: i.i2)) } - - return Index( - i1: base1.index(before: i.i1), - i2: base2.index(before: base2.endIndex)) } } diff --git a/Tests/SwiftAlgorithmsTests/ProductTests.swift b/Tests/SwiftAlgorithmsTests/ProductTests.swift index e4d9462..4fa02a5 100644 --- a/Tests/SwiftAlgorithmsTests/ProductTests.swift +++ b/Tests/SwiftAlgorithmsTests/ProductTests.swift @@ -22,4 +22,19 @@ final class ProductTests: XCTestCase { XCTAssertEqualSequences(product(1...10, ""), [], by: ==) XCTAssertEqualSequences(product("", 1...10), [], by: ==) } + + func testProductReversed() { + XCTAssertEqualSequences( + [(2, "B" as Character), (2, "A"), (1, "B"), (1, "A")], + product(1...2, "AB").reversed(), + by: ==) + + XCTAssertEqualSequences(product(1...10, "").reversed(), [], by: ==) + XCTAssertEqualSequences(product("", 1...10).reversed(), [], by: ==) + } + + func testProductDistanceFromTo() { + let p = product([1, 2], "abc") + XCTAssertEqual(p.distance(from: p.startIndex, to: p.endIndex), 6) + } }