Skip to content

Commit f9e42d9

Browse files
committed
Fix
1 parent 311158e commit f9e42d9

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed

src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
package com.thealgorithms.datastructures.bloomfilter;
22

3+
import java.util.Arrays;
34
import java.util.BitSet;
45

56
/**
67
* A generic BloomFilter implementation for probabilistic membership checking.
78
* <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
9+
* Bloom filters are space-efficient data structures that provide a fast way to
10+
* test whether an
11+
* element is a member of a set. They may produce false positives, indicating an
12+
* element is
1013
* in the set when it is not, but they will never produce false negatives.
1114
* </p>
1215
*
1316
* @param <T> The type of elements to be stored in the Bloom filter.
1417
*/
15-
@SuppressWarnings("rawtypes")
1618
public class BloomFilter<T> {
1719

1820
private final int numberOfHashFunctions;
1921
private final BitSet bitArray;
2022
private final Hash<T>[] hashFunctions;
2123

2224
/**
23-
* Constructs a BloomFilter with a specified number of hash functions and bit array size.
25+
* Constructs a BloomFilter with a specified number of hash functions and bit
26+
* array size.
2427
*
2528
* @param numberOfHashFunctions the number of hash functions to use
26-
* @param bitArraySize the size of the bit array, which determines the capacity of the filter
27-
* @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is less than 1
29+
* @param bitArraySize the size of the bit array, which determines the
30+
* capacity of the filter
31+
* @throws IllegalArgumentException if numberOfHashFunctions or bitArraySize is
32+
* less than 1
2833
*/
2934
@SuppressWarnings("unchecked")
3035
public BloomFilter(int numberOfHashFunctions, int bitArraySize) {
@@ -38,7 +43,8 @@ public BloomFilter(int numberOfHashFunctions, int bitArraySize) {
3843
}
3944

4045
/**
41-
* Initializes the hash functions with unique indices to ensure different hashing.
46+
* Initializes the hash functions with unique indices to ensure different
47+
* hashing.
4248
*/
4349
private void initializeHashFunctions() {
4450
for (int i = 0; i < numberOfHashFunctions; i++) {
@@ -49,7 +55,8 @@ private void initializeHashFunctions() {
4955
/**
5056
* Inserts an element into the Bloom filter.
5157
* <p>
52-
* This method hashes the element using all defined hash functions and sets the corresponding
58+
* This method hashes the element using all defined hash functions and sets the
59+
* corresponding
5360
* bits in the bit array.
5461
* </p>
5562
*
@@ -65,13 +72,16 @@ public void insert(T key) {
6572
/**
6673
* Checks if an element might be in the Bloom filter.
6774
* <p>
68-
* This method checks the bits at the positions computed by each hash function. If any of these
69-
* bits are not set, the element is definitely not in the filter. If all bits are set, the element
75+
* This method checks the bits at the positions computed by each hash function.
76+
* If any of these
77+
* bits are not set, the element is definitely not in the filter. If all bits
78+
* are set, the element
7079
* might be in the filter.
7180
* </p>
7281
*
7382
* @param key the element to check for membership in the Bloom filter
74-
* @return {@code true} if the element might be in the Bloom filter, {@code false} if it is definitely not
83+
* @return {@code true} if the element might be in the Bloom filter,
84+
* {@code false} if it is definitely not
7585
*/
7686
public boolean contains(T key) {
7787
for (Hash<T> hash : hashFunctions) {
@@ -86,7 +96,8 @@ public boolean contains(T key) {
8696
/**
8797
* Inner class representing a hash function used by the Bloom filter.
8898
* <p>
89-
* Each instance of this class represents a different hash function based on its index.
99+
* Each instance of this class represents a different hash function based on its
100+
* index.
90101
* </p>
91102
*
92103
* @param <T> The type of elements to be hashed.
@@ -109,13 +120,37 @@ private static class Hash<T> {
109120
* <p>
110121
* The hash value is calculated by multiplying the index of the hash function
111122
* with the ASCII sum of the string representation of the key.
123+
* For array types, the content of the array is used instead of the default
124+
* toString.
112125
* </p>
113126
*
114127
* @param key the element to hash
115128
* @return the computed hash value
116129
*/
117130
public int compute(T key) {
118-
return index * asciiString(String.valueOf(key));
131+
String keyString;
132+
if (key instanceof Object[] objectArray) {
133+
keyString = Arrays.deepToString(objectArray);
134+
} else if (key instanceof int[] intArray) {
135+
keyString = Arrays.toString(intArray);
136+
} else if (key instanceof long[] longArray) {
137+
keyString = Arrays.toString(longArray);
138+
} else if (key instanceof double[] doubleArray) {
139+
keyString = Arrays.toString(doubleArray);
140+
} else if (key instanceof float[] floatArray) {
141+
keyString = Arrays.toString(floatArray);
142+
} else if (key instanceof boolean[] booleanArray) {
143+
keyString = Arrays.toString(booleanArray);
144+
} else if (key instanceof byte[] byteArray) {
145+
keyString = Arrays.toString(byteArray);
146+
} else if (key instanceof char[] charArray) {
147+
keyString = Arrays.toString(charArray);
148+
} else if (key instanceof short[] shortArray) {
149+
keyString = Arrays.toString(shortArray);
150+
} else {
151+
keyString = String.valueOf(key);
152+
}
153+
return index * asciiString(keyString);
119154
}
120155

121156
/**

0 commit comments

Comments
 (0)