-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.java
61 lines (52 loc) · 1.61 KB
/
Hash.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
/* *************************************************************************
* Student Name: Zhuohan Xie
* Student ID: 871089
*
* Description: Provides an SSparseRec class. It has five functions
* in total.
* It implements the S-Sparse Recovery scheme for the project.
*
* Written: 2/10/2018
* Last updated: 12/10/2018
*
*
***************************************************************************/
import java.math.BigInteger;
public class Hash {
/*
* one long type large prime p, and k mean k-wise, long array for all
* polynomial cofactors and a long type range.
*/
private long p = (long)(Math.pow(2, 61) - 1);
private long[] a;
private long range;
/*
* Constructor takes range and k number for setting all cofactors.
*/
public Hash(long range, int k) {
a = new long[k];
for(int i = 0; i < k; i++) {
a[i] = (long)StdRandom.uniform(1, (double)p-1)+1;
}
this.range = range;
}
/*
* hash function takes an int number and returns a long type hashed
* value. Polynomial calculations are worked here.
*/
public long hash(int x) {
BigInteger bighvalue = BigInteger.valueOf(0);
BigInteger bigx = BigInteger.valueOf(x);
BigInteger bigp = BigInteger.valueOf(p);
BigInteger bigr = BigInteger.valueOf(range);
for(int i = 0; i < a.length; i ++) {
BigInteger bigco = BigInteger.valueOf(a[i]);
BigInteger expo = BigInteger.valueOf(i);
BigInteger plus =
bigco.multiply(bigx.modPow(expo, bigp)).mod(bigp);
bighvalue = bighvalue.add(plus).mod(bigp);
}
bighvalue = bighvalue.mod(bigr);
return bighvalue.longValue();
}
}