diff options
author | Paul Phillips <paulp@improving.org> | 2012-08-13 13:04:18 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-08-13 13:08:42 -0700 |
commit | caf7eb6b56817fd1e1fbc1cf017f30e6f94c6bea (patch) | |
tree | babb9a8b3e7c76f243d1649d72c0553476562cb2 /src/library/scala/collection/TraversableViewLike.scala | |
parent | 8cfba0fb219a49cceeb0318a6562aa3a602d913c (diff) | |
download | scala-caf7eb6b56817fd1e1fbc1cf017f30e6f94c6bea.tar.gz scala-caf7eb6b56817fd1e1fbc1cf017f30e6f94c6bea.tar.bz2 scala-caf7eb6b56817fd1e1fbc1cf017f30e6f94c6bea.zip |
Fix for view isEmpty.
Views have been inheriting the very inefficient isEmpty
from Traversable, and since isEmpty is not specifically
forwarded to the underlying collections, views miss out on all
the important optimizations in those collections which tend to
be implemented via method override. Not to mention, they miss
out on correctness, because calling foreach has a habit of
forcing the first element of the view.
Diffstat (limited to 'src/library/scala/collection/TraversableViewLike.scala')
-rw-r--r-- | src/library/scala/collection/TraversableViewLike.scala | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/library/scala/collection/TraversableViewLike.scala b/src/library/scala/collection/TraversableViewLike.scala index bf4f8205d6..7fbcf1374b 100644 --- a/src/library/scala/collection/TraversableViewLike.scala +++ b/src/library/scala/collection/TraversableViewLike.scala @@ -59,7 +59,7 @@ trait ViewMkString[+A] { * $viewInfo * * All views for traversable collections are defined by creating a new `foreach` method. - * + * * @author Martin Odersky * @version 2.8 * @since 2.8 @@ -162,7 +162,7 @@ trait TraversableViewLike[+A, // if (b.isInstanceOf[NoBuilder[_]]) newFlatMapped(f).asInstanceOf[That] // else super.flatMap[B, That](f)(bf) } - override def flatten[B](implicit asTraversable: A => /*<:<!!!*/ GenTraversableOnce[B]) = + override def flatten[B](implicit asTraversable: A => /*<:<!!!*/ GenTraversableOnce[B]) = newFlatMapped(asTraversable) private[this] implicit def asThis(xs: Transformed[A]): This = xs.asInstanceOf[This] @@ -193,6 +193,15 @@ trait TraversableViewLike[+A, override def span(p: A => Boolean): (This, This) = (newTakenWhile(p), newDroppedWhile(p)) override def splitAt(n: Int): (This, This) = (newTaken(n), newDropped(n)) + // Without this, isEmpty tests go back to the Traversable default, which + // involves starting a foreach, which can force the first element of the + // view. This is just a backstop - it's overridden at all the "def view" + // instantiation points in the collections where the Coll type is known. + override def isEmpty = underlying match { + case x: GenTraversableOnce[_] => x.isEmpty + case _ => super.isEmpty + } + override def scanLeft[B, That](z: B)(op: (B, A) => B)(implicit bf: CanBuildFrom[This, B, That]): That = newForced(thisSeq.scanLeft(z)(op)).asInstanceOf[That] |