forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStride.swift
264 lines (224 loc) · 7.75 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
//===--- Stride.swift - Components for stride(...) iteration --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// Conforming types are notionally continuous, one-dimensional
/// values that can be offset and measured.
public protocol Strideable : Comparable {
// FIXME: We'd like to name this type "Distance" but for
// <rdar://problem/17619038>
/// A type that can represent the distance between two values of `Self`.
typealias Stride : SignedNumberType
/// Returns a stride `x` such that `self.advancedBy(x)` approximates
/// `other`.
///
/// - Complexity: O(1).
///
/// - SeeAlso: `RandomAccessIndexType`'s `distanceTo`, which provides a
/// stronger semantic guarantee.
@warn_unused_result
func distanceTo(other: Self) -> Stride
/// Returns a `Self` `x` such that `self.distanceTo(x)` approximates
/// `n`.
///
/// - Complexity: O(1).
///
/// - SeeAlso: `RandomAccessIndexType`'s `advancedBy`, which
/// provides a stronger semantic guarantee.
@warn_unused_result
func advancedBy(n: Stride) -> Self
}
/// Compare two `Strideable`s.
public func < <T : Strideable>(x: T, y: T) -> Bool {
return x.distanceTo(y) > 0
}
public func == <T : Strideable>(x: T, y: T) -> Bool {
return x.distanceTo(y) == 0
}
@warn_unused_result
public func + <T : Strideable> (lhs: T, rhs: T.Stride) -> T {
return lhs.advancedBy(rhs)
}
@warn_unused_result
public func + <T : Strideable> (lhs: T.Stride, rhs: T) -> T {
return rhs.advancedBy(lhs)
}
@warn_unused_result
public func - <T : Strideable> (lhs: T, rhs: T.Stride) -> T {
return lhs.advancedBy(-rhs)
}
@warn_unused_result
public func - <T : Strideable> (lhs: T, rhs: T) -> T.Stride {
return rhs.distanceTo(lhs)
}
public func += <T : Strideable> (inout lhs: T, rhs: T.Stride) {
lhs = lhs.advancedBy(rhs)
}
public func -= <T : Strideable> (inout lhs: T, rhs: T.Stride) {
lhs = lhs.advancedBy(-rhs)
}
//===--- Deliberately-ambiguous operators for UnsignedIntegerTypes --------===//
// The UnsignedIntegerTypes all have a signed Stride type. Without these //
// overloads, expressions such as UInt(2) + Int(3) would compile. //
//===----------------------------------------------------------------------===//
public func + <T : UnsignedIntegerType> (
lhs: T, rhs: T._DisallowMixedSignArithmetic
) -> T {
_sanityCheckFailure("Should not be callable.")
}
public func + <T : UnsignedIntegerType> (
lhs: T._DisallowMixedSignArithmetic, rhs: T
) -> T {
_sanityCheckFailure("Should not be callable.")
}
public func - <T : _DisallowMixedSignArithmetic> (
lhs: T, rhs: T._DisallowMixedSignArithmetic
) -> T {
_sanityCheckFailure("Should not be callable.")
}
public func - <T : _DisallowMixedSignArithmetic> (
lhs: T, rhs: T
) -> T._DisallowMixedSignArithmetic {
_sanityCheckFailure("Should not be callable.")
}
public func += <T : UnsignedIntegerType> (
inout lhs: T, rhs: T._DisallowMixedSignArithmetic
) {
_sanityCheckFailure("Should not be callable.")
}
public func -= <T : UnsignedIntegerType> (
inout lhs: T, rhs: T._DisallowMixedSignArithmetic
) {
_sanityCheckFailure("Should not be callable.")
}
//===----------------------------------------------------------------------===//
/// A GeneratorType for `StrideTo<Element>`.
public struct StrideToGenerator<Element : Strideable> : GeneratorType {
@available(*, unavailable, renamed="Element")
public typealias T = Element
var current: Element
let end: Element
let stride: Element.Stride
/// Advance to the next element and return it, or `nil` if no next
/// element exists.
public mutating func next() -> Element? {
if stride > 0 ? current >= end : current <= end {
return nil
}
let ret = current
current += stride
return ret
}
}
/// A `SequenceType` of values formed by striding over a half-open interval.
public struct StrideTo<Element : Strideable> : SequenceType {
// FIXME: should really be a CollectionType, as it is multipass
@available(*, unavailable, renamed="Element")
public typealias T = Element
/// Return a *generator* over the elements of this *sequence*.
///
/// - Complexity: O(1).
public func generate() -> StrideToGenerator<Element> {
return StrideToGenerator(current: start, end: end, stride: stride)
}
init(start: Element, end: Element, stride: Element.Stride) {
_precondition(stride != 0, "stride size must not be zero")
// Unreachable endpoints are allowed; they just make for an
// already-empty SequenceType.
self.start = start
self.end = end
self.stride = stride
}
let start: Element
let end: Element
let stride: Element.Stride
}
extension Strideable {
/// Return the sequence of values (`self`, `self + stride`, `self +
/// stride + stride`, ... *last*) where *last* is the last value in
/// the progression that is less than `end`.
@warn_unused_result
public func stride(to end: Self, by stride: Stride) -> StrideTo<Self> {
return StrideTo(start: self, end: end, stride: stride)
}
}
@available(*, unavailable, message="call the 'stride(to:by:)' method instead")
public func stride<
T : Strideable
>(from start: T, to end: T, by stride: T.Stride) -> StrideTo<T> {
fatalError("unavailable function can't be called")
}
/// A GeneratorType for `StrideThrough<Element>`.
public struct StrideThroughGenerator<Element : Strideable> : GeneratorType {
@available(*, unavailable, renamed="Element")
public typealias T = Element
var current: Element
let end: Element
let stride: Element.Stride
var done: Bool = false
/// Advance to the next element and return it, or `nil` if no next
/// element exists.
public mutating func next() -> Element? {
if done {
return nil
}
if stride > 0 ? current >= end : current <= end {
if current == end {
done = true
return current
}
return nil
}
let ret = current
current += stride
return ret
}
}
/// A `SequenceType` of values formed by striding over a closed interval.
public struct StrideThrough<Element : Strideable> : SequenceType {
// FIXME: should really be a CollectionType, as it is multipass
@available(*, unavailable, renamed="Element")
public typealias T = Element
/// Return a *generator* over the elements of this *sequence*.
///
/// - Complexity: O(1).
public func generate() -> StrideThroughGenerator<Element> {
return StrideThroughGenerator(
current: start, end: end, stride: stride, done: false)
}
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
}
let start: Element
let end: Element
let stride: Element.Stride
}
extension Strideable {
/// Return the sequence of values (`start`, `start + stride`, `start +
/// stride + stride`, ... *last*) where *last* is the last value in
/// the progression less than or equal to `end`.
///
/// - Note: There is no guarantee that `end` is an element of the sequence.
@warn_unused_result
public func stride(
through end: Self, by stride: Stride
) -> StrideThrough<Self> {
return StrideThrough(start: self, end: end, stride: stride)
}
}
@available(*, unavailable, message="call the 'stride(through:by:)' method instead")
public func stride<
T : Strideable
>(from start: T, through end: T, by stride: T.Stride) -> StrideThrough<T> {
fatalError("unavailable function can't be called")
}