summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/mutable/WrappedArray.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/library/scala/collection/mutable/WrappedArray.scala')
-rw-r--r--src/library/scala/collection/mutable/WrappedArray.scala71
1 files changed, 10 insertions, 61 deletions
diff --git a/src/library/scala/collection/mutable/WrappedArray.scala b/src/library/scala/collection/mutable/WrappedArray.scala
index d0919c4357..0b5ebe7e9a 100644
--- a/src/library/scala/collection/mutable/WrappedArray.scala
+++ b/src/library/scala/collection/mutable/WrappedArray.scala
@@ -85,26 +85,12 @@ extends AbstractSeq[T]
*/
override protected[this] def newBuilder: Builder[T, WrappedArray[T]] =
new WrappedArrayBuilder[T](elemTag)
-}
-
-private[mutable] abstract class WrappedArrayImpl[T] extends WrappedArray[T] {
- override def slice(from: Int, until: Int): WrappedArray[T] = {
- val start = if (from < 0) 0 else from
- if (until <= start || start >= repr.length)
- return emptyImpl
- val end = if (until > length) length else until
- sliceImpl(start, end)
- }
- protected def emptyImpl: WrappedArray[T]
-
- protected def sliceImpl(from: Int, until: Int): WrappedArray[T]
}
/** A companion object used to create instances of `WrappedArray`.
*/
object WrappedArray {
- import java.util
// This is reused for all calls to empty.
private val EmptyWrappedArray = new ofRef[AnyRef](new Array[AnyRef](0))
def empty[T <: AnyRef]: WrappedArray[T] = EmptyWrappedArray.asInstanceOf[WrappedArray[T]]
@@ -138,17 +124,7 @@ object WrappedArray {
def newBuilder[A]: Builder[A, IndexedSeq[A]] = new ArrayBuffer
- private val emptyWrappedByte = new ofByte(new Array[Byte](0))
- private val emptyWrappedShort = new ofShort(new Array[Short](0))
- private val emptyWrappedInt = new ofInt(new Array[Int](0))
- private val emptyWrappedLong = new ofLong(new Array[Long](0))
- private val emptyWrappedFloat = new ofFloat(new Array[Float](0))
- private val emptyWrappedDouble = new ofDouble(new Array[Double](0))
- private val emptyWrappedUnit = new ofUnit(new Array[Unit](0))
- private val emptyWrappedChar = new ofChar(new Array[Char](0))
- private val emptyWrappedBoolean = new ofBoolean(new Array[Boolean](0))
-
- final class ofRef[T <: AnyRef](val array: Array[T]) extends WrappedArrayImpl[T] with Serializable {
+ final class ofRef[T <: AnyRef](val array: Array[T]) extends WrappedArray[T] with Serializable {
lazy val elemTag = ClassTag[T](array.getClass.getComponentType)
def length: Int = array.length
def apply(index: Int): T = array(index).asInstanceOf[T]
@@ -158,11 +134,9 @@ object WrappedArray {
case that: ofRef[_] => Arrays.equals(array.asInstanceOf[Array[AnyRef]], that.array.asInstanceOf[Array[AnyRef]])
case _ => super.equals(that)
}
- protected override def emptyImpl = new ofRef(util.Arrays.copyOf[T](array,0))
- protected override def sliceImpl(from: Int, until: Int) = new ofRef[T](util.Arrays.copyOfRange[T](array, from, until))
}
- final class ofByte(val array: Array[Byte]) extends WrappedArrayImpl[Byte] with Serializable {
+ final class ofByte(val array: Array[Byte]) extends WrappedArray[Byte] with Serializable {
def elemTag = ClassTag.Byte
def length: Int = array.length
def apply(index: Int): Byte = array(index)
@@ -172,11 +146,9 @@ object WrappedArray {
case that: ofByte => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
- protected override def emptyImpl = emptyWrappedByte
- protected override def sliceImpl(from: Int, until: Int) = new ofByte(util.Arrays.copyOfRange(array, from, until))
}
- final class ofShort(val array: Array[Short]) extends WrappedArrayImpl[Short] with Serializable {
+ final class ofShort(val array: Array[Short]) extends WrappedArray[Short] with Serializable {
def elemTag = ClassTag.Short
def length: Int = array.length
def apply(index: Int): Short = array(index)
@@ -186,11 +158,9 @@ object WrappedArray {
case that: ofShort => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
- protected override def emptyImpl = emptyWrappedShort
- protected override def sliceImpl(from: Int, until: Int) = new ofShort(util.Arrays.copyOfRange(array, from, until))
}
- final class ofChar(val array: Array[Char]) extends WrappedArrayImpl[Char] with Serializable {
+ final class ofChar(val array: Array[Char]) extends WrappedArray[Char] with Serializable {
def elemTag = ClassTag.Char
def length: Int = array.length
def apply(index: Int): Char = array(index)
@@ -200,11 +170,9 @@ object WrappedArray {
case that: ofChar => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
- protected override def emptyImpl = emptyWrappedChar
- protected override def sliceImpl(from: Int, until: Int) = new ofChar(util.Arrays.copyOfRange(array, from, until))
}
- final class ofInt(val array: Array[Int]) extends WrappedArrayImpl[Int] with Serializable {
+ final class ofInt(val array: Array[Int]) extends WrappedArray[Int] with Serializable {
def elemTag = ClassTag.Int
def length: Int = array.length
def apply(index: Int): Int = array(index)
@@ -214,11 +182,9 @@ object WrappedArray {
case that: ofInt => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
- protected override def emptyImpl = emptyWrappedInt
- protected override def sliceImpl(from: Int, until: Int) = new ofInt(util.Arrays.copyOfRange(array, from, until))
}
- final class ofLong(val array: Array[Long]) extends WrappedArrayImpl[Long] with Serializable {
+ final class ofLong(val array: Array[Long]) extends WrappedArray[Long] with Serializable {
def elemTag = ClassTag.Long
def length: Int = array.length
def apply(index: Int): Long = array(index)
@@ -228,11 +194,9 @@ object WrappedArray {
case that: ofLong => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
- protected override def emptyImpl = emptyWrappedLong
- protected override def sliceImpl(from: Int, until: Int) = new ofLong(util.Arrays.copyOfRange(array, from, until))
}
- final class ofFloat(val array: Array[Float]) extends WrappedArrayImpl[Float] with Serializable {
+ final class ofFloat(val array: Array[Float]) extends WrappedArray[Float] with Serializable {
def elemTag = ClassTag.Float
def length: Int = array.length
def apply(index: Int): Float = array(index)
@@ -242,11 +206,9 @@ object WrappedArray {
case that: ofFloat => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
- protected override def emptyImpl = emptyWrappedFloat
- protected override def sliceImpl(from: Int, until: Int) = new ofFloat(util.Arrays.copyOfRange(array, from, until))
}
- final class ofDouble(val array: Array[Double]) extends WrappedArrayImpl[Double] with Serializable {
+ final class ofDouble(val array: Array[Double]) extends WrappedArray[Double] with Serializable {
def elemTag = ClassTag.Double
def length: Int = array.length
def apply(index: Int): Double = array(index)
@@ -256,11 +218,9 @@ object WrappedArray {
case that: ofDouble => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
- protected override def emptyImpl = emptyWrappedDouble
- protected override def sliceImpl(from: Int, until: Int) = new ofDouble(util.Arrays.copyOfRange(array, from, until))
}
- final class ofBoolean(val array: Array[Boolean]) extends WrappedArrayImpl[Boolean] with Serializable {
+ final class ofBoolean(val array: Array[Boolean]) extends WrappedArray[Boolean] with Serializable {
def elemTag = ClassTag.Boolean
def length: Int = array.length
def apply(index: Int): Boolean = array(index)
@@ -270,11 +230,9 @@ object WrappedArray {
case that: ofBoolean => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
- protected override def emptyImpl = emptyWrappedBoolean
- protected override def sliceImpl(from: Int, until: Int) = new ofBoolean(util.Arrays.copyOfRange(array, from, until))
}
- final class ofUnit(val array: Array[Unit]) extends WrappedArrayImpl[Unit] with Serializable {
+ final class ofUnit(val array: Array[Unit]) extends WrappedArray[Unit] with Serializable {
def elemTag = ClassTag.Unit
def length: Int = array.length
def apply(index: Int): Unit = array(index)
@@ -284,14 +242,5 @@ object WrappedArray {
case that: ofUnit => array.length == that.array.length
case _ => super.equals(that)
}
- protected override def emptyImpl = emptyWrappedUnit
- protected override def sliceImpl(from: Int, until: Int) = {
- // cant use
- // new ofUnit(util.Arrays.copyOfRange[Unit](array, from, until)) - Unit is special and doesnt compile
- // cant use util.Arrays.copyOfRange[Unit](repr, from, until) - Unit is special and doesnt compile
- val res = new Array[Unit](until-from)
- System.arraycopy(repr, from, res, 0, until-from)
- new ofUnit(res)
- }
}
}