|
2 | 2 |
|
3 | 3 | import java.util.BitSet;
|
4 | 4 |
|
| 5 | +/** |
| 6 | + * A generic BloomFilter implementation for probabilistic membership checking. |
| 7 | + * |
| 8 | + * @param <T> The type of elements to be stored in the Bloom filter. |
| 9 | + */ |
5 | 10 | public class BloomFilter<T> {
|
6 | 11 |
|
7 |
| - private int numberOfHashFunctions; |
8 |
| - private BitSet bitArray; |
9 |
| - private Hash<T>[] hashFunctions; |
| 12 | + private final int numberOfHashFunctions; |
| 13 | + private final BitSet bitArray; |
| 14 | + private final Hash<T>[] hashFunctions; |
10 | 15 |
|
11 |
| - public BloomFilter(int numberOfHashFunctions, int n) { |
| 16 | + /** |
| 17 | + * Constructs a BloomFilter with a specified number of hash functions and bit array size. |
| 18 | + * |
| 19 | + * @param numberOfHashFunctions the number of hash functions to use |
| 20 | + * @param bitArraySize the size of the bit array |
| 21 | + */ |
| 22 | + @SuppressWarnings("unchecked") |
| 23 | + public BloomFilter(int numberOfHashFunctions, int bitArraySize) { |
12 | 24 | this.numberOfHashFunctions = numberOfHashFunctions;
|
13 |
| - hashFunctions = new Hash[numberOfHashFunctions]; |
14 |
| - bitArray = new BitSet(n); |
15 |
| - insertHash(); |
| 25 | + this.bitArray = new BitSet(bitArraySize); |
| 26 | + this.hashFunctions = new Hash[numberOfHashFunctions]; |
| 27 | + initializeHashFunctions(); |
16 | 28 | }
|
17 | 29 |
|
18 |
| - private void insertHash() { |
| 30 | + /** |
| 31 | + * Initializes the hash functions with unique indices. |
| 32 | + */ |
| 33 | + private void initializeHashFunctions() { |
19 | 34 | for (int i = 0; i < numberOfHashFunctions; i++) {
|
20 |
| - hashFunctions[i] = new Hash(i); |
| 35 | + hashFunctions[i] = new Hash<>(i); |
21 | 36 | }
|
22 | 37 | }
|
23 | 38 |
|
| 39 | + /** |
| 40 | + * Inserts an element into the Bloom filter. |
| 41 | + * |
| 42 | + * @param key the element to insert |
| 43 | + */ |
24 | 44 | public void insert(T key) {
|
25 | 45 | for (Hash<T> hash : hashFunctions) {
|
26 |
| - int position = hash.compute(key) % bitArray.size(); |
| 46 | + int position = Math.abs(hash.compute(key) % bitArray.size()); |
27 | 47 | bitArray.set(position);
|
28 | 48 | }
|
29 | 49 | }
|
30 | 50 |
|
| 51 | + /** |
| 52 | + * Checks if an element might be in the Bloom filter. |
| 53 | + * |
| 54 | + * @param key the element to check |
| 55 | + * @return {@code true} if the element might be in the Bloom filter, {@code false} if it is definitely not |
| 56 | + */ |
31 | 57 | public boolean contains(T key) {
|
32 | 58 | for (Hash<T> hash : hashFunctions) {
|
33 |
| - int position = hash.compute(key) % bitArray.size(); |
| 59 | + int position = Math.abs(hash.compute(key) % bitArray.size()); |
34 | 60 | if (!bitArray.get(position)) {
|
35 | 61 | return false;
|
36 | 62 | }
|
37 | 63 | }
|
38 | 64 | return true;
|
39 | 65 | }
|
40 | 66 |
|
41 |
| - private class Hash<T> { |
| 67 | + /** |
| 68 | + * Inner class representing a hash function used by the Bloom filter. |
| 69 | + * |
| 70 | + * @param <T> The type of elements to be hashed. |
| 71 | + */ |
| 72 | + private static class Hash<T> { |
42 | 73 |
|
43 |
| - int index; |
| 74 | + private final int index; |
44 | 75 |
|
| 76 | + /** |
| 77 | + * Constructs a Hash function with a specified index. |
| 78 | + * |
| 79 | + * @param index the index of this hash function |
| 80 | + */ |
45 | 81 | Hash(int index) {
|
46 | 82 | this.index = index;
|
47 | 83 | }
|
48 | 84 |
|
| 85 | + /** |
| 86 | + * Computes the hash of the given key. |
| 87 | + * |
| 88 | + * @param key the element to hash |
| 89 | + * @return the hash value |
| 90 | + */ |
49 | 91 | public int compute(T key) {
|
50 | 92 | return index * asciiString(String.valueOf(key));
|
51 | 93 | }
|
52 | 94 |
|
| 95 | + /** |
| 96 | + * Computes the ASCII value sum of the characters in a string. |
| 97 | + * |
| 98 | + * @param word the string to compute |
| 99 | + * @return the sum of ASCII values of the characters |
| 100 | + */ |
53 | 101 | private int asciiString(String word) {
|
54 |
| - int number = 0; |
55 |
| - for (int i = 0; i < word.length(); i++) { |
56 |
| - number += word.charAt(i); |
| 102 | + int sum = 0; |
| 103 | + for (char c : word.toCharArray()) { |
| 104 | + sum += c; |
57 | 105 | }
|
58 |
| - return number; |
| 106 | + return sum; |
59 | 107 | }
|
60 | 108 | }
|
61 | 109 | }
|
0 commit comments