forked from apple/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stride.swift
656 lines (599 loc) · 21 KB
/
Stride.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//===--- Stride.swift - Components for stride(...) iteration --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// A type representing continuous, one-dimensional values that can be offset
/// and measured.
///
/// You can use a type that conforms to the `Strideable` protocol with the
/// `stride(from:to:by:)` and `stride(from:through:by:)` functions. For
/// example, you can use `stride(from:to:by:)` to iterate over an
/// interval of floating-point values:
///
/// for radians in stride(from: 0.0, to: .pi * 2, by: .pi / 2) {
/// let degrees = Int(radians * 180 / .pi)
/// print("Degrees: \(degrees), radians: \(radians)")
/// }
/// // Degrees: 0, radians: 0.0
/// // Degrees: 90, radians: 1.5707963267949
/// // Degrees: 180, radians: 3.14159265358979
/// // Degrees: 270, radians: 4.71238898038469
///
/// The last parameter of these functions is of the associated `Stride`
/// type---the type that represents the distance between any two instances of
/// the `Strideable` type.
///
/// Types that have an integer `Stride` can be used as the boundaries of a
/// countable range or as the lower bound of an iterable one-sided range. For
/// example, you can iterate over a range of `Int` and use sequence and
/// collection methods.
///
/// var sum = 0
/// for x in 1...100 {
/// sum += x
/// }
/// // sum == 5050
///
/// let digits = (0..<10).map(String.init)
/// // ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
///
/// Conforming to the Strideable Protocol
/// =====================================
///
/// To add `Strideable` conformance to a custom type, choose a `Stride` type
/// that can represent the distance between two instances and implement the
/// `advanced(by:)` and `distance(to:)` methods. For example, this
/// hypothetical `Date` type stores its value as the number of days before or
/// after January 1, 2000:
///
/// struct Date: Equatable, CustomStringConvertible {
/// var daysAfterY2K: Int
///
/// var description: String {
/// // ...
/// }
/// }
///
/// The `Stride` type for `Date` is `Int`, inferred from the parameter and
/// return types of `advanced(by:)` and `distance(to:)`:
///
/// extension Date: Strideable {
/// func advanced(by n: Int) -> Date {
/// var result = self
/// result.daysAfterY2K += n
/// return result
/// }
///
/// func distance(to other: Date) -> Int {
/// return other.daysAfterY2K - self.daysAfterY2K
/// }
/// }
///
/// The `Date` type can now be used with the `stride(from:to:by:)` and
/// `stride(from:through:by:)` functions and as the bounds of an iterable
/// range.
///
/// let startDate = Date(daysAfterY2K: 0) // January 1, 2000
/// let endDate = Date(daysAfterY2K: 15) // January 16, 2000
///
/// for date in stride(from: startDate, to: endDate, by: 7) {
/// print(date)
/// }
/// // January 1, 2000
/// // January 8, 2000
/// // January 15, 2000
///
/// - Important: The `Strideable` protocol provides default implementations for
/// the equal-to (`==`) and less-than (`<`) operators that depend on the
/// `Stride` type's implementations. If a type conforming to `Strideable` is
/// its own `Stride` type, it must provide concrete implementations of the
/// two operators to avoid infinite recursion.
public protocol Strideable: Comparable {
/// A type that represents the distance between two values.
associatedtype Stride: SignedNumeric, Comparable
/// Returns the distance from this value to the given value, expressed as a
/// stride.
///
/// If this type's `Stride` type conforms to `BinaryInteger`, then for two
/// values `x` and `y`, and a distance `n = x.distance(to: y)`,
/// `x.advanced(by: n) == y`. Using this method with types that have a
/// noninteger `Stride` may result in an approximation.
///
/// - Parameter other: The value to calculate the distance to.
/// - Returns: The distance from this value to `other`.
///
/// - Complexity: O(1)
func distance(to other: Self) -> Stride
/// Returns a value that is offset the specified distance from this value.
///
/// Use the `advanced(by:)` method in generic code to offset a value by a
/// specified distance. If you're working directly with numeric values, use
/// the addition operator (`+`) instead of this method.
///
/// func addOne<T: Strideable>(to x: T) -> T
/// where T.Stride: ExpressibleByIntegerLiteral
/// {
/// return x.advanced(by: 1)
/// }
///
/// let x = addOne(to: 5)
/// // x == 6
/// let y = addOne(to: 3.5)
/// // y = 4.5
///
/// If this type's `Stride` type conforms to `BinaryInteger`, then for a
/// value `x`, a distance `n`, and a value `y = x.advanced(by: n)`,
/// `x.distance(to: y) == n`. Using this method with types that have a
/// noninteger `Stride` may result in an approximation.
///
/// - Parameter n: The distance to advance this value.
/// - Returns: A value that is offset from this value by `n`.
///
/// - Complexity: O(1)
func advanced(by n: Stride) -> Self
/// `_step` is an implementation detail of Strideable; do not use it directly.
static func _step(
after current: (index: Int?, value: Self),
from start: Self, by distance: Self.Stride
) -> (index: Int?, value: Self)
}
extension Strideable {
@inlinable
public static func < (x: Self, y: Self) -> Bool {
return x.distance(to: y) > 0
}
@inlinable
public static func == (x: Self, y: Self) -> Bool {
return x.distance(to: y) == 0
}
}
extension Strideable {
@inlinable // protocol-only
public static func _step(
after current: (index: Int?, value: Self),
from start: Self, by distance: Self.Stride
) -> (index: Int?, value: Self) {
return (nil, current.value.advanced(by: distance))
}
}
extension Strideable where Stride: FloatingPoint {
@inlinable // protocol-only
public static func _step(
after current: (index: Int?, value: Self),
from start: Self, by distance: Self.Stride
) -> (index: Int?, value: Self) {
if let i = current.index {
// When Stride is a floating-point type, we should avoid accumulating
// rounding error from repeated addition.
return (i + 1, start.advanced(by: Stride(i + 1) * distance))
}
return (nil, current.value.advanced(by: distance))
}
}
extension Strideable where Self: FloatingPoint, Self == Stride {
@inlinable // protocol-only
public static func _step(
after current: (index: Int?, value: Self),
from start: Self, by distance: Self.Stride
) -> (index: Int?, value: Self) {
if let i = current.index {
// When both Self and Stride are the same floating-point type, we should
// take advantage of fused multiply-add (where supported) to eliminate
// intermediate rounding error.
return (i + 1, start.addingProduct(Stride(i + 1), distance))
}
return (nil, current.value.advanced(by: distance))
}
}
/// An iterator for a `StrideTo` instance.
@frozen
public struct StrideToIterator<Element: Strideable> {
@usableFromInline
internal let _start: Element
@usableFromInline
internal let _end: Element
@usableFromInline
internal let _stride: Element.Stride
@usableFromInline
internal var _current: (index: Int?, value: Element?)
@inlinable
internal init(_start: Element, end: Element, stride: Element.Stride) {
self._start = _start
_end = end
_stride = stride
_current = (nil, nil)
}
}
extension StrideToIterator: IteratorProtocol {
/// Advances to the next element and returns it, or `nil` if no next element
/// exists.
///
/// Once `nil` has been returned, all subsequent calls return `nil`.
@inlinable
public mutating func next() -> Element? {
if let value = _current.value {
let deltaEnd = value.distance(to: _end)
if _stride > 0 ? deltaEnd > _stride : deltaEnd < _stride {
_current = Element._step(after: (_current.index, value), from: _start, by: _stride)
} else {
return nil
}
} else {
_current = (0, _start)
}
return _current.value
}
}
// FIXME: should really be a Collection, as it is multipass
/// A sequence of values formed by striding over a half-open interval.
///
/// Use the `stride(from:to:by:)` function to create `StrideTo` instances.
@frozen
public struct StrideTo<Element: Strideable> {
@usableFromInline
internal let _start: Element
@usableFromInline
internal let _end: Element
@usableFromInline
internal let _stride: Element.Stride
@inlinable
internal init(_start: Element, end: Element, stride: Element.Stride) {
_precondition(stride != 0, "Stride size must not be zero")
// At start, striding away from end is allowed; it just makes for an
// already-empty Sequence.
self._start = _start
self._end = end
self._stride = stride
}
}
extension StrideTo: Sequence {
/// Returns an iterator over the elements of this sequence.
///
/// - Complexity: O(1).
@inlinable
public __consuming func makeIterator() -> StrideToIterator<Element> {
return StrideToIterator(_start: _start, end: _end, stride: _stride)
}
// FIXME(conditional-conformances): this is O(N) instead of O(1), leaving it
// here until a proper Collection conformance is possible
@inlinable
public var underestimatedCount: Int {
var it = self.makeIterator()
var count = 0
while it.next() != nil {
count += 1
}
return count
}
@inlinable
public func _customContainsEquatableElement(
_ element: Element
) -> Bool? {
if element < _start || _end <= element {
return false
}
return nil
}
}
extension StrideTo: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, children: ["from": _start, "to": _end, "by": _stride])
}
}
// FIXME(conditional-conformances): This does not yet compile (SR-6474).
#if false
extension StrideTo: RandomAccessCollection
where Element.Stride: BinaryInteger {
public typealias Index = Int
public typealias SubSequence = Slice<StrideTo<Element>>
public typealias Indices = Range<Int>
@inlinable
public var startIndex: Index { return 0 }
@inlinable
public var endIndex: Index { return count }
@inlinable
public var count: Int {
let distance = _start.distance(to: _end)
guard distance != 0 && (distance < 0) == (_stride < 0) else { return 0 }
return Int((distance - 1) / _stride) + 1
}
public subscript(position: Index) -> Element {
_failEarlyRangeCheck(position, bounds: startIndex..<endIndex)
return _start.advanced(by: Element.Stride(position) * _stride)
}
public subscript(bounds: Range<Index>) -> Slice<StrideTo<Element>> {
_failEarlyRangeCheck(bounds, bounds: startIndex ..< endIndex)
return Slice(base: self, bounds: bounds)
}
@inlinable
public func index(before i: Index) -> Index {
_failEarlyRangeCheck(i, bounds: startIndex + 1...endIndex)
return i - 1
}
@inlinable
public func index(after i: Index) -> Index {
_failEarlyRangeCheck(i, bounds: startIndex - 1..<endIndex)
return i + 1
}
}
#endif
/// Returns a sequence from a starting value to, but not including, an end
/// value, stepping by the specified amount.
///
/// You can use this function to stride over values of any type that conforms
/// to the `Strideable` protocol, such as integers or floating-point types.
/// Starting with `start`, each successive value of the sequence adds `stride`
/// until the next value would be equal to or beyond `end`.
///
/// for radians in stride(from: 0.0, to: .pi * 2, by: .pi / 2) {
/// let degrees = Int(radians * 180 / .pi)
/// print("Degrees: \(degrees), radians: \(radians)")
/// }
/// // Degrees: 0, radians: 0.0
/// // Degrees: 90, radians: 1.5707963267949
/// // Degrees: 180, radians: 3.14159265358979
/// // Degrees: 270, radians: 4.71238898038469
///
/// You can use `stride(from:to:by:)` to create a sequence that strides upward
/// or downward. Pass a negative value as `stride` to create a sequence from a
/// higher start to a lower end:
///
/// for countdown in stride(from: 3, to: 0, by: -1) {
/// print("\(countdown)...")
/// }
/// // 3...
/// // 2...
/// // 1...
///
/// If you pass a value as `stride` that moves away from `end`, the sequence
/// contains no values.
///
/// for x in stride(from: 0, to: 10, by: -1) {
/// print(x)
/// }
/// // Nothing is printed.
///
/// - Parameters:
/// - start: The starting value to use for the sequence. If the sequence
/// contains any values, the first one is `start`.
/// - end: An end value to limit the sequence. `end` is never an element of
/// the resulting sequence.
/// - stride: The amount to step by with each iteration. A positive `stride`
/// iterates upward; a negative `stride` iterates downward.
/// - Returns: A sequence from `start` toward, but not including, `end`. Each
/// value in the sequence steps by `stride`.
@inlinable
public func stride<T>(
from start: T, to end: T, by stride: T.Stride
) -> StrideTo<T> {
return StrideTo(_start: start, end: end, stride: stride)
}
/// An iterator for a `StrideThrough` instance.
@frozen
public struct StrideThroughIterator<Element: Strideable> {
@usableFromInline
internal let _start: Element
@usableFromInline
internal let _end: Element
@usableFromInline
internal let _stride: Element.Stride
@usableFromInline
internal var _current: (index: Int?, value: Element?)
@usableFromInline
internal var _didReturnEnd: Bool = false
@inlinable
internal init(_start: Element, end: Element, stride: Element.Stride) {
self._start = _start
_end = end
_stride = stride
_current = (nil, nil)
}
}
extension StrideThroughIterator: IteratorProtocol {
/// Advances to the next element and returns it, or `nil` if no next element
/// exists.
///
/// Once `nil` has been returned, all subsequent calls return `nil`.
@inlinable
public mutating func next() -> Element? {
if let value = _current.value {
let deltaEnd = value.distance(to: _end)
if _stride > 0 ? deltaEnd >= self._stride : deltaEnd <= self._stride {
_current = Element._step(after: (_current.index, value), from: _start, by: _stride)
} else {
return nil
}
} else {
_current = (0, _start)
}
return _current.value
}
}
// FIXME: should really be a Collection, as it is multipass
/// A sequence of values formed by striding over a closed interval.
///
/// Use the `stride(from:through:by:)` function to create `StrideThrough`
/// instances.
@frozen
public struct StrideThrough<Element: Strideable> {
@usableFromInline
internal let _start: Element
@usableFromInline
internal let _end: Element
@usableFromInline
internal let _stride: Element.Stride
@inlinable
internal init(_start: Element, end: Element, stride: Element.Stride) {
_precondition(stride != 0, "Stride size must not be zero")
self._start = _start
self._end = end
self._stride = stride
}
}
extension StrideThrough: Sequence {
/// Returns an iterator over the elements of this sequence.
///
/// - Complexity: O(1).
@inlinable
public __consuming func makeIterator() -> StrideThroughIterator<Element> {
return StrideThroughIterator(_start: _start, end: _end, stride: _stride)
}
// FIXME(conditional-conformances): this is O(N) instead of O(1), leaving it
// here until a proper Collection conformance is possible
@inlinable
public var underestimatedCount: Int {
var it = self.makeIterator()
var count = 0
while it.next() != nil {
count += 1
}
return count
}
@inlinable
public func _customContainsEquatableElement(
_ element: Element
) -> Bool? {
if element < _start || _end < element {
return false
}
return nil
}
}
extension StrideThrough: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self,
children: ["from": _start, "through": _end, "by": _stride])
}
}
// FIXME(conditional-conformances): This does not yet compile (SR-6474).
#if false
extension StrideThrough: RandomAccessCollection
where Element.Stride: BinaryInteger {
public typealias Index = ClosedRangeIndex<Int>
public typealias SubSequence = Slice<StrideThrough<Element>>
@inlinable
public var startIndex: Index {
let distance = _start.distance(to: _end)
return distance == 0 || (distance < 0) == (_stride < 0)
? ClosedRangeIndex(0)
: ClosedRangeIndex()
}
@inlinable
public var endIndex: Index { return ClosedRangeIndex() }
@inlinable
public var count: Int {
let distance = _start.distance(to: _end)
guard distance != 0 else { return 1 }
guard (distance < 0) == (_stride < 0) else { return 0 }
return Int(distance / _stride) + 1
}
public subscript(position: Index) -> Element {
let offset = Element.Stride(position._dereferenced) * _stride
return _start.advanced(by: offset)
}
public subscript(bounds: Range<Index>) -> Slice<StrideThrough<Element>> {
return Slice(base: self, bounds: bounds)
}
@inlinable
public func index(before i: Index) -> Index {
switch i._value {
case .inRange(let n):
_precondition(n > 0, "Incrementing past start index")
return ClosedRangeIndex(n - 1)
case .pastEnd:
_precondition(_end >= _start, "Incrementing past start index")
return ClosedRangeIndex(count - 1)
}
}
@inlinable
public func index(after i: Index) -> Index {
switch i._value {
case .inRange(let n):
return n == (count - 1)
? ClosedRangeIndex()
: ClosedRangeIndex(n + 1)
case .pastEnd:
_preconditionFailure("Incrementing past end index")
}
}
}
#endif
/// Returns a sequence from a starting value toward, and possibly including, an end
/// value, stepping by the specified amount.
///
/// You can use this function to stride over values of any type that conforms
/// to the `Strideable` protocol, such as integers or floating-point types.
/// Starting with `start`, each successive value of the sequence adds `stride`
/// until the next value would be beyond `end`.
///
/// for radians in stride(from: 0.0, through: .pi * 2, by: .pi / 2) {
/// let degrees = Int(radians * 180 / .pi)
/// print("Degrees: \(degrees), radians: \(radians)")
/// }
/// // Degrees: 0, radians: 0.0
/// // Degrees: 90, radians: 1.5707963267949
/// // Degrees: 180, radians: 3.14159265358979
/// // Degrees: 270, radians: 4.71238898038469
/// // Degrees: 360, radians: 6.28318530717959
///
/// You can use `stride(from:through:by:)` to create a sequence that strides
/// upward or downward. Pass a negative value as `stride` to create a sequence
/// from a higher start to a lower end:
///
/// for countdown in stride(from: 3, through: 1, by: -1) {
/// print("\(countdown)...")
/// }
/// // 3...
/// // 2...
/// // 1...
///
/// The value you pass as `end` is not guaranteed to be included in the
/// sequence. If stepping from `start` by `stride` does not produce `end`,
/// the last value in the sequence will be one step before going beyond `end`.
///
/// for multipleOfThree in stride(from: 3, through: 10, by: 3) {
/// print(multipleOfThree)
/// }
/// // 3
/// // 6
/// // 9
///
/// If you pass a value as `stride` that moves away from `end`, the sequence
/// contains no values.
///
/// for x in stride(from: 0, through: 10, by: -1) {
/// print(x)
/// }
/// // Nothing is printed.
///
/// - Parameters:
/// - start: The starting value to use for the sequence. If the sequence
/// contains any values, the first one is `start`.
/// - end: An end value to limit the sequence. `end` is an element of
/// the resulting sequence if and only if it can be produced from `start`
/// using steps of `stride`.
/// - stride: The amount to step by with each iteration. A positive `stride`
/// iterates upward; a negative `stride` iterates downward.
/// - Returns: A sequence from `start` toward, and possibly including, `end`.
/// Each value in the sequence is separated by `stride`.
@inlinable
public func stride<T>(
from start: T, through end: T, by stride: T.Stride
) -> StrideThrough<T> {
return StrideThrough(_start: start, end: end, stride: stride)
}
extension StrideToIterator: ConcurrentValue
where Element: ConcurrentValue, Element.Stride: ConcurrentValue { }
extension StrideTo: ConcurrentValue
where Element: ConcurrentValue, Element.Stride: ConcurrentValue { }
extension StrideThroughIterator: ConcurrentValue
where Element: ConcurrentValue, Element.Stride: ConcurrentValue { }
extension StrideThrough: ConcurrentValue
where Element: ConcurrentValue, Element.Stride: ConcurrentValue { }