diff options
author | mihaylov <mihaylov@epfl.ch> | 2007-06-13 12:14:15 +0000 |
---|---|---|
committer | mihaylov <mihaylov@epfl.ch> | 2007-06-13 12:14:15 +0000 |
commit | a384720d2cfbd1ea61c4d605af75be6aebccd389 (patch) | |
tree | 53f1dc034bb51e2bd5eebaaaac3f646c341a681a /src | |
parent | 2d28f7fcc3e2cfcce127b8e0e5851df7bca66df0 (diff) | |
download | scala-a384720d2cfbd1ea61c4d605af75be6aebccd389.tar.gz scala-a384720d2cfbd1ea61c4d605af75be6aebccd389.tar.bz2 scala-a384720d2cfbd1ea61c4d605af75be6aebccd389.zip |
Integrated J2ME version of the library as of re...
Integrated J2ME version of the library as of rev 12002
Diffstat (limited to 'src')
36 files changed, 2050 insertions, 13 deletions
diff --git a/src/cldc-library/scala/Array.scala b/src/cldc-library/scala/Array.scala new file mode 100644 index 0000000000..17e4f68232 --- /dev/null +++ b/src/cldc-library/scala/Array.scala @@ -0,0 +1,338 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala + + +import Predef._ +import compat.Platform.arraycopy + +/** This object contains utility methods operating on arrays. + * + * @author Martin Odersky + * @version 1.0 + */ +object Array { + + /** Copy one array to another. + * Equivalent to + * <code>System.arraycopy(src, srcPos, dest, destPos, length)</code>, + * except that this works also for polymorphic and boxed arrays. + * + * @param src ... + * @param srcPos ... + * @param dest ... + * @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) + } + } + + /** Concatenate all argument arrays into a single array. + * + * @param xs ... + */ + def concat[T](xs: 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) + start += x.length + } + result + } + + /** Create a an array containing of successive integers. + * + * @param from the value of the first element of the array + * @param end the value of the last element fo the array plus 1 + * @return the sorted array of all integers in range [from;end). + */ + def range(start: Int, end: Int): Array[Int] = { + val result = new Array[Int](end - start) + for (i <- start until end) result(i - start) = i + result + } + + /** Create an array with given elements. + * + * @param xs the elements to put in the array + * @return the array containing elements xs. + */ + def apply[A <: AnyRef](xs: A*): Array[A] = { + val array = new Array[A](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + 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 (x <- xs.elements) { array(i) = x; i += 1 } + array + } +*/ + + def apply(xs: Boolean*): Array[Boolean] = { + val array = new Array[Boolean](xs.length) + var i = 0 + 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: Unit*): Array[Unit] = { + val array = new Array[Unit](xs.length) + var i = 0 + for (x <- xs.elements) { array(i) = x; i += 1 } + array + } + + /** Create an array containing several copies of an element. + * + * @param n the length of the resulting array + * @param elem the element composing the resulting array + * @return an array composed of n elements all equal to elem + */ + def make[A](n: Int, elem: A): Array[A] = { + val a = new Array[A](n) + var i = 0 + while (i < n) { + a(i) = elem + i += 1 + } + a + } + + /** This method is called as a result of a pattern match { case Array(...) => } or val Array(...) = .... + * + * @param x the selector value + * @return array wrapped in an option + */ + def unapplySeq[A](x: Array[A]): Option[Seq[A]] = Some(x) +} + +/** This class represents polymorphic arrays. <code>Array[T]</code> is Scala's representation + * for Java's <code>T[]</code>. + * + * @author Martin Odersky + * @version 1.0 + */ +final class Array[A](_length: Int) extends RandomAccessSeq[A] { + + /** The length of the array */ + def length: Int = throw new Error() + + /** The element at given index. + * <p> + * Indices start a <code>0</code>; <code>xs.apply(0)</code> is the first + * element of array <code>xs</code>. + * </p> + * <p> + * Note the indexing syntax <code>xs(i)</code> is a shorthand for + * <code>xs.apply(i)</code>. + * </p> + * + * @param i the index + * @throws ArrayIndexOutOfBoundsException if <code>i < 0</code> or + * <code>length <= i</code> + */ + def apply(i: Int): A = throw new Error() + + /** <p> + * Update the element at given index. + * </p> + * <p> + * Indices start a <code>0</code>; <code>xs.apply(0)</code> is the first + * element of array <code>xs</code>. + * </p> + * <p> + * Note the indexing syntax <code>xs(i) = x</code> is a shorthand + * for <code>xs.update(i, x)</code>. + * </p> + * + * @param i the index + * @param x the value to be written at index <code>i</code> + * @throws ArrayIndexOutOfBoundsException if <code>i < 0</code> or + * <code>length <= i</code> + */ + def update(i: Int, x: A): Unit = throw new Error() + + /** An iterator returning the elements of this array, starting from 0. + */ + override def elements: Iterator[A] = throw new Error() + + /** @deprecated use slice instead */ + def subArray(from: Int, end: Int): Array[A] = throw new Error() + + /** A sub-array of <code>len</code> elements + * starting at index <code>from</code> + * + * @param from The index of the first element of the slice + * @param end The index of the element following the slice + * @throws IndexOutOfBoundsException if <code>from < 0</code> + * or <code>length < from + len<code> + */ + override def slice(from: Int, end: Int): Array[A] = throw new Error() + + /** Returns an array consisting of all elements of this array that satisfy the + * predicate <code>p</code>. The order of the elements is preserved. + * + * @param p the predicate used to filter the array. + * @return the elements of this array satisfying <code>p</code>. + */ + override def filter(p: A => Boolean): Array[A] = throw new Error() + + /** Returns an array consisting of all elements of this array followed + * by all elements of the argument iterable. + */ + override def ++[B >: A](that: Iterable[B]): Array[B] = throw new Error() + + /** Returns the array resulting from applying the given function <code>f</code> to each + * element of this array. + * + * @param f function to apply to each element. + * @return <code>[f(a0), ..., f(an)]</code> if this array is <code>[a0, ..., an]</code>. + */ + override def map[B](f: A => B): Array[B] = throw new Error() + + /** Applies the given function <code>f</code> to each element of + * this array, then concatenates the results. + * + * @param f the function to apply on each element. + * @return <code>f(a<sub>0</sub>) ::: ... ::: f(a<sub>n</sub>)</code> if + * this array is <code>[a<sub>0</sub>, ..., a<sub>n</sub>]</code>. + */ + override def flatMap[B](f: A => Iterable[B]): Array[B] = throw new Error() + + /** Returns an array formed from this array and the specified array + * <code>that</code> by associating each element of the former with + * the element at the same position in the latter. + * If one of the two arrays is longer than the other, its remaining elements are ignored. + * + * @return <code>Array({a<sub>0</sub>,b<sub>0</sub>}, ..., + * {a<sub>min(m,n)</sub>,b<sub>min(m,n)</sub>})</code> when + * <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() + + /** Returns an array that pairs each element of this array + * with its index, counting from 0. + * + * @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() + + /** + * @return a deep string representation of this sequence. + */ + def deepToString(): String = throw new Error() + + /** <p> + * Returns a string representation of this array object. 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>deepToString()</code>) are separated by the string + * <code>sep</code>. For example: + * </p> + * <p> + * <code>Array(Array(1, 2), Array(3)).deepMkString("[", "; ", "]") = "[[1; 2]; [3]]"</code> + * </p> + * + * @param start starting string. + * @param sep separator string. + * @param end ending string. + * @return a string representation of this array object. + */ + def deepMkString(start: String, sep: String, end: String): String = + throw new Error() + + /** Returns a string representation of this array object. The string + * representations of elements (w.r.t. the method <code>deepToString()</code>) + * are separated by the string <code>sep</code>. + * + * @param sep separator string. + * @return a string representation of this array object. + */ + def deepMkString(sep: String): String = throw new Error() + + /** <p> + * Returns <code>true</code> if the two specified arrays are + * <em>deeply equal</em> to one another. + * </p> + * <p> + * Two array references are considered deeply equal if both are null, + * or if they refer to arrays that contain the same number of elements + * and all corresponding pairs of elements in the two arrays are deeply + * equal. + * </p> + * <p> + * See also method <code>deepEquals</code> in the Java class + * <a href="http://java.sun.com/javase/6/docs/api/java/util/Arrays.html" + * target="_top">java.utils.Arrays</a> + * </p> + * + * @param that the second + * @return <code>true</code> iff both arrays are deeply equal. + */ + def deepEquals(that: Any): Boolean = throw new Error() +} diff --git a/src/cldc-library/scala/Console.scala b/src/cldc-library/scala/Console.scala new file mode 100644 index 0000000000..43fe2ae894 --- /dev/null +++ b/src/cldc-library/scala/Console.scala @@ -0,0 +1,96 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala + +import java.io.{OutputStream, PrintStream} + +import Predef._ + + +/** The <code>Console</code> object implements functionality for + * printing Scala values on the terminal. There are also functions + * for reading specific values. <code>Console</code> also defines + * constants for marking up text on ANSI terminals. + * + * @author Matthias Zenger + * @version 1.0, 03/09/2003 + */ +object Console { + + // ANSI colors foreground + final val BLACK = "\033[30m" + final val RED = "\033[31m" + final val GREEN = "\033[32m" + final val YELLOW = "\033[33m" + final val BLUE = "\033[34m" + final val MAGENTA = "\033[35m" + final val CYAN = "\033[36m" + final val WHITE = "\033[37m" + + // ANSI colors background + final val BLACK_B = "\033[40m" + final val RED_B = "\033[41m" + final val GREEN_B = "\033[42m" + final val YELLOW_B = "\033[43m" + final val BLUE_B = "\033[44m" + final val MAGENTA_B = "\033[45m" + final val CYAN_B = "\033[46m" + final val WHITE_B = "\033[47m" + + // ANSI styles + final val RESET = "\033[0m" + final val BOLD = "\033[1m" + final val UNDERLINED = "\033[4m" + final val BLINK = "\033[5m" + final val REVERSED = "\033[7m" + final val INVISIBLE = "\033[8m" + + var out: PrintStream = java.lang.System.out + val err = java.lang.System.err + + /** Set the default output stream. + * + * @param out the new output stream. + */ + def setOut(out: PrintStream): Unit = this.out = out + + /** Set the default output stream. + * + * @param@ out the new output stream. + */ + def setOut(out: OutputStream): Unit = + setOut(new PrintStream(out)) + + /** Print an object on the terminal. + * + * @param obj the object to print. + */ + def print(obj: Any): Unit = + out.print(if (null == obj) "null" else obj.toString()) + + /** Flush the output stream. This function is required when partial + * output (i.e. output not terminated by a new line character) has + * to be made visible on the terminal. + */ + def flush(): Unit = out.flush() + + /** Print a new line character on the terminal. + */ + def println(): Unit = out.println() + + /** Print out an object followed by a new line character. + * + * @param x the object to print. + */ + def println(x: Any): Unit = out.println(x) + +} diff --git a/src/cldc-library/scala/Math.scala b/src/cldc-library/scala/Math.scala new file mode 100644 index 0000000000..74491ae1df --- /dev/null +++ b/src/cldc-library/scala/Math.scala @@ -0,0 +1,58 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala + +/** The object <code>Math</code> contains methods for performing basic numeric + * operations such as the elementary exponential, logarithm, square root, and + * trigonometric functions. + */ +object Math { + + /** The smalles possible value for scala.Byte. */ + val MIN_BYTE = java.lang.Byte.MIN_VALUE + /** The greatest possible value for scala.Byte. */ + val MAX_BYTE = java.lang.Byte.MAX_VALUE + + /** The smalles possible value for scala.Short. */ + val MIN_SHORT = java.lang.Short.MIN_VALUE + /** The greatest possible value for scala.Short. */ + val MAX_SHORT = java.lang.Short.MAX_VALUE + + /** The smalles possible value for scala.Char. */ + val MIN_CHAR = java.lang.Character.MIN_VALUE + /** The greatest possible value for scala.Char. */ + val MAX_CHAR = java.lang.Character.MAX_VALUE + + /** The smalles possible value for scala.Int. */ + val MIN_INT = java.lang.Integer.MIN_VALUE + /** The greatest possible value for scala.Int. */ + val MAX_INT = java.lang.Integer.MAX_VALUE + + /** The smalles possible value for scala.Long. */ + val MIN_LONG = java.lang.Long.MIN_VALUE + /** The greatest possible value for scala.Long. */ + val MAX_LONG = java.lang.Long.MAX_VALUE + + + def abs(x: Int): Int = java.lang.Math.abs(x) + def abs(x: Long): Long = java.lang.Math.abs(x) + + def max(x: Int, y: Int): Int = java.lang.Math.max(x, y) + def max(x: Long, y: Long): Long = java.lang.Math.max(x, y) + + def min(x: Int, y: Int): Int = java.lang.Math.min(x, y) + def min(x: Long, y: Long): Long = java.lang.Math.min(x, y) + + // this is only for use by immutable.Hash{Set,Map} and + // the imprecision is hopefully tolerable + def sqrt(x: Int): Int = x / 4 +} diff --git a/src/cldc-library/scala/Predef.scala b/src/cldc-library/scala/Predef.scala new file mode 100644 index 0000000000..8264693a49 --- /dev/null +++ b/src/cldc-library/scala/Predef.scala @@ -0,0 +1,274 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala + + +/** The <code>Predef</code> object provides definitions that are + * accessible in all Scala compilation units without explicit + * qualification. + */ +object Predef { + + // classOf dummy ------------------------------------------------------ + + /** Return the runtime representation of a class type. */ + def classOf[T]: Class = null + + // aliases ------------------------------------------------------------ + + type byte = scala.Byte + type short = scala.Short + type char = scala.Char + type int = scala.Int + type long = scala.Long + type boolean = scala.Boolean + type unit = scala.Unit + + type All = Nothing + type AllRef = Null + + type String = java.lang.String + type StringBuilder = compat.StringBuilder + type Class = java.lang.Class + type Runnable = java.lang.Runnable + + type Throwable = java.lang.Throwable + type Exception = java.lang.Exception + type Error = java.lang.Error + + type AssertionError = java.lang.Error + type RuntimeException = java.lang.RuntimeException + type NullPointerException = java.lang.NullPointerException + type ClassCastException = java.lang.ClassCastException + type IndexOutOfBoundsException = java.lang.IndexOutOfBoundsException + type ArrayIndexOutOfBoundsException = java.lang.ArrayIndexOutOfBoundsException + type UnsupportedOperationException = RuntimeException //java.lang.UnsupportedOperationException + type IllegalArgumentException = java.lang.IllegalArgumentException + type NoSuchElementException = java.util.NoSuchElementException + type NumberFormatException = java.lang.NumberFormatException + + // miscelleaneous ----------------------------------------------------- + + //val $scope = scala.xml.TopScope + + type Function[-A, +B] = Function1[A, B] + + type Map[A, B] = collection.immutable.Map[A, B] + type Set[A] = collection.immutable.Set[A] + + val Map = collection.immutable.Map + val Set = collection.immutable.Set + + // errors and asserts ------------------------------------------------- + + def error(message: String): Nothing = throw new Error(message) + + def exit: Nothing = exit(0) + + def exit(status: Int): Nothing = { + java.lang.System.exit(status) + throw new Throwable() + } + + def assert(assertion: Boolean) { + if (!assertion) + throw new AssertionError("assertion failed") + } + + def assert(assertion: Boolean, message: Any) { + if (!assertion) + throw new AssertionError("assertion failed: " + message) + } + + def assume(assumption: Boolean) { + if (!assumption) + throw new Error("assumption failed") + } + + def assume(assumption: Boolean, message: Any) { + if (!assumption) + throw new Error("assumption failed: " + message) + } + + // tupling ------------------------------------------------------------ + + type Pair[+A, +B] = Tuple2[A, B] + object Pair { + def apply[A, B](x: A, y: B) = Tuple2(x, y) + def unapply[A, B](x: Tuple2[A, B]): Option[Tuple2[A, B]] = Some(x) + } + + type Triple[+A, +B, +C] = Tuple3[A, B, C] + object Triple { + def apply[A, B, C](x: A, y: B, z: C) = Tuple3(x, y, z) + def unapply[A, B, C](x: Tuple3[A, B, C]): Option[Tuple3[A, B, C]] = Some(x) + } + + class ArrowAssoc[A](x: A) { + def -> [B](y: B): Tuple2[A, B] = Tuple2(x, y) + } + implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] = new ArrowAssoc(x) + + def Tuple[A1](x1: A1) = Tuple1(x1) + def Tuple[A1, A2](x1: A1, x2: A2) = Tuple2(x1, x2) + def Tuple[A1, A2, A3](x1: A1, x2: A2, x3: A3) = Tuple3(x1, x2, x3) + def Tuple[A1, A2, A3, A4](x1: A1, x2: A2, x3: A3, x4: A4) = Tuple4(x1, x2, x3, x4) + def Tuple[A1, A2, A3, A4, A5](x1: A1, x2: A2, x3: A3, x4: A4, x5: A5) = Tuple5(x1, x2, x3, x4, x5) + def Tuple[A1, A2, A3, A4, A5, A6](x1: A1, x2: A2, x3: A3, x4: A4, x5: A5, x6: A6) = Tuple6(x1, x2, x3, x4, x5, x6) + def Tuple[A1, A2, A3, A4, A5, A6, A7](x1: A1, x2: A2, x3: A3, x4: A4, x5: A5, x6: A6, x7: A7) = Tuple7(x1, x2, x3, x4, x5, x6, x7) + 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) + + // printing and reading ----------------------------------------------- + + def print(x: Any) = Console.print(x) + def println() = Console.println() + def println(x: Any) = Console.println(x) + + // views -------------------------------------------------------------- + + implicit def identity[A](x: A): A = x + + implicit def byteWrapper(x: Byte) = new runtime.RichByte(x) + implicit def shortWrapper(x: Short) = new runtime.RichShort(x) + implicit def intWrapper(x: Int) = new runtime.RichInt(x) + implicit def charWrapper(c: Char) = new runtime.RichChar(c) + implicit def longWrapper(x: Long) = new runtime.RichLong(x) + + implicit def booleanWrapper(x: Boolean) = new runtime.RichBoolean(x) + + implicit def stringWrapper(x: String) = new runtime.RichString(x) + + implicit def any2stringadd(x: Any) = new runtime.StringAdd(x) + + implicit def exceptionWrapper(exc: Throwable) = new runtime.RichException(exc) + + implicit def unit2ordered(x: Unit): Ordered[Unit] = new Ordered[Unit] with Proxy { + def self: Any = x + def compare(y: Unit): Int = 0 + } + + implicit def iterable2ordered[A <% Ordered[A]](xs: Iterable[A]): Ordered[Iterable[A]] = + new Ordered[Iterable[A]] with Proxy { + val self = xs + def compare(that: Iterable[A]): Int = { + var res = 0 + val these = xs.elements + val those = that.elements + while (res == 0 && these.hasNext) + res = if (those.hasNext) these.next compare those.next else 1 + res + } + } + + 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 compare(y: Tuple2[A1, A2]): Int = { + val res = x._1 compare y._1 + if (res == 0) x._2 compare y._2 + else res + } + } + + 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 compare(y: Tuple3[A1, A2, A3]): Int = { + val res = x._1 compare y._1 + if (res == 0) Tuple2(x._2, x._3) compare Tuple2(y._2, y._3) + else res + } + } + + 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 compare(y: Tuple4[A1, A2, A3, A4]): Int = { + val res = x._1 compare y._1 + if (res == 0) Tuple3(x._2, x._3, x._4) compare Tuple3(y._2, y._3, y._4) + else res + } + } + + 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 compare(y: Tuple5[A1, A2, A3, A4, A5]): Int = { + val res = x._1 compare y._1 + if (res == 0) Tuple4(x._2, x._3, x._4, x._5) compare Tuple4(y._2, y._3, y._4, y._5) + else res + } + } + + 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 compare(y: Tuple6[A1, A2, A3, A4, A5, A6]): Int = { + val res = x._1 compare y._1 + if (res == 0) Tuple5(x._2, x._3, x._4, x._5, x._6) compare Tuple5(y._2, y._3, y._4, y._5, y._6) + else res + } + } + + 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 compare(y: Tuple7[A1, A2, A3, A4, A5, A6, A7]): Int = { + val res = x._1 compare y._1 + if (res == 0) Tuple6(x._2, x._3, x._4, x._5, x._6, x._7) compare Tuple6(y._2, y._3, y._4, y._5, y._6, y._7) + else res + } + } + + 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 compare(y: Tuple8[A1, A2, A3, A4, A5, A6, A7, A8]): Int = { + val res = x._1 compare y._1 + if (res == 0) Tuple7(x._2, x._3, x._4, x._5, x._6, x._7, x._8) compare Tuple7(y._2, y._3, y._4, y._5, y._6, y._7, y._8) + else res + } + } + + 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 compare(y: Tuple9[A1, A2, A3, A4, A5, A6, A7, A8, A9]): Int = { + val res = x._1 compare y._1 + if (res == 0) Tuple8(x._2, x._3, x._4, x._5, x._6, x._7, x._8, x._9) compare Tuple8(y._2, y._3, y._4, y._5, y._6, y._7, y._8, y._9) + else res + } + } + + implicit def byte2short(x: Byte): Short = x.toShort + implicit def byte2int(x: Byte): Int = x.toInt + implicit def byte2long(x: Byte): Long = x.toLong + + implicit def short2int(x: Short): Int = x.toInt + implicit def short2long(x: Short): Long = x.toLong + + implicit def char2int(x: Char): Int = x.toInt + implicit def char2long(x: Char): Long = x.toLong + + implicit def int2long(x: Int): Long = x.toLong + + implicit def byte2Byte(x: Byte) = new java.lang.Byte(x) + implicit def short2Short(x: Short) = new java.lang.Short(x) + implicit def char2Character(x: Char) = new java.lang.Character(x) + implicit def int2Integer(x: Int) = new java.lang.Integer(x) + implicit def long2Long(x: Long) = new java.lang.Long(x) + implicit def boolean2Boolean(x: Boolean) = new java.lang.Boolean(x) + + def currentThread = java.lang.Thread.currentThread() + +} diff --git a/src/cldc-library/scala/Symbol.scala b/src/cldc-library/scala/Symbol.scala new file mode 100644 index 0000000000..3af5172406 --- /dev/null +++ b/src/cldc-library/scala/Symbol.scala @@ -0,0 +1,48 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala + +/** <p> + * Instances of <code>Symbol</code> can be created easily with + * Scala's built-in quote mechanism. + * </p> + * <p> + * For instance, the <a href="http://scala-lang.org/" target="_top">Scala</a> + * term <code>'mysym</code> will invoke the constructor of the + * <code>Symbol</code> class in the following way: + * <code>new Symbol("mysym")</code>. + * </p> + * + * @author Martin Odersky + * @version 1.7, 08/12/2003 + */ +final case class Symbol(name: String) { + + /** Converts this symbol to a string. + */ + override def toString(): String = { + "'" + name + } + + /** <p> + * Makes this symbol into a unique reference. + * </p> + * <p> + * If two interened symbols are equal (i.e. they have the same name) + * then they must be identical (wrt reference equality). + * </p> + * + * @return the unique reference to this symbol. + */ + def intern: Symbol = this + +} diff --git a/src/cldc-library/scala/collection/mutable/CloneableCollection.scala b/src/cldc-library/scala/collection/mutable/CloneableCollection.scala new file mode 100644 index 0000000000..fed584e5ba --- /dev/null +++ b/src/cldc-library/scala/collection/mutable/CloneableCollection.scala @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection.mutable + +/** The J2ME version of the library defined this trait with a clone method + * to substitute for the lack of Object.clone there + */ +trait CloneableCollection { + def clone(): AnyRef = Predef.error("Cloning not supported") +} diff --git a/src/cldc-library/scala/compat/Platform.scala b/src/cldc-library/scala/compat/Platform.scala new file mode 100644 index 0000000000..8598994a8a --- /dev/null +++ b/src/cldc-library/scala/compat/Platform.scala @@ -0,0 +1,56 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.compat + + +import java.lang.System +import Predef._ + +object Platform { + + //type StackOverflowError = java.lang.StackOverflowError + type ConcurrentModificationException = java.lang.RuntimeException + + /** + * @param src .. + * @param srcPos .. + * @param dest .. + * @param destPos .. + * @param length .. + */ + def arraycopy(src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int): Unit = + System.arraycopy(src, srcPos, dest, destPos, length) + + /** Create array of the same type as arrayInstance with the given + * length. + * + * @param elemClass .. + * @param length .. + * @return .. + */ + def createArray(elemClass: Class, length: Int): AnyRef = + throw new RuntimeException("" + elemClass + "[" + length+ "]") + //java.lang.reflect.Array.newInstance(elemClass, length) + + //def arrayclear(arr: Array[Int]): Unit = java.util.Arrays.fill(arr, 0) + def arrayclear(arr: Array[Int]): Unit = for (i <- 0 to arr.length) arr(i) = 0 + + def getClassForName(name: String): Class = java.lang.Class.forName(name) + + val EOL = "\n" + + def currentTime: Long = System.currentTimeMillis() + + def collectGarbage: Unit = System.gc() + +} + diff --git a/src/cldc-library/scala/compat/StringBuilder.scala b/src/cldc-library/scala/compat/StringBuilder.scala new file mode 100644 index 0000000000..2f8188f0f3 --- /dev/null +++ b/src/cldc-library/scala/compat/StringBuilder.scala @@ -0,0 +1,65 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.compat + + +import java.lang.{String, StringBuffer} + + +/** Consult the documentation of java.lang.StringBuffer for more details + */ +final class StringBuilder(val self: StringBuffer) extends (Int => Char) with Proxy { + + def this() = this(new StringBuffer()) + def this(n: Int) = this(new StringBuffer(n)) + def this(s: String) = this(new StringBuffer(s)) + + def length: Int = self.length() + def length_=(n: Int) { self.setLength(n) } + def setLength(n: Int) { self.setLength(n) } + + def capacity: Int = self.capacity + def capacity_=(n: Int) { self.ensureCapacity(n) } + def ensureCapacity(n: Int) { self.ensureCapacity(n) } + + def charAt(i: Int): Char = self.charAt(i) + def apply(i: Int): Char = self.charAt(i) + + def setCharAt(index: Int, c: Char) { self.setCharAt(index, c) } + def update(i: Int, c: Char) { self.setCharAt(i, c)} + + def substring(i: Int): String = self.toString.substring(i) + def substring(i: Int, j: Int): String = self.toString.substring(i, j) + + def append(x: Any): StringBuilder = { self.append(x); this } + def append(x: Boolean): StringBuilder = { self.append(x); this } + def append(x: Byte): StringBuilder = { self.append(x); this } + def append(x: Short): StringBuilder = { self.append(x); this } + def append(x: Char): StringBuilder = { self.append(x); this } + def append(x: Int): StringBuilder = { self.append(x); this } + def append(x: Long): StringBuilder = { self.append(x); this } + def append(x: String): StringBuilder = { self.append(x); this } + def append(x: Array[Char]): StringBuilder = { self.append(x); this } + def append(x: Array[Char], start: Int, length: Int): StringBuilder = + { self.append(x, start, length); this } + + def insert(at: Int, x: Any): StringBuilder = { self.insert(at, x); this } + def insert(at: Int, x: Boolean): StringBuilder = { self.insert(at, x); this } + def insert(at: Int, x: Byte): StringBuilder = { self.insert(at, x); this } + def insert(at: Int, x: Short): StringBuilder = { self.insert(at, x); this } + def insert(at: Int, x: Char): StringBuilder = { self.insert(at, x); this } + def insert(at: Int, x: Int): StringBuilder = { self.insert(at, x); this } + def insert(at: Int, x: Long): StringBuilder = { self.insert(at, x); this } + def insert(at: Int, x: String): StringBuilder = { self.insert(at, x); this } + def insert(at: Int, x: Array[Char]): StringBuilder = { self.insert(at, x); this } + +} diff --git a/src/cldc-library/scala/runtime/BooleanRef.java b/src/cldc-library/scala/runtime/BooleanRef.java new file mode 100644 index 0000000000..a0b2878506 --- /dev/null +++ b/src/cldc-library/scala/runtime/BooleanRef.java @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + + +public class BooleanRef { + public boolean elem; + public BooleanRef(boolean elem) { this.elem = elem; } + public String toString() { return String.valueOf(elem); } +} diff --git a/src/cldc-library/scala/runtime/BoxedAnyArray.scala b/src/cldc-library/scala/runtime/BoxedAnyArray.scala new file mode 100644 index 0000000000..d0ca1ffd52 --- /dev/null +++ b/src/cldc-library/scala/runtime/BoxedAnyArray.scala @@ -0,0 +1,233 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime + + +import Predef._ +import compat.Platform + +/** + * Arrays created by <code>new Array[T](length)</code> where <code>T</code> + * is a type variable. + */ +@serializable +final class BoxedAnyArray(val length: Int) extends BoxedArray { + + private var boxed = new Array[AnyRef](length) + private val hash = boxed.hashCode() + private var unboxed: AnyRef = null + private var elemClass: Class = null + + def apply(index: Int): Any = synchronized { + if (unboxed eq null) + boxed(index); + else if (elemClass eq classOf[Int]) + Int.box(unboxed.asInstanceOf[Array[Int]](index)) + else if (elemClass eq classOf[Long]) + Long.box(unboxed.asInstanceOf[Array[Long]](index)) + else if (elemClass eq classOf[Char]) + Char.box(unboxed.asInstanceOf[Array[Char]](index)) + else if (elemClass eq classOf[Byte]) + Byte.box(unboxed.asInstanceOf[Array[Byte]](index)) + else if (elemClass eq classOf[Short]) + Short.box(unboxed.asInstanceOf[Array[Short]](index)) + else if (elemClass eq classOf[Boolean]) + Boolean.box(unboxed.asInstanceOf[Array[Boolean]](index)) + else + unboxed.asInstanceOf[Array[AnyRef]](index) + } + + def update(index: Int, _elem: Any): Unit = synchronized { + val elem = _elem.asInstanceOf[AnyRef] + if (unboxed eq null) + boxed(index) = elem + else if (elemClass eq classOf[Int]) + unboxed.asInstanceOf[Array[Int]](index) = Int.unbox(elem) + else if (elemClass eq classOf[Long]) + unboxed.asInstanceOf[Array[Long]](index) = Long.unbox(elem) + else if (elemClass eq classOf[Char]) + unboxed.asInstanceOf[Array[Char]](index) = Char.unbox(elem) + else if (elemClass eq classOf[Byte]) + unboxed.asInstanceOf[Array[Byte]](index) = Byte.unbox(elem) + else if (elemClass eq classOf[Short]) + unboxed.asInstanceOf[Array[Short]](index) = Short.unbox(elem) + else if (elemClass eq classOf[Boolean]) + unboxed.asInstanceOf[Array[Boolean]](index) = Boolean.unbox(elem) + else + unboxed.asInstanceOf[Array[AnyRef]](index) = elem + } + + def unbox(elemTag: String): AnyRef = + if (elemTag eq ScalaRunTime.IntTag) unbox(classOf[Int]) + else if (elemTag eq ScalaRunTime.LongTag) unbox(classOf[Long]) + else if (elemTag eq ScalaRunTime.CharTag) unbox(classOf[Char]) + else if (elemTag eq ScalaRunTime.ByteTag) unbox(classOf[Byte]) + else if (elemTag eq ScalaRunTime.ShortTag) unbox(classOf[Short]) + else if (elemTag eq ScalaRunTime.BooleanTag) unbox(classOf[Boolean]) + else unbox(Platform.getClassForName(elemTag)) + + def unbox(elemClass: Class): AnyRef = synchronized { + if (unboxed eq null) { + this.elemClass = elemClass; + if (elemClass eq classOf[Int]) { + val newvalue = new Array[Int](length) + var i = 0 + while (i < length) { + newvalue(i) = Int.unbox(boxed(i)) + i = i + 1 + } + unboxed = newvalue + } else if (elemClass eq classOf[Long]) { + val newvalue = new Array[Long](length) + var i = 0 + while (i < length) { + newvalue(i) = Long.unbox(boxed(i)) + i = i + 1 + } + unboxed = newvalue; + } else if (elemClass eq classOf[Char]) { + val newvalue = new Array[Char](length) + var i = 0 + while (i < length) { + newvalue(i) = Char.unbox(boxed(i)) + i = i + 1 + } + unboxed = newvalue + } else if (elemClass eq classOf[Byte]) { + val newvalue = new Array[Byte](length) + var i = 0 + while (i < length) { + newvalue(i) = Byte.unbox(boxed(i)) + i = i + 1 + } + unboxed = newvalue; + } else if (elemClass eq classOf[Short]) { + val newvalue = new Array[Short](length) + var i = 0 + while (i < length) { + newvalue(i) = Short.unbox(boxed(i)) + i = i + 1 + } + unboxed = newvalue; + } else if (elemClass eq classOf[Boolean]) { + val newvalue = new Array[Boolean](length) + var i = 0 + while (i < length) { + newvalue(i) = Boolean.unbox(boxed(i)) + i = i + 1 + } + unboxed = newvalue; +// } else if (elemClass == boxed.getClass().getComponentType()) { +// // todo: replace with ScalaRunTime.AnyRef.class +// unboxed = boxed + } else { + unboxed = Platform.createArray(elemClass, length); + Platform.arraycopy(boxed, 0, unboxed, 0, length); + } + boxed = null + } + unboxed + } + + override def equals(other: Any): Boolean = ( + other.isInstanceOf[BoxedAnyArray] && (this eq (other.asInstanceOf[BoxedAnyArray])) || + (if (unboxed eq null) boxed == other else unboxed == other) + ) + + override def hashCode(): Int = hash + + def value: AnyRef = { + if (unboxed eq null) throw new NotDefinedError("BoxedAnyArray.value") + unboxed + } + + private def adapt(other: AnyRef): AnyRef = + if (this.unboxed eq null) + other match { + case that: BoxedAnyArray => + if (that.unboxed eq null) { + that.boxed + } else { + if (ScalaRunTime.isValueClass(that.elemClass)) unbox(that.elemClass); + that.unboxed + } + case that: BoxedArray => + adapt(that.value) + case that: Array[Int] => + unbox(ScalaRunTime.IntTag); that + case that: Array[Long] => + unbox(ScalaRunTime.LongTag); that + case that: Array[Char] => + unbox(ScalaRunTime.CharTag); that + case that: Array[Short] => + unbox(ScalaRunTime.ShortTag); that + case that: Array[Byte] => + unbox(ScalaRunTime.ByteTag); that + case that: Array[Boolean] => + unbox(ScalaRunTime.BooleanTag); that + case _ => + other + } + else + other match { + case that: BoxedAnyArray => + if (that.unboxed ne null) that.unboxed + else if (ScalaRunTime.isValueClass(this.elemClass)) that.unbox(this.elemClass) + else that.boxed + case that: BoxedArray => + adapt(that.value) + case _ => + other + } + + override def copyFrom(src: AnyRef, from: Int, to: Int, len: Int): Unit = { + val src1 = adapt(src) + Array.copy(src1, from, if (unboxed ne null) unboxed else boxed, to, len) + } + + override def copyTo(from: Int, dest: AnyRef, to: Int, len: Int): Unit = { + var dest1 = adapt(dest) + Array.copy(if (unboxed ne null) unboxed else boxed, from, dest1, to, len) + } + + override def subArray(start: Int, end: Int): AnyRef = { + val result = new BoxedAnyArray(end - start); + Array.copy(this, start, result, 0, end - start) + result + } + + final override def filter(p: Any => Boolean): BoxedArray = { + val include = new Array[Boolean](length) + var len = 0 + var i = 0 + while (i < length) { + if (p(this(i))) { include(i) = true; len = len + 1 } + i = i + 1 + } + val result = new BoxedAnyArray(len) + len = 0 + i = 0 + while (len < result.length) { + if (include(i)) { result(len) = this(i); len = len + 1 } + i = i + 1 + } + result + } + + final override def slice(start: Int, end: Int): BoxedArray = { + val len = end - start + val result = new BoxedAnyArray(len) + Array.copy(this, start, result, 0, len) + result + } + +} diff --git a/src/cldc-library/scala/runtime/BoxedObjectArray.scala b/src/cldc-library/scala/runtime/BoxedObjectArray.scala new file mode 100644 index 0000000000..c50b7582a3 --- /dev/null +++ b/src/cldc-library/scala/runtime/BoxedObjectArray.scala @@ -0,0 +1,73 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime + + +import Predef._ +import compat.Platform.createArray + +@serializable +final class BoxedObjectArray(val value: Array[AnyRef]) extends BoxedArray { + + def length: Int = value.length + + def apply(index: Int): Any = value(index) + + def update(index: Int, elem: Any): Unit = { + value(index) = elem.asInstanceOf[AnyRef] + } + + def unbox(elemTag: String): AnyRef = value + def unbox(elemClass: Class): AnyRef = value + + override def equals(other: Any): Boolean = + value == other || + other.isInstanceOf[BoxedObjectArray] && value == other.asInstanceOf[BoxedObjectArray].value + + override def hashCode(): Int = value.hashCode() + + private def create(length: Int): Array[AnyRef] = { + //createArray(value.getClass().getComponentType(), length).asInstanceOf[Array[AnyRef]] + new Array[AnyRef](length) + } + + override def subArray(start: Int, end: Int): Array[AnyRef] = { + val result = create(end - start) + Array.copy(value, start, result, 0, end - start) + result + } + + final override def filter(p: Any => Boolean): BoxedArray = { + 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 = create(len) + len = 0 + i = 0 + while (len < result.length) { + if (include(i)) { result(len) = value(i); len = len + 1 } + i = i + 1 + } + new BoxedObjectArray(result) + } + + final override def slice(start: Int, end: Int): BoxedArray = { + val len = end - start + val result = create(len) + Array.copy(value, start, result, 0, len) + new BoxedObjectArray(result) + } +} diff --git a/src/cldc-library/scala/runtime/BoxedUnit.java b/src/cldc-library/scala/runtime/BoxedUnit.java new file mode 100644 index 0000000000..bafd0ec010 --- /dev/null +++ b/src/cldc-library/scala/runtime/BoxedUnit.java @@ -0,0 +1,33 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + + +public final class BoxedUnit +{ + + public final static BoxedUnit UNIT = new BoxedUnit(); + + private BoxedUnit() { } + + public boolean equals(java.lang.Object other) { + return this == other; + } + + public int hashCode() { + return 0; + } + + public String toString() { + return "()"; + } +} diff --git a/src/cldc-library/scala/runtime/BoxesUtility.java b/src/cldc-library/scala/runtime/BoxesUtility.java new file mode 100644 index 0000000000..de33635e0f --- /dev/null +++ b/src/cldc-library/scala/runtime/BoxesUtility.java @@ -0,0 +1,147 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + +/** + * @author Gilles Dubochet + * @version 1.0 + */ +public class BoxesUtility { + + private static int charLowBound = 0; + private static int charUpBound = 255; + private static Character[] charCache = new Character[charUpBound - charLowBound + 1]; + + private static int byteLowBound = -128; + private static int byteUpBound = 127; + private static Byte[] byteCache = new Byte[byteUpBound - byteLowBound + 1]; + + private static int shortLowBound = -128; + private static int shortUpBound = 127; + private static Short[] shortCache = new Short[shortUpBound - shortLowBound + 1]; + + private static int intLowBound = -128; + private static int intUpBound = 1024; + private static Integer[] intCache = new Integer[intUpBound - intLowBound + 1]; + + private static int longLowBound = -128; + private static int longUpBound = 1024; + private static Long[] longCache = new Long[longUpBound - longLowBound + 1]; + + static { + int idx = 0; + while (idx <= charUpBound - charLowBound) { + charCache[idx] = new Character((char)(idx + charLowBound)); + idx = idx + 1; + } + idx = 0; + while (idx <= byteUpBound - byteLowBound) { + byteCache[idx] = new Byte((byte)(idx + byteLowBound)); + idx = idx + 1; + } + idx = 0; + while (idx <= shortUpBound - shortLowBound) { + shortCache[idx] = new Short((short)(idx + shortLowBound)); + idx = idx + 1; + } + idx = 0; + while (idx <= intUpBound - intLowBound) { + intCache[idx] = new Integer((int)(idx + intLowBound)); + idx = idx + 1; + } + idx = 0; + while (idx <= longUpBound - longLowBound) { + longCache[idx] = new Long((long)(idx + longLowBound)); + idx = idx + 1; + } + } + private static final Boolean TRUE = new Boolean(true); + private static final Boolean FALSE = new Boolean(false); + public static Boolean boxToBoolean(boolean b) { + return b ? TRUE : FALSE; + } + + public static Character boxToCharacter(char c) { + if (c >= charLowBound && c <= charUpBound) + return charCache[(int)c - charLowBound]; + else + return new Character(c); + } + + public static Byte boxToByte(byte b) { + if (b >= byteLowBound && b <= byteUpBound) + return byteCache[(int)b - byteLowBound]; + else + return new Byte(b); + } + + public static Short boxToShort(short s) { + if (s >= shortLowBound && s <= shortUpBound) + return shortCache[(int)s - shortLowBound]; + else + return new Short(s); + } + + public static Integer boxToInteger(int i) { + if (i >= intLowBound && i <= intUpBound) + return intCache[(int)i - intLowBound]; + else + return new Integer(i); + } + + public static Long boxToLong(long l) { + if (l >= longLowBound && l <= longUpBound) + return longCache[(int)l - longLowBound]; + else + return new Long(l); + } + + public static boolean unboxToBoolean(Object b) { + return b == null ? false : ((Boolean)b).booleanValue(); + } + + public static char unboxToChar(Object c) { + if (c == null) + return 0; + else + return ((Character)c).charValue(); + } + + public static byte unboxToByte(Object b) { + if (b == null) + return 0; + else + return ((Byte)b).byteValue(); + } + + public static short unboxToShort(Object s) { + if (s == null) + return 0; + else + return ((Short)s).shortValue(); + } + + public static int unboxToInt(Object i) { + if (i == null) + return 0; + else + return ((Integer)i).intValue(); + } + + public static long unboxToLong(Object l) { + if (l == null) + return 0; + else + return ((Long)l).longValue(); + } + +} diff --git a/src/cldc-library/scala/runtime/ByteRef.java b/src/cldc-library/scala/runtime/ByteRef.java new file mode 100644 index 0000000000..f420362d03 --- /dev/null +++ b/src/cldc-library/scala/runtime/ByteRef.java @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + + +public class ByteRef { + public byte elem; + public ByteRef(byte elem) { this.elem = elem; } + public String toString() { return String.valueOf(elem); } +} diff --git a/src/cldc-library/scala/runtime/CharRef.java b/src/cldc-library/scala/runtime/CharRef.java new file mode 100644 index 0000000000..ea92945134 --- /dev/null +++ b/src/cldc-library/scala/runtime/CharRef.java @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + + +public class CharRef { + public char elem; + public CharRef(char elem) { this.elem = elem; } + public String toString() { return String.valueOf(elem); } +} diff --git a/src/cldc-library/scala/runtime/Comparator.java b/src/cldc-library/scala/runtime/Comparator.java new file mode 100644 index 0000000000..75469e69b1 --- /dev/null +++ b/src/cldc-library/scala/runtime/Comparator.java @@ -0,0 +1,53 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + +/** + * @author Gilles Dubochet + * @author Martin Odersky + * @version 1.2 */ +public class Comparator { + + /** A rich implementation of the equals method that overrides the default + * equals because Java's boxed primitives are utterly broken. This equals + * is inserted instead of a normal equals by the Scala compiler (in the + * ICode phase, method <code>genEqEqPrimitive</code>) only when either + * side of the comparison is a subclass of <code>AnyVal</code>, of + * <code>java.lang.Number</code>, of <code>java.lang.Character</code> or + * is exactly <code>Any</code> or <code>AnyRef</code>, but when both sides + * have different types. */ + public static boolean equals(Object a, Object b) { + if (a == null) + return b == null; + if (b == null) + return false; + if (a.equals(b)) + return true; + + final long left = + (a instanceof Integer) ? ((Integer)a).intValue() : + (a instanceof Character) ? ((Character)a).charValue() : + (a instanceof Long) ? ((Long)a).longValue() : + (a instanceof Byte) ? ((Byte)a).byteValue() : + ((Short)a).shortValue(); + + final long right = + (b instanceof Integer) ? ((Integer)b).intValue() : + (b instanceof Character) ? ((Character)b).charValue() : + (b instanceof Long) ? ((Long)b).longValue() : + (b instanceof Byte) ? ((Byte)b).byteValue() : + ((Short)b).shortValue(); + + return left == right; + } + +} diff --git a/src/cldc-library/scala/runtime/IntRef.java b/src/cldc-library/scala/runtime/IntRef.java new file mode 100644 index 0000000000..dde9b18d17 --- /dev/null +++ b/src/cldc-library/scala/runtime/IntRef.java @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + + +public class IntRef { + public int elem; + public IntRef(int elem) { this.elem = elem; } + public String toString() { return String.valueOf(elem); } +} diff --git a/src/cldc-library/scala/runtime/LongRef.java b/src/cldc-library/scala/runtime/LongRef.java new file mode 100644 index 0000000000..50a5133d87 --- /dev/null +++ b/src/cldc-library/scala/runtime/LongRef.java @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + + +public class LongRef { + public long elem; + public LongRef(long elem) { this.elem = elem; } + public String toString() { return String.valueOf(elem); } +} diff --git a/src/cldc-library/scala/runtime/ObjectRef.java b/src/cldc-library/scala/runtime/ObjectRef.java new file mode 100644 index 0000000000..895fb472ef --- /dev/null +++ b/src/cldc-library/scala/runtime/ObjectRef.java @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + + +public class ObjectRef { + public Object elem; + public ObjectRef(Object elem) { this.elem = elem; } + public String toString() { return "" + elem; } +} diff --git a/src/cldc-library/scala/runtime/RichChar.scala b/src/cldc-library/scala/runtime/RichChar.scala new file mode 100644 index 0000000000..9c7bd95d4e --- /dev/null +++ b/src/cldc-library/scala/runtime/RichChar.scala @@ -0,0 +1,71 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime + + +import java.lang.Character +import Predef.NoSuchElementException + +/** <p> + * For example, in the following code + * </p> + * <pre> + * <b>object</b> test <b>extends</b> Application { + * Console.println(<chr>'\40'</chr>.isWhitespace) + * Console.println('\011'.isWhitespace) + * Console.println('1'.asDigit == 1) + * Console.println('A'.asDigit == 10) + * }</pre> + * <p> + * the implicit conversions are performed using the predefined view + * <a href="../Predef$object.html#charWrapper(scala.Char)" + * target="contentFrame"><code>Predef.charWrapper</code></a>. + * </p> + */ +final class RichChar(x: Char) extends Proxy with Ordered[Char] { + + // Proxy.self + def self: Any = x + + // Ordered[Char].compare + def compare (y: Char): Int = if (x < y) -1 else if (x > y) 1 else 0 + + def asDigit: Int = Character.digit(x, Character.MAX_RADIX) + + //def isControl: Boolean = Character.isISOControl(x) + def isDigit: Boolean = Character.isDigit(x) + //def isLetter: Boolean = Character.isLetter(x) + //def isLetterOrDigit: Boolean = Character.isLetterOrDigit(x) + def isLowerCase: Boolean = Character.isLowerCase(x) + def isUpperCase: Boolean = Character.isUpperCase(x) + //def isWhitespace: Boolean = Character.isWhitespace(x) + + def toLowerCase: Char = Character.toLowerCase(x) + def toUpperCase: Char = Character.toUpperCase(x) + + /** Create an Iterator[Char] over the characters from 'x' to 'y' - 1 + */ + def until(limit: Char): Iterator[Char] = new Iterator[Char] { + private var ch = x + def hasNext: Boolean = ch < limit + def next: Char = + if (hasNext) { val j = ch; ch = (ch + 1).toChar; j } + else throw new NoSuchElementException("next on empty iterator") + } + + //def until(y: Char): Iterator[Char] = to(y) + + /** Create an Iterator[Char] over the characters from 'x' to 'y' + */ + def to(y: Char): Iterator[Char] = until((y + 1).toChar) + +} diff --git a/src/cldc-library/scala/runtime/RichException.scala b/src/cldc-library/scala/runtime/RichException.scala new file mode 100644 index 0000000000..9acab2e052 --- /dev/null +++ b/src/cldc-library/scala/runtime/RichException.scala @@ -0,0 +1,29 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id + + +package scala.runtime + +import Predef._ +import compat.Platform.EOL + +final class RichException(exc: Throwable) { + + def getStackTraceString: String = { + throw new UnsupportedOperationException(exc.toString) +// val s = new StringBuilder() +// for (trElem <- exc.getStackTrace()) { +// s.append(trElem.toString()) +// s.append(EOL) +// } +// s.toString() + } + +} diff --git a/src/cldc-library/scala/runtime/RichString.scala b/src/cldc-library/scala/runtime/RichString.scala new file mode 100644 index 0000000000..4b37a8c3fa --- /dev/null +++ b/src/cldc-library/scala/runtime/RichString.scala @@ -0,0 +1,143 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime + + +import Predef._ + +final class RichString(val self: String) extends Seq[Char] with Ordered[String] with Proxy { + + // Ordered[String] + def compare(other: String) = self compareTo other + + // Seq[Char] + def length = self.length + override def elements = Iterator.fromString(self) + + /** Retrieve the n-th character of the string + * + * @param index into the string + * @return the character at position <code>index</code>. + */ + def apply(n: Int) = self charAt n + + private final val LF: Char = 0x0A + private final val FF: Char = 0x0C + private final val CR: Char = 0x0D + private final val SU: Char = 0x1A + + private def isLineBreak(c: Char) = c == LF || c == FF + + /** <p> + * Strip trailing line end character from this string if it has one. + * A line end character is one of + * </p> + * <ul style="list-style-type: none;"> + * <li>LF - line feed (0x0A hex)</li> + * <li>FF - form feed (0x0C hex)</li> + * </ul> + * <p> + * If a line feed character LF is preceded by a carriage return CR + * (0x0D hex), the CR character is also stripped (Windows convention). + * </p> + */ + def stripLineEnd: String = { + val len = self.length + if (len == 0) self + else { + val last = apply(len - 1) + if (isLineBreak(last)) + self.substring(0, if (last == LF && len >= 2 && apply(len - 2) == CR) len - 2 else len - 1) + else + self + } + } + + /** <p> + * Return all lines in this string in an iterator, including trailing + * line end characters. + * </p> + * <p> + * The number of strings returned is one greater than the number of line + * end characters in this string. For an empty string, a single empty + * line is returned. A line end character is one of + * </p> + * <ul style="list-style-type: none;"> + * <li>LF - line feed (0x0A hex)</li> + * <li>FF - form feed (0x0C hex)</li> + * </ul> + */ + def linesWithSeparators = new Iterator[String] { + val len = self.length + var index = 0 + def hasNext: Boolean = index < len + def next(): String = { + if (index >= len) throw new NoSuchElementException("next on empty iterator") + val start = index + while (index < len && !isLineBreak(apply(index))) index = index + 1 + index = index + 1 + self.substring(start, index min len) + } + } + + /** Return all lines in this string in an iterator, excluding trailing line + * end characters, i.e. apply <code>.stripLineEnd</code> to all lines + * returned by <code>linesWithSeparators</code>. + */ + def lines: Iterator[String] = + linesWithSeparators map (line => new RichString(line).stripLineEnd) + + /** Returns this string with first character converted to upper case */ + def capitalize: String = { + val chars = self.toCharArray + chars(0) = chars(0).toUpperCase + new String(chars) + } + + /** <p> + * For every line in this string: + * </p> + * <blockquote> + * Strip a leading prefix consisting of blanks or control characters + * followed by <code>marginChar</code> from the line. + * </blockquote> + */ + def stripMargin(marginChar: Char): String = { + val buf = new scala.compat.StringBuilder() + for (line <- linesWithSeparators) { + val len = line.length + var index = 0; + while (index < len && line.charAt(index) <= ' ') index = index + 1 + buf append + (if (index < len && line.charAt(index) == marginChar) line.substring(index + 1) else line) + } + buf.toString + } + + /** <p> + * For every line in this string: + * </p> + * <blockquote> + * Strip a leading prefix consisting of blanks or control characters + * followed by <code>|</code> from the line. + * </blockquote> + */ + def stripMargin: String = stripMargin('|') + + //def split(separator: Char): Array[String] = self.split(separator.toString()) + + def toByte: Byte = java.lang.Byte.parseByte(self) + def toShort: Short = java.lang.Short.parseShort(self) + def toInt: Int = java.lang.Integer.parseInt(self) + def toLong: Long = java.lang.Long.parseLong(self) + +} diff --git a/src/cldc-library/scala/runtime/ScalaRunTime.scala b/src/cldc-library/scala/runtime/ScalaRunTime.scala new file mode 100644 index 0000000000..0404d8add0 --- /dev/null +++ b/src/cldc-library/scala/runtime/ScalaRunTime.scala @@ -0,0 +1,144 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime + + +import Predef._ + +/* The object <code>ScalaRunTime</code> provides ... + */ +object ScalaRunTime { + + /** Names for primitive types, used by array unboxing */ + val ByteTag = ".Byte" + val ShortTag = ".Short" + val CharTag = ".Char" + val IntTag = ".Int" + val LongTag = ".Long" + val FloatTag = ".Float" + val DoubleTag = ".Double" + val BooleanTag = ".Boolean" + + def isArray(x: AnyRef): Boolean = x != null && x.getClass.isArray + def isValueTag(tag: String) = tag.charAt(0) == '.' + //def isValueClass(clazz: Class) = clazz.isPrimitive() + def isValueClass(clazz: Class) = (clazz == classOf[Boolean] + || clazz == classOf[Byte] + || clazz == classOf[Short] + || clazz == classOf[Char] + || clazz == classOf[Int] + || clazz == classOf[Long]) + + def checkInitialized[T <: AnyRef](x: T): T = + if (x == null) throw new UninitializedError else x + + abstract class Try[a] { + def Catch[b >: a](handler: PartialFunction[Throwable, b]): b + def Finally(handler: Unit): a + } + + def Try[a](block: => a): Try[a] = new Try[a] with Runnable { + var result: a = _ + var exception: Throwable = ExceptionHandling.tryCatch(this) + + def run(): Unit = result = block + + def Catch[b >: a](handler: PartialFunction[Throwable, b]): b = + if (exception eq null) + result.asInstanceOf[b] + // !!! else if (exception is LocalReturn) + // !!! // ... + else if (handler isDefinedAt exception) + handler(exception) + else + throw exception + + def Finally(handler: Unit): a = + if (exception eq null) + result.asInstanceOf[a] + else + throw exception + } + + def caseFields(x: Product): List[Any] = { + val arity = x.productArity + def fields(from: Int): List[Any] = + if (from == arity) List() + else x.productElement(from) :: fields(from + 1) + fields(0) + } + + def _toString(x: Product): String = + caseFields(x).mkString(x.productPrefix + "(", ",", ")") + + def _hashCode(x: Product): Int = { + var code = x.getClass().hashCode() + val arr = x.productArity + var i = 0 + while (i < arr) { + code = code * 41 + x.productElement(i).hashCode() + i += 1 + } + code + } + + def _equals(x: Product, y: Any): Boolean = y match { + case y1: Product if x.productArity == y1.productArity => + val arity = x.productArity + var i = 0 + while (i < arity && x.productElement(i) == y1.productElement(i)) + i += 1 + i == arity + case _ => + false + } + + def _equalsWithVarArgs(x: Product, y: Any): Boolean = y match { + case y1: Product if x.productArity == y1.productArity => + val arity = x.productArity + var i = 0 + while (i < arity - 1 && x.productElement(i) == y1.productElement(i)) + i += 1 + i == arity - 1 && { + x.productElement(i) match { + case xs: Seq[_] => + y1.productElement(i) match { + case ys: Seq[_] => xs sameElements ys + } + } + } + case _ => + false + } + + //def checkDefined[T >: Null](x: T): T = + // if (x == null) throw new UndefinedException else x + + def Seq[a](xs: a*): Seq[a] = null // interpreted specially by new backend. + + def arrayValue(x: BoxedArray, elemTag: String): AnyRef = + if (x eq null) null else x.unbox(elemTag) + + def arrayValue(x: BoxedArray, elemClass: Class): AnyRef = + if (x eq null) null else x.unbox(elemClass) + + def boxArray(value: AnyRef): 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[Boolean] => new BoxedBooleanArray(x) + case x: Array[AnyRef] => new BoxedObjectArray(x) + case x: BoxedArray => x + } +} diff --git a/src/cldc-library/scala/runtime/ShortRef.java b/src/cldc-library/scala/runtime/ShortRef.java new file mode 100644 index 0000000000..fc844c07a8 --- /dev/null +++ b/src/cldc-library/scala/runtime/ShortRef.java @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.runtime; + + +public class ShortRef { + public short elem; + public ShortRef(short elem) { this.elem = elem; } + public String toString() { return String.valueOf(elem); } +} diff --git a/src/library/scala/Math.scala b/src/library/scala/Math.scala index c5dc0ede1f..a9db1a3692 100644 --- a/src/library/scala/Math.scala +++ b/src/library/scala/Math.scala @@ -117,6 +117,7 @@ object Math { def exp(x: Double): Double = java.lang.Math.exp(x) def log(x: Double): Double = java.lang.Math.log(x) def sqrt(x: Double): Double = java.lang.Math.sqrt(x) + def sqrt(x: Int): Int = java.lang.Math.sqrt(x.toDouble).toInt def IEEEremainder(x: Double, y: Double): Double = java.lang.Math.IEEEremainder(x, y) def ceil(x: Double): Double = java.lang.Math.ceil(x) diff --git a/src/library/scala/collection/immutable/HashMap.scala b/src/library/scala/collection/immutable/HashMap.scala index c91f0c7a43..17d9a38931 100644 --- a/src/library/scala/collection/immutable/HashMap.scala +++ b/src/library/scala/collection/immutable/HashMap.scala @@ -103,7 +103,7 @@ class HashMap[A, B] extends Map[A,B] with mutable.HashTable[A] { private def getValue(e: Entry) = e.value.asInstanceOf[B] - private def logLimit: Int = Math.sqrt(table.length.toDouble).toInt + private def logLimit: Int = Math.sqrt(table.length).toInt private def markUpdated(key: A, ov: Option[B], delta: Int) { val lv = loadFactor diff --git a/src/library/scala/collection/immutable/HashSet.scala b/src/library/scala/collection/immutable/HashSet.scala index 13bd8386a3..4de43cad46 100644 --- a/src/library/scala/collection/immutable/HashSet.scala +++ b/src/library/scala/collection/immutable/HashSet.scala @@ -89,7 +89,7 @@ class HashSet[A] extends Set[A] with mutable.FlatHashTable[A] { super.elements } - private def logLimit: Int = Math.sqrt(table.length.toDouble).toInt + private def logLimit: Int = Math.sqrt(table.length).toInt private def markUpdated(elem: A, del: Boolean) { val lv = loadFactor diff --git a/src/library/scala/collection/mutable/Buffer.scala b/src/library/scala/collection/mutable/Buffer.scala index ee7091ff20..0d8c4539e1 100644 --- a/src/library/scala/collection/mutable/Buffer.scala +++ b/src/library/scala/collection/mutable/Buffer.scala @@ -26,6 +26,7 @@ import Predef._ trait Buffer[A] extends AnyRef with Seq[A] with Scriptable[Message[(Location, A)]] + with CloneableCollection { /** Append a single element to this buffer. diff --git a/src/library/scala/collection/mutable/CloneableCollection.scala b/src/library/scala/collection/mutable/CloneableCollection.scala new file mode 100644 index 0000000000..f00cec798d --- /dev/null +++ b/src/library/scala/collection/mutable/CloneableCollection.scala @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection.mutable + +/** The J2ME version of the library defined this trait with a clone method + * to substitute for the lack of Object.clone there + */ +trait CloneableCollection { + override def clone(): AnyRef = super.clone() +} diff --git a/src/library/scala/collection/mutable/FlatHashTable.scala b/src/library/scala/collection/mutable/FlatHashTable.scala index 80150e6e47..05d08b3eb2 100644 --- a/src/library/scala/collection/mutable/FlatHashTable.scala +++ b/src/library/scala/collection/mutable/FlatHashTable.scala @@ -14,9 +14,10 @@ import Predef._ trait FlatHashTable[A] { - /** The load factor for the hash table; must be < 0.5f + /** The load factor for the hash table; must be < 500 (0.5) */ - protected def loadFactor: Float = 0.45f + protected def loadFactor: Int = 450 + protected final def loadFactorDenum = 1000 /** The initial size of the hash table. */ @@ -151,8 +152,8 @@ trait FlatHashTable[A] { private def newThreshold(size: Int) = { val lf = loadFactor - assert(lf < 0.5f, "loadFactor too large; must be < 0.5") - (size * lf).asInstanceOf[Int] + assert(lf < (loadFactorDenum / 2), "loadFactor too large; must be < 0.5") + (size.toLong * lf / loadFactorDenum ).toInt } protected def clear() { diff --git a/src/library/scala/collection/mutable/HashTable.scala b/src/library/scala/collection/mutable/HashTable.scala index e82a27e506..dbdb232a57 100644 --- a/src/library/scala/collection/mutable/HashTable.scala +++ b/src/library/scala/collection/mutable/HashTable.scala @@ -32,9 +32,10 @@ trait HashTable[A] extends AnyRef { protected type Entry >: Null <: HashEntry[A, Entry] - /** The load factor for the hash table. + /** The load factor for the hash table (in 0.001 step). */ - protected def loadFactor: Float = 0.75f + protected def loadFactor: Int = 750 // corresponds to 75% + protected final val loadFactorDenum = 1000; /** The initial size of the hash table. */ @@ -125,7 +126,7 @@ trait HashTable[A] extends AnyRef { } private def newThreshold(size: Int) = - (size * loadFactor).asInstanceOf[Int] + ((size.toLong * loadFactor)/loadFactorDenum).toInt private def resize(newSize: Int) = { val oldTable = table diff --git a/src/library/scala/collection/mutable/Map.scala b/src/library/scala/collection/mutable/Map.scala index 44b18e20ae..807ce14d90 100644 --- a/src/library/scala/collection/mutable/Map.scala +++ b/src/library/scala/collection/mutable/Map.scala @@ -42,6 +42,7 @@ object Map { trait Map[A, B] extends AnyRef with collection.Map[A, B] with Scriptable[Message[(A, B)]] + with CloneableCollection { /** This method allows one to add a new mapping from <code>key</code> * to <code>value</code> to the map. If the map already contains a diff --git a/src/library/scala/collection/mutable/PriorityQueue.scala b/src/library/scala/collection/mutable/PriorityQueue.scala index 6e86a2f818..0772c545e9 100644 --- a/src/library/scala/collection/mutable/PriorityQueue.scala +++ b/src/library/scala/collection/mutable/PriorityQueue.scala @@ -23,7 +23,7 @@ package scala.collection.mutable */ @serializable @cloneable -class PriorityQueue[A <% Ordered[A]] extends ResizableArray[A] { +class PriorityQueue[A <% Ordered[A]] extends ResizableArray[A] with CloneableCollection { size0 = size0 + 1 // we do not use array(0) protected def fixUp(as: Array[A], m: Int): Unit = { diff --git a/src/library/scala/collection/mutable/Queue.scala b/src/library/scala/collection/mutable/Queue.scala index 2b4138caa2..d654a82888 100644 --- a/src/library/scala/collection/mutable/Queue.scala +++ b/src/library/scala/collection/mutable/Queue.scala @@ -21,7 +21,7 @@ package scala.collection.mutable * @version 1.1, 03/05/2004 */ @serializable @cloneable -class Queue[A] extends MutableList[A] { +class Queue[A] extends MutableList[A] with CloneableCollection { /** Checks if the queue is empty. * diff --git a/src/library/scala/collection/mutable/Set.scala b/src/library/scala/collection/mutable/Set.scala index faa712edf8..1d68687f26 100644 --- a/src/library/scala/collection/mutable/Set.scala +++ b/src/library/scala/collection/mutable/Set.scala @@ -37,7 +37,7 @@ object Set { * @version 1.1, 09/05/2004 */ @cloneable -trait Set[A] extends collection.Set[A] with Scriptable[Message[A]] { +trait Set[A] extends collection.Set[A] with Scriptable[Message[A]] with CloneableCollection { /** This method allows one to add or remove an element <code>elem</code> * from this set depending on the value of parameter <code>included</code>. diff --git a/src/library/scala/collection/mutable/Stack.scala b/src/library/scala/collection/mutable/Stack.scala index 9fb44e6528..be95aee1a5 100644 --- a/src/library/scala/collection/mutable/Stack.scala +++ b/src/library/scala/collection/mutable/Stack.scala @@ -19,7 +19,7 @@ package scala.collection.mutable * @version 1.1, 03/05/2004 */ @serializable @cloneable -class Stack[A] extends MutableList[A] { +class Stack[A] extends MutableList[A] with CloneableCollection { /** Checks if the stack is empty. * |