Skip to content

Implement distance(from:to:) for Chain #3

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
Oct 12, 2020
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
15 changes: 14 additions & 1 deletion Sources/Algorithms/Chain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,20 @@ extension Chain: Collection where Base1: Collection, Base2: Collection {
}
}

// TODO: Add distance(from:to:)
public func distance(from start: Index, to end: Index) -> Int {
switch (start.position, end.position) {
case let (.first(i), .first(j)):
return base1.distance(from: i, to: j)
case let (.second(i), .second(j)):
return base2.distance(from: i, to: j)
case let (.first(i), .second(j)):
return base1.distance(from: i, to: base1.endIndex)
+ base2.distance(from: base2.startIndex, to: j)
case let (.second(i), .first(j)):
return base2.distance(from: i, to: base2.startIndex)
+ base1.distance(from: base1.endIndex, to: j)
}
}
}

extension Chain: BidirectionalCollection
Expand Down
27 changes: 25 additions & 2 deletions Tests/SwiftAlgorithmsTests/ChainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@
//===----------------------------------------------------------------------===//

import XCTest
import Algorithms
@testable import Algorithms

final class ChainTests: XCTestCase {
// intentionally does not depend on `Chain.index(_:offsetBy:)` in order to
// avoid making assumptions about the code being tested
func index<A, B>(atOffset offset: Int, in chain: Chain<A, B>) -> Chain<A, B>.Index {
offset < chain.base1.count
? .init(first: chain.base1.index(chain.base1.startIndex, offsetBy: offset))
: .init(second: chain.base2.index(chain.base2.startIndex, offsetBy: offset - chain.base1.count))
}

func testChainSequences() {
let run = (1...).prefix(10).chained(with: 20...)
XCTAssertEqualSequences(run.prefix(20), Array(1...10) + (20..<30))
Expand All @@ -35,5 +43,20 @@ final class ChainTests: XCTestCase {
XCTAssertEqualSequences(s1.reversed().chained(with: s2), "JIHGFEDCBAklmnopqrstuv")
}

// TODO: Add tests that check distance and index(offsetBy:)
// TODO: Add tests that check index(offsetBy:)

func testChainDistanceFromTo() {
let s1 = "abcde"
let s2 = "VWXYZ"
let chain = s1.chained(with: s2)

XCTAssertEqual(chain.count, s1.count + s2.count)

for (startOffset, endOffset) in product(0...chain.count, 0...chain.count) {
let start = index(atOffset: startOffset, in: chain)
let end = index(atOffset: endOffset, in: chain)
let distance = endOffset - startOffset
XCTAssertEqual(chain.distance(from: start, to: end), distance)
}
}
}