~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to src/library/scala/collection/Iterator.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*                     __                                               *\
2
2
**     ________ ___   / /  ___     Scala API                            **
3
 
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
 
3
**    / __/ __// _ | / /  / _ |    (c) 2003-2013, LAMP/EPFL             **
4
4
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
5
5
** /____/\___/_/ |_/____/_/ | |                                         **
6
6
**                          |/                                          **
7
7
\*                                                                      */
8
8
 
9
 
package scala.collection
 
9
package scala
 
10
package collection
10
11
 
11
12
import mutable.ArrayBuffer
12
 
import annotation.migration
 
13
import scala.annotation.migration
13
14
import immutable.Stream
 
15
import scala.collection.generic.CanBuildFrom
 
16
import scala.annotation.unchecked.{ uncheckedVariance => uV }
14
17
 
15
18
/** The `Iterator` object provides various functions for creating specialized iterators.
16
19
 *
21
24
 */
22
25
object Iterator {
23
26
 
 
27
  /** With the advent of `TraversableOnce` and `Iterator`, it can be useful to have a builder which
 
28
   *  operates on `Iterator`s so they can be treated uniformly along with the collections.
 
29
   *  See `scala.util.Random.shuffle` for an example.
 
30
   */
 
31
  implicit def IteratorCanBuildFrom[A] = new TraversableOnce.BufferedCanBuildFrom[A, Iterator] {
 
32
    def bufferToColl[B](coll: ArrayBuffer[B]) = coll.iterator
 
33
    def traversableToColl[B](t: GenTraversable[B]) = t.toIterator
 
34
  }
 
35
 
24
36
  /** The iterator which produces no values. */
25
 
  val empty = new Iterator[Nothing] {
 
37
  val empty: Iterator[Nothing] = new AbstractIterator[Nothing] {
26
38
    def hasNext: Boolean = false
27
39
    def next(): Nothing = throw new NoSuchElementException("next on empty iterator")
28
40
  }
34
46
   *  @return An iterator which produces `elem` on the first call to `next`,
35
47
   *          and which has no further elements.
36
48
   */
37
 
  def single[A](elem: A) = new Iterator[A] {
 
49
  def single[A](elem: A): Iterator[A] = new AbstractIterator[A] {
38
50
    private var hasnext = true
39
51
    def hasNext: Boolean = hasnext
40
52
    def next(): A =
56
68
   *  @param   elem the element computation
57
69
   *  @return  An iterator that produces the results of `n` evaluations of `elem`.
58
70
   */
59
 
  def fill[A](len: Int)(elem: => A) = new Iterator[A] {
 
71
  def fill[A](len: Int)(elem: => A): Iterator[A] = new AbstractIterator[A] {
60
72
    private var i = 0
61
73
    def hasNext: Boolean = i < len
62
74
    def next(): A =
70
82
   *  @param  f   The function computing element values
71
83
   *  @return An iterator that produces the values `f(0), ..., f(n -1)`.
72
84
   */
73
 
  def tabulate[A](end: Int)(f: Int => A) = new Iterator[A] {
 
85
  def tabulate[A](end: Int)(f: Int => A): Iterator[A] = new AbstractIterator[A] {
74
86
    private var i = 0
75
87
    def hasNext: Boolean = i < end
76
88
    def next(): A =
93
105
   *  @param step  the increment value of the iterator (must be positive or negative)
94
106
   *  @return      the iterator producing values `start, start + step, ...` up to, but excluding `end`
95
107
   */
96
 
  def range(start: Int, end: Int, step: Int) = new Iterator[Int] {
 
108
  def range(start: Int, end: Int, step: Int): Iterator[Int] = new AbstractIterator[Int] {
97
109
    if (step == 0) throw new IllegalArgumentException("zero step")
98
110
    private var i = start
99
111
    def hasNext: Boolean = (step <= 0 || i < end) && (step >= 0 || i > end)
108
120
   *  @param f     the function that's repeatedly applied
109
121
   *  @return      the iterator producing the infinite sequence of values `start, f(start), f(f(start)), ...`
110
122
   */
111
 
  def iterate[T](start: T)(f: T => T): Iterator[T] = new Iterator[T] {
 
123
  def iterate[T](start: T)(f: T => T): Iterator[T] = new AbstractIterator[T] {
112
124
    private[this] var first = true
113
125
    private[this] var acc = start
114
126
    def hasNext: Boolean = true
133
145
   *  @param step  the increment between successive values
134
146
   *  @return      the iterator producing the infinite sequence of values `start, start + 1 * step, start + 2 * step, ...`
135
147
   */
136
 
  def from(start: Int, step: Int): Iterator[Int] = new Iterator[Int] {
 
148
  def from(start: Int, step: Int): Iterator[Int] = new AbstractIterator[Int] {
137
149
    private var i = start
138
150
    def hasNext: Boolean = true
139
151
    def next(): Int = { val result = i; i += step; result }
145
157
   *  @param elem the element computation.
146
158
   *  @return the iterator containing an infinite number of results of evaluating `elem`.
147
159
   */
148
 
  def continually[A](elem: => A): Iterator[A] = new Iterator[A] {
 
160
  def continually[A](elem: => A): Iterator[A] = new AbstractIterator[A] {
149
161
    def hasNext = true
150
162
    def next = elem
151
163
  }
152
 
 
153
 
  @deprecated("use `xs.iterator' or `Iterator(xs)' instead", "2.8.0")
154
 
  def fromValues[a](xs: a*) = xs.iterator
155
 
 
156
 
  /** @param xs the array of elements
157
 
   *  @see also: IndexedSeq.iterator and slice
158
 
   */
159
 
  @deprecated("use `xs.iterator' instead", "2.8.0")
160
 
  def fromArray[a](xs: Array[a]): Iterator[a] =
161
 
    fromArray(xs, 0, xs.length)
162
 
 
163
 
  /**
164
 
   *  @param xs     the array of elements
165
 
   *  @param start  the start index
166
 
   *  @param length  the length
167
 
   *  @see also: IndexedSeq.iterator and slice
168
 
   */
169
 
  @deprecated("use `xs.slice(start, start + length).iterator' instead", "2.8.0")
170
 
  def fromArray[a](xs: Array[a], start: Int, length: Int): Iterator[a] =
171
 
    xs.slice(start, start + length).iterator
172
 
 
173
 
  /**
174
 
   *  @param n the product arity
175
 
   *  @return  the iterator on `Product&lt;n&gt;`.
176
 
   */
177
 
  @deprecated("use product.productIterator instead", "2.8.0")
178
 
  def fromProduct(n: Product): Iterator[Any] = new Iterator[Any] {
179
 
    private var c: Int = 0
180
 
    private val cmax = n.productArity
181
 
    def hasNext = c < cmax
182
 
    def next() = { val a = n productElement c; c += 1; a }
183
 
  }
184
 
 
185
 
  /** Create an iterator with elements
186
 
   *  `e<sub>n+1</sub> = step(e<sub>n</sub>)`
187
 
   *  where `e<sub>0</sub> = start`
188
 
   *  and elements are in the range between `start` (inclusive)
189
 
   *  and `end` (exclusive)
190
 
   *
191
 
   *  @param start the start value of the iterator
192
 
   *  @param end   the end value of the iterator
193
 
   *  @param step  the increment function of the iterator, must be monotonically increasing or decreasing
194
 
   *  @return      the iterator with values in range `[start;end)`.
195
 
   */
196
 
  @deprecated("use Iterator.iterate(start, end - start)(step) instead", "2.8.0")
197
 
  def range(start: Int, end: Int, step: Int => Int) = new Iterator[Int] {
198
 
    private val up = step(start) > start
199
 
    private val down = step(start) < start
200
 
    private var i = start
201
 
    def hasNext: Boolean = (!up || i < end) && (!down || i > end)
202
 
    def next(): Int =
203
 
      if (hasNext) { val j = i; i = step(i); j }
204
 
      else empty.next()
205
 
  }
206
 
 
207
 
  /** Create an iterator with elements
208
 
   *  `e<sub>n+1</sub> = step(e<sub>n</sub>)`
209
 
   *  where `e<sub>0</sub> = start`.
210
 
   *
211
 
   *  @param start the start value of the iterator
212
 
   *  @param step  the increment function of the iterator
213
 
   *  @return      the iterator starting at value `start`.
214
 
   */
215
 
  @deprecated("use iterate(start)(step) instead", "2.8.0")
216
 
  def from(start: Int, step: Int => Int): Iterator[Int] = new Iterator[Int] {
217
 
    private var i = start
218
 
    override def hasNext: Boolean = true
219
 
    def next(): Int = { val j = i; i = step(i); j }
220
 
  }
221
 
 
222
 
  /** Create an iterator that is the concatenation of all iterators
223
 
   *  returned by a given iterator of iterators.
224
 
   *   @param its   The iterator which returns on each call to next
225
 
   *                a new iterator whose elements are to be concatenated to the result.
226
 
   */
227
 
  @deprecated("use its.flatten instead", "2.8.0")
228
 
  def flatten[T](its: Iterator[Iterator[T]]): Iterator[T] = new Iterator[T] {
229
 
    private var cur = its.next
230
 
    def hasNext: Boolean = {
231
 
      while (!cur.hasNext && its.hasNext) cur = its.next
232
 
      cur.hasNext
233
 
    }
234
 
    def next(): T =
235
 
      (if (hasNext) cur else empty).next()
236
 
  }
237
164
}
238
165
 
239
166
import Iterator.empty
376
303
      toDrop -= 1
377
304
    }
378
305
 
379
 
    new Iterator[A] {
 
306
    new AbstractIterator[A] {
380
307
      private var remaining = until - lo
381
308
      def hasNext = remaining > 0 && self.hasNext
382
309
      def next(): A =
396
323
   *          iterator by applying the function `f` to it.
397
324
   *  @note   Reuse: $consumesAndProducesIterator
398
325
   */
399
 
  def map[B](f: A => B): Iterator[B] = new Iterator[B] {
 
326
  def map[B](f: A => B): Iterator[B] = new AbstractIterator[B] {
400
327
    def hasNext = self.hasNext
401
328
    def next() = f(self.next())
402
329
  }
407
334
   *  @return  a new iterator that first yields the values produced by this
408
335
   *  iterator followed by the values produced by iterator `that`.
409
336
   *  @note    Reuse: $consumesTwoAndProducesOneIterator
 
337
   *
410
338
   *  @usecase def ++(that: => Iterator[A]): Iterator[A]
 
339
   *    @inheritdoc
411
340
   */
412
 
  def ++[B >: A](that: => GenTraversableOnce[B]): Iterator[B] = new Iterator[B] {
 
341
  def ++[B >: A](that: => GenTraversableOnce[B]): Iterator[B] = new AbstractIterator[B] {
413
342
    // optimize a little bit to prevent n log n behavior.
414
343
    private var cur : Iterator[B] = self
 
344
    private var selfExhausted : Boolean = false
415
345
    // since that is by-name, make sure it's only referenced once -
416
346
    // if "val it = that" is inside the block, then hasNext on an empty
417
347
    // iterator will continually reevaluate it.  (ticket #3269)
418
348
    lazy val it = that.toIterator
419
349
    // the eq check is to avoid an infinite loop on "x ++ x"
420
 
    def hasNext = cur.hasNext || ((cur eq self) && {
 
350
    def hasNext = cur.hasNext || (!selfExhausted && {
421
351
      it.hasNext && {
422
352
        cur = it
 
353
        selfExhausted = true
423
354
        true
424
355
      }
425
356
    })
434
365
   *           `f` to each value produced by this iterator and concatenating the results.
435
366
   *  @note    Reuse: $consumesAndProducesIterator
436
367
   */
437
 
  def flatMap[B](f: A => GenTraversableOnce[B]): Iterator[B] = new Iterator[B] {
 
368
  def flatMap[B](f: A => GenTraversableOnce[B]): Iterator[B] = new AbstractIterator[B] {
438
369
    private var cur: Iterator[B] = empty
439
370
    def hasNext: Boolean =
440
371
      cur.hasNext || self.hasNext && { cur = f(self.next).toIterator; hasNext }
448
379
   *  @return  an iterator which produces those values of this iterator which satisfy the predicate `p`.
449
380
   *  @note    Reuse: $consumesAndProducesIterator
450
381
   */
451
 
  def filter(p: A => Boolean): Iterator[A] = new Iterator[A] {
 
382
  def filter(p: A => Boolean): Iterator[A] = new AbstractIterator[A] {
452
383
    private var hd: A = _
453
384
    private var hdDefined: Boolean = false
454
385
 
464
395
    def next() = if (hasNext) { hdDefined = false; hd } else empty.next()
465
396
  }
466
397
 
 
398
  /** Tests whether every element of this iterator relates to the
 
399
   *  corresponding element of another collection by satisfying a test predicate.
 
400
   *
 
401
   *  @param   that    the other collection
 
402
   *  @param   p       the test predicate, which relates elements from both collections
 
403
   *  @tparam  B       the type of the elements of `that`
 
404
   *  @return          `true` if both collections have the same length and
 
405
   *                   `p(x, y)` is `true` for all corresponding elements `x` of this iterator
 
406
   *                   and `y` of `that`, otherwise `false`
 
407
   */
 
408
  def corresponds[B](that: GenTraversableOnce[B])(p: (A, B) => Boolean): Boolean = {
 
409
    val that0 = that.toIterator
 
410
    while (hasNext && that0.hasNext)
 
411
      if (!p(next, that0.next)) return false
 
412
 
 
413
    hasNext == that0.hasNext
 
414
  }
 
415
 
467
416
  /** Creates an iterator over all the elements of this iterator that
468
417
   *  satisfy the predicate `p`. The order of the elements
469
418
   *  is preserved.
498
447
  @migration("`collect` has changed. The previous behavior can be reproduced with `toSeq`.", "2.8.0")
499
448
  def collect[B](pf: PartialFunction[A, B]): Iterator[B] = {
500
449
    val self = buffered
501
 
    new Iterator[B] {
 
450
    new AbstractIterator[B] {
502
451
      private def skip() = while (self.hasNext && !pf.isDefinedAt(self.head)) self.next()
503
452
      def hasNext = { skip(); self.hasNext }
504
453
      def next() = { skip(); pf(self.next()) }
517
466
   *  @return        iterator with intermediate results
518
467
   *  @note          Reuse: $consumesAndProducesIterator
519
468
   */
520
 
  def scanLeft[B](z: B)(op: (B, A) => B): Iterator[B] = new Iterator[B] {
 
469
  def scanLeft[B](z: B)(op: (B, A) => B): Iterator[B] = new AbstractIterator[B] {
521
470
    var hasNext = true
522
471
    var elem = z
523
472
    def next() = if (hasNext) {
553
502
   *           the predicate `p`.
554
503
   *  @note    Reuse: $consumesAndProducesIterator
555
504
   */
556
 
  def takeWhile(p: A => Boolean): Iterator[A] = new Iterator[A] {
 
505
  def takeWhile(p: A => Boolean): Iterator[A] = new AbstractIterator[A] {
557
506
    private var hd: A = _
558
507
    private var hdDefined: Boolean = false
559
508
    private var tail: Iterator[A] = self
578
527
   */
579
528
  def partition(p: A => Boolean): (Iterator[A], Iterator[A]) = {
580
529
    val self = buffered
581
 
    class PartitionIterator(p: A => Boolean) extends Iterator[A] {
 
530
    class PartitionIterator(p: A => Boolean) extends AbstractIterator[A] {
582
531
      var other: PartitionIterator = _
583
532
      val lookahead = new mutable.Queue[A]
584
533
      def skip() =
612
561
     * iterator is referring (the finish() method) and thus triggering
613
562
     * handling of structural calls. It's not what's intended here.
614
563
     */
615
 
    class Leading extends Iterator[A] {
 
564
    class Leading extends AbstractIterator[A] {
616
565
      private var isDone = false
617
566
      val lookahead = new mutable.Queue[A]
618
567
      def advance() = {
634
583
      }
635
584
    }
636
585
    val leading = new Leading
637
 
    val trailing = new Iterator[A] {
 
586
    val trailing = new AbstractIterator[A] {
638
587
      private lazy val it = {
639
588
        leading.finish()
640
589
        self
656
605
   */
657
606
  def dropWhile(p: A => Boolean): Iterator[A] = {
658
607
    val self = buffered
659
 
    new Iterator[A] {
 
608
    new AbstractIterator[A] {
660
609
      var dropped = false
661
610
      private def skip() =
662
611
        if (!dropped) {
681
630
   *                 iterator and `that`.
682
631
   *  @note          Reuse: $consumesTwoAndProducesOneIterator
683
632
   */
684
 
  def zip[B](that: Iterator[B]) = new Iterator[(A, B)] {
 
633
  def zip[B](that: Iterator[B]): Iterator[(A, B)] = new AbstractIterator[(A, B)] {
685
634
    def hasNext = self.hasNext && that.hasNext
686
635
    def next = (self.next, that.next)
687
636
  }
694
643
   *          followed by the minimal number of occurrences of `elem` so
695
644
   *          that the number of produced values is at least `len`.
696
645
   *  @note    Reuse: $consumesAndProducesIterator
 
646
   *
697
647
   *  @usecase def padTo(len: Int, elem: A): Iterator[A]
 
648
   *    @inheritdoc
698
649
   */
699
 
  def padTo[A1 >: A](len: Int, elem: A1) = new Iterator[A1] {
 
650
  def padTo[A1 >: A](len: Int, elem: A1): Iterator[A1] = new AbstractIterator[A1] {
700
651
    private var count = 0
701
652
    def hasNext = self.hasNext || count < len
702
653
    def next = {
714
665
   *                 corresponding elements of this iterator and their indices.
715
666
   *  @note          Reuse: $consumesAndProducesIterator
716
667
   */
717
 
  def zipWithIndex = new Iterator[(A, Int)] {
 
668
  def zipWithIndex: Iterator[(A, Int)] = new AbstractIterator[(A, Int)] {
718
669
    var idx = 0
719
670
    def hasNext = self.hasNext
720
671
    def next = {
743
694
   *                  If this iterator is shorter than `that`, `thisElem` values are used to pad the result.
744
695
   *                  If `that` is shorter than this iterator, `thatElem` values are used to pad the result.
745
696
   *  @note           Reuse: $consumesTwoAndProducesOneIterator
 
697
   *
746
698
   *  @usecase def zipAll[B](that: Iterator[B], thisElem: A, thatElem: B): Iterator[(A, B)]
 
699
   *    @inheritdoc
747
700
   */
748
 
  def zipAll[B, A1 >: A, B1 >: B](that: Iterator[B], thisElem: A1, thatElem: B1) = new Iterator[(A1, B1)] {
 
701
  def zipAll[B, A1 >: A, B1 >: B](that: Iterator[B], thisElem: A1, thatElem: B1): Iterator[(A1, B1)] = new AbstractIterator[(A1, B1)] {
749
702
    def hasNext = self.hasNext || that.hasNext
750
703
    def next(): (A1, B1) =
751
704
      if (self.hasNext) {
767
720
   *              but this is not necessary.
768
721
   *
769
722
   *  @note    Reuse: $consumesIterator
 
723
   *
770
724
   *  @usecase def foreach(f: A => Unit): Unit
 
725
   *    @inheritdoc
771
726
   */
772
727
  def foreach[U](f: A =>  U) { while (hasNext) f(next()) }
773
728
 
804
759
   *
805
760
   *  @param elem  the element to test.
806
761
   *  @return     `true` if this iterator produces some value that is
807
 
   *               is equal (wrt `==`) to `elem`, `false` otherwise.
 
762
   *               is equal (as determined by `==`) to `elem`, `false` otherwise.
808
763
   *  @note        Reuse: $consumesIterator
809
764
   */
810
765
  def contains(elem: Any): Boolean = exists(_ == elem)
872
827
 
873
828
  /** Creates a buffered iterator from this iterator.
874
829
   *
875
 
   *  @see BufferedIterator
 
830
   *  @see [[scala.collection.BufferedIterator]]
876
831
   *  @return  a buffered iterator producing the same values as this iterator.
877
832
   *  @note    Reuse: $consumesAndProducesIterator
878
833
   */
879
 
  def buffered = new BufferedIterator[A] {
 
834
  def buffered: BufferedIterator[A] = new AbstractIterator[A] with BufferedIterator[A] {
880
835
    private var hd: A = _
881
836
    private var hdDefined: Boolean = false
882
837
 
904
859
   *
905
860
   *  Typical uses can be achieved via methods `grouped` and `sliding`.
906
861
   */
907
 
  class GroupedIterator[B >: A](self: Iterator[A], size: Int, step: Int) extends Iterator[Seq[B]] {
 
862
  class GroupedIterator[B >: A](self: Iterator[A], size: Int, step: Int)
 
863
  extends AbstractIterator[Seq[B]]
 
864
     with Iterator[Seq[B]] {
 
865
 
908
866
    require(size >= 1 && step >= 1, "size=%d and step=%d, but both must be positive".format(size, step))
909
867
 
910
868
    private[this] var buffer: ArrayBuffer[B] = ArrayBuffer()  // the buffer
1086
1044
  def duplicate: (Iterator[A], Iterator[A]) = {
1087
1045
    val gap = new scala.collection.mutable.Queue[A]
1088
1046
    var ahead: Iterator[A] = null
1089
 
    class Partner extends Iterator[A] {
 
1047
    class Partner extends AbstractIterator[A] {
1090
1048
      def hasNext: Boolean = self.synchronized {
1091
1049
        (this ne ahead) && !gap.isEmpty || self.hasNext
1092
1050
      }
1117
1075
   *  @param replaced   The number of values in the original iterator that are replaced by the patch.
1118
1076
   *  @note           Reuse: $consumesTwoAndProducesOneIterator
1119
1077
   */
1120
 
  def patch[B >: A](from: Int, patchElems: Iterator[B], replaced: Int) = new Iterator[B] {
 
1078
  def patch[B >: A](from: Int, patchElems: Iterator[B], replaced: Int): Iterator[B] = new AbstractIterator[B] {
1121
1079
    private var origElems = self
1122
1080
    private var i = 0
1123
1081
    def hasNext: Boolean =
1124
1082
      if (i < from) origElems.hasNext
1125
1083
      else patchElems.hasNext || origElems.hasNext
1126
1084
    def next(): B = {
 
1085
      // We have to do this *first* just in case from = 0.
 
1086
      if (i == from) origElems = origElems drop replaced
1127
1087
      val result: B =
1128
1088
        if (i < from || !patchElems.hasNext) origElems.next()
1129
1089
        else patchElems.next()
1130
1090
      i += 1
1131
 
      if (i == from) origElems = origElems drop replaced
1132
1091
      result
1133
1092
    }
1134
1093
  }
1139
1098
   *  Copying will stop once either the end of the current iterator is reached,
1140
1099
   *  or the end of the array is reached, or `len` elements have been copied.
1141
1100
   *
1142
 
   *  $willNotTerminateInf
1143
 
   *
1144
1101
   *  @param  xs     the array to fill.
1145
1102
   *  @param  start  the starting index.
1146
1103
   *  @param  len    the maximal number of elements to copy.
1147
1104
   *  @tparam B      the type of the elements of the array.
1148
1105
   *
1149
1106
   *  @note    Reuse: $consumesIterator
 
1107
   *
1150
1108
   *  @usecase def copyToArray(xs: Array[A], start: Int, len: Int): Unit
 
1109
   *    @inheritdoc
 
1110
   *
 
1111
   *    $willNotTerminateInf
1151
1112
   */
1152
1113
  def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Unit = {
 
1114
    require(start >= 0 && (start < xs.length || xs.length == 0), s"start $start out of range ${xs.length}")
1153
1115
    var i = start
1154
 
    val end = start + math.min(len, xs.length)
1155
 
    while (hasNext && i < end) {
 
1116
    val end = start + math.min(len, xs.length - start)
 
1117
    while (i < end && hasNext) {
1156
1118
      xs(i) = next()
1157
1119
      i += 1
1158
1120
    }
1181
1143
    if (self.hasNext) Stream.cons(self.next, self.toStream)
1182
1144
    else Stream.empty[A]
1183
1145
 
 
1146
 
1184
1147
  /** Converts this iterator to a string.
1185
1148
   *
1186
1149
   *  @return `"empty iterator"` or `"non-empty iterator"`, depending on
1188
1151
   *  @note    Reuse: $preservesIterator
1189
1152
   */
1190
1153
  override def toString = (if (hasNext) "non-empty" else "empty")+" iterator"
1191
 
 
1192
 
  /** Returns a new iterator that first yields the elements of this
1193
 
   *  iterator followed by the elements provided by iterator `that`.
1194
 
   */
1195
 
  @deprecated("use `++`", "2.3.2")
1196
 
  def append[B >: A](that: Iterator[B]) = self ++ that
1197
 
 
1198
 
  /** Returns index of the first element satisfying a predicate, or -1. */
1199
 
  @deprecated("use `indexWhere` instead", "2.8.0")
1200
 
  def findIndexOf(p: A => Boolean): Int = indexWhere(p)
1201
 
 
1202
 
  /** Returns a counted iterator from this iterator.
1203
 
   */
1204
 
  @deprecated("use zipWithIndex in Iterator", "2.8.0")
1205
 
  def counted = new CountedIterator[A] {
1206
 
    private var cnt = 0
1207
 
    def count = cnt
1208
 
    def hasNext: Boolean = self.hasNext
1209
 
    def next(): A = { cnt += 1; self.next }
1210
 
  }
1211
 
 
1212
 
  /** Fills the given array `xs` with the elements of
1213
 
   *  this sequence starting at position `start`.  Like `copyToArray`,
1214
 
   *  but designed to accomodate IO stream operations.
1215
 
   *
1216
 
   *  '''Note:'''   the array must be large enough to hold `sz` elements.
1217
 
   *  @param  xs    the array to fill.
1218
 
   *  @param  start the starting index.
1219
 
   *  @param  sz    the maximum number of elements to be read.
1220
 
   */
1221
 
  @deprecated("use copyToArray instead", "2.8.0")
1222
 
  def readInto[B >: A](xs: Array[B], start: Int, sz: Int) {
1223
 
    var i = start
1224
 
    while (hasNext && i - start < sz) {
1225
 
      xs(i) = next
1226
 
      i += 1
1227
 
    }
1228
 
  }
1229
 
 
1230
 
  @deprecated("use copyToArray instead", "2.8.0")
1231
 
  def readInto[B >: A](xs: Array[B], start: Int) {
1232
 
    readInto(xs, start, xs.length - start)
1233
 
  }
1234
 
 
1235
 
  @deprecated("use copyToArray instead", "2.8.0")
1236
 
  def readInto[B >: A](xs: Array[B]) {
1237
 
    readInto(xs, 0, xs.length)
1238
 
  }
1239
1154
}
 
1155
 
 
1156
/** Explicit instantiation of the `Iterator` trait to reduce class file size in subclasses. */
 
1157
private[scala] abstract class AbstractIterator[+A] extends Iterator[A]