- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathBloomFilter.java
More file actions
Latest commit
105 lines (89 loc) · 3.35 KB
/
Copy pathBloomFilter.java
File metadata and controls
105 lines (89 loc) · 3.35 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
importjava.util.BitSet;
publicclassBloomFilter {
privateBitSetbitArray;
privateintsize;
privateinthashFunctions;
publicBloomFilter(intsize, inthashFunctions) {
this.size = size;
this.hashFunctions = hashFunctions;
this.bitArray = newBitSet(size);
}
publicvoidadd(Objectelement) {
for (inti = 0; i < hashFunctions; i++) {
intindex = hash(element, i) % size;
bitArray.set(index);
}
}
publicbooleanquery(Objectelement) {
for (inti = 0; i < hashFunctions; i++) {
intindex = hash(element, i) % size;
if (!bitArray.get(index)) {
returnfalse; // Definitely not in the set
}
}
returntrue; // Probably in the set
}
privateinthash(Objectelement, intseed) {
inthashValue = seed;
byte[] bytes = elementToBytes(element);
for (byteb : bytes) {
hashValue = (hashValue * 65599) ^ (b & 0xFF);
}
returnMath.abs(hashValue);
}
privatebyte[] elementToBytes(Objectelement) {
if (elementinstanceofString) {
return ((String) element).getBytes();
} elseif (elementinstanceofInteger) {
returnintToBytes((Integer) element);
} elseif (elementinstanceofFloat) {
returnfloatToBytes((Float) element);
} elseif (elementinstanceofByte) {
returnnewbyte[]{(Byte) element};
} elseif (elementinstanceofDouble) {
returndoubleToBytes((Double) element);
} elseif (elementinstanceofLong) {
returnlongToBytes((Long) element);
} else {
thrownewIllegalArgumentException("Unsupported element type");
}
}
privatebyte[] intToBytes(intvalue) {
returnnewbyte[]{
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
(byte) value
};
}
privatebyte[] floatToBytes(floatvalue) {
returnintToBytes(Float.floatToIntBits(value));
}
privatebyte[] doubleToBytes(doublevalue) {
returnlongToBytes(Double.doubleToLongBits(value));
}
privatebyte[] longToBytes(longvalue) {
returnnewbyte[]{
(byte) (value >>> 56),
(byte) (value >>> 48),
(byte) (value >>> 40),
(byte) (value >>> 32),
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
(byte) value
};
}
publicstaticvoidmain(String[] args) {
BloomFilterfilter = newBloomFilter(100, 2);
intintValue = 42;
filter.add(intValue);
StringstringValue = "example";
filter.add(stringValue);
// Querying for membership
System.out.println("Querying 42: " + filter.query(intValue)); // Output: true (probably in the set)
System.out.println("Querying 'example': " + filter.query(stringValue)); // Output: true (probably in the set)
System.out.println("Querying 'world': " + filter.query("world")); // Output: false (definitely not in the set)
System.out.println("Querying 'worldd': " + filter.query("worldd")); // Output: false (definitely not in the set -> false positive)
}
}