Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override equals and hashCode for WrappedArray #5607

Merged
merged 1 commit into from Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions bincompat-forward.whitelist.conf
Expand Up @@ -34,6 +34,14 @@ filter {
{
matchName="scala.collection.Iterator#Leading#1.trailer"
problemName=DirectMissingMethodProblem
},
{
matchName="scala.util.hashing.MurmurHash3.wrappedBytesHash"
problemName=DirectMissingMethodProblem
},
{
matchName="scala.util.hashing.MurmurHash3.wrappedArrayHash"
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