Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #348 from julienrf/indexed-seq-reverse-iterator
Browse files Browse the repository at this point in the history
Pull up reverseIterator implementation from Vector to IndexedSeq
  • Loading branch information
julienrf authored Jan 19, 2018
2 parents 9951cf7 + 2028077 commit 883115e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 11 additions & 1 deletion collections/src/main/scala/strawman/collection/IndexedSeq.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package strawman
package collection

import scala.{Any, Int}
import scala.{Any, Boolean, Int}

/** Base trait for indexed sequences that have efficient `apply` and `length` */
trait IndexedSeq[+A] extends Seq[A] with IndexedSeqOps[A, IndexedSeq, IndexedSeq[A]]
Expand All @@ -13,6 +13,16 @@ trait IndexedSeqOps[+A, +CC[X] <: IndexedSeq[X], +C] extends Any with SeqOps[A,

def iterator(): Iterator[A] = view.iterator()

override def reverseIterator(): Iterator[A] = new AbstractIterator[A] {
private var i = self.length
def hasNext: Boolean = 0 < i
def next(): A =
if (0 < i) {
i -= 1
self(i)
} else Iterator.empty.next()
}

override def view: IndexedView[A] = new IndexedView[A] {
def length: Int = self.length
def apply(i: Int): A = self(i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@ final class Vector[+A] private[immutable] (private[collection] val startIndex: I
s
}

override def reverseIterator(): Iterator[A] = new Iterator[A] {
private var i = self.length
def hasNext: Boolean = 0 < i
def next(): A =
if (0 < i) {
i -= 1
self(i)
} else Iterator.empty.next()
}

// Ideally, clients will inline calls to map all the way down, including the iterator/builder methods.
// In principle, escape analysis could even remove the iterator/builder allocations and do it
// with local variables exclusively. But we're not quite there yet ...
Expand Down

0 comments on commit 883115e

Please sign in to comment.