位图和布隆过滤器

位图基础:https://blog.csdn.net/lucky52529/article/details/90172264

布隆过滤器基础:https://blog.csdn.net/lucky52529/article/details/90341779

布隆过滤器公式推导:https://blog.csdn.net/qq_32834005/article/details/105540066

 

//大数据中找出现过一次的数据
	class BitMap
	{
	public:
		BitMap(size_t range)
		{
			_bitTable.resize((range >> 5) + 1);
		}

		//标识一个数字在位图中的位置
		void SetBit(size_t x)
		{
			size_t index = x >> 5;
			size_t num = x % 32;

			_bitTable[index] |= (1 << num);
		}

		//取消数字在位图当中的标识.
		void RemoveBit(size_t x)
		{
			size_t index = x >> 5;
			size_t num = x % 32;

			_bitTable[index] &= ~(1 << num);
		}


		bool TestBit(size_t x)
		{
			size_t index = x >> 5;
			size_t num = x % 32;

			return _bitTable[index] & (1 << num);
		}

	private:
		vector<int> _bitTable;
	};
//大数据中找出现过两次的数字
namespace jj01
{
	class NBitMap
	{
	public:
		NBitMap(size_t range)
		{
			_bitTable.resize((range >> 4) + 1);
		}

		void SetBit(size_t x)
		{
			size_t index = x >> 4;
			size_t num = x % 16;
			num *= 2;

			bool first = _bitTable[index] & (1 << num);
			bool second = _bitTable[index] & (1 << (num + 1));

			//if (!(first && second))
			//{
			//	_bitTable[index] += (1 << num);
			//}
			if (!(first && second)) 
			{ //if current bit is already set 1, set the (num + 1) is 1 
				if (_bitTable[index] >> num) _bitTable[index] |= (1 << (num + 1)); 
				else _bitTable[index] |= (1 << num); 
			}
		}

		bool TestBit(size_t x)
		{
			size_t index = x >> 4;
			size_t num = x % 16;
			num *= 2;

			return (_bitTable[index] >> num) & 0x02;
		}

		void RemoveBit(size_t x)
		{
			size_t index = x >> 4;
			size_t num = x % 16;
			num *= 2;
			_bitTable[index] &= ~(1 << num);
			_bitTable[index] &= ~(1 << (num + 1));

		}

	private:
		vector<int> _bitTable;
	};

  int test()
    {
	  NBitMap map(10000);
	  for (int i = 100; i < 10000; i++)
	  {
		  map.SetBit(i);
	  }
	  for (int i = 1000; i < 5000; i++)
	  {
		  map.SetBit(i);
	  }
	  bool flag = map.TestBit(899);
	  
      return 0;
    }
}

 

//布隆过滤器实现
namespace jj01
{

	class BitMap
	{
	public:
		BitMap(size_t range)
		{
			_bitTable.resize((range >> 5) + 1);
		}

		//标识一个数字在位图中的位置
		void SetBit(size_t x)
		{
			size_t index = x >> 5;
			size_t num = x % 32;

			_bitTable[index] |= (1 << num);
		}

		//取消数字在位图当中的标识.
		void RemoveBit(size_t x)
		{
			size_t index = x >> 5;
			size_t num = x % 32;

			_bitTable[index] &= ~(1 << num);
		}


		bool TestBit(size_t x)
		{
			size_t index = x >> 5;
			size_t num = x % 32;

			return _bitTable[index] & (1 << num);
		}

	private:
		vector<int> _bitTable;
	};

	template<typename T>
	struct __HashFunc1
	{
		size_t operator()(const T& s)
		{
			size_t hash = 0;
			for (int i = 0; i < s.size(); i++)
			{
				hash = hash * 131 + s[i];
			}
			return hash;
		}
	};

	template<typename T>
	struct __HashFunc2
	{
		size_t operator()(const T& s)
		{
			size_t hash = 0;
			for (int i = 0; i < s.size(); i++)
			{
				hash = hash * 65599 + s[i];
			}
			return hash;
		}
	};

	template<typename T>
	struct __HashFunc3
	{
		size_t operator()(const T& s)
		{
			size_t hash = 0;
			for (int i = 0; i < s.size(); i++)
			{
				hash = hash * 1313 + s[i];
			}
			return hash;
		}
	};

	// 布隆过滤器
	template<class K = string,
		class HashFunc1 = __HashFunc1<K>,
		class HashFunc2 = __HashFunc2<K>,
		class HashFunc3 = __HashFunc3<K>>
	class BloomFilter
	{
	public:
		BloomFilter(size_t num)
			:_bm(num * 5)
			, _size(num * 5)
		{}

		void Set(const K& key)
		{
			size_t index1 = HashFunc1()(key) % _size;
			_bm.SetBit(index1);

			size_t index2 = HashFunc2()(key) % _size;
			_bm.SetBit(index2);

			size_t index3 = HashFunc3()(key) % _size;
			_bm.SetBit(index3);
		}

		bool Test(const K& key)
		{
			size_t index1 = HashFunc1()(key) % _size;
			if (_bm.TestBit(index1) == false)
			{
				return false;
			}

			size_t index2 = HashFunc2()(key) % _size;
			if (_bm.TestBit(index2) == false)
			{
				return false;
			}

			size_t index3 = HashFunc3()(key) % _size;
			if (_bm.TestBit(index3) == false)
			{
				return false;
			}
			//所有位置都为真. 但是它是不准确的.
			return true;
		}
	private:
		BitMap _bm;
		size_t _size;
	};
  int test()
    {
	  vector<string> url{ "http://www.csdn.net/",
			"http://www.baidu.com/",
			"http://www.google.com.hk",
			"http://www.cnblogs.com/",
			"http://www.zhihu.com/",
			"https://www.shiyanlou.com/",
			"http://www.google.com.hk",
			"https://www.shiyanlou.com/",
			"http://www.csdn.net/" };
	  
	  BloomFilter<string> fliter(5);
	  
	  for (int i = 0; i < url.size(); i++)
	  {
		  fliter.Set(url[i]);
	  }

	  bool myurlFlag = fliter.Test("http://www.csdn.net/");
      return 0;
    }
}

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值