Skip to content

Commit

Permalink
Override equals and hashCode for WrappedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyr authored and adriaanm committed Dec 19, 2016
1 parent 56fb917 commit c5b46e9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
6 changes: 0 additions & 6 deletions bincompat-forward.whitelist.conf
Expand Up @@ -28,12 +28,6 @@ filter {
{
matchName="scala.reflect.io.PlainNioFile"
problemName=MissingClassProblem
},
# this one can be removed once there is a fix for
# https://github.com/typesafehub/migration-manager/issues/147
{
matchName="scala.collection.Iterator#Leading#1.trailer"
problemName=DirectMissingMethodProblem
}
]
}
54 changes: 54 additions & 0 deletions src/library/scala/collection/mutable/WrappedArray.scala
Expand Up @@ -13,8 +13,12 @@ package collection
package mutable

import scala.reflect.ClassTag
import scala.runtime.BoxedUnit
import scala.collection.generic._
import scala.collection.parallel.mutable.ParArray
import scala.util.hashing.MurmurHash3

import java.util.Arrays

/**
* A class representing `Array[T]`.
Expand Down Expand Up @@ -125,68 +129,118 @@ object WrappedArray {
def length: Int = array.length
def apply(index: Int): T = array(index).asInstanceOf[T]
def update(index: Int, elem: T) { array(index) = elem }
override def hashCode = MurmurHash3.wrappedArrayHash(array)
override def equals(that: Any) = that match {
case that: ofRef[_] => Arrays.equals(array.asInstanceOf[Array[AnyRef]], that.array.asInstanceOf[Array[AnyRef]])
case _ => super.equals(that)
}
}

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)
def update(index: Int, elem: Byte) { array(index) = elem }
override def hashCode = MurmurHash3.wrappedBytesHash(array)
override def equals(that: Any) = that match {
case that: ofByte => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
}

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)
def update(index: Int, elem: Short) { array(index) = elem }
override def hashCode = MurmurHash3.wrappedArrayHash(array)
override def equals(that: Any) = that match {
case that: ofShort => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
}

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)
def update(index: Int, elem: Char) { array(index) = elem }
override def hashCode = MurmurHash3.wrappedArrayHash(array)
override def equals(that: Any) = that match {
case that: ofChar => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
}

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)
def update(index: Int, elem: Int) { array(index) = elem }
override def hashCode = MurmurHash3.wrappedArrayHash(array)
override def equals(that: Any) = that match {
case that: ofInt => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
}

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)
def update(index: Int, elem: Long) { array(index) = elem }
override def hashCode = MurmurHash3.wrappedArrayHash(array)
override def equals(that: Any) = that match {
case that: ofLong => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
}

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)
def update(index: Int, elem: Float) { array(index) = elem }
override def hashCode = MurmurHash3.wrappedArrayHash(array)
override def equals(that: Any) = that match {
case that: ofFloat => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
}

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)
def update(index: Int, elem: Double) { array(index) = elem }
override def hashCode = MurmurHash3.wrappedArrayHash(array)
override def equals(that: Any) = that match {
case that: ofDouble => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
}

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)
def update(index: Int, elem: Boolean) { array(index) = elem }
override def hashCode = MurmurHash3.wrappedArrayHash(array)
override def equals(that: Any) = that match {
case that: ofBoolean => Arrays.equals(array, that.array)
case _ => super.equals(that)
}
}

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)
def update(index: Int, elem: Unit) { array(index) = elem }
override def hashCode = MurmurHash3.wrappedArrayHash(array)
override def equals(that: Any) = that match {
case that: ofUnit => array.length == that.array.length
case _ => super.equals(that)
}
}
}
3 changes: 3 additions & 0 deletions src/library/scala/util/hashing/MurmurHash3.scala
Expand Up @@ -212,6 +212,9 @@ object MurmurHash3 extends MurmurHash3 {
def stringHash(x: String): Int = stringHash(x, stringSeed)
def unorderedHash(xs: TraversableOnce[Any]): Int = unorderedHash(xs, traversableSeed)

private[scala] def wrappedArrayHash[@specialized T](a: Array[T]): Int = arrayHash(a, seqSeed)
private[scala] def wrappedBytesHash(data: Array[Byte]): Int = bytesHash(data, seqSeed)

/** To offer some potential for optimization.
*/
def seqHash(xs: scala.collection.Seq[_]): Int = xs match {
Expand Down

0 comments on commit c5b46e9

Please sign in to comment.