Passed
Push — main ( b7a647...25fd53 )
by Eran
03:14
created

murmurhash3_gc.js ➔ murmurhash3_32_gc   B

Complexity

Conditions 3

Size

Total Lines 51
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 38
dl 0
loc 51
rs 8.968
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * JS Implementation of MurmurHash3 (r136) (as of May 20, 2011)
3
 * 
4
 * @author <a href="mailto:[email protected]">Gary Court</a>
5
 * @see http://github.com/garycourt/murmurhash-js
6
 * @author <a href="mailto:[email protected]">Austin Appleby</a>
7
 * @see http://sites.google.com/site/murmurhash/
8
 * 
9
 * @param {string} key ASCII only
10
 * @param {number} seed Positive integer only
11
 * @return {number} 32-bit positive integer hash 
12
 */
13
14
function murmurhash3_32_gc(key, seed) {
15
	var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i;
0 ignored issues
show
Unused Code introduced by
The variable c2b seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The variable c1b seems to be never used. Consider removing it.
Loading history...
16
	
17
	remainder = key.length & 3; // key.length % 4
18
	bytes = key.length - remainder;
19
	h1 = seed;
20
	c1 = 0xcc9e2d51;
21
	c2 = 0x1b873593;
22
	i = 0;
23
	
24
	while (i < bytes) {
25
	  	k1 = 
26
	  	  ((key.charCodeAt(i) & 0xff)) |
27
	  	  ((key.charCodeAt(++i) & 0xff) << 8) |
28
	  	  ((key.charCodeAt(++i) & 0xff) << 16) |
29
	  	  ((key.charCodeAt(++i) & 0xff) << 24);
30
		++i;
31
		
32
		k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff;
33
		k1 = (k1 << 15) | (k1 >>> 17);
34
		k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff;
35
36
		h1 ^= k1;
37
        h1 = (h1 << 13) | (h1 >>> 19);
38
		h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff;
39
		h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16));
40
	}
41
	
42
	k1 = 0;
43
	
44
	switch (remainder) {
45
		case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
0 ignored issues
show
introduced by
This node falls through to the next case due to this statement. Please add a comment either directly below this line or between the cases to explain.
Loading history...
46
		case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
0 ignored issues
show
introduced by
This node falls through to the next case due to this statement. Please add a comment either directly below this line or between the cases to explain.
Loading history...
47
		case 1: k1 ^= (key.charCodeAt(i) & 0xff);
48
		
49
		k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
50
		k1 = (k1 << 15) | (k1 >>> 17);
51
		k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
52
		h1 ^= k1;
53
	}
54
	
55
	h1 ^= key.length;
56
57
	h1 ^= h1 >>> 16;
58
	h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
59
	h1 ^= h1 >>> 13;
60
	h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
61
	h1 ^= h1 >>> 16;
62
63
	return h1 >>> 0;
64
}