diff options
author | Martin Odersky <odersky@gmail.com> | 2006-03-09 12:12:37 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2006-03-09 12:12:37 +0000 |
commit | 083f4dd55a5377e740e7581ff2dcdfafe3ae9f76 (patch) | |
tree | c884b7102026544b305676b5c08ba91d45ad5b56 /src | |
parent | ca9017c139803bfd56b62b5e08e8832dcce88745 (diff) | |
download | scala-083f4dd55a5377e740e7581ff2dcdfafe3ae9f76.tar.gz scala-083f4dd55a5377e740e7581ff2dcdfafe3ae9f76.tar.bz2 scala-083f4dd55a5377e740e7581ff2dcdfafe3ae9f76.zip |
fix arrays some more.
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/Erasure.scala | 5 | ||||
-rw-r--r-- | src/library/scala/Array.scala | 26 | ||||
-rw-r--r-- | src/library/scala/Boolean.java | 4 | ||||
-rw-r--r-- | src/library/scala/List.scala | 46 | ||||
-rw-r--r-- | src/library/scala/Predef.scala | 222 | ||||
-rw-r--r-- | src/library/scala/Seq.scala | 33 | ||||
-rw-r--r-- | src/library/scala/collection/immutable/Queue.scala | 19 | ||||
-rw-r--r-- | src/library/scala/collection/mutable/Stack.scala | 8 | ||||
-rw-r--r-- | src/library/scala/dbc/syntax/Statement.scala | 26 | ||||
-rw-r--r-- | src/library/scala/dbc/value/Conversion.scala | 20 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedAnyArray.scala | 24 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedArray.scala | 65 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedIntArray.scala | 24 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedObjectArray.scala | 33 | ||||
-rw-r--r-- | src/library/scala/runtime/ScalaRunTime.scala | 13 |
15 files changed, 345 insertions, 223 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/Erasure.scala b/src/compiler/scala/tools/nsc/transform/Erasure.scala index 7bd622cdcf..d49e76693b 100644 --- a/src/compiler/scala/tools/nsc/transform/Erasure.scala +++ b/src/compiler/scala/tools/nsc/transform/Erasure.scala @@ -198,6 +198,7 @@ abstract class Erasure extends AddInterfaces with typechecker.Analyzer { runtimeCall(newTermName(elemClass.name.toString() + "Tag"), List()) else Literal(signature(pt.typeArgs.head)); + //System.out.println("unboxing " + tree + ":" + tree.tpe + " to " + pt);//DEBUG runtimeCall(nme.arrayValue, List(tree1, elemTag)) } else { assert(isNumericValueClass(pt.symbol)); @@ -220,7 +221,7 @@ abstract class Erasure extends AddInterfaces with typechecker.Analyzer { Select(x(), Object_isInstanceOf), List(TypeTree(BoxedArrayClass.tpe))), List()), - unbox(x(), pt), + unbox(gen.cast(x(), BoxedArrayClass.tpe), pt), x()), pt)) } @@ -246,7 +247,7 @@ abstract class Erasure extends AddInterfaces with typechecker.Analyzer { /** Is symbol a member of unboxed arrays (which will be expanded directly later)? */ private def isUnboxedArrayMember(sym: Symbol) = ( sym.name == nme.apply || sym.name == nme.length || sym.name == nme.update || - sym.owner == ObjectClass && sym.name != nme.toString + sym.owner == ObjectClass ); /** Is symbol a member of a boxed value class (which will not be expanded later)? */ diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala index de9b49c47f..21176ce965 100644 --- a/src/library/scala/Array.scala +++ b/src/library/scala/Array.scala @@ -26,12 +26,30 @@ object Array { System.arraycopy(src, srcPos, dest, destPos, length) } } + + def concat[T](xs: Array[T]*) = { + var len = 0 + for (val x <- xs) { + len = len + x.length + } + val result = new Array[T](len) + var start = 0 + for (val x <- xs) { + copy(x, 0, result, start, x.length) + start = start + x.length + } + result + } } [cloneable,serializable] -final class Array[A](_length: Int) extends Seq[A] { +final class Array[a](_length: Int) extends Seq[a] { def length: Int = throw new Error(); - def apply(i: Int): A = throw new Error(); - def update(i: Int, x: A): Unit = throw new Error(); - def elements: Iterator[A] = throw new Error(); + def apply(i: Int): a = throw new Error(); + def update(i: Int, x: a): Unit = throw new Error(); + def elements: Iterator[a] = throw new Error(); + def subArray(from: Int, end: Int): Array[a] = throw new Error(); + def filter(p: a => Boolean): Array[a] = throw new Error(); + def map[b](f: a => b): Array[b] = throw new Error(); + def flatMap[b](f: a => Array[b]): Array[b] = throw new Error(); } diff --git a/src/library/scala/Boolean.java b/src/library/scala/Boolean.java index a02d62ab49..2a554ea472 100644 --- a/src/library/scala/Boolean.java +++ b/src/library/scala/Boolean.java @@ -40,10 +40,6 @@ public abstract class Boolean extends AnyVal implements java.io.Serializable { /** @meta method []scala.Boolean; */ public boolean $bang ( ) { return !value ; } - - - public String $plus (String that) { return value + that; } - public boolean $eq$eq (boolean that) { return value == that; } public boolean $bang$eq (boolean that) { return value != that; } public boolean $bar$bar (boolean that) { return value || that; } diff --git a/src/library/scala/List.scala b/src/library/scala/List.scala index 49aedf7c74..d604490821 100644 --- a/src/library/scala/List.scala +++ b/src/library/scala/List.scala @@ -109,32 +109,36 @@ object List { } /** Concatenate all the elements of a given list of lists. - * @param l the list of lists that are to be concatenated + * @param xss the list of lists that are to be concatenated * @return the concatenation of all the lists */ - def flatten[a](l: List[List[a]]): List[a] = { + def flatten[a](xss: List[List[a]]): List[a] = concat(xss: _*) + + /** Concatenate all the argument lists into a single list. + * @param xss the lists that are to be concatenated + * @return the concatenation of all the lists + */ + def concat[a](xss: List[a]*): List[a] = { val b = new ListBuffer[a] - var xsc = l - while (!xsc.isEmpty) { - var xc = xsc.head + for (val xs <- xss) { + var xc = xs while (!xc.isEmpty) { b += xc.head xc = xc.tail } - xsc = xsc.tail } b.toList } /** Transforms a list of pair into a pair of lists. * - * @param l the list of pairs to unzip + * @param xs the list of pairs to unzip * @return a pair of lists: the first list in the pair contains the list */ - def unzip[a,b](l: List[Pair[a,b]]): Pair[List[a], List[b]] = { + def unzip[a,b](xs: List[Pair[a,b]]): Pair[List[a], List[b]] = { val b1 = new ListBuffer[a] val b2 = new ListBuffer[b] - var xc = l + var xc = xs while (!xc.isEmpty) { b1 += xc.head._1 b2 += xc.head._2 @@ -946,28 +950,6 @@ sealed abstract class List[+a] extends Seq[a] { def reverse: List[a] = foldLeft(Nil : List[a])((xs, x) => x :: xs); - /** Returns a string representation of this list. The resulting string - * begins with the string <code>start</code> and is finished by the string - * <code>end</code>. Inside, the string representations of elements (w.r.t. - * the method <code>toString()</code>) are separated by the string - * <code>sep</code>. - * <p/> - * Ex: <br/> - * <code>List(1, 2, 3).mkString("(", "; ", ")") = "(1; 2; 3)"</code> - * - * @param start starting string. - * @param sep separator string. - * @param end ending string. - * @return a string representation of this list. - */ - def mkString(start: String, sep: String, end: String): String = this match { - case Nil => start + end - case last :: Nil => start + last + end - case fst :: tail => start + fst + sep + tail.mkString("", sep, end) - } - - override def toString() = mkString("List(", ",", ")"); - /** Returns a list formed from this list and the specified list * <code>that</code> by associating each element of the former with * the element at the same position in the latter. @@ -1077,6 +1059,8 @@ sealed abstract class List[+a] extends Seq[a] { } b.toList } + + override protected def stringPrefix: String = "List" } /** The empty list. diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala index 27c1c3b06a..37acc7a764 100644 --- a/src/library/scala/Predef.scala +++ b/src/library/scala/Predef.scala @@ -34,7 +34,7 @@ object Predef { type NullPointerException = java.lang.NullPointerException; type Throwable = java.lang.Throwable; - type Pair[+p, +q] = Tuple2[p, q]; + type Pair[+a, +b] = Tuple2[a, b]; def Pair[a, b](x: a, y: b) = Tuple2(x, y); type Triple[+a, +b, +c] = Tuple3[a, b, c]; @@ -49,10 +49,12 @@ object Predef { def Tuple[a1, a2, a3, a4, a5, a6, a7, a8](x1: a1, x2: a2, x3: a3, x4: a4, x5: a5, x6: a6, x7: a7, x8: a8) = Tuple8(x1, x2, x3, x4, x5, x6, x7, x8); def Tuple[a1, a2, a3, a4, a5, a6, a7, a8, a9](x1: a1, x2: a2, x3: a3, x4: a4, x5: a5, x6: a6, x7: a7, x8: a8, x9: a9) = Tuple9(x1, x2, x3, x4, x5, x6, x7, x8, x9); +/* def id[a](x: a): a = x; def fst[a](x: a, y: Any): a = x; def scd[a](x: Any, y: a): a = y; - +*/ +//todo: remove from here! val namespace$default = ""; val $scope = scala.xml.TopScope; @@ -65,20 +67,31 @@ object Predef { * @param xs the elements to put in the array * @return the array containing elements xs. */ -/* - def Array[A](xs: A*): Array[A] = { + def Array[A <: AnyRef](xs: A*): Array[A] = { val array = new Array[A](xs.length); var i = 0; for (val x <- xs.elements) { array(i) = x; i = i + 1; } array; } -*/ - def Array[A <: AnyRef](xs: A*): Array[A] = { + + val Array = scala.Array; + +/* The following metod clashes with the previous one, and has therefore been + * removed. Note that this is a choice between efficiency and generality. + * The previous factory method is more efficient than the one that has been + * commented out. Since it is anyway possible to create a polymorphic array + * using + * new Array[T] + * it was preferred to restrict the definition of the factory method. + + def Array[A](xs: A*): Array[A] = { val array = new Array[A](xs.length); var i = 0; for (val x <- xs.elements) { array(i) = x; i = i + 1; } array; } +*/ + def Array(xs: boolean*): Array[boolean] = { val array = new Array[boolean](xs.length); var i = 0; @@ -179,7 +192,6 @@ object Predef { case _ => -(y compareTo x) } } - def view(x: int): Ordered[int] = int2ordered(x); implicit def char2ordered(x: char): Ordered[char] = new Ordered[char] with Proxy { def self: Any = x; @@ -191,7 +203,6 @@ object Predef { case _ => -(y compareTo x) } } - def view(x: char): Ordered[char] = char2ordered(x); implicit def long2ordered(x: long): Ordered[long] = new Ordered[long] with Proxy { def self: Any = x; @@ -203,7 +214,6 @@ object Predef { case _ => -(y compareTo x) } } - def view(x: long): Ordered[long] = long2ordered(x); implicit def float2ordered(x: float): Ordered[float] = new Ordered[float] with Proxy { def self: Any = x; @@ -215,7 +225,6 @@ object Predef { case _ => -(y compareTo x) } } - def view(x: float): Ordered[float] = float2ordered(x); implicit def double2ordered(x: double): Ordered[double] = new Ordered[double] with Proxy { def self: Any = x; @@ -227,7 +236,6 @@ object Predef { case _ => -(y compareTo x) } } - def view(x: double): Ordered[double] = double2ordered(x); implicit def boolean2ordered(x: boolean): Ordered[boolean] = new Ordered[boolean] with Proxy { def self: Any = x; @@ -239,80 +247,118 @@ object Predef { case _ => -(y compareTo x) } } - def view(x: boolean): Ordered[boolean] = boolean2ordered(x); - - implicit def array2ordered[A <% Ordered[A]](xs: Array[A]): Ordered[Array[A]] = new Ordered[Array[A]] with Proxy { - def self: Any = xs; - def compareTo[B >: Array[A] <% Ordered[B]](that: B): Int = that match { - case ys: Array[A] => - var i, res = 0; - while ((i < xs.length) && (i < ys.length) && (res == 0)) { - res = xs(i) compareTo ys(i); - i = i + 1; - } - if (res != 0) res - else if (i < xs.length) 1 - else if (i < ys.length) -1 - else 0 - case _ => - -(that compareTo xs) + + implicit def seq2ordered[a <% Ordered[a]](xs: Array[a]): Ordered[Seq[a]] = + new Ordered[Seq[a]] with Proxy { + val self = xs + def compareTo[b >: Seq[a] <% Ordered[b]](that: b): Int = that match { + case that: Seq[a] => + var res = 0 + val these = xs.elements + val those = that.elements + while (res == 0 && these.hasNext) + res = if (those.hasNext) these.next compareTo those.next else 1 + res + case _ => + -(that compareTo xs) + } } - } - def view[A <% Ordered[A]](xs: Array[A]): Ordered[Array[A]] = array2ordered(xs); - private def first(xs: Int*): Int = xs.elements.find(x => x != 0) match { - case Some(r) => r - case _ => 0 - } + implicit def tuple22ordered[a1 <% Ordered[a1], a2 <% Ordered[a2]](x: Tuple2[a1, a2]): Ordered[Tuple2[a1, a2]] = + new Ordered[Tuple2[a1, a2]] with Proxy { + val self = x + def compareTo[T >: Tuple2[a1, a2] <% Ordered[T]](y: T): Int = y match { + case y: Tuple2[a1, a2] => + val res = x._1 compareTo y._1 + if (res == 0) x._2 compareTo y._2 + else res + case _ => -(y compareTo x) + } + } - /* We can't bootstrap currently with the following views included. We have to - * wait for the next release... - * - implicit def view[A <% Ordered[A], B <% Ordered[B]](x: Tuple2[A, B]): Ordered[Tuple2[A, B]] = - new Ordered[Tuple2[A, B]] with Proxy(x) { - def compareTo[T >: Tuple2[A, B] <% Ordered[T]](y: T): Int = y match { - case y1: Tuple2[A, B] => first(x._1.compareTo(y1._1), - x._2.compareTo(y1._2)); - case _ => -(y compareTo x) - } - } - - implicit def view[A <% Ordered[A], B <% Ordered[B], C <% Ordered[C]] - (x: Tuple3[A, B, C]): Ordered[Tuple3[A, B, C]] = - new Ordered[Tuple3[A, B, C]] with Proxy(x) { - def compareTo[T >: Tuple3[A, B, C] <% Ordered[T]](y: T): Int = y match { - case y1: Tuple3[A, B, C] => first(x._1.compareTo(y1._1), - x._2.compareTo(y1._2), - x._3.compareTo(y1._3)); - case _ => -(y compareTo x) - } - } - - implicit def view[A <% Ordered[A], B <% Ordered[B], C <% Ordered[C], D <% Ordered[D]] - (x: Tuple4[A, B, C, D]): Ordered[Tuple4[A, B, C, D]] = - new Ordered[Tuple4[A, B, C, D]] with Proxy(x) { - def compareTo[T >: Tuple4[A, B, C, D] <% Ordered[T]](y: T): Int = y match { - case y1: Tuple4[A, B, C, D] => first(x._1.compareTo(y1._1), - x._2.compareTo(y1._2), - x._3.compareTo(y1._3), - x._4.compareTo(y1._4)); - case _ => -(y compareTo x) - } - } - - implicit def view[A <% Ordered[A], B <% Ordered[B], C <% Ordered[C], D <% Ordered[D], E <% Ordered[E]] - (x: Tuple5[A, B, C, D, E]): Ordered[Tuple5[A, B, C, D, E]] = - new Ordered[Tuple5[A, B, C, D, E]] with Proxy(x) { - def compareTo[T >: Tuple5[A, B, C, D, E] <% Ordered[T]](y: T): Int = y match { - case y1: Tuple5[A, B, C, D, E] => first(x._1.compareTo(y1._1), - x._2.compareTo(y1._2), - x._3.compareTo(y1._3), - x._4.compareTo(y1._4), - x._5.compareTo(y1._5)); - case _ => -(y compareTo x) - } - } - */ + implicit def tuple32ordered[a1 <% Ordered[a1], a2 <% Ordered[a2], a3 <% Ordered[a3]](x: Tuple3[a1, a2, a3]): Ordered[Tuple3[a1, a2, a3]] = + new Ordered[Tuple3[a1, a2, a3]] with Proxy { + val self = x + def compareTo[T >: Tuple3[a1, a2, a3] <% Ordered[T]](y: T): Int = y match { + case y: Tuple3[a1, a2, a3] => + val res = x._1 compareTo y._1; + if (res == 0) Tuple2(x._2, x._3) compareTo Tuple2(y._2, y._3) + else res + case _ => -(y compareTo x) + } + } + + implicit def tuple42ordered[a1 <% Ordered[a1], a2 <% Ordered[a2], a3 <% Ordered[a3], a4 <% Ordered[a4]](x: Tuple4[a1, a2, a3, a4]): Ordered[Tuple4[a1, a2, a3, a4]] = + new Ordered[Tuple4[a1, a2, a3, a4]] with Proxy { + val self = x + def compareTo[T >: Tuple4[a1, a2, a3, a4] <% Ordered[T]](y: T): Int = y match { + case y: Tuple4[a1, a2, a3, a4] => + val res = x._1 compareTo y._1; + if (res == 0) Tuple3(x._2, x._3, x._4) compareTo Tuple3(y._2, y._3, y._4) + else res + case _ => -(y compareTo x) + } + } + + implicit def tuple52ordered[a1 <% Ordered[a1], a2 <% Ordered[a2], a3 <% Ordered[a3], a4 <% Ordered[a4], a5 <% Ordered[a5]](x: Tuple5[a1, a2, a3, a4, a5]): Ordered[Tuple5[a1, a2, a3, a4, a5]] = + new Ordered[Tuple5[a1, a2, a3, a4, a5]] with Proxy { + val self = x + def compareTo[T >: Tuple5[a1, a2, a3, a4, a5] <% Ordered[T]](y: T): Int = y match { + case y: Tuple5[a1, a2, a3, a4, a5] => + val res = x._1 compareTo y._1; + if (res == 0) Tuple4(x._2, x._3, x._4, x._5) compareTo Tuple4(y._2, y._3, y._4, y._5) + else res + case _ => -(y compareTo x) + } + } + + implicit def tuple62ordered[a1 <% Ordered[a1], a2 <% Ordered[a2], a3 <% Ordered[a3], a4 <% Ordered[a4], a5 <% Ordered[a5], a6 <% Ordered[a6]](x: Tuple6[a1, a2, a3, a4, a5, a6]): Ordered[Tuple6[a1, a2, a3, a4, a5, a6]] = + new Ordered[Tuple6[a1, a2, a3, a4, a5, a6]] with Proxy { + val self = x + def compareTo[T >: Tuple6[a1, a2, a3, a4, a5, a6] <% Ordered[T]](y: T): Int = y match { + case y: Tuple6[a1, a2, a3, a4, a5, a6] => + val res = x._1 compareTo y._1; + if (res == 0) Tuple5(x._2, x._3, x._4, x._5, x._6) compareTo Tuple5(y._2, y._3, y._4, y._5, y._6) + else res + case _ => -(y compareTo x) + } + } + + implicit def tuple72ordered[a1 <% Ordered[a1], a2 <% Ordered[a2], a3 <% Ordered[a3], a4 <% Ordered[a4], a5 <% Ordered[a5], a6 <% Ordered[a6], a7 <% Ordered[a7]](x: Tuple7[a1, a2, a3, a4, a5, a6, a7]): Ordered[Tuple7[a1, a2, a3, a4, a5, a6, a7]] = + new Ordered[Tuple7[a1, a2, a3, a4, a5, a6, a7]] with Proxy { + val self = x + def compareTo[T >: Tuple7[a1, a2, a3, a4, a5, a6, a7] <% Ordered[T]](y: T): Int = y match { + case y: Tuple7[a1, a2, a3, a4, a5, a6, a7] => + val res = x._1 compareTo y._1; + if (res == 0) Tuple6(x._2, x._3, x._4, x._5, x._6, x._7) compareTo Tuple6(y._2, y._3, y._4, y._5, y._6, y._7) + else res + case _ => -(y compareTo x) + } + } + + implicit def tuple82ordered[a1 <% Ordered[a1], a2 <% Ordered[a2], a3 <% Ordered[a3], a4 <% Ordered[a4], a5 <% Ordered[a5], a6 <% Ordered[a6], a7 <% Ordered[a7], a8 <% Ordered[a8]](x: Tuple8[a1, a2, a3, a4, a5, a6, a7, a8]): Ordered[Tuple8[a1, a2, a3, a4, a5, a6, a7, a8]] = + new Ordered[Tuple8[a1, a2, a3, a4, a5, a6, a7, a8]] with Proxy { + val self = x + def compareTo[T >: Tuple8[a1, a2, a3, a4, a5, a6, a7, a8] <% Ordered[T]](y: T): Int = y match { + case y: Tuple8[a1, a2, a3, a4, a5, a6, a7, a8] => + val res = x._1 compareTo y._1; + if (res == 0) Tuple7(x._2, x._3, x._4, x._5, x._6, x._7, x._8) compareTo Tuple7(y._2, y._3, y._4, y._5, y._6, y._7, y._8) + else res + case _ => -(y compareTo x) + } + } + + implicit def tuple92ordered[a1 <% Ordered[a1], a2 <% Ordered[a2], a3 <% Ordered[a3], a4 <% Ordered[a4], a5 <% Ordered[a5], a6 <% Ordered[a6], a7 <% Ordered[a7], a8 <% Ordered[a8], a9 <% Ordered[a9]](x: Tuple9[a1, a2, a3, a4, a5, a6, a7, a8, a9]): Ordered[Tuple9[a1, a2, a3, a4, a5, a6, a7, a8, a9]] = + new Ordered[Tuple9[a1, a2, a3, a4, a5, a6, a7, a8, a9]] with Proxy { + val self = x + def compareTo[T >: Tuple9[a1, a2, a3, a4, a5, a6, a7, a8, a9] <% Ordered[T]](y: T): Int = y match { + case y: Tuple9[a1, a2, a3, a4, a5, a6, a7, a8, a9] => + val res = x._1 compareTo y._1; + if (res == 0) Tuple8(x._2, x._3, x._4, x._5, x._6, x._7, x._8, x._9) compareTo Tuple8(y._2, y._3, y._4, y._5, y._6, y._7, y._8, y._9) + else res + case _ => -(y compareTo x) + } + } implicit def string2ordered(x: String): Ordered[String] = new Ordered[String] with Proxy { def self: Any = x; @@ -321,17 +367,6 @@ object Predef { case _ => -(y compareTo x) } } - def view(x: String): Ordered[String] = string2ordered(x); - - implicit def array2seq[A](xs: Array[A]): Seq[A] = new Seq[A] { - def length = xs.length; - def elements = Iterator.fromArray(xs); - def apply(n: Int) = xs(n); - override def hashCode(): Int = xs.hashCode(); - override def equals(y: Any): Boolean = (xs == y); - override protected def stringPrefix: String = "Array"; - } - def view[A](xs: Array[A]): Seq[A] = array2seq(xs); implicit def string2seq(str: String): Seq[Char] = new Seq[Char] { def length = str.length(); @@ -341,7 +376,6 @@ object Predef { override def equals(y: Any): Boolean = (str == y); override protected def stringPrefix: String = "String"; } - def view(x: String): Seq[Char] = string2seq(x); implicit def byte2short(x: byte): short = x.toShort; implicit def byte2int(x: byte): int = x.toInt; diff --git a/src/library/scala/Seq.scala b/src/library/scala/Seq.scala index fb262586e2..6ae7fa2a8b 100644 --- a/src/library/scala/Seq.scala +++ b/src/library/scala/Seq.scala @@ -176,15 +176,32 @@ mixin class Seq[+A] extends AnyRef with PartialFunction[Int, A] with Iterable[A] * * @return a string representation of this sequence. */ - override def toString() = { - val iter = elements; - var res = stringPrefix + "("; - if (iter.hasNext) { - res = res + iter.next; - while (iter.hasNext) - res = res + ", " + iter.next; + override def toString() = mkString(stringPrefix+"(", ",", ")") + + /** Returns a string representation of this sequence. The resulting string + * begins with the string <code>start</code> and is finished by the string + * <code>end</code>. Inside, the string representations of elements (w.r.t. + * the method <code>toString()</code>) are separated by the string + * <code>sep</code>. + * <p/> + * Ex: <br/> + * <code>List(1, 2, 3).mkString("(", "; ", ")") = "(1; 2; 3)"</code> + * + * @param start starting string. + * @param sep separator string. + * @param end ending string. + * @return a string representation of this sequence. + */ + def mkString(start: String, sep: String, end: String): String = { + val buf = new StringBuffer() + buf.append(start) + val elems = elements + if (elems.hasNext) buf.append(elems.next) + while (elems.hasNext) { + buf.append(sep); buf.append(elems.next) } - res + ")" + buf.append(end) + buf.toString } /** Defines the prefix of the string representation. diff --git a/src/library/scala/collection/immutable/Queue.scala b/src/library/scala/collection/immutable/Queue.scala index 88b725a656..238a192c87 100644 --- a/src/library/scala/collection/immutable/Queue.scala +++ b/src/library/scala/collection/immutable/Queue.scala @@ -109,26 +109,9 @@ class Queue[+A](elem: A*) extends Seq[A] { } else out.head; - /** Returns a string representation of this queue. The resulting string - * begins with the string <code>start</code> and is finished by the string - * <code>end</code>. Inside, the string representations of elements (w.r.t. - * the method <code>toString()</code>) are separated by the string - * <code>sep</code>. - * <p/> - * Ex: <br/> - * <code>Queue(1, 2, 3).mkString("(", "; ", ")") = "(1; 2; 3)"</code> - * - * @param start starting string. - * @param sep separator string. - * @param end ending string. - * @return a string representation of this list. - */ - def mkString(start: String, sep: String, end: String): String = - (out ::: in.reverse).mkString(start, sep, end); - /** Returns a string representation of this queue. */ - override def toString() = (out ::: in.reverse).mkString("Queue(", ",", ")"); + override def toString() = mkString("Queue(", ",", ")"); /** Compares two queues for equality by comparing * each element in the queues. diff --git a/src/library/scala/collection/mutable/Stack.scala b/src/library/scala/collection/mutable/Stack.scala index ed5d551e46..ad0efa7111 100644 --- a/src/library/scala/collection/mutable/Stack.scala +++ b/src/library/scala/collection/mutable/Stack.scala @@ -116,12 +116,6 @@ class Stack[A] extends MutableList[A] { */ override def hashCode(): Int = error("unsuitable as hash key"); - /** Returns a textual representation of a stack as a string. - * - * @return the string representation of this stack. - */ - override def toString(): String = toList.mkString("Stack(", ", ", ")"); - /** This method clones the stack. * * @return a stack with the same elements. @@ -131,4 +125,6 @@ class Stack[A] extends MutableList[A] { res ++= this; res } + + override protected def stringPrefix: String = "Stack" } diff --git a/src/library/scala/dbc/syntax/Statement.scala b/src/library/scala/dbc/syntax/Statement.scala index 82ae56c31d..5973bbc529 100644 --- a/src/library/scala/dbc/syntax/Statement.scala +++ b/src/library/scala/dbc/syntax/Statement.scala @@ -70,11 +70,11 @@ object Statement { } } - implicit def view (fv:StatementField): SelectDerivedField = new SelectDerivedField { + implicit def view1 (fv:StatementField): SelectDerivedField = new SelectDerivedField { val fieldValue = fv; } - implicit def view (fv:String): SelectDerivedField = new SelectDerivedField { + implicit def view2 (fv:String): SelectDerivedField = new SelectDerivedField { val fieldValue: StatementField = StatementExpression.view(fv); } @@ -91,7 +91,7 @@ object Statement { } } - implicit def view (sdf:SelectDerivedField): SelectDerivedColumns = new SelectDerivedColumns { + implicit def view3 (sdf:SelectDerivedField): SelectDerivedColumns = new SelectDerivedColumns { val selectList = List(new statement.DerivedColumn { val valueExpression = sdf.fieldValue.toStatement; val asClause = sdf.fieldRename; @@ -99,9 +99,9 @@ object Statement { val selectTypes = if (sdf.fieldType.isEmpty) Nil else List(sdf.fieldType.get); } - implicit def view (sdfs:String): SelectDerivedColumns = { + implicit def view4 (sdfs:String): SelectDerivedColumns = { val sdf: SelectDerivedField = sdfs; - view(sdf); + view3(sdf); } // SELECT OF ... @@ -161,7 +161,7 @@ object Statement { } } - implicit def view (sct:String): SelectSourceTable = new SelectSourceTable { + implicit def view5 (sct:String): SelectSourceTable = new SelectSourceTable { val fromRelation = new statement.Table { val tableName = sct; val tableRename = None; @@ -169,7 +169,7 @@ object Statement { } } - implicit def view (sct:statement.Select): SelectSourceTable = new SelectSourceTable { + implicit def view6 (sct:statement.Select): SelectSourceTable = new SelectSourceTable { val fromRelation = sct; } @@ -180,7 +180,7 @@ object Statement { } } - implicit def view (sct:String): SelectSourceTables = new SelectSourceTables { + implicit def view7 (sct:String): SelectSourceTables = new SelectSourceTables { val fromClause = List(new statement.Table { val tableName = sct; val tableRename = None; @@ -188,11 +188,11 @@ object Statement { }); } - implicit def view (sct:statement.Select): SelectSourceTables = new SelectSourceTables { + implicit def view8 (sct:statement.Select): SelectSourceTables = new SelectSourceTables { val fromClause = List(sct); } - implicit def view (sct:SelectSourceTable): SelectSourceTables = new SelectSourceTables { + implicit def view9 (sct:SelectSourceTable): SelectSourceTables = new SelectSourceTables { val fromClause = List(sct.fromRelation); } @@ -235,7 +235,7 @@ object Statement { } } - implicit def view (sb:SelectBeyond): statement.Select = new statement.Select { + implicit def view10 (sb:SelectBeyond): statement.Select = new statement.Select { val setQuantifier = sb.setQuantifier; val selectList = sb.selectList; val fromClause = sb.fromClause; @@ -260,11 +260,11 @@ object Statement { } } - implicit def view (se:StatementExpression): SelectGroupBy = new SelectGroupBy { + implicit def view11 (se:StatementExpression): SelectGroupBy = new SelectGroupBy { val groupByClause = List(se.toStatement); } - implicit def view (se:String): SelectGroupBy = new SelectGroupBy { + implicit def view12 (se:String): SelectGroupBy = new SelectGroupBy { val groupByClause = List(new statement.expression.Field { val tableName = None; val fieldName = se; diff --git a/src/library/scala/dbc/value/Conversion.scala b/src/library/scala/dbc/value/Conversion.scala index 870cb3ddd1..093cf55902 100644 --- a/src/library/scala/dbc/value/Conversion.scala +++ b/src/library/scala/dbc/value/Conversion.scala @@ -18,7 +18,7 @@ object Conversion { class Illegal (msg:String) extends Exception(msg); - implicit def view (value:Value): Byte = { + implicit def view1 (value:Value): Byte = { if (value.dataType.nativeTypeId == DataType.BYTE) { val v = value.asInstanceOf[dbc.value.ExactNumeric[Byte]]; v.nativeValue @@ -27,7 +27,7 @@ object Conversion { } } - implicit def view (value:Value): Short = { + implicit def view2 (value:Value): Short = { if (value.dataType.nativeTypeId == DataType.BYTE) { val v = value.asInstanceOf[dbc.value.ExactNumeric[Byte]]; v.nativeValue.coerce @@ -39,7 +39,7 @@ object Conversion { } } - implicit def view (value:Value): Int = { + implicit def view3 (value:Value): Int = { if (value.dataType.nativeTypeId == DataType.BYTE) { val v = value.asInstanceOf[dbc.value.ExactNumeric[Byte]]; v.nativeValue.coerce @@ -54,7 +54,7 @@ object Conversion { } } - implicit def view (value:Value): Long = { + implicit def view4 (value:Value): Long = { if (value.dataType.nativeTypeId == DataType.BYTE) { val v = value.asInstanceOf[dbc.value.ExactNumeric[Byte]]; v.nativeValue.coerce @@ -72,7 +72,7 @@ object Conversion { } } - implicit def view (value:Value): BigInteger = { + implicit def view5 (value:Value): BigInteger = { if (value.dataType.nativeTypeId == DataType.BYTE) { val v = value.asInstanceOf[dbc.value.ExactNumeric[Byte]]; new BigInteger(v.nativeValue.toString(),10) @@ -93,7 +93,7 @@ object Conversion { } } - implicit def view (value:Value): BigDecimal = { + implicit def view6 (value:Value): BigDecimal = { if (value.dataType.nativeTypeId == DataType.BYTE) { val v = value.asInstanceOf[dbc.value.ExactNumeric[Byte]]; new BigDecimal(v.nativeValue.toString()) @@ -117,7 +117,7 @@ object Conversion { } } - implicit def view (value:Value): Float = { + implicit def view7 (value:Value): Float = { if (value.dataType.nativeTypeId == DataType.FLOAT) { val v = value.asInstanceOf[dbc.value.ApproximateNumeric[Float]]; v.nativeValue @@ -126,7 +126,7 @@ object Conversion { } } - implicit def view (value:Value): Double = { + implicit def view8 (value:Value): Double = { if (value.dataType.nativeTypeId == DataType.FLOAT) { val v = value.asInstanceOf[dbc.value.ApproximateNumeric[Float]]; v.nativeValue.coerce @@ -138,7 +138,7 @@ object Conversion { } } - implicit def view (value:Value): scala.Boolean = { + implicit def view9 (value:Value): scala.Boolean = { if (value.dataType.nativeTypeId == DataType.BOOLEAN) { val v = value.asInstanceOf[dbc.value.Boolean]; v.nativeValue @@ -147,7 +147,7 @@ object Conversion { } } - implicit def view (value:Value): String = value match { + implicit def view10 (value:Value): String = value match { case v:dbc.value.Character => v.nativeValue; case v:dbc.value.CharacterLargeObject => v.nativeValue; case v:dbc.value.CharacterVarying => v.nativeValue; diff --git a/src/library/scala/runtime/BoxedAnyArray.scala b/src/library/scala/runtime/BoxedAnyArray.scala index d7589d6d9b..1945016977 100644 --- a/src/library/scala/runtime/BoxedAnyArray.scala +++ b/src/library/scala/runtime/BoxedAnyArray.scala @@ -76,7 +76,8 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { val newvalue = new Array[Int](length); var i = 0; while (i < length) { - newvalue(i) = boxed(i).asInstanceOf[BoxedNumber].intValue(); + val x = boxed(i).asInstanceOf[BoxedNumber]; + if (x ne null) newvalue(i) = x.intValue(); i = i + 1 } unboxed = newvalue; @@ -84,7 +85,8 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { val newvalue = new Array[Double](length); var i = 0; while (i < length) { - newvalue(i) = boxed(i).asInstanceOf[BoxedNumber].doubleValue(); + val x = boxed(i).asInstanceOf[BoxedNumber]; + if (x ne null) newvalue(i) = x.doubleValue(); i = i + 1 } unboxed = newvalue; @@ -92,7 +94,8 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { val newvalue = new Array[Float](length); var i = 0; while (i < length) { - newvalue(i) = boxed(i).asInstanceOf[BoxedNumber].floatValue(); + val x = boxed(i).asInstanceOf[BoxedNumber]; + if (x ne null) newvalue(i) = x.floatValue(); i = i + 1 } unboxed = newvalue; @@ -100,7 +103,8 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { val newvalue = new Array[Long](length); var i = 0; while (i < length) { - newvalue(i) = boxed(i).asInstanceOf[BoxedNumber].longValue(); + val x = boxed(i).asInstanceOf[BoxedNumber]; + if (x ne null) newvalue(i) = x.longValue(); i = i + 1 } unboxed = newvalue; @@ -108,7 +112,8 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { val newvalue = new Array[Char](length); var i = 0; while (i < length) { - newvalue(i) = boxed(i).asInstanceOf[BoxedNumber].charValue(); + val x = boxed(i).asInstanceOf[BoxedNumber]; + if (x ne null) newvalue(i) = x.charValue(); i = i + 1 } unboxed = newvalue; @@ -116,7 +121,8 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { val newvalue = new Array[Byte](length); var i = 0; while (i < length) { - newvalue(i) = boxed(i).asInstanceOf[BoxedNumber].byteValue(); + val x = boxed(i).asInstanceOf[BoxedNumber]; + if (x ne null) newvalue(i) = x.byteValue(); i = i + 1 } unboxed = newvalue; @@ -124,7 +130,8 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { val newvalue = new Array[Short](length); var i = 0; while (i < length) { - newvalue(i) = boxed(i).asInstanceOf[BoxedNumber].shortValue(); + val x = boxed(i).asInstanceOf[BoxedNumber]; + if (x ne null) newvalue(i) = x.shortValue(); i = i + 1 } unboxed = newvalue; @@ -132,7 +139,8 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { val newvalue = new Array[Boolean](length); var i = 0; while (i < length) { - newvalue(i) = boxed(i).asInstanceOf[BoxedBoolean].value; + val x = boxed(i).asInstanceOf[BoxedBoolean]; + if (x ne null) newvalue(i) = x.value; i = i + 1 } unboxed = newvalue; diff --git a/src/library/scala/runtime/BoxedArray.scala b/src/library/scala/runtime/BoxedArray.scala index 793301cb23..8fad91eb2e 100644 --- a/src/library/scala/runtime/BoxedArray.scala +++ b/src/library/scala/runtime/BoxedArray.scala @@ -5,57 +5,72 @@ ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** \* */ - // $Id$ - - -package scala.runtime; - +package scala.runtime /** * A class representing Array[T] */ abstract class BoxedArray extends PartialFunction[Int, Object] with Seq[Object] { /** The length of the array */ - def length: Int; + def length: Int /** The element at given index */ - def apply(index: Int): Object; + def apply(index: Int): Object /** Update element at given index */ - def update(index: Int, elem: Object): Unit; + def update(index: Int, elem: Object): Unit /** Convert to Java array. * @param elemTag Either one of the tags ".N" where N is the name of a primitive type * (@see ScalaRunTime), or a full class name. */ - def unbox(elemTag: String): Object; - - override def isDefinedAt(x: Int): Boolean = 0 <= x && x < length; - - override def toString(): String = { - val buf = new StringBuffer(); - buf.append("Array("); - val len = length; - var i = 0; - while (i < len) { buf.append(apply(i)); i = i + 1 } - buf.append(")"); - buf.toString() - } + def unbox(elemTag: String): Object + + override def isDefinedAt(x: Int): Boolean = 0 <= x && x < length def elements = new Iterator[Object] { - var index = 0; - def hasNext: Boolean = index < length; + var index = 0 + def hasNext: Boolean = index < length def next: Object = { val i = index; index = i + 1; apply(i) } } /** The underlying array value */ - def value: Object; + def value: Object def copyFrom(src: Object, from: Int, to: Int, len: Int): Unit = Array.copy(src, from, value, to, len) - def copyTo(from: Int, dest: Object, to: Int, len: Int): Unit = + def copyTo(from: Int, dest: Object, to: Int, len: Int): Unit = { Array.copy(value, from, dest, to, len) + } + + def subArray(from: Int, end: Int): Object = null + + def filter(p: Any => Boolean): Object = null + + def map[b](f: Any => b): Array[b] = { + val len = length + val result = new Array[b](len) + var i = 0 + while (i < len) { + result(i) = f(apply(i)) + i = i + 1 + } + result + } + + def flatMap[b](f: Any => Array[b]): Array[b] = { + val len = length + val tmp = new Array[Array[b]](len) + var i = 0 + while (i < len) { + tmp(i) = f(apply(i)) + i = i + 1 + } + Array.concat(tmp: _*) + } + + override final def stringPrefix: String = "Array" } diff --git a/src/library/scala/runtime/BoxedIntArray.scala b/src/library/scala/runtime/BoxedIntArray.scala index 73e4c19375..bbfc82407c 100644 --- a/src/library/scala/runtime/BoxedIntArray.scala +++ b/src/library/scala/runtime/BoxedIntArray.scala @@ -31,5 +31,29 @@ final class BoxedIntArray(val value: Array[Int]) extends BoxedArray { ); override def hashCode(): Int = value.hashCode(); + + override def subArray(start: Int, end: Int): Array[Int] = { + val result = new Array[Int](end - start); + Array.copy(value, 0, result, 0, end - start) + result + } + + override def filter(p: Any => Boolean): Array[Int] = { + val include = new Array[Boolean](value.length); + var len = 0; + var i = 0; + while (i < value.length) { + if (p(value(i))) { include(i) = true; len = len + 1 } + i = i + 1 + } + val result = new Array[Int](len); + len = 0; + i = 0; + while (len < result.length) { + if (include(i)) { result(len) = value(i); len = len + 1 } + i = i + 1 + } + result + } } diff --git a/src/library/scala/runtime/BoxedObjectArray.scala b/src/library/scala/runtime/BoxedObjectArray.scala index 864108b5b1..31db3ca428 100644 --- a/src/library/scala/runtime/BoxedObjectArray.scala +++ b/src/library/scala/runtime/BoxedObjectArray.scala @@ -29,4 +29,37 @@ final class BoxedObjectArray(val value: Array[Object]) extends BoxedArray { ); override def hashCode(): Int = value.hashCode(); + + override def subArray(start: Int, end: Int): Array[Object] = { + val result = new Array[Object](end - start); + Array.copy(value, 0, result, 0, end - start) + result + } + + override def filter(p: Any => Boolean): Array[Object] = { + val include = new Array[Boolean](value.length); + var len = 0; + var i = 0; + while (i < value.length) { + if (p(value(i))) { include(i) = true; len = len + 1 } + i = i + 1 + } + val result = new Array[Object](len); + len = 0; + i = 0; + while (len < result.length) { + if (include(i)) { result(len) = value(i); len = len + 1 } + i = i + 1 + } + result + } + +/* + def map(p: Any => Any): BoxedAnyArray = { + val include = new Array + var len = 0 + for (val x <- value) if (p(x)) { tmp(len) = x; len = len + 1 } + slice(tmp, 0, len) + } +*/ } diff --git a/src/library/scala/runtime/ScalaRunTime.scala b/src/library/scala/runtime/ScalaRunTime.scala index 8e6a0649c2..83fa174f4e 100644 --- a/src/library/scala/runtime/ScalaRunTime.scala +++ b/src/library/scala/runtime/ScalaRunTime.scala @@ -105,4 +105,17 @@ object ScalaRunTime { def doubleValue (x: BoxedNumber ): Double = if (x eq null) 0.0D else x.doubleValue(); def arrayValue (x: BoxedArray, elemTag: String): Object = if (x eq null) null else x.unbox(elemTag); + + def boxArray(value: Object): BoxedArray = value match { + case x: Array[Byte] => new BoxedByteArray(x) + case x: Array[Short] => new BoxedShortArray(x) + case x: Array[Char] => new BoxedCharArray(x) + case x: Array[Int] => new BoxedIntArray(x) + case x: Array[Long] => new BoxedLongArray(x) + case x: Array[Float] => new BoxedFloatArray(x) + case x: Array[Double] => new BoxedDoubleArray(x) + case x: Array[Boolean] => new BoxedBooleanArray(x) + case x: Array[Object] => new BoxedObjectArray(x) + case x: BoxedArray => x + } } |