@@ -38,75 +38,6 @@ public struct ClosedRangeIndex<Bound> : Comparable
38
38
39
39
/// Creates a position `p` for which `r[p] == x`.
40
40
internal init ( _ x: Bound ) { _value = . inRange( x) }
41
-
42
- internal func _successor( upperBound limit: Bound ) -> ClosedRangeIndex {
43
- switch _value {
44
- case . inRange( let x) : return x == limit
45
- ? ClosedRangeIndex ( ) : ClosedRangeIndex ( x. advanced ( by: 1 ) )
46
- case . pastEnd: _preconditionFailure ( " Incrementing past end index " )
47
- }
48
- }
49
-
50
- internal func _predecessor(
51
- lowerBound: Bound , upperBound limit: Bound
52
- ) -> ClosedRangeIndex {
53
- switch _value {
54
- case . inRange( let x) :
55
- _precondition ( x > lowerBound, " Incrementing past start index " )
56
- return ClosedRangeIndex ( x. advanced ( by: - 1 ) )
57
- case . pastEnd:
58
- _precondition ( limit >= lowerBound, " Incrementing past start index " )
59
- return ClosedRangeIndex ( limit)
60
- }
61
- }
62
-
63
- internal func _advanced(
64
- by n: Bound . Stride , lowerBound: Bound , upperBound limit: Bound
65
- ) -> ClosedRangeIndex {
66
- switch _value {
67
- case . inRange( let x) :
68
- let d = x. distance ( to: limit)
69
- if n <= d {
70
- let newPosition = x. advanced ( by: n)
71
- _precondition ( newPosition >= lowerBound,
72
- " Advancing past start index " )
73
- return ClosedRangeIndex ( newPosition)
74
- }
75
- if d - - 1 == n { return ClosedRangeIndex ( ) }
76
- _preconditionFailure ( " Advancing past end index " )
77
- case . pastEnd:
78
- if n == 0 {
79
- return self
80
- } else if n > 0 {
81
- _preconditionFailure ( " Advancing past end index " )
82
- } else {
83
- return ClosedRangeIndex ( limit) . _advanced (
84
- by: ( n + 1 ) ,
85
- lowerBound: lowerBound,
86
- upperBound: limit
87
- )
88
- }
89
- }
90
- }
91
-
92
- internal func _distance(
93
- to: ClosedRangeIndex < Bound > , upperBound limit: Bound
94
- ) -> Bound . Stride {
95
- switch ( _value, to. _value) {
96
- case let ( . inRange( left) , . inRange( right) ) :
97
- // in range <--> in range
98
- return left. distance ( to: right)
99
- case let ( . inRange( left) , . pastEnd) :
100
- // in range --> end
101
- return 1 + left. distance ( to: limit)
102
- case let ( . pastEnd, . inRange( right) ) :
103
- // in range <-- end
104
- return limit. distance ( to: right) - 1
105
- case ( . pastEnd, . pastEnd) :
106
- // end <--> end
107
- return 0
108
- }
109
- }
110
41
111
42
internal var _value : _ClosedRangeIndexRepresentation < Bound >
112
43
internal var _dereferenced : Bound {
@@ -258,19 +189,65 @@ public struct CountableClosedRange<Bound> : RandomAccessCollection
258
189
}
259
190
260
191
public func index( after i: Index ) -> Index {
261
- return i. _successor ( upperBound: upperBound)
192
+ switch i. _value {
193
+ case . inRange( let x) :
194
+ return x == upperBound
195
+ ? ClosedRangeIndex ( )
196
+ : ClosedRangeIndex ( x. advanced ( by: 1 ) )
197
+ case . pastEnd:
198
+ _preconditionFailure ( " Incrementing past end index " )
199
+ }
262
200
}
263
201
264
202
public func index( before i: Index ) -> Index {
265
- return i. _predecessor ( lowerBound: lowerBound, upperBound: upperBound)
203
+ switch i. _value {
204
+ case . inRange( let x) :
205
+ _precondition ( x > lowerBound, " Incrementing past start index " )
206
+ return ClosedRangeIndex ( x. advanced ( by: - 1 ) )
207
+ case . pastEnd:
208
+ _precondition ( upperBound >= lowerBound, " Incrementing past start index " )
209
+ return ClosedRangeIndex ( upperBound)
210
+ }
266
211
}
267
212
268
213
public func index( _ i: Index , offsetBy n: IndexDistance ) -> Index {
269
- return i. _advanced ( by: n, lowerBound: lowerBound, upperBound: upperBound)
214
+ switch i. _value {
215
+ case . inRange( let x) :
216
+ let d = x. distance ( to: upperBound)
217
+ if n <= d {
218
+ let newPosition = x. advanced ( by: n)
219
+ _precondition ( newPosition >= lowerBound,
220
+ " Advancing past start index " )
221
+ return ClosedRangeIndex ( newPosition)
222
+ }
223
+ if d - - 1 == n { return ClosedRangeIndex ( ) }
224
+ _preconditionFailure ( " Advancing past end index " )
225
+ case . pastEnd:
226
+ if n == 0 {
227
+ return i
228
+ } else if n > 0 {
229
+ _preconditionFailure ( " Advancing past end index " )
230
+ } else {
231
+ return index ( ClosedRangeIndex ( upperBound) , offsetBy: ( n + 1 ) )
232
+ }
233
+ }
270
234
}
271
235
272
236
public func distance( from start: Index , to end: Index ) -> IndexDistance {
273
- return start. _distance ( to: end, upperBound: upperBound)
237
+ switch ( start. _value, end. _value) {
238
+ case let ( . inRange( left) , . inRange( right) ) :
239
+ // in range <--> in range
240
+ return left. distance ( to: right)
241
+ case let ( . inRange( left) , . pastEnd) :
242
+ // in range --> end
243
+ return 1 + left. distance ( to: upperBound)
244
+ case let ( . pastEnd, . inRange( right) ) :
245
+ // in range <-- end
246
+ return upperBound. distance ( to: right) - 1
247
+ case ( . pastEnd, . pastEnd) :
248
+ // end <--> end
249
+ return 0
250
+ }
274
251
}
275
252
276
253
/// Accesses the element at specified position.
0 commit comments