Skip to content

Commit

Permalink
Restore lazy iterator.drop
Browse files Browse the repository at this point in the history
Fix incorrect internal usage of drop.
Simplify reverse view iterator slice.
  • Loading branch information
som-snytt committed Nov 2, 2021
1 parent 5035461 commit 978a527
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
27 changes: 11 additions & 16 deletions src/library/scala/collection/IndexedSeqView.scala
Expand Up @@ -84,8 +84,8 @@ object IndexedSeqView {
}
@SerialVersionUID(3L)
private[collection] class IndexedSeqViewReverseIterator[A](self: IndexedSeqView[A]) extends AbstractIterator[A] with Serializable {
private[this] var pos = self.length - 1
private[this] var remainder = self.length
private[this] var pos = remainder - 1
@inline private[this] def _hasNext: Boolean = remainder > 0
def hasNext: Boolean = _hasNext
def next(): A =
Expand All @@ -96,22 +96,17 @@ object IndexedSeqView {
r
} else Iterator.empty.next()

override def drop(n: Int): Iterator[A] = {
if (n > 0) {
pos -= n
remainder = Math.max(0, remainder - n)
}
this
}

override def sliceIterator(from: Int, until: Int): Iterator[A] = {
override protected def sliceIterator(from: Int, until: Int): Iterator[A] = {
if (_hasNext) {
val startCutoff = pos
val untilCutoff = startCutoff - remainder + 1
val nextStartCutoff = if (from < 0) startCutoff else if (startCutoff - from < 0) 0 else startCutoff - from
val nextUntilCutoff = if (until < 0) startCutoff else if (startCutoff - until < untilCutoff) untilCutoff else startCutoff - until + 1
remainder = Math.max(0, nextStartCutoff - nextUntilCutoff + 1)
pos = nextStartCutoff
if (remainder <= from) remainder = 0
else if (from <= 0) {
if (until >= 0 && until < remainder) remainder = until
}
else {
pos = pos - from
if (until >= 0 && until < remainder) remainder = until - from
else remainder -= from
}
}
this
}
Expand Down
15 changes: 4 additions & 11 deletions src/library/scala/collection/Iterator.scala
Expand Up @@ -409,9 +409,9 @@ trait Iterator[+A] extends IterableOnce[A] with IterableOnceOps[A, Iterator, Ite

def indexWhere(p: A => Boolean, from: Int = 0): Int = {
var i = math.max(from, 0)
drop(from)
while (hasNext) {
if (p(next())) return i
val dropped = drop(from)
while (dropped.hasNext) {
if (p(dropped.next())) return i
i += 1
}
-1
Expand Down Expand Up @@ -635,14 +635,7 @@ trait Iterator[+A] extends IterableOnce[A] with IterableOnceOps[A, Iterator, Ite
def next() = if (hasNext) { hdDefined = false; hd } else Iterator.empty.next()
}

def drop(n: Int): Iterator[A] = {
var i = 0
while (i < n && hasNext) {
next()
i += 1
}
this
}
def drop(n: Int): Iterator[A] = sliceIterator(n, -1)

def dropWhile(p: A => Boolean): Iterator[A] = new AbstractIterator[A] {
// Magic value: -1 = hasn't dropped, 0 = found first, 1 = defer to parent iterator
Expand Down

0 comments on commit 978a527

Please sign in to comment.