-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloomFilterRan.java
More file actions
65 lines (55 loc) · 1.54 KB
/
BloomFilterRan.java
File metadata and controls
65 lines (55 loc) · 1.54 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
import java.util.Random;
public class BloomFilterRan extends BloomFilterDet {
private int MAXPRIME;
private int HASHA;
private int HASHB;
public BloomFilterRan(int setSize, int bitsPerElement) {
super(setSize, bitsPerElement);
this.hash();
}
public void add(String s) {
s.toLowerCase();
long hash = this.HASHB;
for(int i = 0; i < this.numHashes; i++) {
for(int j = 0; j < s.length(); j++) {
long check = (hash * this.HASHA) + s.charAt(j);
hash = (check % this.MAXPRIME) % this.filterSize;
}
this.bitArray.set((int) hash);
}
this.dataSize++;
}
public boolean appears(String s) {
s.toLowerCase();
long hash = this.HASHB;
for(int i = 0; i < this.numHashes; i++) {
for(int j = 0; j < s.length(); j++) {
long check = (hash * this.HASHA) + s.charAt(j);
hash = (check % this.MAXPRIME) % this.filterSize;
}
if(!this.bitArray.get((int) hash)) return false;
}
return true;
}
public void hash() {
this.MAXPRIME = prime(this.filterSize);
Random rand = new Random();
this.HASHA = rand.nextInt(this.MAXPRIME - 1) + 1;
this.HASHB = rand.nextInt(this.MAXPRIME);
}
public int prime(int max) {
Random rand = new Random();
boolean run = true;
long number = 0;
while(run) {
if(max == 1) return 2;
number = rand.nextInt(max) + max + 1;
int divide = 3;
while((divide <= Math.sqrt(number)) && (number % divide != 0) && (number % 2 != 0)) {
divide += 2;
}
if((number % divide != 0) && (number % 2 != 0)) run = false;
}
return (int) number;
}
}