forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequence.swift
651 lines (580 loc) · 20.8 KB
/
Sequence.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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
/// Encapsulates iteration state and interface for iteration over a
/// *sequence*.
///
/// - Note: While it is safe to copy a *generator*, advancing one
/// copy may invalidate the others.
///
/// Any code that uses multiple generators (or `for`...`in` loops)
/// over a single *sequence* should have static knowledge that the
/// specific *sequence* is multi-pass, either because its concrete
/// type is known or because it is constrained to `CollectionType`.
/// Also, the generators must be obtained by distinct calls to the
/// *sequence's* `generate()` method, rather than by copying.
public protocol GeneratorType {
/// The type of element generated by `self`.
typealias Element
/// Advance to the next element and return it, or `nil` if no next
/// element exists.
///
/// - Requires: `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`. Specific implementations of this protocol
/// are encouraged to respond to violations of this requirement by
/// calling `preconditionFailure("...")`.
@warn_unused_result
mutating func next() -> Element?
}
/// A type that can be iterated with a `for`...`in` loop.
///
/// `SequenceType` makes no requirement on conforming types regarding
/// whether they will be destructively "consumed" by iteration. To
/// ensure non-destructive iteration, constrain your *sequence* to
/// `CollectionType`.
///
/// As a consequence, it is not possible to run multiple `for` loops
/// on a sequence to "resume" iteration:
///
/// for element in sequence {
/// if ... some condition { break }
/// }
///
/// for element in sequence {
/// // Not guaranteed to continue from the next element.
/// }
///
/// `SequenceType` makes no requirement about the behavior in that
/// case. It is not correct to assume that a sequence will either be
/// "consumable" and will resume iteration, or that a sequence is a
/// collection and will restart iteration from the first element.
/// A conforming sequence that is not a collection is allowed to
/// produce an arbitrary sequence of elements from the second generator.
public protocol SequenceType {
/// A type that provides the *sequence*'s iteration interface and
/// encapsulates its iteration state.
typealias Generator : GeneratorType
// FIXME: should be constrained to SequenceType
// (<rdar://problem/20715009> Implement recursive protocol
// constraints)
/// A type that represents a subsequence of some of the elements.
typealias SubSequence
/// Return a *generator* over the elements of this *sequence*.
///
/// - Complexity: O(1).
@warn_unused_result
func generate() -> Generator
/// Return a value less than or equal to the number of elements in
/// `self`, **nondestructively**.
///
/// - Complexity: O(N).
@warn_unused_result
func underestimateCount() -> Int
/// Return an `Array` containing the results of mapping `transform`
/// over `self`.
///
/// - Complexity: O(N).
@warn_unused_result
func map<T>(
@noescape transform: (Generator.Element) throws -> T
) rethrows -> [T]
/// Return an `Array` containing the elements of `self`,
/// in order, that satisfy the predicate `includeElement`.
@warn_unused_result
func filter(
@noescape includeElement: (Generator.Element) throws -> Bool
) rethrows -> [Generator.Element]
/// Call `body` on each element in `self` in the same order as a
/// *for-in loop.*
///
/// sequence.forEach {
/// // body code
/// }
///
/// is similar to:
///
/// for element in sequence {
/// // body code
/// }
///
/// - Note: You cannot use the `break` or `continue` statement to exit the
/// current call of the `body` closure or skip subsequent calls.
/// - Note: Using the `return` statement in the `body` closure will only
/// exit from the current call to `body`, not any outer scope, and won't
/// skip subsequent calls.
///
/// - Complexity: O(`self.count`)
func forEach(@noescape body: (Generator.Element) throws -> Void) rethrows
/// Returns a subsequence containing all but the first `n` elements.
///
/// - Requires: `n >= 0`
/// - Complexity: O(`n`)
@warn_unused_result
func dropFirst(n: Int) -> SubSequence
/// Returns a subsequence containing all but the last `n` elements.
///
/// - Requires: `self` is a finite sequence.
/// - Requires: `n >= 0`
/// - Complexity: O(`self.count`)
@warn_unused_result
func dropLast(n: Int) -> SubSequence
/// Returns a subsequence, up to `maxLength` in length, containing the
/// initial elements.
///
/// If `maxLength` exceeds `self.count`, the result contains all
/// the elements of `self`.
///
/// - Requires: `maxLength >= 0`
@warn_unused_result
func prefix(maxLength: Int) -> SubSequence
/// Returns a slice, up to `maxLength` in length, containing the
/// final elements of `s`.
///
/// If `maxLength` exceeds `s.count`, the result contains all
/// the elements of `s`.
///
/// - Requires: `self` is a finite sequence.
/// - Requires: `maxLength >= 0`
@warn_unused_result
func suffix(maxLength: Int) -> SubSequence
/// Returns the maximal `SubSequence`s of `self`, in order, that
/// don't contain elements satisfying the predicate `isSeparator`.
///
/// - Parameter maxSplit: The maximum number of `SubSequence`s to
/// return, minus 1.
/// If `maxSplit + 1` `SubSequence`s are returned, the last one is
/// a suffix of `self` containing the remaining elements.
/// The default value is `Int.max`.
///
/// - Parameter allowEmptySubsequences: If `true`, an empty `SubSequence`
/// is produced in the result for each pair of consecutive elements
/// satisfying `isSeparator`.
/// The default value is `false`.
///
/// - Requires: `maxSplit >= 0`
@warn_unused_result
func split(maxSplit: Int, allowEmptySlices: Bool,
@noescape isSeparator: (Generator.Element) throws -> Bool
) rethrows -> [SubSequence]
@warn_unused_result
func _customContainsEquatableElement(
element: Generator.Element
) -> Bool?
/// If `self` is multi-pass (i.e., a `CollectionType`), invoke
/// `preprocess` on `self` and return its result. Otherwise, return
/// `nil`.
func _preprocessingPass<R>(@noescape preprocess: (Self) -> R) -> R?
/// Create a native array buffer containing the elements of `self`,
/// in the same order.
func _copyToNativeArrayBuffer() -> _ContiguousArrayBuffer<Generator.Element>
/// Copy a Sequence into an array, returning one past the last
/// element initialized.
func _initializeTo(ptr: UnsafeMutablePointer<Generator.Element>)
-> UnsafeMutablePointer<Generator.Element>
}
/// A default generate() function for `GeneratorType` instances that
/// are declared to conform to `SequenceType`
extension SequenceType
where Self.Generator == Self, Self : GeneratorType {
public func generate() -> Self {
return self
}
}
/// A sequence that lazily consumes and drops `n` elements from an underlying
/// `Base` generator before possibly returning the first available element.
///
/// The underlying generator's sequence may be infinite.
///
/// This is a class - we require reference semantics to keep track
/// of how many elements we've already dropped from the underlying sequence.
internal class _DropFirstSequence<Base : GeneratorType>
: SequenceType, GeneratorType {
internal var generator: Base
internal let limit: Int
internal var dropped: Int
internal init(_ generator: Base, limit: Int, dropped: Int = 0) {
self.generator = generator
self.limit = limit
self.dropped = dropped
}
internal func generate() -> _DropFirstSequence<Base> {
return self
}
internal func next() -> Base.Element? {
while dropped < limit {
if generator.next() == nil {
dropped = limit
return nil
}
dropped += 1
}
return generator.next()
}
internal func dropFirst(n: Int) -> AnySequence<Base.Element> {
// If this is already a _DropFirstSequence, we need to fold in
// the current drop count and drop limit so no data is lost.
//
// i.e. [1,2,3,4].dropFirst(1).dropFirst(1) should be equivalent to
// [1,2,3,4].dropFirst(2).
return AnySequence(
_DropFirstSequence(generator, limit: limit + n, dropped: dropped))
}
}
/// A sequence that only consumes up to `n` elements from an underlying
/// `Base` generator.
///
/// The underlying generator's sequence may be infinite.
///
/// This is a class - we require reference semantics to keep track
/// of how many elements we've already taken from the underlying sequence.
internal class _PrefixSequence<Base : GeneratorType>
: SequenceType, GeneratorType {
internal let maxLength: Int
internal var generator: Base
internal var taken: Int
internal init(_ generator: Base, maxLength: Int, taken: Int = 0) {
self.generator = generator
self.maxLength = maxLength
self.taken = taken
}
internal func generate() -> _PrefixSequence<Base> {
return self
}
internal func next() -> Base.Element? {
if taken >= maxLength { return nil }
taken += 1
if let next = generator.next() {
return next
}
taken = maxLength
return nil
}
internal func prefix(maxLength: Int) -> AnySequence<Base.Element> {
return AnySequence(
_PrefixSequence(generator,
maxLength: min(maxLength, self.maxLength), taken: taken))
}
}
//===----------------------------------------------------------------------===//
// Default implementations for SequenceType
//===----------------------------------------------------------------------===//
extension SequenceType {
/// Return an `Array` containing the results of mapping `transform`
/// over `self`.
///
/// - Complexity: O(N).
@warn_unused_result
public func map<T>(
@noescape transform: (Generator.Element) throws -> T
) rethrows -> [T] {
let initialCapacity = underestimateCount()
var result = ContiguousArray<T>()
result.reserveCapacity(initialCapacity)
var generator = generate()
// Add elements up to the initial capacity without checking for regrowth.
for _ in 0..<initialCapacity {
result.append(try transform(generator.next()!))
}
// Add remaining elements, if any.
while let element = generator.next() {
result.append(try transform(element))
}
return Array(result)
}
/// Return an `Array` containing the elements of `self`,
/// in order, that satisfy the predicate `includeElement`.
@warn_unused_result
public func filter(
@noescape includeElement: (Generator.Element) throws -> Bool
) rethrows -> [Generator.Element] {
var result = ContiguousArray<Generator.Element>()
var generator = generate()
while let element = generator.next() {
if try includeElement(element) {
result.append(element)
}
}
return Array(result)
}
@warn_unused_result
public func suffix(maxLength: Int) -> AnySequence<Generator.Element> {
_precondition(maxLength >= 0, "Can't take a suffix of negative length from a sequence")
if maxLength == 0 { return AnySequence([]) }
// FIXME: <rdar://problem/21885650> Create reusable RingBuffer<T>
// Put incoming elements into a ring buffer to save space. Once all
// elements are consumed, reorder the ring buffer into an `Array`
// and return it. This saves memory for sequences particularly longer
// than `maxLength`.
var ringBuffer: [Generator.Element] = []
ringBuffer.reserveCapacity(min(maxLength, underestimateCount()))
var i = ringBuffer.startIndex
for element in self {
if ringBuffer.count < maxLength {
ringBuffer.append(element)
} else {
ringBuffer[i] = element
i = i.successor() % maxLength
}
}
if i != ringBuffer.startIndex {
return AnySequence(
[ringBuffer[i..<ringBuffer.endIndex], ringBuffer[0..<i]].flatten())
}
return AnySequence(ringBuffer)
}
/// Returns the maximal `SubSequence`s of `self`, in order, that
/// don't contain elements satisfying the predicate `isSeparator`.
///
/// - Parameter maxSplit: The maximum number of `SubSequence`s to
/// return, minus 1.
/// If `maxSplit + 1` `SubSequence`s are returned, the last one is
/// a suffix of `self` containing the remaining elements.
/// The default value is `Int.max`.
///
/// - Parameter allowEmptySubsequences: If `true`, an empty `SubSequence`
/// is produced in the result for each pair of consecutive elements
/// satisfying `isSeparator`.
/// The default value is `false`.
///
/// - Requires: `maxSplit >= 0`
@warn_unused_result
public func split(
maxSplit: Int = Int.max,
allowEmptySlices: Bool = false,
@noescape isSeparator: (Generator.Element) throws -> Bool
) rethrows -> [AnySequence<Generator.Element>] {
_precondition(maxSplit >= 0, "Must take zero or more splits")
var result: [AnySequence<Generator.Element>] = []
var subSequence: [Generator.Element] = []
func appendSubsequence() -> Bool {
if subSequence.isEmpty && !allowEmptySlices {
return false
}
result.append(AnySequence(subSequence))
subSequence = []
return true
}
if maxSplit == 0 {
// We aren't really splitting the sequence. Convert `self` into an
// `Array` using a fast entry point.
subSequence = Array(self)
appendSubsequence()
return result
}
var hitEnd = false
var generator = self.generate()
while true {
guard let element = generator.next() else {
hitEnd = true
break
}
if try isSeparator(element) {
if !appendSubsequence() {
continue
}
if result.count == maxSplit {
break
}
} else {
subSequence.append(element)
}
}
if !hitEnd {
while let element = generator.next() {
subSequence.append(element)
}
}
appendSubsequence()
return result
}
/// Return a value less than or equal to the number of elements in
/// `self`, **nondestructively**.
///
/// - Complexity: O(N).
@warn_unused_result
public func underestimateCount() -> Int {
return 0
}
public func _preprocessingPass<R>(@noescape preprocess: (Self) -> R) -> R? {
return nil
}
@warn_unused_result
public func _customContainsEquatableElement(
element: Generator.Element
) -> Bool? {
return nil
}
/// Call `body` on each element in `self` in the same order as a
/// *for-in loop.*
///
/// sequence.forEach {
/// // body code
/// }
///
/// is similar to:
///
/// for element in sequence {
/// // body code
/// }
///
/// - Note: You cannot use the `break` or `continue` statement to exit the
/// current call of the `body` closure or skip subsequent calls.
/// - Note: Using the `return` statement in the `body` closure will only
/// exit from the current call to `body`, not any outer scope, and won't
/// skip subsequent calls.
///
/// - Complexity: O(`self.count`)
public func forEach(
@noescape body: (Generator.Element) throws -> Void
) rethrows {
for element in self {
try body(element)
}
}
}
extension SequenceType where Generator.Element : Equatable {
/// Returns the maximal `SubSequence`s of `self`, in order, around elements
/// equatable to `separator`.
///
/// - Parameter maxSplit: The maximum number of `SubSequence`s to
/// return, minus 1.
/// If `maxSplit + 1` `SubSequence`s are returned, the last one is
/// a suffix of `self` containing the remaining elements.
/// The default value is `Int.max`.
///
/// - Parameter allowEmptySubsequences: If `true`, an empty `SubSequence`
/// is produced in the result for each pair of consecutive elements
/// satisfying `isSeparator`.
/// The default value is `false`.
///
/// - Requires: `maxSplit >= 0`
@warn_unused_result
public func split(
separator: Generator.Element,
maxSplit: Int = Int.max,
allowEmptySlices: Bool = false
) -> [AnySequence<Generator.Element>] {
return split(maxSplit, allowEmptySlices: allowEmptySlices,
isSeparator: { $0 == separator })
}
}
extension SequenceType where
SubSequence : SequenceType,
SubSequence.Generator.Element == Generator.Element,
SubSequence.SubSequence == SubSequence {
/// Returns a subsequence containing all but the first `n` elements.
///
/// - Requires: `n >= 0`
/// - Complexity: O(`n`)
@warn_unused_result
public func dropFirst(n: Int) -> AnySequence<Generator.Element> {
_precondition(n >= 0, "Can't drop a negative number of elements from a sequence")
if n == 0 { return AnySequence(self) }
return AnySequence(_DropFirstSequence(generate(), limit: n))
}
/// Returns a subsequence containing all but the last `n` elements.
///
/// - Requires: `self` is a finite collection.
/// - Requires: `n >= 0`
/// - Complexity: O(`self.count`)
@warn_unused_result
public func dropLast(n: Int) -> AnySequence<Generator.Element> {
_precondition(n >= 0, "Can't drop a negative number of elements from a sequence")
if n == 0 { return AnySequence(self) }
// FIXME: <rdar://problem/21885650> Create reusable RingBuffer<T>
// Put incoming elements from this sequence in a holding tank, a ring buffer
// of size <= n. If more elements keep coming in, pull them out of the
// holding tank into the result, an `Array`. This saves
// `n` * sizeof(Generator.Element) of memory, because slices keep the entire
// memory of an `Array` alive.
var result: [Generator.Element] = []
var ringBuffer: [Generator.Element] = []
var i = ringBuffer.startIndex
for element in self {
if ringBuffer.count < n {
ringBuffer.append(element)
} else {
result.append(ringBuffer[i])
ringBuffer[i] = element
i = i.successor() % n
}
}
return AnySequence(result)
}
@warn_unused_result
public func prefix(maxLength: Int) -> AnySequence<Generator.Element> {
_precondition(maxLength >= 0, "Can't take a prefix of negative length from a sequence")
if maxLength == 0 {
return AnySequence(EmptyCollection<Generator.Element>())
}
return AnySequence(_PrefixSequence(generate(), maxLength: maxLength))
}
}
extension SequenceType {
/// Returns a subsequence containing all but the first element.
///
/// - Complexity: O(1)
@warn_unused_result
public func dropFirst() -> SubSequence { return dropFirst(1) }
/// Returns a subsequence containing all but the last element.
///
/// - Requires: `self` is a finite sequence.
/// - Complexity: O(`self.count`)
@warn_unused_result
public func dropLast() -> SubSequence { return dropLast(1) }
}
/// Return an underestimate of the number of elements in the given
/// sequence, without consuming the sequence. For Sequences that are
/// actually Collections, this will return `x.count`.
@available(*, unavailable, message="call the 'underestimateCount()' method on the sequence")
public func underestimateCount<T : SequenceType>(x: T) -> Int {
fatalError("unavailable function can't be called")
}
extension SequenceType {
public func _initializeTo(ptr: UnsafeMutablePointer<Generator.Element>)
-> UnsafeMutablePointer<Generator.Element> {
var p = UnsafeMutablePointer<Generator.Element>(ptr)
for x in GeneratorSequence(self.generate()) {
p.initialize(x)
p += 1
}
return p
}
}
// Pending <rdar://problem/14011860> and <rdar://problem/14396120>,
// pass a GeneratorType through GeneratorSequence to give it "SequenceType-ness"
/// A sequence built around a generator of type `G`.
///
/// Useful mostly to recover the ability to use `for`...`in`,
/// given just a generator `g`:
///
/// for x in GeneratorSequence(g) { ... }
public struct GeneratorSequence<
Base : GeneratorType
> : GeneratorType, SequenceType {
@available(*, unavailable, renamed="Base")
public typealias G = Base
/// Construct an instance whose generator is a copy of `base`.
public init(_ base: Base) {
_base = base
}
/// Advance to the next element and return it, or `nil` if no next
/// element exists.
///
/// - Requires: `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() -> Base.Element? {
return _base.next()
}
internal var _base: Base
}