diff --git a/Sources/Algorithms/Stride.swift b/Sources/Algorithms/Stride.swift index 480b468..1dbd2ae 100644 --- a/Sources/Algorithms/Stride.swift +++ b/Sources/Algorithms/Stride.swift @@ -86,7 +86,7 @@ extension Stride: Collection { precondition(i.base < base.endIndex, "Advancing past end index") return index(i, offsetBy: 1) } - + public func index( _ i: Index, offsetBy n: Int, @@ -94,34 +94,65 @@ extension Stride: Collection { ) -> Index? { guard n != 0 else { return i } guard limit != i else { return nil } - switch (i, n) { - case (endIndex, ..<0): - let baseEnd = base.index(base.endIndex, offsetBy: -((base.count - 1) % stride + 1)) - return Index(base.index(baseEnd, offsetBy: (n - n.signum()) * stride, limitedBy: limit.base)) - case (_, 1...): - let max = limit < i ? endIndex.base : limit.base - let idx = base.index(i.base, offsetBy: n * stride, limitedBy: max) - if let idx = idx { - return idx > max ? endIndex : Index(idx) + + return n > 0 + ? offsetForward(i, offsetBy: n, limitedBy: limit) + : offsetBackward(i, offsetBy: n, limitedBy: limit) + } + + private func offsetForward( + _ i: Index, + offsetBy n: Int, + limitedBy limit: Index + ) -> Index? { + if limit < i { + if let idx = base.index( + i.base, + offsetBy: n * stride, + limitedBy: base.endIndex + ) { + return Index(idx) + } else { + assert(distance(from: i, to: endIndex) == n, "Advancing past end index") + return endIndex } - guard i >= limit || limit == endIndex else { - return nil - } - let isToEnd = distance(from: i, to: endIndex) == n - return isToEnd ? endIndex : nil - case _: - return Index(base.index(i.base, offsetBy: n * stride, limitedBy: limit.base)) + } else if let idx = base.index( + i.base, + offsetBy: n * stride, + limitedBy: limit.base + ) { + return Index(idx) + } else { + return distance(from: i, to: limit) == n + ? endIndex + : nil } } - + + private func offsetBackward( + _ i: Index, + offsetBy n: Int, + limitedBy limit: Index + ) -> Index? { + let distance = i == endIndex + ? -((base.count - 1) % stride + 1) + (n + 1) * stride + : n * stride + return Index( + base.index( + i.base, + offsetBy: distance, + limitedBy: limit.base + ) + ) + } + public var count: Int { - let limit = base.count - 1 - return limit / stride + (limit < 0 ? 0 : 1) + base.isEmpty ? 0 : (base.count - 1) / stride + 1 } public func distance(from start: Index, to end: Index) -> Int { let distance = base.distance(from: start.base, to: end.base) - return distance / stride + (abs(distance % stride) > 0 ? distance.signum() : 0) + return distance / stride + (distance % stride).signum() } public func index(_ i: Index, offsetBy distance: Int) -> Index { diff --git a/Tests/SwiftAlgorithmsTests/StrideTests.swift b/Tests/SwiftAlgorithmsTests/StrideTests.swift index da4fe56..3a54233 100644 --- a/Tests/SwiftAlgorithmsTests/StrideTests.swift +++ b/Tests/SwiftAlgorithmsTests/StrideTests.swift @@ -89,13 +89,18 @@ final class StridingTests: XCTestCase { } func testIndexTraversals() { + let empty = [Int]() + validateIndexTraversals( + empty.striding(by: 1), + empty.striding(by: 2) + ) let zero_to_one_hundered_range = 0...100 validateIndexTraversals( zero_to_one_hundered_range.striding(by: 10), zero_to_one_hundered_range.striding(by: 11), zero_to_one_hundered_range.striding(by: 101) ) - let zero_to_one_hundered_array = zero_to_one_hundered_range.map{ $0 } + let zero_to_one_hundered_array = Array(zero_to_one_hundered_range) validateIndexTraversals( zero_to_one_hundered_array.striding(by: 10), zero_to_one_hundered_array.striding(by: 11), @@ -113,7 +118,7 @@ final class StridingTests: XCTestCase { func testOffsetBy() { let a = (0...100).striding(by: 22) let b = [0, 22, 44, 66, 88] - for i in 0..