MCPcopy Create free account
hub / github.com/BirolLab/RNA-Bloom / SimpleBloomFilter

Class SimpleBloomFilter

src/rnabloom/bloom/SimpleBloomFilter.java:27–85  ·  view source on GitHub ↗

@author Ka Ming Nip

Source from the content-addressed store, hash-verified

25 * @author Ka Ming Nip
26 */
27public class SimpleBloomFilter implements BloomFilterInterface {
28 protected AbstractLargeBitBuffer bitArray;
29 protected long size;
30 protected long popcount = -1;
31
32 public SimpleBloomFilter(long size) {
33 this.size = size;
34 try {
35 this.bitArray = new UnsafeBitBuffer(size);
36 }
37 catch(NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
38 this.bitArray = new LargeBitBuffer(size);
39 }
40 }
41
42 protected long getIndex(String key) {
43 long hashCode = (long) key.hashCode();
44 return (hashCode + (long) Integer.MAX_VALUE + 1L) % size;
45 }
46
47 @Override
48 public void add(String key) {
49 bitArray.set(getIndex(key));
50 }
51
52 @Override
53 public boolean lookup(String key) {
54 return bitArray.get(getIndex(key));
55 }
56
57 public boolean lookupAndAdd(String key) {
58 return bitArray.getAndSet(getIndex(key));
59 }
60
61 @Override
62 public float getFPR() {
63 /* (1 - e(-kn/m))^k
64 k = num hash
65 m = size
66 n = pop count
67 */
68
69 popcount = bitArray.popCount();
70 return (float) ((double) popcount / (double) size);
71 }
72
73 public void empty() {
74 if (this.bitArray != null) {
75 this.bitArray.empty();
76 }
77 }
78
79 public void destroy() {
80 if (this.bitArray != null) {
81 this.bitArray.destroy();
82 this.bitArray = null;
83 }
84 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected