diff options
author | Martin Odersky <odersky@gmail.com> | 2006-05-05 15:30:46 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2006-05-05 15:30:46 +0000 |
commit | b1e41df94da49a57f382776c51acb2b0add80594 (patch) | |
tree | 70b1716caffadc7265f3fca755489feb1702de7c /src/library | |
parent | 12a9f76471f19c8b134b3ea5bba32042020d6ca6 (diff) | |
download | scala-b1e41df94da49a57f382776c51acb2b0add80594.tar.gz scala-b1e41df94da49a57f382776c51acb2b0add80594.tar.bz2 scala-b1e41df94da49a57f382776c51acb2b0add80594.zip |
Changed array unboxing to use classes rather th...
Changed array unboxing to use classes rather than strings.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/runtime/BoxedAnyArray.scala | 74 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedArray.scala | 2 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedBooleanArray.scala | 1 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedByteArray.scala | 1 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedCharArray.scala | 1 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedDoubleArray.scala | 1 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedFloatArray.scala | 1 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedIntArray.scala | 1 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedLongArray.scala | 1 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedObjectArray.scala | 1 | ||||
-rw-r--r-- | src/library/scala/runtime/BoxedShortArray.scala | 1 | ||||
-rw-r--r-- | src/library/scala/runtime/ScalaRunTime.scala | 3 |
12 files changed, 57 insertions, 31 deletions
diff --git a/src/library/scala/runtime/BoxedAnyArray.scala b/src/library/scala/runtime/BoxedAnyArray.scala index 92359d8555..a7f35023bf 100644 --- a/src/library/scala/runtime/BoxedAnyArray.scala +++ b/src/library/scala/runtime/BoxedAnyArray.scala @@ -21,26 +21,26 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { private var boxed = new Array[Object](length); private val hash = boxed.hashCode(); private var unboxed: Object = null; - private var elemTag: String = null; + private var elemClass: Class = null; def apply(index: Int): Object = synchronized { if (unboxed == null) boxed(index); - else if (elemTag eq ScalaRunTime.IntTag) + else if (elemClass eq ScalaRunTime.IntTYPE) BoxedInt.box(unboxed.asInstanceOf[Array[Int]](index)) - else if (elemTag eq ScalaRunTime.DoubleTag) + else if (elemClass eq ScalaRunTime.DoubleTYPE) BoxedDouble.box(unboxed.asInstanceOf[Array[Double]](index)) - else if (elemTag eq ScalaRunTime.FloatTag) + else if (elemClass eq ScalaRunTime.FloatTYPE) BoxedFloat.box(unboxed.asInstanceOf[Array[Float]](index)) - else if (elemTag eq ScalaRunTime.LongTag) + else if (elemClass eq ScalaRunTime.LongTYPE) BoxedLong.box(unboxed.asInstanceOf[Array[Long]](index)) - else if (elemTag eq ScalaRunTime.CharTag) + else if (elemClass eq ScalaRunTime.CharTYPE) BoxedChar.box(unboxed.asInstanceOf[Array[Char]](index)) - else if (elemTag eq ScalaRunTime.ByteTag) + else if (elemClass eq ScalaRunTime.ByteTYPE) BoxedByte.box(unboxed.asInstanceOf[Array[Byte]](index)) - else if (elemTag eq ScalaRunTime.ShortTag) + else if (elemClass eq ScalaRunTime.ShortTYPE) BoxedShort.box(unboxed.asInstanceOf[Array[Short]](index)) - else if (elemTag eq ScalaRunTime.BooleanTag) + else if (elemClass eq ScalaRunTime.BooleanTYPE) BoxedBoolean.box(unboxed.asInstanceOf[Array[Boolean]](index)) else unboxed.asInstanceOf[Array[Object]](index) @@ -49,30 +49,41 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { def update(index: Int, elem: Object): Unit = synchronized { if (unboxed == null) boxed(index) = elem; - else if (elemTag eq ScalaRunTime.IntTag) + else if (elemClass eq ScalaRunTime.IntTYPE) unboxed.asInstanceOf[Array[Int]](index) = elem.asInstanceOf[BoxedNumber].intValue() - else if (elemTag eq ScalaRunTime.DoubleTag) + else if (elemClass eq ScalaRunTime.DoubleTYPE) unboxed.asInstanceOf[Array[Double]](index) = elem.asInstanceOf[BoxedNumber].doubleValue() - else if (elemTag eq ScalaRunTime.FloatTag) + else if (elemClass eq ScalaRunTime.FloatTYPE) unboxed.asInstanceOf[Array[Float]](index) = elem.asInstanceOf[BoxedNumber].floatValue() - else if (elemTag eq ScalaRunTime.LongTag) + else if (elemClass eq ScalaRunTime.LongTYPE) unboxed.asInstanceOf[Array[Long]](index) = elem.asInstanceOf[BoxedNumber].longValue() - else if (elemTag eq ScalaRunTime.CharTag) + else if (elemClass eq ScalaRunTime.CharTYPE) unboxed.asInstanceOf[Array[Char]](index) = elem.asInstanceOf[BoxedNumber].charValue() - else if (elemTag eq ScalaRunTime.ByteTag) + else if (elemClass eq ScalaRunTime.ByteTYPE) unboxed.asInstanceOf[Array[Byte]](index) = elem.asInstanceOf[BoxedNumber].byteValue() - else if (elemTag eq ScalaRunTime.ShortTag) + else if (elemClass eq ScalaRunTime.ShortTYPE) unboxed.asInstanceOf[Array[Short]](index) = elem.asInstanceOf[BoxedNumber].shortValue() - else if (elemTag eq ScalaRunTime.BooleanTag) + else if (elemClass eq ScalaRunTime.BooleanTYPE) unboxed.asInstanceOf[Array[Boolean]](index) = elem.asInstanceOf[BoxedBoolean].value else unboxed.asInstanceOf[Array[Object]](index) = elem } - def unbox(elemTag: String): Object = synchronized { + def unbox(elemTag: String): Object = + if (elemTag eq ScalaRunTime.IntTag) unbox(ScalaRunTime.IntTYPE) + else if (elemTag eq ScalaRunTime.DoubleTag) unbox(ScalaRunTime.DoubleTYPE) + else if (elemTag eq ScalaRunTime.FloatTag) unbox(ScalaRunTime.FloatTYPE) + else if (elemTag eq ScalaRunTime.LongTag) unbox(ScalaRunTime.LongTYPE) + else if (elemTag eq ScalaRunTime.CharTag) unbox(ScalaRunTime.CharTYPE) + else if (elemTag eq ScalaRunTime.ByteTag) unbox(ScalaRunTime.ByteTYPE) + else if (elemTag eq ScalaRunTime.ShortTag) unbox(ScalaRunTime.ShortTYPE) + else if (elemTag eq ScalaRunTime.BooleanTag) unbox(ScalaRunTime.BooleanTYPE) + else unbox(Class.forName(elemTag)) + + def unbox(elemClass: Class): Object = synchronized { if (unboxed == null) { - this.elemTag = elemTag; - if (elemTag eq ScalaRunTime.IntTag) { + this.elemClass = elemClass; + if (elemClass eq ScalaRunTime.IntTYPE) { val newvalue = new Array[Int](length); var i = 0; while (i < length) { @@ -81,7 +92,7 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { i = i + 1 } unboxed = newvalue; - } else if (elemTag eq ScalaRunTime.DoubleTag) { + } else if (elemClass eq ScalaRunTime.DoubleTYPE) { val newvalue = new Array[Double](length); var i = 0; while (i < length) { @@ -90,7 +101,7 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { i = i + 1 } unboxed = newvalue; - } else if (elemTag eq ScalaRunTime.FloatTag) { + } else if (elemClass eq ScalaRunTime.FloatTYPE) { val newvalue = new Array[Float](length); var i = 0; while (i < length) { @@ -99,7 +110,7 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { i = i + 1 } unboxed = newvalue; - } else if (elemTag eq ScalaRunTime.LongTag) { + } else if (elemClass eq ScalaRunTime.LongTYPE) { val newvalue = new Array[Long](length); var i = 0; while (i < length) { @@ -108,7 +119,7 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { i = i + 1 } unboxed = newvalue; - } else if (elemTag eq ScalaRunTime.CharTag) { + } else if (elemClass eq ScalaRunTime.CharTYPE) { val newvalue = new Array[Char](length); var i = 0; while (i < length) { @@ -117,7 +128,7 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { i = i + 1 } unboxed = newvalue; - } else if (elemTag eq ScalaRunTime.ByteTag) { + } else if (elemClass eq ScalaRunTime.ByteTYPE) { val newvalue = new Array[Byte](length); var i = 0; while (i < length) { @@ -126,7 +137,7 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { i = i + 1 } unboxed = newvalue; - } else if (elemTag eq ScalaRunTime.ShortTag) { + } else if (elemClass eq ScalaRunTime.ShortTYPE) { val newvalue = new Array[Short](length); var i = 0; while (i < length) { @@ -135,7 +146,7 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { i = i + 1 } unboxed = newvalue; - } else if (elemTag eq ScalaRunTime.BooleanTag) { + } else if (elemClass eq ScalaRunTime.BooleanTYPE) { val newvalue = new Array[Boolean](length); var i = 0; while (i < length) { @@ -144,10 +155,11 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { i = i + 1 } unboxed = newvalue; - } else if (elemTag == boxed.getClass().getComponentType()) { + } else if (elemClass == boxed.getClass().getComponentType()) { + // todo: replace with ScalaRunTime.Object.class unboxed = boxed; } else { - unboxed = java.lang.reflect.Array.newInstance(Class.forName(elemTag), length); + unboxed = java.lang.reflect.Array.newInstance(elemClass, length); System.arraycopy(boxed, 0, unboxed, 0, length); } boxed = null @@ -174,7 +186,7 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { if (that.unboxed == null) { that.boxed } else { - if (ScalaRunTime.isValueTag(that.elemTag)) unbox(that.elemTag); + if (ScalaRunTime.isValueClass(that.elemClass)) unbox(that.elemClass); that.unboxed } case that: BoxedArray => @@ -202,7 +214,7 @@ final class BoxedAnyArray(val length: Int) extends BoxedArray { other match { case that: BoxedAnyArray => if (that.unboxed != null) that.unboxed - else if (ScalaRunTime.isValueTag(this.elemTag)) that.unbox(this.elemTag) + else if (ScalaRunTime.isValueClass(this.elemClass)) that.unbox(this.elemClass) else that.boxed case that: BoxedArray => adapt(that.value) diff --git a/src/library/scala/runtime/BoxedArray.scala b/src/library/scala/runtime/BoxedArray.scala index 8309143095..b55fe83de2 100644 --- a/src/library/scala/runtime/BoxedArray.scala +++ b/src/library/scala/runtime/BoxedArray.scala @@ -27,6 +27,8 @@ abstract class BoxedArray extends PartialFunction[Int, Object] with Seq[Object] */ def unbox(elemTag: String): Object + def unbox(elemClass: Class): Object + override def isDefinedAt(x: Int): Boolean = 0 <= x && x < length def elements = new Iterator[Object] { diff --git a/src/library/scala/runtime/BoxedBooleanArray.scala b/src/library/scala/runtime/BoxedBooleanArray.scala index a157047652..348be7010e 100644 --- a/src/library/scala/runtime/BoxedBooleanArray.scala +++ b/src/library/scala/runtime/BoxedBooleanArray.scala @@ -24,6 +24,7 @@ final class BoxedBooleanArray(val value: Array[Boolean]) extends BoxedArray { } def unbox(elemTag: String): Object = value; + def unbox(elemClass: Class): Object = value; override def equals(other: Any) = ( value == other || diff --git a/src/library/scala/runtime/BoxedByteArray.scala b/src/library/scala/runtime/BoxedByteArray.scala index d5195da463..3f85f6a38b 100644 --- a/src/library/scala/runtime/BoxedByteArray.scala +++ b/src/library/scala/runtime/BoxedByteArray.scala @@ -24,6 +24,7 @@ final class BoxedByteArray(val value: Array[Byte]) extends BoxedArray { } def unbox(elemTag: String): Object = value; + def unbox(elemClass: Class): Object = value; override def equals(other: Any) = ( value == other || diff --git a/src/library/scala/runtime/BoxedCharArray.scala b/src/library/scala/runtime/BoxedCharArray.scala index bcf42c8d7b..95a6a4476a 100644 --- a/src/library/scala/runtime/BoxedCharArray.scala +++ b/src/library/scala/runtime/BoxedCharArray.scala @@ -24,6 +24,7 @@ final class BoxedCharArray(val value: Array[Char]) extends BoxedArray { } def unbox(elemTag: String): Object = value; + def unbox(elemClass: Class): Object = value; override def equals(other: Any) = ( value == other || diff --git a/src/library/scala/runtime/BoxedDoubleArray.scala b/src/library/scala/runtime/BoxedDoubleArray.scala index 7affbd86d3..4905d12ed8 100644 --- a/src/library/scala/runtime/BoxedDoubleArray.scala +++ b/src/library/scala/runtime/BoxedDoubleArray.scala @@ -24,6 +24,7 @@ final class BoxedDoubleArray(val value: Array[Double]) extends BoxedArray { } def unbox(elemTag: String): Object = value; + def unbox(elemClass: Class): Object = value; override def equals(other: Any) = ( value == other || diff --git a/src/library/scala/runtime/BoxedFloatArray.scala b/src/library/scala/runtime/BoxedFloatArray.scala index cd1887408f..c88ddacb67 100644 --- a/src/library/scala/runtime/BoxedFloatArray.scala +++ b/src/library/scala/runtime/BoxedFloatArray.scala @@ -24,6 +24,7 @@ final class BoxedFloatArray(val value: Array[Float]) extends BoxedArray { } def unbox(elemTag: String): Object = value; + def unbox(elemClass: Class): Object = value; override def equals(other: Any) = ( value == other || diff --git a/src/library/scala/runtime/BoxedIntArray.scala b/src/library/scala/runtime/BoxedIntArray.scala index ebd51114af..cc876e9ea9 100644 --- a/src/library/scala/runtime/BoxedIntArray.scala +++ b/src/library/scala/runtime/BoxedIntArray.scala @@ -24,6 +24,7 @@ final class BoxedIntArray(val value: Array[Int]) extends BoxedArray { } def unbox(elemTag: String): Object = value; + def unbox(elemClass: Class): Object = value; override def equals(other: Any) = ( value == other || diff --git a/src/library/scala/runtime/BoxedLongArray.scala b/src/library/scala/runtime/BoxedLongArray.scala index 8eb83ee10c..6c7f43c2e9 100644 --- a/src/library/scala/runtime/BoxedLongArray.scala +++ b/src/library/scala/runtime/BoxedLongArray.scala @@ -24,6 +24,7 @@ final class BoxedLongArray(val value: Array[Long]) extends BoxedArray { } def unbox(elemTag: String): Object = value; + def unbox(elemClass: Class): Object = value; override def equals(other: Any) = ( value == other || diff --git a/src/library/scala/runtime/BoxedObjectArray.scala b/src/library/scala/runtime/BoxedObjectArray.scala index 25c4d739c5..3d1b2d8c46 100644 --- a/src/library/scala/runtime/BoxedObjectArray.scala +++ b/src/library/scala/runtime/BoxedObjectArray.scala @@ -22,6 +22,7 @@ final class BoxedObjectArray(val value: Array[Object]) extends BoxedArray { def update(index: Int, elem: Object): Unit = { value(index) = elem } def unbox(elemTag: String): Object = value; + def unbox(elemClass: Class): Object = value; override def equals(other: Any): Boolean = ( value == other || diff --git a/src/library/scala/runtime/BoxedShortArray.scala b/src/library/scala/runtime/BoxedShortArray.scala index 796d82957f..47ead80af4 100644 --- a/src/library/scala/runtime/BoxedShortArray.scala +++ b/src/library/scala/runtime/BoxedShortArray.scala @@ -24,6 +24,7 @@ final class BoxedShortArray(val value: Array[Short]) extends BoxedArray { } def unbox(elemTag: String): Object = value; + def unbox(elemClass: Class): Object = value; override def equals(other: Any) = ( value == other || diff --git a/src/library/scala/runtime/ScalaRunTime.scala b/src/library/scala/runtime/ScalaRunTime.scala index e5a87e512f..b3ec5496d1 100644 --- a/src/library/scala/runtime/ScalaRunTime.scala +++ b/src/library/scala/runtime/ScalaRunTime.scala @@ -35,6 +35,7 @@ object ScalaRunTime { val UnitTYPE = java.lang.Void.TYPE def isValueTag(tag: String) = tag.charAt(0) == '.' + def isValueClass(clazz: Class) = clazz.isPrimitive() abstract class Try[a] { def Catch[b >: a](handler: PartialFunction[Throwable, b]): b; @@ -115,6 +116,8 @@ 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 arrayValue (x: BoxedArray, elemClass: Class): Object = + if (x eq null) null else x.unbox(elemClass); def boxArray(value: Object): BoxedArray = value match { case x: Array[Byte] => new BoxedByteArray(x) |