-
Notifications
You must be signed in to change notification settings - Fork 0
/
FNV1.java
126 lines (100 loc) · 3.09 KB
/
FNV1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.hashfunctions;
public class FNV1 {
private static final int FNV1_32_INIT = 0x811c9dc5;
private static final int FNV1_PRIME_32 = 16777619;
private static final long FNV1_64_INIT = 0xcbf29ce484222325L;
private static final long FNV1_PRIME_64 = 1099511628211L;
public static int hash32(byte[] data) {
return hash32(data, data.length);
}
public static int hash32a(byte[] data) {
return hash32a(data, data.length);
}
public static int hash32a(char[] data) {
return hash32a(data, data.length);
}
public static int hash32(byte[] data, int length) {
int hash = FNV1_32_INIT;
for (int i = 0; i < length; i++) {
hash *= FNV1_PRIME_32;
hash ^= (data[i] & 0xff);
}
return hash;
}
public static int hash32(String data) {
int hash = FNV1_32_INIT;
for (int i = 0; i < data.length(); i++) {
hash *= FNV1_PRIME_32;
hash ^= (data.charAt(i) & 0xff);
}
return hash;
}
public static int hash32a(String data) {
int hash = FNV1_32_INIT;
for (int i = 0; i < data.length(); i++) {
hash ^= (data.charAt(i) & 0xff);
hash *= FNV1_PRIME_32;
}
return hash;
}
public static int hash32a(byte[] data, int length) {
int hash = FNV1_32_INIT;
for (int i = 0; i < length; i++) {
hash ^= (data[i] & 0xff);
hash *= FNV1_PRIME_32;
}
return hash;
}
public static int hash32a(char[] data, int length) {
int hash = FNV1_32_INIT;
for (int i = 0; i < length; i++) {
hash ^= (data[i] & 0xff);
hash *= FNV1_PRIME_32;
}
return hash;
}
public static long hash64(byte[] data) {
return hash64(data, data.length);
}
public static long hash64(char[] data) {
return hash64(data, data.length);
}
public static long hash64a(byte[] data) {
return hash64a(data, data.length);
}
public static long hash64a(char[] data) {
return hash64a(data, data.length);
}
public static long hash64(byte[] data, int length) {
long hash = FNV1_64_INIT;
for (int i = 0; i < length; i++) {
hash *= FNV1_PRIME_64;
hash ^= (data[i] & 0xff);
}
return hash;
}
public static long hash64(char[] data, int length) {
long hash = FNV1_64_INIT;
for (int i = 0; i < length; i++) {
hash *= FNV1_PRIME_64;
hash ^= (data[i] & 0xff);
}
return hash;
}
public static long hash64a(byte[] data, int length) {
long hash = FNV1_64_INIT;
for (int i = 0; i < length; i++) {
hash ^= (data[i] & 0xff);
hash *= FNV1_PRIME_64;
}
return hash;
}
public static long hash64a(char[] data, int length) {
long hash = FNV1_64_INIT;
for (int i = 0; i < length; i++) {
hash ^= (data[i] & 0xff);
hash *= FNV1_PRIME_64;
}
return hash;
}
}