Skip to content

Commit 846ee2b

Browse files
author
Sunil Mishra
committed
[backport] 11330: Port fix for byte array hashcode
Issue 10690 fixed this issue for scala 2.13, and a similar fix has been implemented here. There are some additional changes over those in 10690, removing byte array hashing entirely. Hashing is now consistent in that it produces the same result regardless of the underlying numeric type, in so far as the numbers in question fit into the byte width of the type.
1 parent 329deac commit 846ee2b

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/library/scala/util/hashing/MurmurHash3.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,14 @@ object MurmurHash3 extends MurmurHash3 {
210210
final val setSeed = "Set".hashCode
211211

212212
def arrayHash[@specialized T](a: Array[T]): Int = arrayHash(a, arraySeed)
213-
def bytesHash(data: Array[Byte]): Int = bytesHash(data, arraySeed)
213+
def bytesHash(data: Array[Byte]): Int = arrayHash(data, arraySeed)
214214
def orderedHash(xs: TraversableOnce[Any]): Int = orderedHash(xs, symmetricSeed)
215215
def productHash(x: Product): Int = productHash(x, productSeed)
216216
def stringHash(x: String): Int = stringHash(x, stringSeed)
217217
def unorderedHash(xs: TraversableOnce[Any]): Int = unorderedHash(xs, traversableSeed)
218218

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

222222
/** To offer some potential for optimization.
223223
*/

test/junit/scala/collection/mutable/WrappedArrayTest.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package scala.collection.mutable
22

3+
import org.junit.Assert._
34
import org.junit.runner.RunWith
45
import org.junit.runners.JUnit4
56
import org.junit.Test
67

8+
import scala.collection.immutable
9+
710
@RunWith(classOf[JUnit4])
811
class WrappedArrayTest {
912
@Test
@@ -16,4 +19,12 @@ class WrappedArrayTest {
1619
assertOfRef(Array(Int.box(65)), Array(Char.box('A')))
1720
assertOfRef(Array(Char.box('A')), Array(Int.box(65)))
1821
}
22+
23+
@Test
24+
def byteArrayHashCodeEquality(): Unit = {
25+
val x = immutable.Seq[Byte](10)
26+
val y = Array[Byte](10).toSeq
27+
assertEquals(x, y)
28+
assertEquals(x.hashCode(), y.hashCode())
29+
}
1930
}

0 commit comments

Comments
 (0)