10000 Changed hashing bit rotation to use Integer.rotateRight · l0rinc/scala@bc91223 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc91223

Browse files
committed
Changed hashing bit rotation to use Integer.rotateRight
1 parent 0339663 commit bc91223

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

src/library/scala/collection/mutable/FlatHashTable.scala

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ package scala
1010
package collection
1111
package mutable
1212

13+
import java.lang.Integer.rotateRight
14+
import scala.util.hashing.byteswap32
15+
1316
/** An implementation class backing a `HashSet`.
1417
*
1518
* This trait is used internally. It can be mixed in with various collections relying on
@@ -415,20 +418,7 @@ private[collection] object FlatHashTable {
415418
// so that:
416419
protected final def sizeMapBucketSize = 1 << sizeMapBucketBitSize
417420

418-
protected final def improve(hcode: Int, seed: Int) = {
419-
//var h: Int = hcode + ~(hcode << 9)
420-
//h = h ^ (h >>> 14)
421-
//h = h + (h << 4)
422-
//h ^ (h >>> 10)
423-
424-
val improved= scala.util.hashing.byteswap32(hcode)
425-
426-
// for the remainder, see SI-5293
427-
// to ensure that different bits are used for different hash tables, we have to rotate based on the seed
428-
val rotation = seed % 32
429-
val rotated = (improved >>> rotation) | (improved << (32 - rotation))
430-
rotated
431-
}
421+
protected final def improve(hcode: Int, seed: Int) = rotateRight(byteswap32(hcode), seed)
432422

433423
/**
434424
* Elems have type A, but we store AnyRef in the table. Plus we need to deal with

src/library/scala/collection/mutable/HashTable.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ package scala
1212
package collection
1313
package mutable
1414

15+
import java.lang.Integer.rotateRight
16+
import scala.util.hashing.byteswap32
17+
1518
/** This class can be used to construct data structures that are based
1619
* on hashtables. Class `HashTable[A]` implements a hashtable
1720
* that maps keys of type `A` to values of the fully abstract
@@ -424,11 +427,7 @@ private[collection] object HashTable {
424427
* }}}
425428
* the rest of the computation is due to SI-5293
426429
*/
427-
protected final def improve(hcode: Int, seed: Int): Int = {
428-
val hash = scala.util.hashing.byteswap32(hcode)
429-
val shift = seed & ((1 << 5) - 1)
430-
(hash >>> shift) | (hash << (32 - shift))
431-
}
430+
protected final def improve(hcode: Int, seed: Int): Int = rotateRight(byteswap32(hcode), seed)
432431
}
433432

434433
/**

0 commit comments

Comments
 (0)
0