diff options
Diffstat (limited to 'src/library/scala/collection/mutable/ArrayOps.scala')
-rwxr-xr-x | src/library/scala/collection/mutable/ArrayOps.scala | 69 |
1 files changed, 53 insertions, 16 deletions
diff --git a/src/library/scala/collection/mutable/ArrayOps.scala b/src/library/scala/collection/mutable/ArrayOps.scala index 6a85d7cd02..b5c0d0e458 100755 --- a/src/library/scala/collection/mutable/ArrayOps.scala +++ b/src/library/scala/collection/mutable/ArrayOps.scala @@ -12,24 +12,61 @@ package scala.collection package mutable -abstract class ArrayOps[T] extends VectorLike[T, AnyRef /*!!!Array[T]!!!*/] { -} +import generic.Builder + +import scala.reflect.ClassManifest + +abstract class ArrayOps[T] extends ArrayLike[T, Array[T]] { + + private def rowBuilder[U]: Builder[U, Array[U]] = + Array.newBuilder( + ClassManifest.fromClass( + repr.getClass.getComponentType.getComponentType.asInstanceOf[Predef.Class[U]])) + /** Flattens a two-dimensional array by concatenating all its rows + * into a single array + */ + def flatten[U](implicit asArray: T => /*<:<!!!*/ Array[U]): Array[U] = { + val b = rowBuilder[U] + for (xs <- this) + b ++= asArray(xs) + b.result + } + + /** Transposes a two dimensional array + */ + def transpose[U](implicit asArray: T => Array[U]): Array[Array[U]] = { + val bs = asArray(head) map (_ => rowBuilder[U]) + for (xs <- this) { + var i = 0 + for (x <- asArray(xs)) { + bs(i) += x + i += 1 + } + } + val bb: Builder[Array[U], Array[Array[U]]] = Array.newBuilder( + ClassManifest.fromClass( + repr.getClass.getComponentType.asInstanceOf[Predef.Class[Array[U]]])) + for (b <- bs) bb += b.result + bb.result + } +} object ArrayOps { - class ofRef[T <: AnyRef](override val repr: Array[AnyRef]) extends ArrayOps[T] with VectorLike[T, Array[AnyRef]] { + class ofRef[T <: AnyRef](override val repr: Array[T]) extends ArrayOps[T] with ArrayLike[T, Array[T]] { override protected[this] def thisCollection: WrappedArray[T] = new WrappedArray.ofRef[T](repr) - override protected[this] def toCollection(repr: Array[AnyRef]): WrappedArray[T] = new WrappedArray.ofRef[T](repr) - override protected[this] def newBuilder = new ArrayBuilder.ofRef[T] + override protected[this] def toCollection(repr: Array[T]): WrappedArray[T] = new WrappedArray.ofRef[T](repr) + override protected[this] def newBuilder = new ArrayBuilder.ofRef[T]()( + ClassManifest.classType[T](repr.getClass.getComponentType)) def length: Int = repr.length - def apply(index: Int): T = repr(index).asInstanceOf[T] - def update(index: Int, elem: T) { repr(index) = elem.asInstanceOf[AnyRef] } + def apply(index: Int): T = repr(index) + def update(index: Int, elem: T) { repr(index) = elem } } - class ofByte(override val repr: Array[Byte]) extends ArrayOps[Byte] with VectorLike[Byte, Array[Byte]] { + class ofByte(override val repr: Array[Byte]) extends ArrayOps[Byte] with ArrayLike[Byte, Array[Byte]] { override protected[this] def thisCollection: WrappedArray[Byte] = new WrappedArray.ofByte(repr) override protected[this] def toCollection(repr: Array[Byte]): WrappedArray[Byte] = new WrappedArray.ofByte(repr) @@ -40,7 +77,7 @@ object ArrayOps { def update(index: Int, elem: Byte) { repr(index) = elem } } - class ofShort(override val repr: Array[Short]) extends ArrayOps[Short] with VectorLike[Short, Array[Short]] { + class ofShort(override val repr: Array[Short]) extends ArrayOps[Short] with ArrayLike[Short, Array[Short]] { override protected[this] def thisCollection: WrappedArray[Short] = new WrappedArray.ofShort(repr) override protected[this] def toCollection(repr: Array[Short]): WrappedArray[Short] = new WrappedArray.ofShort(repr) @@ -51,7 +88,7 @@ object ArrayOps { def update(index: Int, elem: Short) { repr(index) = elem } } - class ofChar(override val repr: Array[Char]) extends ArrayOps[Char] with VectorLike[Char, Array[Char]] { + class ofChar(override val repr: Array[Char]) extends ArrayOps[Char] with ArrayLike[Char, Array[Char]] { override protected[this] def thisCollection: WrappedArray[Char] = new WrappedArray.ofChar(repr) override protected[this] def toCollection(repr: Array[Char]): WrappedArray[Char] = new WrappedArray.ofChar(repr) @@ -62,7 +99,7 @@ object ArrayOps { def update(index: Int, elem: Char) { repr(index) = elem } } - class ofInt(override val repr: Array[Int]) extends ArrayOps[Int] with VectorLike[Int, Array[Int]] { + class ofInt(override val repr: Array[Int]) extends ArrayOps[Int] with ArrayLike[Int, Array[Int]] { override protected[this] def thisCollection: WrappedArray[Int] = new WrappedArray.ofInt(repr) override protected[this] def toCollection(repr: Array[Int]): WrappedArray[Int] = new WrappedArray.ofInt(repr) @@ -73,7 +110,7 @@ object ArrayOps { def update(index: Int, elem: Int) { repr(index) = elem } } - class ofLong(override val repr: Array[Long]) extends ArrayOps[Long] with VectorLike[Long, Array[Long]] { + class ofLong(override val repr: Array[Long]) extends ArrayOps[Long] with ArrayLike[Long, Array[Long]] { override protected[this] def thisCollection: WrappedArray[Long] = new WrappedArray.ofLong(repr) override protected[this] def toCollection(repr: Array[Long]): WrappedArray[Long] = new WrappedArray.ofLong(repr) @@ -84,7 +121,7 @@ object ArrayOps { def update(index: Int, elem: Long) { repr(index) = elem } } - class ofFloat(override val repr: Array[Float]) extends ArrayOps[Float] with VectorLike[Float, Array[Float]] { + class ofFloat(override val repr: Array[Float]) extends ArrayOps[Float] with ArrayLike[Float, Array[Float]] { override protected[this] def thisCollection: WrappedArray[Float] = new WrappedArray.ofFloat(repr) override protected[this] def toCollection(repr: Array[Float]): WrappedArray[Float] = new WrappedArray.ofFloat(repr) @@ -95,7 +132,7 @@ object ArrayOps { def update(index: Int, elem: Float) { repr(index) = elem } } - class ofDouble(override val repr: Array[Double]) extends ArrayOps[Double] with VectorLike[Double, Array[Double]] { + class ofDouble(override val repr: Array[Double]) extends ArrayOps[Double] with ArrayLike[Double, Array[Double]] { override protected[this] def thisCollection: WrappedArray[Double] = new WrappedArray.ofDouble(repr) override protected[this] def toCollection(repr: Array[Double]): WrappedArray[Double] = new WrappedArray.ofDouble(repr) @@ -106,7 +143,7 @@ object ArrayOps { def update(index: Int, elem: Double) { repr(index) = elem } } - class ofBoolean(override val repr: Array[Boolean]) extends ArrayOps[Boolean] with VectorLike[Boolean, Array[Boolean]] { + class ofBoolean(override val repr: Array[Boolean]) extends ArrayOps[Boolean] with ArrayLike[Boolean, Array[Boolean]] { override protected[this] def thisCollection: WrappedArray[Boolean] = new WrappedArray.ofBoolean(repr) override protected[this] def toCollection(repr: Array[Boolean]): WrappedArray[Boolean] = new WrappedArray.ofBoolean(repr) @@ -117,7 +154,7 @@ object ArrayOps { def update(index: Int, elem: Boolean) { repr(index) = elem } } - class ofUnit(override val repr: Array[Unit]) extends ArrayOps[Unit] with VectorLike[Unit, Array[Unit]] { + class ofUnit(override val repr: Array[Unit]) extends ArrayOps[Unit] with ArrayLike[Unit, Array[Unit]] { override protected[this] def thisCollection: WrappedArray[Unit] = new WrappedArray.ofUnit(repr) override protected[this] def toCollection(repr: Array[Unit]): WrappedArray[Unit] = new WrappedArray.ofUnit(repr) |