File tree 1 file changed +8
-4
lines changed
src/main/java/com/thealgorithms/datastructures/bloomfilter
1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change 1
1
package com .thealgorithms .datastructures .bloomfilter ;
2
2
3
3
4
+ import java .util .BitSet ;
5
+
4
6
public class BloomFilter <T > {
5
7
6
8
private int numberOfHashFunctions ;
7
- private int [] bitArray ;
9
+ private BitSet bitArray ;
8
10
private Hash <T >[] hashFunctions ;
9
11
10
12
public BloomFilter (int numberOfHashFunctions , int n ) {
11
13
this .numberOfHashFunctions = numberOfHashFunctions ;
12
14
hashFunctions = new Hash [numberOfHashFunctions ];
13
- bitArray = new int [ n ] ;
15
+ bitArray = new BitSet ( n ) ;
14
16
insertHash ();
15
17
}
16
18
@@ -22,13 +24,15 @@ private void insertHash() {
22
24
23
25
public void insert (T key ) {
24
26
for (Hash <T > hash : hashFunctions ){
25
- bitArray [hash .compute (key ) % bitArray .length ] = 1 ;
27
+ int position = hash .compute (key ) % bitArray .size ();
28
+ bitArray .set (position );
26
29
}
27
30
}
28
31
29
32
public boolean contains (T key ) {
30
33
for (Hash <T > hash : hashFunctions ){
31
- if (bitArray [hash .compute (key ) % bitArray .length ] == 0 ){
34
+ int position = hash .compute (key ) % bitArray .size ();
35
+ if (!bitArray .get (position )) {
32
36
return false ;
33
37
}
34
38
}
You can’t perform that action at this time.
0 commit comments