diff options
-rw-r--r-- | src/library/scala/Array.scala | 68 | ||||
-rw-r--r-- | src/library/scala/compat/Platform.scala | 9 | ||||
-rw-r--r-- | test/files/run/arrays.check | 2 | ||||
-rw-r--r-- | test/files/run/arrays.scala | 61 |
4 files changed, 83 insertions, 57 deletions
diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala index b3f08c5ef8..edd508e6ef 100644 --- a/src/library/scala/Array.scala +++ b/src/library/scala/Array.scala @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2002-2008, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -33,29 +33,32 @@ object Array { * @param destPos ... * @param length ... */ - def copy(src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int): Unit = 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 _ => - arraycopy(src, srcPos, dest, destPos, 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 _ => + arraycopy(src, srcPos, dest, destPos, length) + } + } } - /** Concatenate all argument arrays into a single array. + /** Concatenate all argument sequences into a single array. * - * @param xs ... + * @param xs the given argument sequences + * @return the array created from the concatenated arguments */ - def concat[T](xs: Seq[T]*) = { + def concat[T](xs: Seq[T]*): Array[T] = { var len = 0 for (x <- xs) len += x.length val result = new Array[T](len) var start = 0 for (x <- xs) { - copy(x, 0, result, start, x.length) + copy(x.toArray, 0, result, start, x.length) start += x.length } result @@ -108,48 +111,56 @@ object Array { for (x <- xs.elements) { array(i) = x; i += 1 } array } + def apply(xs: Byte*): Array[Byte] = { val array = new Array[Byte](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } + def apply(xs: Short*): Array[Short] = { val array = new Array[Short](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } + def apply(xs: Char*): Array[Char] = { val array = new Array[Char](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } + def apply(xs: Int*): Array[Int] = { val array = new Array[Int](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } + def apply(xs: Long*): Array[Long] = { val array = new Array[Long](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } + def apply(xs: Float*): Array[Float] = { val array = new Array[Float](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } + def apply(xs: Double*): Array[Double] = { val array = new Array[Double](xs.length) var i = 0 for (x <- xs.elements) { array(i) = x; i += 1 } array } + def apply(xs: Unit*): Array[Unit] = { val array = new Array[Unit](xs.length) var i = 0 @@ -183,6 +194,7 @@ object Array { trait ArrayLike[A] extends RandomAccessSeq.Mutable[A] { def force : Array[A] } + trait Projection[A] extends RandomAccessSeq.MutableProjection[A] with ArrayLike[A] { protected def newArray[B >: A](length : Int, elements : Iterator[A]) : Array[B] override def toArray[B >: A] = (newArray(length, elements))//:Any).asInstanceOf[Array[B]] @@ -197,13 +209,13 @@ object Array { val c = length + 1 take((findIndexOf(!p(_)) + c) % c) } - override def slice(from0 : Int, until0 : Int) : Projection[A] = new RandomAccessSeq.MutableSlice[A] with Projection[A] { + override def slice(from0: Int, until0: Int): Projection[A] = new RandomAccessSeq.MutableSlice[A] with Projection[A] { override def from = from0 override def until = until0 override def underlying = Projection.this - override protected def newArray[B >: A](length : Int, elements : Iterator[A]) = + override protected def newArray[B >: A](length: Int, elements: Iterator[A]) = underlying.newArray(length, elements) - override def slice(from0 : Int, until0 : Int) = + override def slice(from0: Int, until0: Int) = Projection.this.slice(from + from0, from + until0) } @@ -239,19 +251,19 @@ final class Array[A](_length: Int) extends Array.Array0[A] { /** Multidimensional array creation */ def this(dim1: Int, dim2: Int) = { - this(dim1); + this(dim1) throw new Error() } /** Multidimensional array creation */ def this(dim1: Int, dim2: Int, dim3: Int) = { - this(dim1); + this(dim1) throw new Error() } /** Multidimensional array creation */ def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int) = { - this(dim1); + this(dim1) throw new Error() } @@ -263,25 +275,25 @@ final class Array[A](_length: Int) extends Array.Array0[A] { /** Multidimensional array creation */ def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int, dim5: Int, dim6: Int) = { - this(dim1); + this(dim1) throw new Error() } /** Multidimensional array creation */ def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int, dim5: Int, dim6: Int, dim7: Int) = { - this(dim1); + this(dim1) throw new Error() } /** Multidimensional array creation */ def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int, dim5: Int, dim6: Int, dim7: Int, dim8: Int) = { - this(dim1); + this(dim1) throw new Error() } /** Multidimensional array creation */ def this(dim1: Int, dim2: Int, dim3: Int, dim4: Int, dim5: Int, dim6: Int, dim7: Int, dim8: Int, dim9: Int) = { - this(dim1); + this(dim1) throw new Error() } @@ -321,7 +333,7 @@ final class Array[A](_length: Int) extends Array.Array0[A] { * @throws ArrayIndexOutOfBoundsException if <code>i < 0</code> or * <code>length <= i</code> */ - override def update(i: Int, x: A): Unit = throw new Error() + override def update(i: Int, x: A) { throw new Error() } /** An iterator returning the elements of this array, starting from 0. */ @@ -370,7 +382,7 @@ final class Array[A](_length: Int) extends Array.Array0[A] { * <code>Array(a<sub>0</sub>, ..., a<sub>m</sub>) * zip Array(b<sub>0</sub>, ..., b<sub>n</sub>)</code> is invoked. */ - def zip[B](that: Array[B]): Array[Tuple2[A,B]] = throw new Error() + def zip[B](that: Array[B]): Array[(A, B)] = throw new Error() /** Returns an array that pairs each element of this array * with its index, counting from 0. @@ -378,7 +390,7 @@ final class Array[A](_length: Int) extends Array.Array0[A] { * @return the array <code>Array({a<sub>0</sub>,0}, {a<sub>1</sub>,1},...)</code> * where <code>a<sub>i</sub></code> are the elements of this stream. */ - def zipWithIndex: Array[Tuple2[A,Int]] = throw new Error() + def zipWithIndex: Array[(A, Int)] = throw new Error() /** Returns an array that contains all indices of this array */ def indices: Array[Int] = throw new Error() diff --git a/src/library/scala/compat/Platform.scala b/src/library/scala/compat/Platform.scala index a0a3fe07ab..03cf1d28e2 100644 --- a/src/library/scala/compat/Platform.scala +++ b/src/library/scala/compat/Platform.scala @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2002-2008, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -27,8 +27,9 @@ object Platform { * @param destPos .. * @param length .. */ - def arraycopy(src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int): Unit = + def arraycopy(src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int) { System.arraycopy(src, srcPos, dest, destPos, length) + } /** Create array of the same type as arrayInstance with the given * length. @@ -40,7 +41,7 @@ object Platform { def createArray(elemClass: Class[_], length: Int): AnyRef = java.lang.reflect.Array.newInstance(elemClass, length) - def arrayclear(arr: Array[Int]): Unit = java.util.Arrays.fill(arr, 0) + def arrayclear(arr: Array[Int]) { java.util.Arrays.fill(arr, 0) } def getClassForName(name: String): Class[_] = java.lang.Class.forName(name) @@ -56,6 +57,4 @@ object Platform { new OutputStreamWriter(new ByteArrayOutputStream).getEncoding() } - } - diff --git a/test/files/run/arrays.check b/test/files/run/arrays.check index a649c9b4f8..658b849025 100644 --- a/test/files/run/arrays.check +++ b/test/files/run/arrays.check @@ -1 +1 @@ -checks: 2301 +checks: 2304 diff --git a/test/files/run/arrays.scala b/test/files/run/arrays.scala index 927112d851..a58caded00 100644 --- a/test/files/run/arrays.scala +++ b/test/files/run/arrays.scala @@ -10,10 +10,10 @@ object Test { //########################################################################## // Types - type Strings = List[String]; - type Map = scala.collection.Map[Int, Any]; - type HashMap = scala.collection.mutable.HashMap[Int, Any]; - type TreeMap = scala.collection.immutable.TreeMap[Int, Any]; + type Strings = List[String] + type Map = scala.collection.Map[Int, Any] + type HashMap = scala.collection.mutable.HashMap[Int, Any] + type TreeMap = scala.collection.immutable.TreeMap[Int, Any] //########################################################################## // Identity Functions @@ -142,21 +142,21 @@ object Test { } def check_Tm[T <: Map ](xs: Array[T], l: Int, x0: T, c: Check[T]) { - check(xs.length == l, xs.length, l); - check(xs(0) == x0, xs(0), x0); - check_Ta(xs, l, x0, c); - check_Tr(xs, l, x0, c); - check_To(xs, l, x0, c); - c(xs); + check(xs.length == l, xs.length, l) + check(xs(0) == x0, xs(0), x0) + check_Ta(xs, l, x0, c) + check_Tr(xs, l, x0, c) + check_To(xs, l, x0, c) + c(xs) } def check_Tn[T <: Strings](xs: Array[T], l: Int, x0: T, c: Check[T]) { - check(xs.length == l, xs.length, l); - check(xs(0) == x0, xs(0), x0); - check_Ta(xs, l, x0, c); - check_Tr(xs, l, x0, c); - check_To(xs, l, x0, c); - c(xs); + check(xs.length == l, xs.length, l) + check(xs(0) == x0, xs(0), x0) + check_Ta(xs, l, x0, c) + check_Tr(xs, l, x0, c) + check_To(xs, l, x0, c) + c(xs) } //########################################################################## @@ -328,21 +328,35 @@ object Test { check(xs(2) == m2, xs(2), m2); } - def ncheck(xs: Array[Strings]): Unit = { - check(xs.length == 3, xs.length, 3); - check(xs(0) == n0, xs(0), n0); - check(xs(1) == n1, xs(1), n1); - check(xs(2) == n2, xs(2), n2); + def ncheck(xs: Array[Strings]) { + check(xs.length == 3, xs.length, 3) + check(xs(0) == n0, xs(0), n0) + check(xs(1) == n1, xs(1), n1) + check(xs(2) == n2, xs(2), n2) } //########################################################################## // Miscellaneous checks - def checkZip(): Unit = { + + def checkZip { val zipped = Array("a", "b", "c").zip(Array(1, 2)) val expected = Array(("a",1), ("b",2)) check(zipped sameElements expected, zipped.toList, expected.toList) } + def checkConcat { // ticket #713 + val x1 = Array.concat(Array(1, 2), Array(3, 4)) + val y1 = Array(1, 2, 3, 4) + check(x1 sameElements y1, x1.toList, y1.toList) + + val x2 = Array.concat(List(1, 2), List(3, 4)) + check(x2 sameElements y1, x2.toList, y1.toList) + + val x3 = Array.concat(<a>aaa</a>, <x>xxx</x>) + val y3 = <a>aaa</a><x>xxx</x>; + check(x3 sameElements y3, x3.toList, y3.toList) + } + //########################################################################## // Arrays @@ -911,7 +925,8 @@ object Test { //###################################################################### - checkZip() + checkZip + checkConcat //###################################################################### |