哈希表的实现(线性探测,开散列,位图,布隆过滤器)

//线性探测,性能较差,一般不用
#include
#include
#include
using namespace std;
enum Status{
EXIST,DEL,EMPTY
};
template<class K,class V>
struct HashNode
{
pair<K,V>_val;
Status _status;
HashNode(const pair<K,V>& val=pair<K,V>())
:_val(val)
,_status(EMPTY)
{}
};
template<class K,class V>
class HashTable
{
public:
HashTable(int n=10)
{
_table.resize(n);
_size=0;
}
bool insert(const pair<K,V>& val)
{
checkCapacity();
int idx= val.first%_table.size();
while(_table[idx]._statusEXIST)
{
if(_table[idx]._val.first
val.first)
return false;
idx++;
if(idx==_table.size())
idx=0;
}
_table[idx]._val=val;
_table[idx]._status=EXIST;
_size++;
return true;
}
void checkCapacity()
{
if((_size10)/_table.size()>=7)
{
HashTable<K,V>ht(_table.size()2);
for(int i=0;i<_table.size();++i)
{
if(_table[i]._status==EXIST)
{
ht.insert(_table[i]._val);
}
}
swap(ht._table,_table);
}
}
HashNode<K,V>
find(const K& key)
{
int idx=key%_table.size();
while(_table[idx]._status!=EMPTY)
{
if(_table[idx]._val.firstkey&&_table[idx]._statusEXIST)
{
return &_table[idx];
}
idx++;
if(idx==_table.size())
idx=0;
}
return nullptr;
}
bool erase(const K& key)
{
HashNode<K,V>
ptr=find(key);
if(ptr)
{
ptr->_status=DEL;
–_size;
return true;
}
return false;
}
private:
vector<HashNode<K,V>>_table;
size_t _size;
};
int main()
{
HashTable<int,int>hs;
hs.insert(make_pair(1,1));
hs.insert(make_pair(2,1));
hs.insert(make_pair(3,1));
hs.insert(make_pair(4,1));
hs.insert(make_pair(6,1));
hs.insert(make_pair(6,1));
hs.insert(make_pair(7,1));
hs.insert(make_pair(8,1));
hs.insert(make_pair(9,1));
hs.insert(make_pair(10,1));
hs.insert(make_pair(11,1));
hs.insert(make_pair(12,1));
hs.insert(make_pair(13,1));
cout<<hs.erase(18)<<" "<<hs.erase(12)<<endl;
return 0;
}
//开列发
//我插的是链表,如果你恨牛,可以尝试插红黑树
#include
#include
#include
using namespace std;
template
struct HashNode
{
V _val;
HashNode* _next;
HashNode(const V& val=V())
:_val(val)
,_next(nullptr)
{}
};
struct StrToInt
{
size_t operator ()(const string& str)
{
size_t res=0;
for(auto& a: str)
{
res=res131+a;
}
return res;
}
};
template
struct hashFun{
size_t operator () (const K& key)
{
return key;
}
};
template<class K,class V,class KOV,class HF>
class HashTable;
template<class K,class V,class KOV,class HF>
struct HashIterator
{
typedef HashTable<K,V,KOV,HF> HT;
typedef HashNode Node;
typedef HashIterator<K,V,KOV,HF> Self;
Node
_node;
HT* _ht;
HashIterator(Node* node,HT* ht)
:_node(node)
,_ht(ht)
{}
V& operator ()
{
return _node->_val;
}
V
operator ->()
{
return &_node->_val;
}
bool operator == (const Self& p)
{
return _nodep._node;
}
bool operator !=(const Self& p)
{
return _node!=p._node;
}
Self& operator ++()
{
KOV kov;
HF hf;
Node* cur=_node->_next;
if(cur)
{
_node=_node->_next;
}
else
{
size_t idx=hf(kov(_node->_val)) % _ht->_table.size();
++idx;
for(;idx<_ht->_table.size();idx++)
{
if(_ht->_table[idx])
{
_node=_ht->_table[idx];
break;
}
}
if(idx
_ht->_table.size())
_node=nullptr;
}
return this;
}
};
template<class K,class V,class KOV,class HF>
class HashTable
{
public:
friend struct HashIterator<K,V,KOV,HF>;
typedef HashNode Node;
typedef HashIterator<K,V,KOV,HF> iterator;
iterator begin()
{
for(int i=0;i<_table.size();i++)
{
Node
cur=_table[i];
if(cur)
{
return iterator(cur,this);
}
}
return iterator(nullptr,this);
}
iterator end()
{
return iterator(nullptr,this);
}
bool insert(const V& val)
{
//检查容量
checkCapacity();

//寻找插入位置
KOV kov;
HF hf;
size_t idx=hf(kov(val)) % _table.size();
Node* cur=_table[idx];
while(cur)
{
  if(kov(cur->_val)==kov(val))
  {
    return false;
  }
  cur=cur->_next;
}
//创建新的节点并插入
cur=new Node(val);
cur->_next=_table[idx];
_table[idx]=cur;
++_size;
return true;

}
void checkCapacity()
{
if(_size==_table.size())
{
//确定大小
size_t newSize=_size==0?5:_size2;
//建立新表
vector<Node
> ht;
ht.resize(newSize);
//将旧表插入新表
//1.拿到旧表的每一个位置
KOV kov;
HF hf;
for(size_t i=0;i<_size;i++)
{
Node* cur=_table[i];
while(cur)
{
Node* next=cur->_next;
size_t idx=hf(kov(cur->_val))%newSize;
cur->_next=ht[idx];
ht[idx]=cur;
cur=next;
}
_table[i]=nullptr;
}
swap(_table,ht);
}
}
Node* find(const K& key)
{
if(_table.size()==0)
return nullptr;
HF hf;
KOV kov;
int idx=hf(key)%_table.size();
Node* cur=_table[idx];
while(cur)
{
if(kov(cur->_val)key)
{
return cur;
}
cur=cur->_next;
}
return nullptr;
}
bool erase(const K& key)
{
HF hf;
size_t idx=hf(key)%_size;
Node* cur=_table[idx];
Node* prev=cur;
KOV kov;
while(cur)
{
if(kov(cur->_val)key)
{
if(prev
nullptr)
{
_table[idx]=cur->_next;
}
else
{
prev->_next=cur->_next;
}
delete cur;
return true;
}
prev=cur;
cur=cur->_next;
}
return false;
}
private:
size_t _size=0;
vector<Node*>_table;
};
template <class K,class V,class HF=hashFun>
class Unorderedmap
{
struct KeyOfVal
{
K operator ()(const pair<K,V>& value)
{
return value.first;
}
};
public:
typedef typename HashTable<K,pair<K,V>,KeyOfVal,HF>::iterator iterator;
iterator begin()
{
return _table.begin();
}
iterator end()
{
return _table.end();
}
bool insert(const pair<K,V>& val)
{
return _table.insert(val);
}
HashNode* find(const K& key)
{
HashNode* res=_table.find(key);
if(res
nullptr)
{
return nullptr;
}
else
{
return res;
}
}
bool erase(const K& key)
{
return _table.erase(key);
}
private:
HashTable<K,pair<K,V>,KeyOfVal,HF>_table;
};
void test()
{
Unorderedmap<string,int,StrToInt> mp;
mp.insert(make_pair(“luodong”,1));
mp.insert(make_pair(“wangdingyang”,1));
mp.insert(make_pair(“xxx”,1));
mp.insert(make_pair(“gdsadasd”,1));
mp.insert(make_pair(“dasdsa”,1));
mp.insert(make_pair(“dsadsadsa”,1));
Unorderedmap<string,int,StrToInt>::iterator p=mp.begin();
while(p!=mp.end())
{
cout<first<<"---->"<second<<endl;
++p;
}
}
int main()
{
test();
return 0;
}
//位图和布隆过滤器
#include
#include
#include
using namespace std;
struct strToInt1
{
size_t operator()(const string& str)
{
size_t res=0;
for(size_t i=0;i<str.size();i++)
{
res=res131+str[i];
}
return res;
}
};
struct strToInt2
{
size_t operator()(const string& str)
{
size_t res=0;
for(size_t i=0;i<str.size();i++)
{
res=res
65535+str[i];
}
return res;
}
};
struct strToInt3
{
size_t operator()(const string& str)
{
size_t res=0;
size_t magic=63689;
for(size_t i=0;i<str.size();i++)
{
res=res%magic+str[i];
magic*=378551;
}
return res;
}
};
class BitMap
{
public:
BitMap(size_t range)
{
_table.resize(range/32+1,0);
}
//查询:
bool Test(int cnt)
{
int idx=cnt/32;
int bitIdx=cnt%32;
if(((1<<bitIdx)&(_table[idx])))
return true;
return false;
}
//存储:set
void Set(int cnt)
{
int idx=cnt/32;
int bitIdx=cnt%32;
_table[idx]=(_table[idx] | (1<<bitIdx));
}
//删除:set
void ReSet(int cnt)
{
int idx=cnt/32;
int bitIdx=cnt%32;
int rest=~(1<<bitIdx);
_table[idx]&=rest;
}
private:
vector_table;
};
template <class T,class HF1,class HF2,class HF3>
class BloomFilter
{
public:
BloomFilter(int range)
:_bit(range5)
,_bitcount(range
5)
{}
void Set(const T& val)
{
HF1 hf1;
HF2 hf2;
HF3 hf3;
size_t hashCode1=hf1(val);
size_t hashCode2=hf2(val);
size_t hashCode3=hf3(val);
_bit.Set(hashCode1%_bitcount);
_bit.Set(hashCode2%_bitcount);
_bit.Set(hashCode3%_bitcount);
}
bool Test(const T& val)
{
HF1 hf1;
HF2 hf2;
HF3 hf3;
size_t hashCode1=hf1(val);
size_t hashCode2=hf2(val);
size_t hashCode3=hf3(val);
if(_bit.Test(hashCode1%_bitcount)==false)
return false;
if(_bit.Test(hashCode2%_bitcount)==false)
return false;
if(_bit.Test(hashCode3%_bitcount)==false)
return false;
return true;
}
private:
BitMap _bit;
size_t _bitcount;
};
int main()
{
BloomFilter<string,strToInt1,strToInt2,strToInt3>bloom(100000);
bloom.Set(“asdasdada”);
bloom.Set(“luodong”);
bloom.Set(“asda”);
bloom.Set(“asdada”);
cout<<bloom.Test(“luodong”)<<bloom.Test(“wangdingyang”)<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值