55/**
66 * A generic BloomFilter implementation for probabilistic membership checking.
77 * <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
8+ * Bloom filters are space-efficient data structures that provide a fast way to
9+ * test whether an
10+ * element is a member of a set. They may produce false positives, indicating an
11+ * element is
1012 * in the set when it is not, but they will never produce false negatives.
1113 * </p>
1214 *
@@ -20,11 +22,14 @@ public class BloomFilter<T> {
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.
@@ -115,7 +126,28 @@ private static class Hash<T> {
115126 * @return the computed hash value
116127 */
117128 public int compute (T key ) {
118- return index * asciiString (String .valueOf (key ));
129+ return index * asciiString (objectToString (key ));
130+ }
131+
132+ /**
133+ * Converts the key to a string suitable for hashing. Handles arrays properly.
134+ */
135+ private String objectToString (Object key ) {
136+ if (key == null ) return "null" ;
137+ Class <?> clazz = key .getClass ();
138+ if (clazz .isArray ()) {
139+ if (clazz == byte [].class ) return java .util .Arrays .toString ((byte []) key );
140+ if (clazz == short [].class ) return java .util .Arrays .toString ((short []) key );
141+ if (clazz == int [].class ) return java .util .Arrays .toString ((int []) key );
142+ if (clazz == long [].class ) return java .util .Arrays .toString ((long []) key );
143+ if (clazz == char [].class ) return java .util .Arrays .toString ((char []) key );
144+ if (clazz == float [].class ) return java .util .Arrays .toString ((float []) key );
145+ if (clazz == double [].class ) return java .util .Arrays .toString ((double []) key );
146+ if (clazz == boolean [].class ) return java .util .Arrays .toString ((boolean []) key );
147+ // For object arrays or multi-dimensional arrays
148+ return java .util .Arrays .deepToString ((Object []) key );
149+ }
150+ return String .valueOf (key );
119151 }
120152
121153 /**
0 commit comments