From 202807712f95209cf68c2feee8095e5d2faccb76 Mon Sep 17 00:00:00 2001 From: Julien Richard-Foy Date: Thu, 18 Jan 2018 11:22:04 +0100 Subject: [PATCH] Pull up reverseIterator implementation from Vector to IndexedSeq --- .../main/scala/strawman/collection/IndexedSeq.scala | 12 +++++++++++- .../scala/strawman/collection/immutable/Vector.scala | 10 ---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/collections/src/main/scala/strawman/collection/IndexedSeq.scala b/collections/src/main/scala/strawman/collection/IndexedSeq.scala index 270b2dde66..60a284fa42 100644 --- a/collections/src/main/scala/strawman/collection/IndexedSeq.scala +++ b/collections/src/main/scala/strawman/collection/IndexedSeq.scala @@ -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]] @@ -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) diff --git a/collections/src/main/scala/strawman/collection/immutable/Vector.scala b/collections/src/main/scala/strawman/collection/immutable/Vector.scala index 5dc5dc803f..9f94bfe538 100644 --- a/collections/src/main/scala/strawman/collection/immutable/Vector.scala +++ b/collections/src/main/scala/strawman/collection/immutable/Vector.scala @@ -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 ...