Sunday, November 8, 2015

[SD] Bloom filter [DONE]

import java.util.BitSet;
public class  simplebloomfilter
 {
     private static final  int  default_size  = 2 << 24 ;
     private static final  int [] seeds =new  int []{5,7, 11 , 13 , 41 , 43 , 61};
     private  BitSet bits= new  BitSet(default_size);
     private  simplehash[]  func=new  simplehash[seeds.length];
     public static void  main(String[] args) {
        String value  = "simplebloomfilter@gmail.cn" ;
        simplebloomfilter
 filter=new  simplebloomfilter
();
        System.out.println(filter.contains(value));
        filter.add(value);
        System.out.println(filter.contains(value));
    }
     public  simplebloomfilter
() {
         for( int  i= 0 ; i< seeds.length; i ++ ) {
            func[i]=new  simplehash(default_size, seeds[i]);
        }
    }
     public void  add(String value) {
         for(simplehash f : func) {
            bits.set(f.hash(value),  true );
        }
    }
     public boolean  contains(String value) {
         if(value ==null ) {
             return false ;
        }
         boolean  ret  = true ;
         for(simplehash f : func) {
            ret=ret&& bits.get(f.hash(value));
        }
         return  ret;
    }
     public static class simplehash {
         private int  cap;
         private int  seed;
         public  simplehash( int cap, int seed) {
             this.cap= cap;
             this.seed =seed;
        }
         public int hash(String value) {
             int  result=0 ;
             int  len= value.length();
             for  (int i= 0 ; i< len; i ++ ) {
                result =seed* result + value.charAt(i);// Rolling Hash
                System.out.println(result);
            }
             return (cap - 1 ) & result;// 2 << 24 - 1
        }
    }
} 


// http://www.programgo.com/article/42342753493/#tfbml-data%7B%22iframe_height%22%3A162%2C%22location_url%22%3A%22http%3A%2F%2Fwww.programgo.com%2Farticle%2F42342753493%2F%22%7D
//-----------------------------------------------------------------------------  
// MurmurHash2, by Austin Appleby  
  
// Note - This code makes a few assumptions about how your machine behaves -  
  
// 1. We can read a 4-byte value from any address without crashing  
// 2. sizeof(int) == 4  
  
// And it has a few limitations -  
  
// 1. It will not work incrementally.  
// 2. It will not produce the same results on little-endian and big-endian  
//    machines.  
  
unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed )  
{  
    // 'm' and 'r' are mixing constants generated offline.  
    // They're not really 'magic', they just happen to work well.  
  
    const unsigned int m = 0x5bd1e995;  
    const int r = 24;  
  
    // Initialize the hash to a 'random' value  
  
    unsigned int h = seed ^ len;  
  
    // Mix 4 bytes at a time into the hash  
  
    const unsigned char * data = (const unsigned char *)key;  
  
    while(len >= 4)  
    {  
        unsigned int k = *(unsigned int *)data;  
  
        k *= m;  
        k ^= k >> r;  
        k *= m;  
  
        h *= m;  
        h ^= k;  
  
        data += 4;  
        len -= 4;  
    }  
  
    // Handle the last few bytes of the input array  
  
    switch(len)  
    {  
    case 3: h ^= data[2] << 16;  
    case 2: h ^= data[1] << 8;  
    case 1: h ^= data[0];  
            h *= m;  
    };  
  
    // Do a few final mixes of the hash to ensure the last few  
    // bytes are well-incorporated.  
  
    h ^= h >> 13;  
    h *= m;  
    h ^= h >> 15;  
  
    return h;  
}  


#include   
  
using namespace std;  
  
unsigned int MurmurHash2 ( const void * key, int len, unsigned int seed );  
  
int main() {  
    unsigned int result = MurmurHash2("abcd",4,1);  
    cout<

1 comment:

  1. Race Tech Titanium-Arms Racetech Titanium-Arms | Tithology
    Racing. titanium nipple barbells RaceTech. Racing. titanium stud earrings Track-by-track. RaceTech. Racing. Track titanium apple watch band by-track. RaceTech. trekz titanium pairing Track titanium water bottle by-track. RaceTech.

    ReplyDelete