位图和布隆过滤器以及哈希切分

今天分享一下,有关于位图和布隆过滤器以及哈希切分的相关问题。
位图简单来说就是以一个bit位来表示一个整形数字存在或者不存在,它适用于在大数据中简单查找确定一个整形数字是否存在?
其实位图与哈希表的直接定值法的原理是一致的,哈希表的定值是一个具体的类型,而位图定值到一个bit上,用该bit位上的0或1来表示某个整形数字存在或者不存在。like that.


位图代码的简单实现如下:
Bitset.h
#pragma once
#include<vector>
 
class Bitset
{
public:
Bitset(int size)
{
int index = (size>>5)+1;
_a.resize(index);
}
 
~Bitset()
{
}
 
void Set(const int num)//置1
{
size_t index = num>>5;//下标
if(index >= _a.size())
{
return ;
}
 
int pos = num % 32;
_a[index] |= 1<<pos;
}
 
void Reset(const int num)//置0//删除
{
size_t  index = num>>5;//下标
if(index >= _a.size())
{
return ;
}
 
int pos = num % 32;
_a[index] &= ~(1<<pos);
}
 
bool Test(const int num)
{
size_t  index = num>>5;//下标
if(index >= _a.size())
{
return false;
}
 
int pos = num % 32;
if((_a[index] &= 1<<pos) == 0)
return false;
 
return true;
}
 
//bool operator[](const int num)
//{
// return Test(num);
//}
protected:
vector<int> _a;
};
 
void TestBitset()//测试位图
{
Bitset s(1000);
 
s.Set(22);
s.Set(112);
s.Set(221);
s.Set(999);
 
cout<<"s.Test(22)"<<s.Test(22)<<endl;
cout<<"s.Test(12)"<<s.Test(12)<<endl;
cout<<"s.Test(122)"<<s.Test(122)<<endl;
cout<<"s.Test(112)"<<s.Test(112)<<endl;
cout<<"s.Test(221)"<<s.Test(221)<<endl;
cout<<"s.Test(212)"<<s.Test(212)<<endl;
cout<<"s.Test(2)"<<s.Test(2)<<endl;
cout<<"s.Test(999)"<<s.Test(999)<<endl;
 
s.Reset(22);
cout<<"s.Reset(22)->s.Test(22)"<<s.Test(22)<<endl;
 
 
}
run.cpp
#include<iostream>
using namespace std;
 
#include"Bitset.h"
 
int main()
{
cout<<"***测试位图***"<<endl;
TestBitset();//测试位图
 
system("pause");
return 0;
}

位图适用于判断整形数字存在于否,而布隆过滤器普遍来说可以判断任何类型的实例化对象是否存在。布隆过滤器可以准确判断对象不存在,但是它的缺点是判断对象存在时可能存在误判。
布隆过滤器的底层依旧是一个位图,布隆过滤器首先利用(字符串)哈希转整形的方法把任意类型转化为整形,在此过程中可能导致转化的整形是相同的,于是位图在直接定值时,可能把几个不同对象的值映射到相同位置,即在确定某个并不存在的对象时,找到映射位置已存在的(某一个存在的其他经过哈希转换映射到这个位置),从而导致误判。为了降低映射冲突的误判率,我们把一个对象用不同的哈希转换映射几个位置,查找时,只有所有转化映射的位置都存在,才能判断其存在。但是纵使如此也无法完全避免映射冲突。即存在误判的情况仍然会发生。
图解如下:


布隆过滤器代码如下:
Bloomfilter.h
#pragma once
#include"Bitset.h"
 
//布隆过滤器//
struct HashString1
{
size_t BKDRHash(const char *str)  
{  
register size_t hash = 0;  
while (size_t ch = (size_t)*str++)  
{         
hash = hash * 131 + ch;   // 也可以乘以31、131、1313、13131、131313..          
}  
return hash;  
}
 
size_t operator()(const string& key)
{
return BKDRHash(key.c_str());
}
};
 
struct HashString2
{
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& key)
{
return SDBMHash(key.c_str());
}
};
 
struct HashString3
{
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& key)
{
return RSHash(key.c_str());
}
};
 
template<class K= string, class HashStr1 = HashString1,
 class HashStr2 = HashString2, class HashStr3 = HashString3>
class BloomFilter//简单的布隆,无引用计数,不能实现Rset
{
public:
BloomFilter(size_t n)
:_bs(n*6)
,_size(n*6)
{
}
 
~BloomFilter(){}
//set,test
void Set(const K& num)
{
size_t pos1 = HashStr1()(num);//仿函数要创造临时对象
size_t pos2 = HashStr2()(num);
size_t pos3 = HashStr3()(num);
 
_bs.Set(pos1%_size);//pos1可能会大于_size
_bs.Set(pos2%_size);
_bs.Set(pos3%_size);
 
}
 
bool Test(const K& num)
{
size_t pos1 = HashStr1()(num);
if(!_bs.Test(pos1%_size))
return false;
 
size_t pos2 = HashStr2()(num);
if(!_bs.Test(pos1%_size))
return false;
size_t pos3 = HashStr3()(num);
if(!_bs.Test(pos1%_size))
return false;
 
return true;
}
protected:
Bitset _bs;
size_t _size;//计数
};
 
void TestBloom()//测试布隆过滤器
{
BloomFilter<> _bf(1000);
 
_bf.Set("solf");
_bf.Set("sort");
_bf.Set("good");
_bf.Set("parent");
_bf.Set("uhper");
 
cout<<"solf?"<<_bf.Test("solf")<<endl;
cout<<"sort?"<<_bf.Test("sort")<<endl;
cout<<"ptres?"<<_bf.Test("ptres")<<endl;
cout<<"parent?"<<_bf.Test("parent")<<endl;
cout<<"uhper?"<<_bf.Test("uhper")<<endl;
}
run.cpp
#include<iostream>
using namespace std;
 
#include"BloomFilter.h"
 
int main()
{
cout<<"***测试布隆过滤器***"<<endl;
TestBloom();//测试布隆过滤器
 
system("pause");
return 0;
}

而当大数据中的数据实在太大,即使使用位图也无法在内存中放下,或者需要确定某数是否存在以及统计次数时(暂不适合用位图时),就需要用哈希切分,把一个大数据文件切分为若干内存能够容纳的小文件分别进行相应的操作等等,完成既定目标。
哈希切分思路:
预计把文件N分能够存放到内存(因为哈希切分的文件不一样大,所以尽量把文件分的多一点点),把文件中的对象用哈希字符串转换的方式转化,再分别映射到相应的文件编号,即把该对象写入该文件。知道吧大数据文件全部哈希切分成N个适合大小的文件即可。
但如果只是随意切分,可能依旧可能达不到切分处理的效果,内存可能还是无法存放文件对象并进行相应的操作,也就无法完成哈希切分的目的。


but,有些时候,完成一个目的不一定只有一种方法,条条大路通罗马,算法种类繁杂,多种多样,需要多学习多联系,如果你有更好的思路,期待一起分享探讨。

分享到此为止,thanks!愿各位学得开心!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值