Skip to content

Commit 3669ff0

Browse files
committed
offsetBackward n is positive and remove init? from Index
1 parent 0a6bcc3 commit 3669ff0

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

Sources/Algorithms/Stride.swift

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ extension Stride: Collection where Base: Collection {
8585
self.base = base
8686
}
8787

88-
init?(_ base: Base.Index?) {
89-
guard let base = base else { return nil }
90-
self.base = base
91-
}
92-
9388
public static func < (lhs: Index, rhs: Index) -> Bool {
9489
lhs.base < rhs.base
9590
}
@@ -122,7 +117,7 @@ extension Stride: Collection where Base: Collection {
122117

123118
return n > 0
124119
? offsetForward(i, offsetBy: n, limitedBy: limit)
125-
: offsetBackward(i, offsetBy: n, limitedBy: limit)
120+
: offsetBackward(i, offsetBy: -n, limitedBy: limit)
126121
}
127122

128123
private func offsetForward(
@@ -160,15 +155,13 @@ extension Stride: Collection where Base: Collection {
160155
limitedBy limit: Index
161156
) -> Index? {
162157
let distance = i == endIndex
163-
? -((base.count - 1) % stride + 1) + (n + 1) * stride
164-
: n * stride
165-
return Index(
166-
base.index(
158+
? -((base.count - 1) % stride + 1) + (1 - n) * stride
159+
: n * -stride
160+
return base.index(
167161
i.base,
168162
offsetBy: distance,
169163
limitedBy: limit.base
170-
)
171-
)
164+
).map(Index.init)
172165
}
173166

174167
public var count: Int {

0 commit comments

Comments
 (0)