diff options
author | Rui Gonçalves <ruippeixotog@gmail.com> | 2015-04-04 15:03:06 +0100 |
---|---|---|
committer | Rui Gonçalves <ruippeixotog@gmail.com> | 2015-04-04 15:03:06 +0100 |
commit | 065388637eb07d90a32f22a2a00de93bbb934b34 (patch) | |
tree | 4efdcdfe62f3c8e3624ad9a518bf87e0f2bde752 /src/library/scala/collection/Iterator.scala | |
parent | ebf0976c363c67e6a46c66d70b39704f1ce5e74a (diff) | |
download | scala-065388637eb07d90a32f22a2a00de93bbb934b34.tar.gz scala-065388637eb07d90a32f22a2a00de93bbb934b34.tar.bz2 scala-065388637eb07d90a32f22a2a00de93bbb934b34.zip |
SI-8627 Two-argument indexOf does not work for Iterator
Adds two new methods to `Iterator`, overloading `indexOf` and `indexWhere` with a two-arg version whose second argument indicates the index where to start the search. These methods were already present in instances of `GenSeqLike` but not on iterators, leading to strange behavior when two arguments were passed to `indexOf`.
Diffstat (limited to 'src/library/scala/collection/Iterator.scala')
-rw-r--r-- | src/library/scala/collection/Iterator.scala | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index 34a025e5b8..5c4b15ad0f 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -852,8 +852,25 @@ trait Iterator[+A] extends TraversableOnce[A] { * or -1 if such an element does not exist until the end of the iterator is reached. * @note Reuse: $consumesIterator */ - def indexWhere(p: A => Boolean): Int = { + def indexWhere(p: A => Boolean): Int = indexWhere(p, 0) + + /** Returns the index of the first produced value satisfying a predicate, or -1, after or at + * some start index. + * $mayNotTerminateInf + * + * @param p the predicate to test values + * @param from the start index + * @return the index `>= from` of the first produced value satisfying `p`, + * or -1 if such an element does not exist until the end of the iterator is reached. + * @note Reuse: $consumesIterator + */ + def indexWhere(p: A => Boolean, from: Int): Int = { var i = 0 + while (i < from && hasNext) { + next() + i += 1 + } + var found = false while (!found && hasNext) { if (p(next())) { @@ -874,8 +891,26 @@ trait Iterator[+A] extends TraversableOnce[A] { * or -1 if such an element does not exist until the end of the iterator is reached. * @note Reuse: $consumesIterator */ - def indexOf[B >: A](elem: B): Int = { + def indexOf[B >: A](elem: B): Int = indexOf(elem, 0) + + /** Returns the index of the first occurrence of the specified object in this iterable object + * after or at some start index. + * $mayNotTerminateInf + * + * @param elem element to search for. + * @param from the start index + * @return the index `>= from` of the first occurrence of `elem` in the values produced by this + * iterator, or -1 if such an element does not exist until the end of the iterator is + * reached. + * @note Reuse: $consumesIterator + */ + def indexOf[B >: A](elem: B, from: Int): Int = { var i = 0 + while (i < from && hasNext) { + next() + i += 1 + } + var found = false while (!found && hasNext) { if (next() == elem) { |