mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-27 04:34:21 +00:00
Add implementation for Sequence iterator
This commit is contained in:
3 files changed
+30
-8
No files matched your search
+3
-3
@@ -5,7 +5,7 @@
|
||||
|
||||
A type that steps over a collection’s elements by the specified amount.
|
||||
|
||||
This is available through the `striding(by:)` method on any `Collection`.
|
||||
This is available through the `striding(by:)` method on any `Sequence`.
|
||||
|
||||
```swift
|
||||
(0...10).striding(by: 2) // == [0, 2, 4, 6, 8, 10]
|
||||
@@ -18,11 +18,11 @@ The stride amount must be a positive value.
|
||||
|
||||
## Detailed Design
|
||||
|
||||
The `striding(by:)` method is declared as a `Collection` extension, and returns a
|
||||
The `striding(by:)` method is declared as a `Sequence` extension, and returns a
|
||||
`Stride` type:
|
||||
|
||||
```swift
|
||||
extension Collection {
|
||||
extension Sequence {
|
||||
public func striding(by step: Int) -> Stride<Self>
|
||||
}
|
||||
```
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
// striding(by:)
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension Collection {
|
||||
extension Sequence {
|
||||
/// Returns a collection stepping through the elements every `step` starting
|
||||
/// at the first value. Any remainders of the stride will be trimmed.
|
||||
///
|
||||
@@ -32,7 +32,7 @@ extension Collection {
|
||||
}
|
||||
}
|
||||
|
||||
public struct Stride<Base: Collection> {
|
||||
public struct Stride<Base: Sequence> {
|
||||
|
||||
let base: Base
|
||||
let stride: Int
|
||||
@@ -50,7 +50,29 @@ extension Stride {
|
||||
}
|
||||
}
|
||||
|
||||
extension Stride: Collection {
|
||||
extension Stride: Sequence {
|
||||
|
||||
public struct Iterator: IteratorProtocol {
|
||||
|
||||
var iterator: Base.Iterator
|
||||
let stride: Int
|
||||
|
||||
public mutating func next() -> Base.Element? {
|
||||
defer {
|
||||
for _ in 0..<(stride - 1) {
|
||||
guard iterator.next() != nil else { break }
|
||||
}
|
||||
}
|
||||
return iterator.next()
|
||||
}
|
||||
}
|
||||
|
||||
public func makeIterator() -> Stride<Base>.Iterator {
|
||||
Iterator(iterator: base.makeIterator(), stride: stride)
|
||||
}
|
||||
}
|
||||
|
||||
extension Stride: Collection where Base: Collection {
|
||||
|
||||
public struct Index: Comparable {
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import Algorithms
|
||||
final class StridingTests: XCTestCase {
|
||||
|
||||
func testStride() {
|
||||
let a = (0...10)
|
||||
let a = 0...10
|
||||
XCTAssertEqualSequences(a.striding(by: 1), (0...10))
|
||||
XCTAssertEqualSequences(a.striding(by: 2), [0, 2, 4, 6, 8, 10])
|
||||
XCTAssertEqualSequences(a.striding(by: 3), [0, 3, 6, 9])
|
||||
@@ -48,7 +48,7 @@ final class StridingTests: XCTestCase {
|
||||
XCTAssertEqual(a[i], 2)
|
||||
a.formIndex(before: &i)
|
||||
XCTAssertEqual(a[i], 0)
|
||||
// a.formIndex(before: &i) // Precondition failed: Advancing past start index
|
||||
// a.formIndex(before: &i) // Precondition failed: Incrementing past start index
|
||||
// a.index(after: a.endIndex) // Precondition failed: Advancing past end index
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user