@@ -331,63 +331,6 @@ extension SequenceType {
331
331
return Array ( result)
332
332
}
333
333
334
- /// Returns a subsequence containing all but the first `n` elements.
335
- ///
336
- /// - Requires: `n >= 0`
337
- /// - Complexity: O(`n`)
338
- @warn_unused_result
339
- public func dropFirst( n: Int ) -> AnySequence < Generator . Element > {
340
- _precondition ( n >= 0 , " Can't drop a negative number of elements from a sequence " )
341
- if n == 0 { return AnySequence ( self ) }
342
- // If this is already a _DropFirstSequence, we need to fold in
343
- // the current drop count and drop limit so no data is lost.
344
- //
345
- // i.e. [1,2,3,4].dropFirst(1).dropFirst(1) should be equivalent to
346
- // [1,2,3,4].dropFirst(2).
347
- // FIXME: <rdar://problem/21885675> Use method dispatch to fold
348
- // _PrefixSequence and _DropFirstSequence counts
349
- if let any = self as? AnySequence < Generator . Element > ,
350
- let box = any. _box as? _SequenceBox < _DropFirstSequence < Generator > > {
351
- let base = box. _base
352
- let folded = _DropFirstSequence ( base. generator, limit: base. limit + n,
353
- dropped: base. dropped)
354
- return AnySequence ( folded)
355
- }
356
-
357
- return AnySequence ( _DropFirstSequence ( generate ( ) , limit: n) )
358
- }
359
-
360
- /// Returns a subsequence containing all but the last `n` elements.
361
- ///
362
- /// - Requires: `self` is a finite collection.
363
- /// - Requires: `n >= 0`
364
- /// - Complexity: O(`self.count`)
365
- @warn_unused_result
366
- public func dropLast( n: Int ) -> AnySequence < Generator . Element > {
367
- _precondition ( n >= 0 , " Can't drop a negative number of elements from a sequence " )
368
- if n == 0 { return AnySequence ( self ) }
369
- // FIXME: <rdar://problem/21885650> Create reusable RingBuffer<T>
370
- // Put incoming elements from this sequence in a holding tank, a ring buffer
371
- // of size <= n. If more elements keep coming in, pull them out of the
372
- // holding tank into the result, an `Array`. This saves
373
- // `n` * sizeof(Generator.Element) of memory, because slices keep the entire
374
- // memory of an `Array` alive.
375
- var result : [ Generator . Element ] = [ ]
376
- var ringBuffer : [ Generator . Element ] = [ ]
377
- var i = ringBuffer. startIndex
378
-
379
- for element in self {
380
- if ringBuffer. count < n {
381
- ringBuffer. append ( element)
382
- } else {
383
- result. append ( ringBuffer [ i] )
384
- ringBuffer [ i] = element
385
- i = i. successor ( ) % n
386
- }
387
- }
388
- return AnySequence ( result)
389
- }
390
-
391
334
@warn_unused_result
392
335
public func prefix( maxLength: Int ) -> AnySequence < Generator . Element > {
393
336
_precondition ( maxLength >= 0 , " Can't take a prefix of negative length from a sequence " )
@@ -526,9 +469,7 @@ extension SequenceType {
526
469
) -> Bool ? {
527
470
return nil
528
471
}
529
- }
530
472
531
- extension SequenceType {
532
473
/// Call `body` on each element in `self` in the same order as a
533
474
/// *for-in loop.*
534
475
///
@@ -585,6 +526,69 @@ extension SequenceType where Generator.Element : Equatable {
585
526
}
586
527
}
587
528
529
+ extension SequenceType where
530
+ SubSequence : SequenceType ,
531
+ SubSequence. Generator. Element == Generator . Element ,
532
+ SubSequence. SubSequence == SubSequence {
533
+
534
+ /// Returns a subsequence containing all but the first `n` elements.
535
+ ///
536
+ /// - Requires: `n >= 0`
537
+ /// - Complexity: O(`n`)
538
+ @warn_unused_result
539
+ public func dropFirst( n: Int ) -> AnySequence < Generator . Element > {
540
+ _precondition ( n >= 0 , " Can't drop a negative number of elements from a sequence " )
541
+ if n == 0 { return AnySequence ( self ) }
542
+ // If this is already a _DropFirstSequence, we need to fold in
543
+ // the current drop count and drop limit so no data is lost.
544
+ //
545
+ // i.e. [1,2,3,4].dropFirst(1).dropFirst(1) should be equivalent to
546
+ // [1,2,3,4].dropFirst(2).
547
+ // FIXME: <rdar://problem/21885675> Use method dispatch to fold
548
+ // _PrefixSequence and _DropFirstSequence counts
549
+ if let any = self as? AnySequence < Generator . Element > ,
550
+ let box = any. _box as? _SequenceBox < _DropFirstSequence < Generator > > {
551
+ let base = box. _base
552
+ let folded = _DropFirstSequence ( base. generator, limit: base. limit + n,
553
+ dropped: base. dropped)
554
+ return AnySequence ( folded)
555
+ }
556
+
557
+ return AnySequence ( _DropFirstSequence ( generate ( ) , limit: n) )
558
+ }
559
+
560
+ /// Returns a subsequence containing all but the last `n` elements.
561
+ ///
562
+ /// - Requires: `self` is a finite collection.
563
+ /// - Requires: `n >= 0`
564
+ /// - Complexity: O(`self.count`)
565
+ @warn_unused_result
566
+ public func dropLast( n: Int ) -> AnySequence < Generator . Element > {
567
+ _precondition ( n >= 0 , " Can't drop a negative number of elements from a sequence " )
568
+ if n == 0 { return AnySequence ( self ) }
569
+ // FIXME: <rdar://problem/21885650> Create reusable RingBuffer<T>
570
+ // Put incoming elements from this sequence in a holding tank, a ring buffer
571
+ // of size <= n. If more elements keep coming in, pull them out of the
572
+ // holding tank into the result, an `Array`. This saves
573
+ // `n` * sizeof(Generator.Element) of memory, because slices keep the entire
574
+ // memory of an `Array` alive.
575
+ var result : [ Generator . Element ] = [ ]
576
+ var ringBuffer : [ Generator . Element ] = [ ]
577
+ var i = ringBuffer. startIndex
578
+
579
+ for element in self {
580
+ if ringBuffer. count < n {
581
+ ringBuffer. append ( element)
582
+ } else {
583
+ result. append ( ringBuffer [ i] )
584
+ ringBuffer [ i] = element
585
+ i = i. successor ( ) % n
586
+ }
587
+ }
588
+ return AnySequence ( result)
589
+ }
590
+ }
591
+
588
592
extension SequenceType {
589
593
/// Returns a subsequence containing all but the first element.
590
594
///
0 commit comments