File tree Expand file tree Collapse file tree 2 files changed +39
-3
lines changed
Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Original file line number Diff line number Diff line change 1+ package main .java .course ;
2+
3+ public class BloomFilterMain {
4+ public static void main (String [] args ) {
5+ BloomFilter bloomFilter = new BloomFilter (1000 );
6+ bloomFilter .mark (141432 );
7+ bloomFilter .mark (2354364 );
8+ System .out .println (bloomFilter .mayExist (141432 ));
9+ System .out .println (bloomFilter .mayExist (723 ));
10+ //This should be false, but bloom filters provide no guarantee on definite existence
11+ System .out .println (bloomFilter .mayExist (1432 ));
12+ }
13+ }
14+
15+ class BloomFilter {
16+ final long [] points ;
17+ private final int size ;
18+
19+ BloomFilter (final int size ) {
20+ this .size = size ;
21+ points = new long [(size + 63 ) >> 6 ];
22+ }
23+
24+ public void mark (int point ) {
25+ point %= size ;
26+ points [point >> 6 ] |= (1 << (point & 63 ));
27+ }
28+
29+ public boolean mayExist (int point ) {
30+ point %= size ;
31+ return (points [point >> 6 ] & (1 << (point & 63 ))) != 0 ;
32+ }
33+ }
Original file line number Diff line number Diff line change @@ -40,13 +40,16 @@ private static int binaryGcd(int a, int b) {
4040 while ((a & 1 ) == 0 ) {
4141 a = a >> 1 ;
4242 }
43+ while ((b & 1 ) == 0 ) {
44+ b = b >> 1 ;
45+ }
4346 while (b != 0 ) {
44- while ((b & 1 ) == 0 ) {
45- b = b >> 1 ;
46- }
4747 int temp = a - b ;
4848 a = b ;
4949 b = temp ;
50+ while ((b & 1 ) == 0 && b > 0 ) {
51+ b = b >> 1 ;
52+ }
5053 if (a < b ) {
5154 int tempSwap = a ;
5255 a = b ;
You can’t perform that action at this time.
0 commit comments