-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy path0019-rdar21511651.swift
431 lines (355 loc) · 14 KB
/
0019-rdar21511651.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
// RUN: not %target-swift-frontend %s -parse
internal protocol _SequenceWrapper {
typealias Base : Sequence
typealias Iterator : IteratorProtocol = Base.Iterator
var _base: Base {get}
}
extension Sequence
where Self : _SequenceWrapper, Self.Iterator == Self.Base.Iterator {
public func makeIterator() -> Base.Iterator {
return self._base.makeIterator()
}
public func underestimatedCount() -> Int {
return _base.underestimatedCount()
}
public func _customContainsEquatableElement(
element: Base.Iterator.Element
) -> Bool? {
return _base._customContainsEquatableElement(element)
}
/// If `self` is multi-pass (i.e., a `Collection`), invoke
/// `preprocess` on `self` and return its result. Otherwise, return
/// `nil`.
public func _preprocessingPass<R>(@noescape _ preprocess: (Self) -> R) -> R? {
return _base._preprocessingPass { _ in preprocess(self) }
}
/// Create a native array buffer containing the elements of `self`,
/// in the same order.
public func _copyToNativeArrayBuffer()
-> _ContiguousArrayBuffer<Base.Iterator.Element> {
return _base._copyToNativeArrayBuffer()
}
/// Copy a Sequence into an array.
public func _copyContents(
initializing ptr: UnsafeMutablePointer<Base.Iterator.Element>
) {
return _base._copyContents(initializing: ptr)
}
}
internal protocol _CollectionWrapper : _SequenceWrapper {
typealias Base : Collection
typealias Index : ForwardIndex = Base.Index
var _base: Base {get}
}
extension Collection
where Self : _CollectionWrapper, Self.Index == Self.Base.Index {
/// The position of the first element in a non-empty collection.
///
/// In an empty collection, `startIndex == endIndex`.
public var startIndex: Base.Index {
return _base.startIndex
}
/// The collection's "past the end" position.
///
/// `endIndex` is not a valid argument to `subscript`, and is always
/// reachable from `startIndex` by zero or more applications of
/// `successor()`.
public var endIndex: Base.Index {
return _base.endIndex
}
/// Access the element at `position`.
///
/// - Precondition: `position` is a valid position in `self` and
/// `position != endIndex`.
public subscript(position: Base.Index) -> Base.Iterator.Element {
return _base[position]
}
}
//===--- New stuff --------------------------------------------------------===//
public protocol _prext_LazySequence : Sequence {
/// A Sequence that can contain the same elements as this one,
/// possibly with a simpler type.
///
/// This associated type is used to keep the result type of
/// `lazy(x).operation` from growing a `_prext_LazySequence` layer.
typealias Elements: Sequence = Self
/// A sequence containing the same elements as this one, possibly with
/// a simpler type.
///
/// When implementing lazy operations, wrapping `elements` instead
/// of `self` can prevent result types from growing a `_prext_LazySequence`
/// layer.
///
/// Note: this property need not be implemented by conforming types,
/// it has a default implementation in a protocol extension that
/// just returns `self`.
var elements: Elements {get}
/// An Array, created on-demand, containing the elements of this
/// lazy Sequence.
///
/// Note: this property need not be implemented by conforming types, it has a
/// default implementation in a protocol extension.
var array: [Iterator.Element] {get}
}
extension _prext_LazySequence {
/// an Array, created on-demand, containing the elements of this
/// lazy Sequence.
public var array: [Iterator.Element] {
return Array(self)
}
}
extension _prext_LazySequence where Elements == Self {
public var elements: Self { return self }
}
extension _prext_LazySequence where Self : _SequenceWrapper {
public var elements: Base { return _base }
}
/// A sequence that forwards its implementation to an underlying
/// sequence instance while exposing lazy computations as methods.
public struct _prext_LazySequence<Base_ : Sequence> : _SequenceWrapper {
var _base: Base_
}
/// Augment `s` with lazy methods such as `map`, `filter`, etc.
public func _prext_lazy<S : Sequence>(s: S) -> _prext_LazySequence<S> {
return _prext_LazySequence(_base: s)
}
public extension Sequence
where Self.Iterator == Self, Self : IteratorProtocol {
public func makeIterator() -> Self {
return self
}
}
//===--- LazyCollection.swift ---------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
//===----------------------------------------------------------------------===//
public protocol _prext_LazyCollection : Collection, _prext_LazySequence {
/// A Collection that can contain the same elements as this one,
/// possibly with a simpler type.
///
/// This associated type is used to keep the result type of
/// `lazy(x).operation` from growing a `_prext_LazyCollection` layer.
typealias Elements: Collection = Self
}
extension _prext_LazyCollection where Elements == Self {
public var elements: Self { return self }
}
extension _prext_LazyCollection where Self : _CollectionWrapper {
public var elements: Base { return _base }
}
/// A collection that forwards its implementation to an underlying
/// collection instance while exposing lazy computations as methods.
public struct _prext_LazyCollection<Base_ : Collection>
: /*_prext_LazyCollection,*/ _CollectionWrapper {
typealias Base = Base_
typealias Index = Base.Index
/// Construct an instance with `base` as its underlying Collection
/// instance.
public init(_ base: Base_) {
self._base = base
}
public var _base: Base_
// FIXME: Why is this needed?
// public var elements: Base { return _base }
}
/// Augment `s` with lazy methods such as `map`, `filter`, etc.
public func _prext_lazy<Base: Collection>(s: Base) -> _prext_LazyCollection<Base> {
return _prext_LazyCollection(s)
}
//===--- New stuff --------------------------------------------------------===//
/// The `IteratorProtocol` used by `_prext_MapSequence` and `_prext_MapCollection`.
/// Produces each element by passing the output of the `Base`
/// `IteratorProtocol` through a transform function returning `T`.
public struct _prext_MapIterator<
Base: IteratorProtocol, T
> : IteratorProtocol, Sequence {
/// Advance to the next element and return it, or `nil` if no next
/// element exists.
///
/// - Precondition: `next()` has not been applied to a copy of `self`
/// since the copy was made, and no preceding call to `self.next()`
/// has returned `nil`.
public mutating func next() -> T? {
let x = _base.next()
if x != nil {
return _transform(x!)
}
return nil
}
var _base: Base
var _transform: (Base.Element)->T
}
//===--- Sequences --------------------------------------------------------===//
/// A `Sequence` whose elements consist of those in a `Base`
/// `Sequence` passed through a transform function returning `T`.
/// These elements are computed lazily, each time they're read, by
/// calling the transform function on a base element.
public struct _prext_MapSequence<Base : Sequence, T>
: _prext_LazySequence, _SequenceWrapper {
typealias Elements = _prext_MapSequence
public func makeIterator() -> _prext_MapIterator<Base.Iterator,T> {
return _prext_MapIterator(
_base: _base.makeIterator(), _transform: _transform)
}
var _base: Base
var _transform: (Base.Iterator.Element)->T
}
//===--- Collections ------------------------------------------------------===//
/// A `Collection` whose elements consist of those in a `Base`
/// `Collection` passed through a transform function returning `T`.
/// These elements are computed lazily, each time they're read, by
/// calling the transform function on a base element.
public struct _prext_MapCollection<Base : Collection, T>
: _prext_LazyCollection, _CollectionWrapper {
public var startIndex: Base.Index { return _base.startIndex }
public var endIndex: Base.Index { return _base.endIndex }
/// Access the element at `position`.
///
/// - Precondition: `position` is a valid position in `self` and
/// `position != endIndex`.
public subscript(position: Base.Index) -> T {
return _transform(_base[position])
}
public func makeIterator() -> _prext_MapIterator<Base.Iterator, T> {
return _prext_MapIterator(_base: _base.makeIterator(), _transform: _transform)
}
public func underestimatedCount() -> Int {
return _base.underestimatedCount()
}
var _base: Base
var _transform: (Base.Iterator.Element)->T
}
//===--- Support for lazy(s) ----------------------------------------------===//
extension _prext_LazySequence {
/// Return a `_prext_MapSequence` over this `Sequence`. The elements of
/// the result are computed lazily, each time they are read, by
/// calling `transform` function on a base element.
public func map<U>(
transform: (Elements.Iterator.Element) -> U
) -> _prext_MapSequence<Self.Elements, U> {
return _prext_MapSequence(_base: self.elements, _transform: transform)
}
}
extension _prext_LazyCollection {
/// Return a `_prext_MapCollection` over this `Collection`. The elements of
/// the result are computed lazily, each time they are read, by
/// calling `transform` function on a base element.
public func map<U>(
transform: (Elements.Iterator.Element) -> U
) -> _prext_MapCollection<Self.Elements, U> {
return _prext_MapCollection(_base: self.elements, _transform: transform)
}
}
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End:
//===--- New stuff --------------------------------------------------------===//
internal protocol __prext_ReverseCollection : _prext_LazyCollection {
typealias Base : Collection
var _base : Base {get}
}
/// A wrapper for a `BidirectionalIndex` that reverses its
/// direction of traversal.
public struct _prext_ReverseIndex<I : BidirectionalIndex> : BidirectionalIndex {
var _base: I
init(_ _base: I) { self._base = _base }
/// Returns the next consecutive value after `self`.
///
/// - Precondition: The next value is representable.
public func successor() -> _prext_ReverseIndex {
return _prext_ReverseIndex(_base.predecessor())
}
/// Returns the previous consecutive value before `self`.
///
/// - Precondition: The previous value is representable.
public func predecessor() -> _prext_ReverseIndex {
return _prext_ReverseIndex(_base.successor())
}
/// A type that can represent the number of steps between pairs of
/// `_prext_ReverseIndex` values where one value is reachable from the other.
typealias Distance = I.Distance
}
/// A wrapper for a `${IndexProtocol}` that reverses its
/// direction of traversal.
public struct _prext_ReverseRandomAccessIndex<I : RandomAccessIndex> : RandomAccessIndex {
var _base: I
init(_ _base: I) { self._base = _base }
/// Returns the next consecutive value after `self`.
///
/// - Precondition: The next value is representable.
public func successor() -> _prext_ReverseRandomAccessIndex {
return _prext_ReverseRandomAccessIndex(_base.predecessor())
}
/// Returns the previous consecutive value before `self`.
///
/// - Precondition: The previous value is representable.
public func predecessor() -> _prext_ReverseRandomAccessIndex {
return _prext_ReverseRandomAccessIndex(_base.successor())
}
/// A type that can represent the number of steps between pairs of
/// `_prext_ReverseRandomAccessIndex` values where one value is reachable from the other.
typealias Distance = I.Distance
/// Return the minimum number of applications of `successor` or
/// `predecessor` required to reach `other` from `self`.
///
/// - Complexity: O(1).
public func distance(to other: _prext_ReverseRandomAccessIndex) -> Distance {
return other._base.distance(to: _base)
}
/// Return `self` offset by `n` steps.
///
/// - Returns: If `n > 0`, the result of applying `successor` to
/// `self` `n` times. If `n < 0`, the result of applying
/// `predecessor` to `self` `-n` times. Otherwise, `self`.
///
/// - Complexity: O(1).
public func advanced(by amount: Distance) -> _prext_ReverseRandomAccessIndex {
return _prext_ReverseRandomAccessIndex(_base.advanced(by: -amount))
}
}
public func == <I> (lhs: _prext_ReverseIndex<I>, rhs: _prext_ReverseIndex<I>) -> Bool {
return lhs._base == rhs._base
}
public func == <I> (lhs: _prext_ReverseRandomAccessIndex<I>, rhs: _prext_ReverseRandomAccessIndex<I>) -> Bool {
return lhs._base == rhs._base
}
extension Collection
where Self : __prext_ReverseCollection, Self.Base.Index : BidirectionalIndex {
public var startIndex : _prext_ReverseIndex<Base.Index> {
return _prext_ReverseIndex<Base.Index>(_base.endIndex)
}
public var endIndex : _prext_ReverseIndex<Base.Index> {
return _prext_ReverseIndex<Base.Index>(_base.startIndex)
}
public subscript(position: _prext_ReverseIndex<Base.Index>) -> Base.Iterator.Element {
return _base[position._base.predecessor()]
}
}
extension Collection
where Self : __prext_ReverseCollection, Self.Base.Index : RandomAccessIndex {
public var startIndex : _prext_ReverseRandomAccessIndex<Base.Index> {
return _prext_ReverseRandomAccessIndex<Base.Index>(_base.endIndex)
}
public var endIndex : _prext_ReverseRandomAccessIndex<Base.Index> {
return _prext_ReverseRandomAccessIndex<Base.Index>(_base.startIndex)
}
public subscript(position: _prext_ReverseRandomAccessIndex<Base.Index>) -> Base.Iterator.Element {
return _base[position._base.predecessor()]
}
}
/// The lazy `Collection` returned by `c.reversed()` where `c` is a
/// `Collection` with an `Index` conforming to `${IndexProtocol}`.
public struct _prext_ReverseCollection<Base : Collection>
: Collection, __prext_ReverseCollection {
public init(_ _base: Base) {
self._base = _base
}
internal var _base: Base
}