資源簡介
C++實現的布隆過濾器,其中使用到的bitset也是自己簡單實現的一個BitContainer??梢蕴幚砬f條到億條記錄的存在性判斷。做成dll可以在很多場合使用,如自己寫爬蟲,要判斷一個url是否已經訪問過,判斷一個單詞是否在某個字典內,當集合很大的時候,用布隆過濾器很有優勢,不過使用前,請了解它的優缺點(缺點是有一定的誤判率)

代碼片段和文件信息
#include?“bloomfilter.h“
typedef?unsigned?long?ULONG;
ULONG?BloomFilter::hashCode(const?char*?strint?n)
{
ULONG?seeds[]?=?{1323314151617173798387};?//
ULONG?seed?=?seeds[n];
ULONG?hash?=?0;
while?(*str)
{
hash?=?hash?*?seed?+?(*str++);
}
return?(hash?&?0x7FFFFFFF)%?_mem;?//確保得到的值不會數組越界
}
bool?BloomFilter::add(const?char*?str)
{
if(NULL?==?str)
return?false;
if(exists(str))
return?false;
for(int?i=0;i<_k;++i)
{
ULONG?hcode?=?hashCode(stri);
_bitset.set(hcode);
}
return?true;
}
bool?BloomFilter::exists(const?char*?str)
{
bool?result=false;
if(NULL?==?str)
return?result;
for(int?i=0;i<_k;++i)
{
ULONG?hc?=?hashCode(stri);
if(!?_bitset.test(hc))
{
return?result;
}
}
return?true;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????456??2011-11-20?11:00??test.cpp
?????文件???????1616??2011-11-20?09:51??bitcontainer.h
?????文件????????687??2011-11-20?10:38??bloomfilter.h
?????文件????????807??2011-11-20?10:37??bloomfilter.cpp
-----------?---------??----------?-----??----
?????????????????3566????????????????????4
- 上一篇:文件MD5查看器工具軟件
- 下一篇:MFC實現填充算法
評論
共有 條評論