diff options
author | Paul Phillips <paulp@improving.org> | 2009-05-07 15:43:48 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-05-07 15:43:48 +0000 |
commit | 8155f5e712b80a3136c4e08362b8d85b1ef52d60 (patch) | |
tree | 4aeb5ee8dde7c52d8631d762519bd6d440c42548 /src/library | |
parent | ee2d15b908360a415c7e30fdde62a2c9bf5f849f (diff) | |
download | scala-8155f5e712b80a3136c4e08362b8d85b1ef52d60.tar.gz scala-8155f5e712b80a3136c4e08362b8d85b1ef52d60.tar.bz2 scala-8155f5e712b80a3136c4e08362b8d85b1ef52d60.zip |
Modernized DRMacIver's languishing array patch ...
Modernized DRMacIver's languishing array patch and added test cases to
exercise primitive to any array conversion. This fixes bugs #1300 and
#1301.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Array.scala | 67 |
1 files changed, 23 insertions, 44 deletions
diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala index 99b3332ac6..45d12b433e 100644 --- a/src/library/scala/Array.scala +++ b/src/library/scala/Array.scala @@ -21,6 +21,24 @@ import compat.Platform.arraycopy * @version 1.0 */ object Array { + import runtime.BoxedArray; + import scala.runtime.ScalaRunTime.boxArray; + private def slowcopy( + src : AnyRef, + srcPos : Int, + dest : AnyRef, + destPos : Int, + length : Int) { + + val srcArray = boxArray(src).asInstanceOf[BoxedArray[AnyRef]] + val destArray = boxArray(dest).asInstanceOf[BoxedArray[AnyRef]] + + var i = 0; + while(i < length) { + destArray(destPos + i) = srcArray(srcPos + i) + i += 1 + } + } /** Copy one array to another. * Equivalent to @@ -34,50 +52,11 @@ object Array { * @param length ... */ def copy(src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int) { - src match { - case xs: runtime.BoxedArray[_] => - xs.copyTo(srcPos, dest, destPos, length) - case _ => - dest match { - case xs: runtime.BoxedArray[_] => - xs.copyFrom(src, srcPos, destPos, length) - case _ => - def fillDest[T](da: Array[T], sa: Int=>T) { - var d = destPos - for (s <- srcPos to srcPos+length-1) { - da(d) = sa(s); d += 1 - } - } - if (dest.isInstanceOf[Array[Any]]) { - def fill(sa: Int=>Any) = fillDest(dest.asInstanceOf[Array[Any]], sa) - src match { - case sa:Array[Int] => fill(s=>Int.box(sa(s))) - case sa:Array[Long] => fill(s=>Long.box(sa(s))) - case sa:Array[Char] => fill(s=>Char.box(sa(s))) - case sa:Array[Boolean] => fill(s=>Boolean.box(sa(s))) - case sa:Array[Byte] => fill(s=>Byte.box(sa(s))) - case sa:Array[Short] => fill(s=>Short.box(sa(s))) - case sa:Array[Double] => fill(s=>Double.box(sa(s))) - case sa:Array[Float] => fill(s=>Float.box(sa(s))) - case _ => arraycopy(src, srcPos, dest, destPos, length) - } - } else if (dest.isInstanceOf[Array[AnyVal]]) { - def fill(sa: Int=>AnyVal) = fillDest(dest.asInstanceOf[Array[AnyVal]], sa) - src match { - case sa:Array[Int] => fill(sa(_)) - case sa:Array[Long] => fill(sa(_)) - case sa:Array[Char] => fill(sa(_)) - case sa:Array[Boolean] => fill(sa(_)) - case sa:Array[Byte] => fill(sa(_)) - case sa:Array[Short] => fill(sa(_)) - case sa:Array[Double] => fill(sa(_)) - case sa:Array[Float] => fill(sa(_)) - case _ => arraycopy(src, srcPos, dest, destPos, length) - } - } else - arraycopy(src, srcPos, dest, destPos, length) - } - } + val srcClass = src.getClass + if (srcClass.isArray && dest.getClass.isAssignableFrom(srcClass)) + arraycopy(src, srcPos, dest, destPos, length) + else + slowcopy(src, srcPos, dest, destPos, length) } /** Concatenate all argument sequences into a single array. |