mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-29 04:44:21 +00:00
Implement distance(from:to:) for Chain (#3)
* Add Chain.distance(from:to:) * Add test
This commit is contained in:
1 parent
546c8bf7ba
commit
c67dd3419c
2 files changed
+39
-3
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user