14
14
//===----------------------------------------------------------------------===//
15
15
16
16
extension Sequence {
17
+ /// Returns a sequence stepping through the elements every `step` starting
18
+ /// at the first value. Any remainders of the stride will be trimmed.
19
+ ///
20
+ /// (0...10).striding(by: 2) // == [0, 2, 4, 6, 8, 10]
21
+ /// (0...10).striding(by: 3) // == [0, 3, 6, 9]
22
+ ///
23
+ /// - Complexity: O(1). Access to successive values is O(k) where
24
+ /// _k_ is the striding `step`.
25
+ ///
26
+ /// - Parameter step: The amount to step with each iteration.
27
+ /// - Returns: Returns a sequence for stepping through the
28
+ /// elements by the specified amount.
29
+ @inlinable
30
+ public func striding( by step: Int ) -> StrideSequence < Self > {
31
+ StrideSequence ( base: self , stride: step)
32
+ }
33
+ }
34
+
35
+ extension Collection {
17
36
/// Returns a sequence stepping through the elements every `step` starting
18
37
/// at the first value. Any remainders of the stride will be trimmed.
19
38
///
@@ -25,16 +44,17 @@ extension Sequence {
25
44
/// O(_k_), where _k_ is the striding `step`.
26
45
///
27
46
/// - Parameter step: The amount to step with each iteration.
28
- /// - Returns: Returns a sequence or collection for stepping through the
47
+ /// - Returns: Returns a collection for stepping through the
29
48
/// elements by the specified amount.
30
49
@inlinable
31
- public func striding( by step: Int ) -> Stride < Self > {
32
- Stride ( base: self , stride: step)
50
+ public func striding( by step: Int ) -> StrideCollection < Self > {
51
+ StrideCollection ( base: self , stride: step)
33
52
}
34
53
}
35
54
36
- /// A wrapper that strides over a base sequence or collection.
37
- public struct Stride < Base: Sequence > {
55
+ /// A wrapper that strides over a base sequence.
56
+ public struct StrideSequence < Base: Sequence > : Sequence {
57
+
38
58
@usableFromInline
39
59
internal let base : Base
40
60
@@ -49,14 +69,8 @@ public struct Stride<Base: Sequence> {
49
69
}
50
70
}
51
71
52
- extension Stride {
53
- @inlinable
54
- public func striding( by step: Int ) -> Self {
55
- Stride ( base: base, stride: stride * step)
56
- }
57
- }
58
-
59
- extension Stride : Sequence {
72
+ extension StrideSequence {
73
+
60
74
/// An iterator over a `Stride` sequence.
61
75
public struct Iterator : IteratorProtocol {
62
76
@usableFromInline
@@ -88,12 +102,43 @@ extension Stride: Sequence {
88
102
}
89
103
90
104
@inlinable
91
- public func makeIterator( ) -> Stride < Base > . Iterator {
105
+ public func makeIterator( ) -> Iterator {
92
106
Iterator ( iterator: base. makeIterator ( ) , stride: stride)
93
107
}
94
108
}
95
109
96
- extension Stride : Collection where Base: Collection {
110
+ extension StrideSequence {
111
+ @inlinable
112
+ public func striding( by step: Int ) -> Self {
113
+ Self ( base: base, stride: stride * step)
114
+ }
115
+ }
116
+
117
+ /// A wrapper that strides over a base collection.
118
+ public struct StrideCollection < Base: Collection > {
119
+ @usableFromInline
120
+ internal let base : Base
121
+
122
+ @usableFromInline
123
+ internal let stride : Int
124
+
125
+ @usableFromInline
126
+ internal init ( base: Base , stride: Int ) {
127
+ precondition ( stride > 0 , " striding must be greater than zero " )
128
+ self . base = base
129
+ self . stride = stride
130
+ }
131
+ }
132
+
133
+ extension StrideCollection {
134
+ @inlinable
135
+ public func striding( by step: Int ) -> Self {
136
+ Self ( base: base, stride: stride * step)
137
+ }
138
+ }
139
+
140
+ extension StrideCollection : Collection {
141
+
97
142
/// A position in a `Stride` collection.
98
143
public struct Index : Comparable {
99
144
@usableFromInline
@@ -213,7 +258,7 @@ extension Stride: Collection where Base: Collection {
213
258
}
214
259
}
215
260
216
- extension Stride : BidirectionalCollection
261
+ extension StrideCollection : BidirectionalCollection
217
262
where Base: RandomAccessCollection {
218
263
219
264
@inlinable
@@ -223,16 +268,16 @@ extension Stride: BidirectionalCollection
223
268
}
224
269
}
225
270
226
- extension Stride : RandomAccessCollection where Base: RandomAccessCollection { }
271
+ extension StrideCollection : RandomAccessCollection where Base: RandomAccessCollection { }
227
272
228
- extension Stride : Equatable where Base. Element: Equatable {
273
+ extension StrideCollection : Equatable where Base. Element: Equatable {
229
274
@inlinable
230
- public static func == ( lhs: Stride , rhs: Stride ) -> Bool {
275
+ public static func == ( lhs: StrideCollection , rhs: StrideCollection ) -> Bool {
231
276
lhs. elementsEqual ( rhs, by: == )
232
277
}
233
278
}
234
279
235
- extension Stride : Hashable where Base. Element: Hashable {
280
+ extension StrideCollection : Hashable where Base. Element: Hashable {
236
281
@inlinable
237
282
public func hash( into hasher: inout Hasher ) {
238
283
hasher. combine ( stride)
@@ -242,4 +287,4 @@ extension Stride: Hashable where Base.Element: Hashable {
242
287
}
243
288
}
244
289
245
- extension Stride . Index : Hashable where Base. Index: Hashable { }
290
+ extension StrideCollection . Index : Hashable where Base. Index: Hashable { }
0 commit comments