4
4
5
5
/**
6
6
* A generic BloomFilter implementation for probabilistic membership checking.
7
+ * <p>
8
+ * Bloom filters are space-efficient data structures that provide a fast way to test whether an
9
+ * element is a member of a set. They may produce false positives, indicating an element is
10
+ * in the set when it is not, but they will never produce false negatives.
11
+ * </p>
7
12
*
8
13
* @param <T> The type of elements to be stored in the Bloom filter.
9
14
*/
@@ -17,18 +22,22 @@ public class BloomFilter<T> {
17
22
* Constructs a BloomFilter with a specified number of hash functions and bit array size.
18
23
*
19
24
* @param numberOfHashFunctions the number of hash functions to use
20
- * @param bitArraySize the size of the bit array
25
+ * @param bitArraySize the size of the bit array, which determines the capacity of the filter
26
+ * @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is less than 1
21
27
*/
22
28
@ SuppressWarnings ("unchecked" )
23
29
public BloomFilter (int numberOfHashFunctions , int bitArraySize ) {
30
+ if (numberOfHashFunctions < 1 || bitArraySize < 1 ) {
31
+ throw new IllegalArgumentException ("Number of hash functions and bit array size must be greater than 0" );
32
+ }
24
33
this .numberOfHashFunctions = numberOfHashFunctions ;
25
34
this .bitArray = new BitSet (bitArraySize );
26
35
this .hashFunctions = new Hash [numberOfHashFunctions ];
27
36
initializeHashFunctions ();
28
37
}
29
38
30
39
/**
31
- * Initializes the hash functions with unique indices.
40
+ * Initializes the hash functions with unique indices to ensure different hashing .
32
41
*/
33
42
private void initializeHashFunctions () {
34
43
for (int i = 0 ; i < numberOfHashFunctions ; i ++) {
@@ -38,8 +47,12 @@ private void initializeHashFunctions() {
38
47
39
48
/**
40
49
* Inserts an element into the Bloom filter.
50
+ * <p>
51
+ * This method hashes the element using all defined hash functions and sets the corresponding
52
+ * bits in the bit array.
53
+ * </p>
41
54
*
42
- * @param key the element to insert
55
+ * @param key the element to insert into the Bloom filter
43
56
*/
44
57
public void insert (T key ) {
45
58
for (Hash <T > hash : hashFunctions ) {
@@ -50,8 +63,13 @@ public void insert(T key) {
50
63
51
64
/**
52
65
* Checks if an element might be in the Bloom filter.
66
+ * <p>
67
+ * This method checks the bits at the positions computed by each hash function. If any of these
68
+ * bits are not set, the element is definitely not in the filter. If all bits are set, the element
69
+ * might be in the filter.
70
+ * </p>
53
71
*
54
- * @param key the element to check
72
+ * @param key the element to check for membership in the Bloom filter
55
73
* @return {@code true} if the element might be in the Bloom filter, {@code false} if it is definitely not
56
74
*/
57
75
public boolean contains (T key ) {
@@ -66,6 +84,9 @@ public boolean contains(T key) {
66
84
67
85
/**
68
86
* Inner class representing a hash function used by the Bloom filter.
87
+ * <p>
88
+ * Each instance of this class represents a different hash function based on its index.
89
+ * </p>
69
90
*
70
91
* @param <T> The type of elements to be hashed.
71
92
*/
@@ -76,27 +97,35 @@ private static class Hash<T> {
76
97
/**
77
98
* Constructs a Hash function with a specified index.
78
99
*
79
- * @param index the index of this hash function
100
+ * @param index the index of this hash function, used to create a unique hash
80
101
*/
81
102
Hash (int index ) {
82
103
this .index = index ;
83
104
}
84
105
85
106
/**
86
107
* Computes the hash of the given key.
108
+ * <p>
109
+ * The hash value is calculated by multiplying the index of the hash function
110
+ * with the ASCII sum of the string representation of the key.
111
+ * </p>
87
112
*
88
113
* @param key the element to hash
89
- * @return the hash value
114
+ * @return the computed hash value
90
115
*/
91
116
public int compute (T key ) {
92
117
return index * asciiString (String .valueOf (key ));
93
118
}
94
119
95
120
/**
96
121
* Computes the ASCII value sum of the characters in a string.
122
+ * <p>
123
+ * This method iterates through each character of the string and accumulates
124
+ * their ASCII values to produce a single integer value.
125
+ * </p>
97
126
*
98
127
* @param word the string to compute
99
- * @return the sum of ASCII values of the characters
128
+ * @return the sum of ASCII values of the characters in the string
100
129
*/
101
130
private int asciiString (String word ) {
102
131
int sum = 0 ;
0 commit comments