Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.lang.common.hash.integers

File Murmur3.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
19% of files have more coverage

Code metrics

4
32
9
1
76
63
11
0.34
3.56
9
1.22

Classes

Class Line # Actions
Murmur3 3 32 11
0.777777877.8%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    package io.sunshower.lang.common.hash.integers;
2   
 
3    public final class Murmur3 implements IntegerHashFunction {
4   
5    private static final int Constant1 = 0xcc9e2d51;
6    private static final int Constant2 = 0x1b873593;
7   
8    private final int seed;
9   
 
10  6 toggle public Murmur3(int seed) {
11  6 this.seed = seed;
12    }
13   
 
14  0 toggle @Override
15    public int bits() {
16  0 return 32;
17    }
18   
 
19  0 toggle @Override
20    public String toString() {
21  0 return "Murmur3(blocksize: 32 bits)(" + seed + ")";
22    }
23   
 
24  0 toggle @Override
25    public int hashCode() {
26  0 return getClass().hashCode() ^ seed;
27    }
28   
 
29  25000003 toggle private static int createK(int k) {
30  25000003 k *= Constant1;
31  25000003 k = (k << 15) | (k >>> 15);
32  25000003 k *= Constant2;
33  25000003 return k;
34    }
35   
 
36  25000002 toggle private static int createH(int h, int k) {
37  25000002 h ^= k;
38  25000002 h = (h << 13) | (h >>> 13);
39  25000002 h = h * 5 + 0xe6546b64;
40  25000002 return h;
41    }
42   
 
43  501 toggle private static int mix(int h1, int length) {
44  501 h1 ^= length;
45  501 h1 ^= h1 >>> 16;
46  501 h1 *= 0x85ebca6b;
47  501 h1 ^= h1 >>> 13;
48  501 h1 *= 0xc2b2ae35;
49  501 h1 ^= h1 >>> 16;
50  501 return h1;
51    }
52   
 
53  0 toggle @Override
54    public int apply(int value) {
55  0 int k1 = createK(value);
56  0 int h1 = createH(seed, k1);
57  0 return mix(h1, 4);
58    }
59   
 
60  501 toggle @Override
61    public int apply(CharSequence sequence) {
62  501 int h1 = seed;
63  25000503 for (int i = 1; i < sequence.length(); i += 2) {
64  25000002 int k1 = sequence.charAt(i - 1) | (sequence.charAt(i) << 16);
65  25000002 k1 = createK(k1);
66  25000002 h1 = createH(h1, k1);
67    }
68   
69  501 if ((sequence.length() & 1) == 1) {
70  1 int k1 = sequence.charAt(sequence.length() - 1);
71  1 k1 = createK(k1);
72  1 h1 ^= k1;
73    }
74  501 return mix(h1, 2 * sequence.length());
75    }
76    }