]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/types/HashCodeUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / types / HashCodeUtils.java
1 package org.simantics.scl.compiler.internal.types;
2
3 /**
4  * Utils for computing 32-bit Murmur3 hash function
5  */
6 public class HashCodeUtils {
7     
8     private static final int C1 = 0xcc9e2d51;
9     private static final int C2 = 0x1b873593;
10     private static final int R1 = 15;
11     private static final int R2 = 13;
12     private static final int M = 5;
13     private static final int N = 0xe6546b64;
14     
15     public static final int SEED = 0;
16     
17     public static int update(int hash, int value) {
18         value *= C1;
19         value = Integer.rotateLeft(value, R1);
20         value *= C2;
21         
22         hash ^= value;
23         return Integer.rotateLeft(hash, R2) * M + N;
24     }
25     
26     public static int preprocessValue(int value) {
27         value *= C1;
28         value = Integer.rotateLeft(value, R1);
29         value *= C2;
30         return value;
31     }
32     
33     public static int updateWithPreprocessedValue(int hash, int preprocessedValue) {
34         hash ^= preprocessedValue;
35         return Integer.rotateLeft(hash, R2) * M + N;
36     }
37     
38     public static int finalize(int hash) {
39         hash ^= hash >> 16;
40         hash *= 0x85ebca6b;
41         hash ^= hash >> 13;
42         hash *= 0xc2b2ae35;
43         hash ^= hash >> 16;
44         return hash;
45     }
46 }