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

  • 相关阅读:
    关于以追加模式写入文件时,为什么第一行是空行?
    使用正则表达式的技巧
    super关键字的三种用法
    Excel快捷键
    重载与重写的区别
    方法重名
    区分子类方法中重名的三种变量
    继承
    Chapter08【String类、static、Arrays类、Math类】
    Math类
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/13251808.html
Copyright © 2011-2022 走看看