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

* Add Chain.distance(from:to:)

* Add test
This commit is contained in:
Tim Vermeulen authored and GitHub committed 2020-10-12 09:36:13 -05:00
1 parent 546c8bf7ba
commit c67dd3419c
2 files changed
+39 -3

No files matched your search

+14 -1
View File
@@ -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