布隆过滤器:实现代码
#pragma once #include#include "BitMap.h" struct HashFunc1 { size_t BKDRHash(const char *str) { register size_t hash = 0; while (size_t ch = (size_t)*str++) { hash = hash * 131 + ch; // 也可以乘以31、131 return hash; } } size_t operator()(const string& s) { return BKDRHash(s.c_str()); } }; struct HashFunc2 { size_t SDBMHash(const char *str) { register size_t hash = 0; while (size_t ch = (size_t)*str++) { hash = 65599 * hash + ch; } return hash; } size_t operator()(const string& s) { return SDBMHash(s.c_str()); } }; struct HashFunc3 { 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 hash; } size_t operator()(const string& s) { return RSHash(s.c_str()); } }; struct HashFunc4 { size_t APHash(const char *str) { register size_t hash = 0; size_t ch; for (long i = 0; ch = (size_t)*str++; i++) { if ((i & 1) == 0) { hash ^= ((hash << 7) ^ ch ^ (hash >> 3)); } else { hash ^= (~((hash << 11) ^ ch ^ (hash >> 5))); } } return hash; } size_t operator()(const string& s) { return APHash(s.c_str()); } }; struct HashFunc5 { size_t JSHash(const char *str) { if(!*str) return 0; register size_t hash = 1315423911; while (size_t ch = (size_t)*str++) { hash ^= ((hash << 5) + ch + (hash >> 2)); } return hash; } size_t operator()(const string& s) { return JSHash(s.c_str()); } }; template class BloomFilter { public: BloomFilter(size_t size) :_bitMap(size) ,_capacity(size) {} void Set(const K& key) { size_t index1 = __HashFunc1()(key); size_t index2 = __HashFunc2()(key); size_t index3 = __HashFunc3()(key); size_t index4 = __HashFunc4()(key); size_t index5 = __HashFunc5()(key); cout< countMap; /*vector _v;*/ }; void Test1() { BloomFilter<> bf(-1); bf.Set("http://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html"); bf.Set("http://www.cnblogs.com/-clq/archive/2012/05/31/2528154.html"); bf.Set("http://www.cnblogs.com/-clq/archive/2012/05/31/2528155.html"); bf.Set("http://www.cnblogs.com/-clq/archive/2012/05/31/2528156.html"); cout< 以上
创新互联公司专注于乌当企业网站建设,响应式网站开发,商城建设。乌当网站建设公司,为乌当等地区提供建站服务。全流程定制制作,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
分享名称:布隆过滤器:实现代码
文章URL:http://cdiso.cn/article/gpcjos.html