From 6bbf35496c2cb4f4b4fa55ef80b4eaa85e0de63f Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Fri, 1 May 2015 15:43:06 +0200 Subject: Implement clone on VCArrays without mutable field. --- src/dotty/runtime/vc/VCBooleanPrototype.scala | 11 ++++++----- src/dotty/runtime/vc/VCBytePrototype.scala | 13 +++++++------ src/dotty/runtime/vc/VCCharPrototype.scala | 13 +++++++------ src/dotty/runtime/vc/VCDoublePrototype.scala | 12 +++++++----- src/dotty/runtime/vc/VCFloatPrototype.scala | 12 +++++++----- src/dotty/runtime/vc/VCIntPrototype.scala | 12 +++++++----- src/dotty/runtime/vc/VCLongPrototype.scala | 12 +++++++----- src/dotty/runtime/vc/VCObjectPrototype.scala | 12 +++++++----- src/dotty/runtime/vc/VCShortPrototype.scala | 12 +++++++----- 9 files changed, 62 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/dotty/runtime/vc/VCBooleanPrototype.scala b/src/dotty/runtime/vc/VCBooleanPrototype.scala index c647eaeae..a651ee820 100644 --- a/src/dotty/runtime/vc/VCBooleanPrototype.scala +++ b/src/dotty/runtime/vc/VCBooleanPrototype.scala @@ -34,8 +34,11 @@ abstract class VCBooleanCompanion[T <: VCBooleanPrototype] extends ClassTag[T] { def productPrefix$extension(underlying: Boolean): String } -final class VCBooleanArray[T <: VCBooleanPrototype](val ct: VCBooleanCompanion[T], sz: Int) extends VCArrayPrototype[T] { - var arr = new Array[Boolean](sz) // mutable for clone() +final class VCBooleanArray[T <: VCBooleanPrototype] private (val arr: Array[Boolean], val ct: VCBooleanCompanion[T]) + extends VCArrayPrototype[T] { + def this(ct: VCBooleanCompanion[T], sz: Int) = + this(new Array[Boolean](sz), ct) + def apply(idx: Int) = ct.box(arr(idx)) def update(idx: Int, elem: T) = @@ -43,9 +46,7 @@ final class VCBooleanArray[T <: VCBooleanPrototype](val ct: VCBooleanCompanion[T def length: Int = arr.length override def clone(): VCBooleanArray[T] = { - val t = super.clone().asInstanceOf[VCBooleanArray[T]] - t.arr = this.arr.clone() - t + new VCBooleanArray[T](arr.clone(), ct) } override def toString: String = { diff --git a/src/dotty/runtime/vc/VCBytePrototype.scala b/src/dotty/runtime/vc/VCBytePrototype.scala index bf21a1cfb..dafd43815 100644 --- a/src/dotty/runtime/vc/VCBytePrototype.scala +++ b/src/dotty/runtime/vc/VCBytePrototype.scala @@ -27,14 +27,18 @@ abstract class VCByteCompanion[T <: VCBytePrototype] extends ClassTag[T] { override def newArray(len: Int): Array[T] = new VCByteArray(this, len).asInstanceOf[Array[T]] + final def _1$extension(underlying: Byte) = underlying final def hashCode$extension(underlying: Byte) = underlying.hashCode() final def toString$extension(underlying: Byte) = s"${productPrefix$extension(underlying)}($underlying)" def productPrefix$extension(underlying: Byte): String } -final class VCByteArray[T <: VCBytePrototype](val ct: VCByteCompanion[T], sz: Int) extends VCArrayPrototype[T] { - var arr = new Array[Byte](sz) // mutable for clone +final class VCByteArray[T <: VCBytePrototype] private (val arr: Array[Byte], val ct: VCByteCompanion[T]) + extends VCArrayPrototype[T] { + def this(ct: VCByteCompanion[T], sz: Int) = + this(new Array[Byte](sz), ct) + def apply(idx: Int) = ct.box(arr(idx)) def update(idx: Int, elem: T) = @@ -42,12 +46,9 @@ final class VCByteArray[T <: VCBytePrototype](val ct: VCByteCompanion[T], sz: In def length: Int = arr.length override def clone(): VCByteArray[T] = { - val t = super.clone().asInstanceOf[VCByteArray[T]] - t.arr = this.arr.clone() - t + new VCByteArray[T](arr.clone(), ct) } - override def toString: String = { "[" + ct.runtimeClass } diff --git a/src/dotty/runtime/vc/VCCharPrototype.scala b/src/dotty/runtime/vc/VCCharPrototype.scala index 5b6aae2e4..338e9c132 100644 --- a/src/dotty/runtime/vc/VCCharPrototype.scala +++ b/src/dotty/runtime/vc/VCCharPrototype.scala @@ -27,14 +27,18 @@ abstract class VCCharCompanion[T <: VCCharPrototype] extends ClassTag[T] { override def newArray(len: Int): Array[T] = new VCCharArray(this, len).asInstanceOf[Array[T]] + final def _1$extension(underlying: Char) = underlying final def hashCode$extension(underlying: Char) = underlying.hashCode() final def toString$extension(underlying: Char) = s"${productPrefix$extension(underlying)}($underlying)" def productPrefix$extension(underlying: Char): String } -final class VCCharArray[T <: VCCharPrototype](val ct: VCCharCompanion[T], sz: Int) extends VCArrayPrototype[T] { - var arr = new Array[Char](sz) // mutable for clone +final class VCCharArray[T <: VCCharPrototype] private (val arr: Array[Char], val ct: VCCharCompanion[T]) + extends VCArrayPrototype[T] { + def this(ct: VCCharCompanion[T], sz: Int) = + this(new Array[Char](sz), ct) + def apply(idx: Int) = ct.box(arr(idx)) def update(idx: Int, elem: T) = @@ -42,12 +46,9 @@ final class VCCharArray[T <: VCCharPrototype](val ct: VCCharCompanion[T], sz: In def length: Int = arr.length override def clone(): VCCharArray[T] = { - val t = super.clone().asInstanceOf[VCCharArray[T]] - t.arr = this.arr.clone() - t + new VCCharArray[T](arr.clone(), ct) } - override def toString: String = { "[" + ct.runtimeClass } diff --git a/src/dotty/runtime/vc/VCDoublePrototype.scala b/src/dotty/runtime/vc/VCDoublePrototype.scala index 047b4cbe4..f960acd4e 100644 --- a/src/dotty/runtime/vc/VCDoublePrototype.scala +++ b/src/dotty/runtime/vc/VCDoublePrototype.scala @@ -27,14 +27,18 @@ abstract class VCDoubleCompanion[T <: VCDoublePrototype] extends ClassTag[T] { override def newArray(len: Int): Array[T] = new VCDoubleArray(this, len).asInstanceOf[Array[T]] + final def _1$extension(underlying: Double) = underlying final def hashCode$extension(underlying: Double) = underlying.hashCode() final def toString$extension(underlying: Double) = s"${productPrefix$extension(underlying)}($underlying)" def productPrefix$extension(underlying: Double): String } -final class VCDoubleArray[T <: VCDoublePrototype](val ct: VCDoubleCompanion[T], sz: Int) extends VCArrayPrototype[T] { - var arr = new Array[Double](sz) // mutable for clone +final class VCDoubleArray[T <: VCDoublePrototype] private (val arr: Array[Double], val ct: VCDoubleCompanion[T]) + extends VCArrayPrototype[T] { + def this(ct: VCDoubleCompanion[T], sz: Int) = + this(new Array[Double](sz), ct) + def apply(idx: Int) = ct.box(arr(idx)) def update(idx: Int, elem: T) = @@ -42,9 +46,7 @@ final class VCDoubleArray[T <: VCDoublePrototype](val ct: VCDoubleCompanion[T], def length: Int = arr.length override def clone(): VCDoubleArray[T] = { - val t = super.clone().asInstanceOf[VCDoubleArray[T]] - t.arr = this.arr.clone() - t + new VCDoubleArray[T](arr.clone(), ct) } override def toString: String = { diff --git a/src/dotty/runtime/vc/VCFloatPrototype.scala b/src/dotty/runtime/vc/VCFloatPrototype.scala index 09cefbd9e..d2a085d86 100644 --- a/src/dotty/runtime/vc/VCFloatPrototype.scala +++ b/src/dotty/runtime/vc/VCFloatPrototype.scala @@ -27,14 +27,18 @@ abstract class VCFloatCompanion[T <: VCFloatPrototype] extends ClassTag[T] { override def newArray(len: Int): Array[T] = new VCFloatArray(this, len).asInstanceOf[Array[T]] + final def _1$extension(underlying: Float) = underlying final def hashCode$extension(underlying: Float) = underlying.hashCode() final def toString$extension(underlying: Float) = s"${productPrefix$extension(underlying)}($underlying)" def productPrefix$extension(underlying: Float): String } -final class VCFloatArray[T <: VCFloatPrototype](val ct: VCFloatCompanion[T], sz: Int) extends VCArrayPrototype[T] { - var arr = new Array[Float](sz) // mutable for clone +final class VCFloatArray[T <: VCFloatPrototype] private (val arr: Array[Float], val ct: VCFloatCompanion[T]) + extends VCArrayPrototype[T] { + def this(ct: VCFloatCompanion[T], sz: Int) = + this(new Array[Float](sz), ct) + def apply(idx: Int) = ct.box(arr(idx)) def update(idx: Int, elem: T) = @@ -42,9 +46,7 @@ final class VCFloatArray[T <: VCFloatPrototype](val ct: VCFloatCompanion[T], sz: def length: Int = arr.length override def clone(): VCFloatArray[T] = { - val t = super.clone().asInstanceOf[VCFloatArray[T]] - t.arr = this.arr.clone() - t + new VCFloatArray[T](arr.clone(), ct) } override def toString: String = { diff --git a/src/dotty/runtime/vc/VCIntPrototype.scala b/src/dotty/runtime/vc/VCIntPrototype.scala index cc781efcf..bd788026e 100644 --- a/src/dotty/runtime/vc/VCIntPrototype.scala +++ b/src/dotty/runtime/vc/VCIntPrototype.scala @@ -27,14 +27,18 @@ abstract class VCIntCompanion[T <: VCIntPrototype] extends ClassTag[T] { override def newArray(len: Int): Array[T] = new VCIntArray(this, len).asInstanceOf[Array[T]] + final def _1$extension(underlying: Int) = underlying final def hashCode$extension(underlying: Int) = underlying.hashCode() final def toString$extension(underlying: Int) = s"${productPrefix$extension(underlying)}($underlying)" def productPrefix$extension(underlying: Int): String } -final class VCIntArray[T <: VCIntPrototype](val ct: VCIntCompanion[T], sz: Int) extends VCArrayPrototype[T] { - var arr = new Array[Int](sz) // mutable for clone +final class VCIntArray[T <: VCIntPrototype] private (val arr: Array[Int], val ct: VCIntCompanion[T]) + extends VCArrayPrototype[T] { + def this(ct: VCIntCompanion[T], sz: Int) = + this(new Array[Int](sz), ct) + def apply(idx: Int) = ct.box(arr(idx)) def update(idx: Int, elem: T) = @@ -42,9 +46,7 @@ final class VCIntArray[T <: VCIntPrototype](val ct: VCIntCompanion[T], sz: Int) def length: Int = arr.length override def clone(): VCIntArray[T] = { - val t = super.clone().asInstanceOf[VCIntArray[T]] - t.arr = this.arr.clone() - t + new VCIntArray[T](arr.clone(), ct) } override def toString: String = { diff --git a/src/dotty/runtime/vc/VCLongPrototype.scala b/src/dotty/runtime/vc/VCLongPrototype.scala index 4eecddd9b..eb7e5a6d2 100644 --- a/src/dotty/runtime/vc/VCLongPrototype.scala +++ b/src/dotty/runtime/vc/VCLongPrototype.scala @@ -27,14 +27,18 @@ abstract class VCLongCompanion[T <: VCLongPrototype] extends ClassTag[T] { override def newArray(len: Int): Array[T] = new VCLongArray(this, len).asInstanceOf[Array[T]] + final def _1$extension(underlying: Long) = underlying final def hashCode$extension(underlying: Long) = underlying.hashCode() final def toString$extension(underlying: Long) = s"${productPrefix$extension(underlying)}($underlying)" def productPrefix$extension(underlying: Long): String } -final class VCLongArray[T <: VCLongPrototype](val ct: VCLongCompanion[T], sz: Int) extends VCArrayPrototype[T] { - var arr = new Array[Long](sz) // mutable for clone +final class VCLongArray[T <: VCLongPrototype] private (val arr: Array[Long], val ct: VCLongCompanion[T]) + extends VCArrayPrototype[T] { + def this(ct: VCLongCompanion[T], sz: Int) = + this(new Array[Long](sz), ct) + def apply(idx: Int) = ct.box(arr(idx)) def update(idx: Int, elem: T) = @@ -42,9 +46,7 @@ final class VCLongArray[T <: VCLongPrototype](val ct: VCLongCompanion[T], sz: In def length: Int = arr.length override def clone(): VCLongArray[T] = { - val t = super.clone().asInstanceOf[VCLongArray[T]] - t.arr = this.arr.clone() - t + new VCLongArray[T](arr.clone(), ct) } override def toString: String = { diff --git a/src/dotty/runtime/vc/VCObjectPrototype.scala b/src/dotty/runtime/vc/VCObjectPrototype.scala index ef2c0f0aa..01a0da5ee 100644 --- a/src/dotty/runtime/vc/VCObjectPrototype.scala +++ b/src/dotty/runtime/vc/VCObjectPrototype.scala @@ -27,14 +27,18 @@ abstract class VCObjectCompanion[T <: VCObjectPrototype] extends ClassTag[T] { override def newArray(len: Int): Array[T] = new VCObjectArray(this, len).asInstanceOf[Array[T]] + final def _1$extension(underlying: Object) = underlying final def hashCode$extension(underlying: Object) = underlying.hashCode() final def toString$extension(underlying: Object) = s"${productPrefix$extension(underlying)}($underlying)" def productPrefix$extension(underlying: Object): String } -final class VCObjectArray[T <: VCObjectPrototype](val ct: VCObjectCompanion[T], sz: Int) extends VCArrayPrototype[T] { - var arr = new Array[Object](sz) // mutable for clone +final class VCObjectArray[T <: VCObjectPrototype] private (val arr: Array[Object], val ct: VCObjectCompanion[T]) + extends VCArrayPrototype[T] { + def this(ct: VCObjectCompanion[T], sz: Int) = + this(new Array[Object](sz), ct) + def apply(idx: Int) = ct.box(arr(idx)) def update(idx: Int, elem: T) = @@ -42,9 +46,7 @@ final class VCObjectArray[T <: VCObjectPrototype](val ct: VCObjectCompanion[T], def length: Int = arr.length override def clone(): VCObjectArray[T] = { - val t = super.clone().asInstanceOf[VCObjectArray[T]] - t.arr = this.arr.clone() - t + new VCObjectArray[T](arr.clone(), ct) } override def toString: String = { diff --git a/src/dotty/runtime/vc/VCShortPrototype.scala b/src/dotty/runtime/vc/VCShortPrototype.scala index 255f85ca9..94bf36908 100644 --- a/src/dotty/runtime/vc/VCShortPrototype.scala +++ b/src/dotty/runtime/vc/VCShortPrototype.scala @@ -27,14 +27,18 @@ abstract class VCShortCompanion[T <: VCShortPrototype] extends ClassTag[T] { override def newArray(len: Int): Array[T] = new VCShortArray(this, len).asInstanceOf[Array[T]] + final def _1$extension(underlying: Short) = underlying final def hashCode$extension(underlying: Short) = underlying.hashCode() final def toString$extension(underlying: Short) = s"${productPrefix$extension(underlying)}($underlying)" def productPrefix$extension(underlying: Short): String } -final class VCShortArray[T <: VCShortPrototype](val ct: VCShortCompanion[T], sz: Int) extends VCArrayPrototype[T] { - var arr = new Array[Short](sz) // mutable for clone +final class VCShortArray[T <: VCShortPrototype] private (val arr: Array[Short], val ct: VCShortCompanion[T]) + extends VCArrayPrototype[T] { + def this(ct: VCShortCompanion[T], sz: Int) = + this(new Array[Short](sz), ct) + def apply(idx: Int) = ct.box(arr(idx)) def update(idx: Int, elem: T) = @@ -42,9 +46,7 @@ final class VCShortArray[T <: VCShortPrototype](val ct: VCShortCompanion[T], sz: def length: Int = arr.length override def clone(): VCShortArray[T] = { - val t = super.clone().asInstanceOf[VCShortArray[T]] - t.arr = this.arr.clone() - t + new VCShortArray[T](arr.clone(), ct) } override def toString: String = { -- cgit v1.2.3