diff options
author | Paul Phillips <paulp@improving.org> | 2013-01-04 12:08:32 -0800 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2013-01-04 12:08:32 -0800 |
commit | 54aca491463f25985d45aeb823d2cddda54b6a44 (patch) | |
tree | 064e96da0b40876fdab603e1da478969e3fd579f | |
parent | e112ac94d04be7ee715ebf8724709123b0f3e1f6 (diff) | |
parent | 02b2da63409af6a28824cbb74d00d0ec04518c8d (diff) | |
download | scala-54aca491463f25985d45aeb823d2cddda54b6a44.tar.gz scala-54aca491463f25985d45aeb823d2cddda54b6a44.tar.bz2 scala-54aca491463f25985d45aeb823d2cddda54b6a44.zip |
Merge pull request #1739 from jedesah/Array_opt
SI-5017 Poor performance of :+ operator on Arrays
-rw-r--r-- | src/library/scala/collection/mutable/ArrayOps.scala | 14 | ||||
-rw-r--r-- | test/files/run/array-addition.check | 4 | ||||
-rw-r--r-- | test/files/run/array-addition.scala | 11 |
3 files changed, 29 insertions, 0 deletions
diff --git a/src/library/scala/collection/mutable/ArrayOps.scala b/src/library/scala/collection/mutable/ArrayOps.scala index bb938a7aeb..6b778b26f5 100644 --- a/src/library/scala/collection/mutable/ArrayOps.scala +++ b/src/library/scala/collection/mutable/ArrayOps.scala @@ -52,6 +52,20 @@ trait ArrayOps[T] extends Any with ArrayLike[T, Array[T]] with CustomParalleliza super.toArray[U] } + def :+[B >: T: scala.reflect.ClassTag](elem: B): Array[B] = { + val result = Array.ofDim[B](repr.length + 1) + Array.copy(repr, 0, result, 0, repr.length) + result(repr.length) = elem + result + } + + def +:[B >: T: scala.reflect.ClassTag](elem: B): Array[B] = { + val result = Array.ofDim[B](repr.length + 1) + result(0) = elem + Array.copy(repr, 0, result, 1, repr.length) + result + } + override def par = ParArray.handoff(repr) /** Flattens a two-dimensional array by concatenating all its rows diff --git a/test/files/run/array-addition.check b/test/files/run/array-addition.check new file mode 100644 index 0000000000..7bfbd9c711 --- /dev/null +++ b/test/files/run/array-addition.check @@ -0,0 +1,4 @@ +Array(1, 2, 3, 4) +Array(1, 2, 3, 4) +Array(1) +Array(1) diff --git a/test/files/run/array-addition.scala b/test/files/run/array-addition.scala new file mode 100644 index 0000000000..8def48e85c --- /dev/null +++ b/test/files/run/array-addition.scala @@ -0,0 +1,11 @@ +object Test { + def prettyPrintArray(x: Array[_]) = println("Array(" + x.mkString(", ") + ")") + + def main(args: Array[String]): Unit = { + prettyPrintArray(Array(1,2,3) :+ 4) + prettyPrintArray(1 +: Array(2,3,4)) + prettyPrintArray(Array() :+ 1) + prettyPrintArray(1 +: Array()) + } +} + |