summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2010-04-06 13:53:39 +0000
committerMartin Odersky <odersky@gmail.com>2010-04-06 13:53:39 +0000
commite0cf98dd424980a8d6d93af7650ba75c835b80bc (patch)
tree5f6b70d06f33a7a18dc4ac014df0025863f0cc94 /src/library
parenta292a87fc5b6145ea3a790780c0d24c83375b228 (diff)
downloadscala-e0cf98dd424980a8d6d93af7650ba75c835b80bc.tar.gz
scala-e0cf98dd424980a8d6d93af7650ba75c835b80bc.tar.bz2
scala-e0cf98dd424980a8d6d93af7650ba75c835b80bc.zip
Optimized toArray for ArrayOps and WrappedArrays.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/collection/SeqView.scala2
-rw-r--r--src/library/scala/collection/mutable/ArrayOps.scala6
-rw-r--r--src/library/scala/collection/mutable/IndexedSeqView.scala23
-rw-r--r--src/library/scala/collection/mutable/WrappedArray.scala7
-rw-r--r--src/library/scala/runtime/ScalaRunTime.scala3
5 files changed, 35 insertions, 6 deletions
diff --git a/src/library/scala/collection/SeqView.scala b/src/library/scala/collection/SeqView.scala
index 61443b3b90..79d50f1de6 100644
--- a/src/library/scala/collection/SeqView.scala
+++ b/src/library/scala/collection/SeqView.scala
@@ -21,6 +21,8 @@ import TraversableView.NoBuilder
*/
trait SeqView[+A, +Coll] extends SeqViewLike[A, Coll, SeqView[A, Coll]]
+/** $factoryInfo
+ */
object SeqView {
type Coll = TraversableView[_, C] forSome {type C <: Traversable[_]}
implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, SeqView[A, Seq[_]]] =
diff --git a/src/library/scala/collection/mutable/ArrayOps.scala b/src/library/scala/collection/mutable/ArrayOps.scala
index 61fcc77e14..553461c805 100644
--- a/src/library/scala/collection/mutable/ArrayOps.scala
+++ b/src/library/scala/collection/mutable/ArrayOps.scala
@@ -24,6 +24,12 @@ abstract class ArrayOps[T] extends ArrayLike[T, Array[T]] {
ClassManifest.fromClass(
repr.getClass.getComponentType.getComponentType.asInstanceOf[Predef.Class[U]]))
+ override def toArray[U >: T : ClassManifest]: Array[U] =
+ if (implicitly[ClassManifest[U]].erasure eq repr.getClass.getComponentType)
+ repr.asInstanceOf[Array[U]]
+ else
+ super.toArray[U]
+
/** Flattens a two-dimensional array by concatenating all its rows
* into a single array
*/
diff --git a/src/library/scala/collection/mutable/IndexedSeqView.scala b/src/library/scala/collection/mutable/IndexedSeqView.scala
index c6c92db1e7..d870b762d3 100644
--- a/src/library/scala/collection/mutable/IndexedSeqView.scala
+++ b/src/library/scala/collection/mutable/IndexedSeqView.scala
@@ -91,9 +91,22 @@ self =>
override def reverse: IndexedSeqView[A, Coll] = newReversed.asInstanceOf[IndexedSeqView[A, Coll]]
}
-/*
- * object IndexedSeqView {
- type Coll = TraversableView[_, C] forSome { type C <: scala.collection.Traversable[_] }
- implicit def canBuildFrom[A]: CanBuildFrom[IndexedSeq[_], A, IndexedSeqView[A], Coll] = new CanBuildFrom[mutable.IndexedSeq[_], A, IndexedSeqView[A], Coll] { : Coll) = new NoBuilder }
+/** $factoryInfo
+ * Note that the canBuildFrom factories yield SeqViews, not IndexedSewqViews.
+ * This is intentional, because not all operations yield again a mutable.IndexedSeqView.
+ * For instance, map just gives a SeqView, which reflects the fact that
+ * map cannot do its work and maintain a pointer into the original indexed sequence.
+ */
+object IndexedSeqView {
+ type Coll = TraversableView[_, C] forSome {type C <: Traversable[_]}
+ implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, SeqView[A, Seq[_]]] =
+ new CanBuildFrom[Coll, A, SeqView[A, Seq[_]]] {
+ def apply(from: Coll) = new NoBuilder
+ def apply() = new NoBuilder
+ }
+ implicit def arrCanBuildFrom[A]: CanBuildFrom[TraversableView[_, Array[_]], A, SeqView[A, Array[A]]] =
+ new CanBuildFrom[TraversableView[_, Array[_]], A, SeqView[A, Array[A]]] {
+ def apply(from: TraversableView[_, Array[_]]) = new NoBuilder
+ def apply() = new NoBuilder
+ }
}
-*/
diff --git a/src/library/scala/collection/mutable/WrappedArray.scala b/src/library/scala/collection/mutable/WrappedArray.scala
index 6652f5e40a..10117a1086 100644
--- a/src/library/scala/collection/mutable/WrappedArray.scala
+++ b/src/library/scala/collection/mutable/WrappedArray.scala
@@ -41,6 +41,13 @@ abstract class WrappedArray[T] extends IndexedSeq[T] with ArrayLike[T, WrappedAr
/** The underlying array */
def array: Array[T]
+
+ override def toArray[U >: T : ClassManifest]: Array[U] =
+ if (implicitly[ClassManifest[U]].erasure eq array.getClass.getComponentType)
+ array.asInstanceOf[Array[U]]
+ else
+ super.toArray[U]
+
override def stringPrefix = "WrappedArray"
/** Clones this object, including the underlying Array. */
diff --git a/src/library/scala/runtime/ScalaRunTime.scala b/src/library/scala/runtime/ScalaRunTime.scala
index 2970cf1980..dffebfc892 100644
--- a/src/library/scala/runtime/ScalaRunTime.scala
+++ b/src/library/scala/runtime/ScalaRunTime.scala
@@ -12,7 +12,7 @@
package scala.runtime
import scala.reflect.ClassManifest
-import scala.collection.{ Seq, IndexedSeq }
+import scala.collection.{ Seq, IndexedSeq, TraversableView }
import scala.collection.mutable.WrappedArray
import scala.collection.immutable.{ List, Stream, Nil, :: }
import scala.xml.{ Node, MetaData }
@@ -240,6 +240,7 @@ object ScalaRunTime {
// Not to mention MetaData extends Iterable[MetaData]
case x: MetaData => x toString
case x: AnyRef if isArray(x) => WrappedArray make x map inner mkString ("Array(", ", ", ")")
+ case x: TraversableView[_, _] => x.toString
case x: Traversable[_] if !x.hasDefiniteSize => x.toString
case x: Traversable[_] =>
// Some subclasses of AbstractFile implement Iterable, then throw an