File tree Expand file tree Collapse file tree 4 files changed +548
-0
lines changed
library/src/scala/util/hashing Expand file tree Collapse file tree 4 files changed +548
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Scala (https://www.scala-lang.org)
3
+ *
4
+ * Copyright EPFL and Lightbend, Inc. dba Akka
5
+ *
6
+ * Licensed under Apache License 2.0
7
+ * (http://www.apache.org/licenses/LICENSE-2.0).
8
+ *
9
+ * See the NOTICE file distributed with this work for
10
+ * additional information regarding copyright ownership.
11
+ */
12
+
13
+ package scala
14
+ package util .hashing
15
+
16
+
17
+
18
+
19
+
20
+
21
+ /** A fast multiplicative hash by Phil Bagwell.
22
+ */
23
+ final class ByteswapHashing [T ] extends Hashing [T ] {
24
+
25
+ def hash (v : T ) = byteswap32(v.## )
26
+
27
+ }
28
+
29
+
30
+ object ByteswapHashing {
31
+
32
+ private class Chained [T ](h : Hashing [T ]) extends Hashing [T ] {
33
+ def hash (v : T ) = byteswap32(h.hash(v))
34
+ }
35
+
36
+ /** Composes another `Hashing` with the Byteswap hash.
37
+ */
38
+ def chain [T ](h : Hashing [T ]): Hashing [T ] = new Chained (h)
39
+
40
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Scala (https://www.scala-lang.org)
3
+ *
4
+ * Copyright EPFL and Lightbend, Inc. dba Akka
5
+ *
6
+ * Licensed under Apache License 2.0
7
+ * (http://www.apache.org/licenses/LICENSE-2.0).
8
+ *
9
+ * See the NOTICE file distributed with this work for
10
+ * additional information regarding copyright ownership.
11
+ */
12
+
13
+ package scala
14
+ package util .hashing
15
+
16
+ import scala .annotation .implicitNotFound
17
+
18
+ /** `Hashing` is a trait whose instances each represent a strategy for hashing
19
+ * instances of a type.
20
+ *
21
+ * `Hashing`'s companion object defines a default hashing strategy for all
22
+ * objects - it calls their `##` method.
23
+ *
24
+ * Note: when using a custom `Hashing`, make sure to use it with the `Equiv`
25
+ * such that if any two objects are equal, then their hash codes must be equal.
26
+ */
27
+ @ implicitNotFound(msg = " No implicit Hashing defined for ${T}." )
28
+ trait Hashing [T ] extends Serializable {
29
+ def hash (x : T ): Int
30
+ }
31
+
32
+ object Hashing {
33
+ final class Default [T ] extends Hashing [T ] {
34
+ def hash (x : T ) = x.##
35
+ }
36
+
37
+ implicit def default [T ]: Default [T ] = new Default [T ]
38
+
39
+ def fromFunction [T ](f : T => Int ) = new Hashing [T ] {
40
+ def hash (x : T ) = f(x)
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments