stone2083

          布隆過濾器(BloomFilter)

          資料:
          wikipedia--bloom filter

          使用場景,原理簡介之中文資料:
          數學之美系列二十一 - 布隆過濾器(Bloom Filter)

          核心內容(摘自Google黑板報文章內容):


          BloomFilter簡易實現:
          public class SimpleBloomFilter {

              
          private static final int   DEFAULT_SIZE = 2 << 24;
              
          private static final int[] seeds        = new int[] { 71113313761, };

              
          private BitSet             bits         = new BitSet(DEFAULT_SIZE);
              
          private SimpleHash[]       func         = new SimpleHash[seeds.length];

              
          public static void main(String[] args) {
                  String value 
          = "stone2083@yahoo.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);
                      }
                      
          return (cap - 1& result;
                  }

              }

          }



          posted on 2010-01-30 15:00 stone2083 閱讀(2592) 評論(0)  編輯  收藏 所屬分類: java

          主站蜘蛛池模板: 即墨市| 肥城市| 错那县| 高雄市| 万荣县| 陇川县| 滕州市| 台南县| 湖南省| 廊坊市| 武平县| 临高县| 鹿泉市| 定边县| 池州市| 汉川市| 若尔盖县| 西乌| 临桂县| 桑植县| 泗水县| 唐海县| 肇源县| 泗洪县| 阜平县| 临夏县| 祁东县| 礼泉县| 泌阳县| 稷山县| 洞口县| 安康市| 密山市| 谷城县| 出国| 株洲市| 德令哈市| 广昌县| 临夏市| 迭部县| 体育|