-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloomFilter.java
More file actions
89 lines (59 loc) · 1.75 KB
/
Copy pathBloomFilter.java
File metadata and controls
89 lines (59 loc) · 1.75 KB
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
/*
Ori Paso - 209493683
*/
package test;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.BitSet;
public class BloomFilter {
BitSet bitSet;
ArrayList<MessageDigest> Md = new ArrayList<>();
public BloomFilter(int sizeOfBit, String... allArgsOfStrings) {
bitSet = new BitSet(sizeOfBit);
for (String args : allArgsOfStrings) {
try {
Md.add(MessageDigest.getInstance(args));
} catch (NoSuchAlgorithmException e) {}
}
}
public void add(String word) {
byte[] bytes;
for (MessageDigest md : Md) {
bytes = md.digest(word.getBytes());
BigInteger a = new BigInteger(bytes);
// convert the value to be positive and do module
bitSet.set(Math.abs(a.intValue()) % bitSet.size());
}
}
public boolean contains(String word) {
byte[] bytes;
for (MessageDigest md : Md) {
bytes = md.digest(word.getBytes());
BigInteger a = new BigInteger(bytes);
// convert the value to be positive and do module
int index = (Math.abs(a.intValue()) % bitSet.size());
if (!bitSet.get(index))
return false;
}
return true;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(bitSet.length());
int i = 0;
while(i<bitSet.length())
{
if(bitSet.get(i))
{
sb.append(1);
}
else {
sb.append(0);
}
i++;
}
return sb.toString();
}
}