zoukankan      html  css  js  c++  java
  • 布隆过滤器(c#简单实现)【转】

     public class BloomFilter
        {
            public BitArray _BloomArray;
            public Int64 BloomArryLength { get; }
            public Int64 BitIndexCount { get; }
    
            /// <summary>
            /// 初始化
            /// </summary>
            /// <param name="BloomArryLength">布隆数组的大小</param>
            /// <param name="bitIndexCount">hash次数</param>
            public BloomFilter(int BloomArryLength,  int bitIndexCount)
            {
                _BloomArray = new BitArray(BloomArryLength);
                this.BloomArryLength = BloomArryLength;
                this.BitIndexCount = bitIndexCount;
            }
    
    
            public void Add(string str)
            {
                var hashCode = GetHashCode(str);
                Random random = new Random(hashCode);
                for (int i = 0; i < BitIndexCount; i++)
                {
                    var c = random.Next((int)(this.BloomArryLength - 1));
                    _BloomArray[c] = true;
                }
            }
    
            public bool isExist(string str)
            {
                var hashCode = GetHashCode(str);
                Random random = new Random(hashCode);
                for (int i = 0; i < BitIndexCount; i++)
                {
                    if (!_BloomArray[random.Next((int)(this.BloomArryLength - 1))])
                    {
                        return false;
                    }
                }
                return true;
            }
    
            public int GetHashCode(object value)
            {
                return value.GetHashCode();
            }
        }
    

    客户端

          BloomFilter bf = new BloomFilter(1000000, 3);
                for (int i = 0; i < 100000; i++)
                {
                    bf.Add(i.ToString());
                }
                int errorCount = 0;
                for (int i = 100000; i < 110000; i++)
                {
    
                    if (bf.isExist(i.ToString()))
                    {
                        errorCount++;
                    }
                }
    

    转:https://www.cnblogs.com/yueyue184/p/10037587.html

  • 相关阅读:
    Ubiquitous Religions-并查集(5)
    The Suspects-并查集(4)
    Is It A Tree?-并查集(3)
    Html5 缓存
    HTML 5 Web 存储 localStorage
    html5画布显示图片问题
    html5画布
    html5拖动
    html5音频及视频
    linux mint的小方法
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/13251808.html
Copyright © 2011-2022 走看看