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 閱讀(2591) 評論(0)  編輯  收藏 所屬分類: java

          主站蜘蛛池模板: 富宁县| 克拉玛依市| 精河县| 鄂伦春自治旗| 本溪| 康定县| 池州市| 威远县| 鄂尔多斯市| 城口县| 新巴尔虎左旗| 黄冈市| 彩票| 友谊县| 西平县| 孝感市| 阿拉尔市| 闻喜县| 外汇| 涿州市| 察哈| 伊通| 桂阳县| 濉溪县| 古交市| 舟曲县| 道孚县| 锡林郭勒盟| 长兴县| 永昌县| 扶沟县| 开原市| 福贡县| 鄂尔多斯市| 沙坪坝区| 通化县| 平阴县| 浦北县| 中卫市| 全椒县| 兰考县|