【干货】位图的实现与布隆过滤器

  

,,,,位图是用一个btye位来表示一个数据是否存在,再通过哈希函数确定一个数据所在的位置,这样处理会使当仅需要判断一个数据在不在的时候大大的提高效率,缩小内存的使用,如一个数据为int型,而一个int型的数据构成的位图能表示32个数据的存在状态。代码实现如下:

位图。h:

# include   class 位图   {   公众:   位图(size_t 大小)   :_size (0)   {   大小(尺寸);   }   void 集(size_t 键)   {   size_t  index =, key /, 32;   size_t  offset =, key  %, 32;   _map(指数)=_map(指数),|,(1,& lt; & lt;,抵消);   + + _size;   }   void 重置(size_t 键)   {   size_t  index =, key /, 32;   size_t  offset =, key  %, 32;   if  ((_map(指数),在祝辞,抵消),,,1)   {   _map(指数),=,_map(指数),,,(~ (1,& lt; & lt;,抵消));   + + _size;   }   }   void 大小(size_t 大小)   {   _map.resize(大小);   }   bool 联系(size_t 键)   {   size_t  index =, key /, 32;   size_t  offset =, key  %, 32;   if  ((_map(指数),在祝辞,抵消),,,1)   return 真实;   return 假;   }   保护:   size_t  _size;   vector

,,,, <强>布隆过滤器:二进制

# include“BitMap.h”   size_t  BKDRHash (const  char  * str)//这里定义了5个映射算法,仅供参考   {   时间=register  size_t  hash  0;   while  (size_t  ch =, (size_t) * str + +)   {   时间=hash  hash  *, 131, +, ch,,,,,,,,,,,,   }   return 散列;   }   size_t  SDBMHash (const  char  * str)   {   时间=register  size_t  hash  0;   while  (size_t  ch =, (size_t) * str + +)   {   时间=hash  65599, *, hash  +, ch;//hash =, (size_t) ch  +, (hash  & lt; & lt;, 6), +, (hash  & lt; & lt;, 16),背后,散列,,,   }   return 散列;   }      size_t  RSHash (const  char  * str)   {   时间=register  size_t  hash  0;   size_t  magic =, 63689;   while  (size_t  ch =, (size_t) * str + +)   {   时间=hash  hash  *, magic  +, ch;   *=magic  378551;   }   return 散列;   }      size_t  APHash (const  char , * str)   {   时间=register  size_t  hash  0;   size_t  ch;   for  (long 小姐:=,0;,ch =, (size_t) * str + +;,我+ +)   {   if ((小姐,,,1),==,0)   {   hash  ^=, ((& lt; hash  & lt; 7), ^, ch  ^, (hash 在祝辞,3));   }   其他的   {   hash  ^=, (~ ((hash  & lt; & lt;, 11), ^, ch  ^, (hash 在祝辞,5)));   }   }   return 散列;   }   size_t  JSHash (const  char  * str)   {   if  (! * str),,,,,,,,//,以保证空字符串返回哈希值0,,   return  0;   时间=register  size_t  hash  1315423911;   while  (size_t  ch =, (size_t) * str + +)   {   hash  ^=, ((hash  & lt; & lt;, 5), +, ch  +, (hash 在祝辞,2));   }   return 散列;   }   class  BloomFilter   {   公众:   BloomFilter (size_t 大小)   :_capacity(大小)   ,地图(大小)   {}   void 集(const  string 和键)   {   size_t  index1 =, BKDRHash (key.c_str ()) % _capacity;   size_t  index2 =, SDBMHash (key.c_str ()), %, _capacity;   size_t  index3 =, RSHash (key.c_str ()), %, _capacity;   size_t  index4 =, APHash (key.c_str ()), %, _capacity;   size_t  index5 =, JSHash (key.c_str ()), %, _capacity;   map.Set (index1);   map.Set (index2);   map.Set (index3);   map.Set (index4);   map.Set (index5);   }   bool 联系(const  string 和键)   {   if  (! map.Touch (BKDRHash (key.c_str ()), %, _capacity))   return 假;   if  (! map.Touch (SDBMHash (key.c_str ()), %, _capacity))   return 假;   if  (! map.Touch (RSHash (key.c_str ()), %, _capacity))   return 假;   if  (! map.Touch (APHash (key.c_str ()), %, _capacity))   return 假;   if  (! map.Touch (JSHash (key.c_str ()), %, _capacity))   return 假;   return 真实;   }   保护:   size_t  _capacity;   BitMap 地图;   };

【干货】位图的实现与布隆过滤器