#include <iostream>
#include <cstring>
#include <string>
typedef unsigned int SIZE_T;
using namespace std; /** HashMap模板的C++实现,用拉链法解决冲突
*注意:需要为每一种KeyType两个仿函数:HashFunc and EqualKey
*/ template<typename KeyType, typename ValueType, typename HashFunc, typename EqualKey>
class HashMap{
#define DEFAULT_CAPACITY 43 // hash表初始化容量
#define LOADFACTOR 0.7 //负载因子 class KeyNode{ //存放key value对
public:
KeyType key;
ValueType value;
KeyNode *next;
KeyNode(){}
KeyNode(const KeyNode & rhs){
Assign(rhs);
}
const KeyNode & operator =(const KeyNode & rhs){
Assign(rhs);
return *this;
}
void Assign(const KeyNode & rhs){
this->key = rhs.key;
this->value = rhs.value;
this->next = rhs.next;
}
};
public:
HashMap(){
table = new KeyNode* [DEFAULT_CAPACITY];
::memset(table,,DEFAULT_CAPACITY * sizeof(KeyNode*));
size =;
capacity = DEFAULT_CAPACITY;
hash = HashFunc();
equal = EqualKey();
}
bool put(const KeyType & key, const ValueType & value){
SIZE_T index = hash(key) % capacity;
if(table[index] == NULL){
KeyNode *temp = new KeyNode();
temp->key = key;
temp->value = value;
temp->next =NULL;
table[index] = temp; }else{
KeyNode * pre=table[index]; while(pre->next != NULL){
if(equal(pre->key, key)) return false; //重复
pre = pre->next;
}
if(equal(pre->key, key)) return false; //重复
KeyNode *temp = new KeyNode();
temp->key = key;
temp->value = value;
temp->next =NULL;
pre->next = temp;
}
size ++;
if(size*1.0/capacity > LOADFACTOR) this->rehash();
return true;
}
bool remove(const KeyType & key){
SIZE_T index = hash(key) % capacity;
if(table[index] == NULL) return false;
KeyNode *temp = table[index];
if(equal(temp->key,key)){
table[index] = temp->next;
delete temp;
size --;
return true;
}
while(temp->next != NULL){
if(equal(temp->next->key, key)){
KeyNode * del = temp->next;
temp->next = del->next;
delete del;
size --;
return true;
}else{
temp = temp->next;
}
}
return false;
}
bool exist(const KeyType & key)const{
SIZE_T index = hash(key) % capacity;
if(table[index] == NULL) return false;
KeyNode *temp = table[index];
while(temp!= NULL){
if(equal(temp->key,key)) return true;
temp = temp->next;
}
return false;
}
/*
KeyNode find(const KeyType & key){
SIZE_T index = hash(key) % capacity;
if(table[index] == NULL) return NULL;
KeyNode *temp = table[index];
while(temp!= NULL){
if(equal(temp->key,key)) return *temp;
temp = temp->next;
}
return NULL;
}
*/
ValueType & operator[](const KeyType & key){
if(!exist(key)) put(key,ValueType());
return get(key);
}
SIZE_T Size(){
return size;
}
void display(){
cout << "size:"<<size<<endl;
for(SIZE_T i=;i<this->capacity;i++){
KeyNode * temp = table[i];
while(temp != NULL){
cout <<"("<<temp->key<<","<<temp->value<<") ";
temp = temp->next;
}
if(table[i]!=NULL) cout << endl;
}
}
~HashMap(){
this->destroy(table);
} private:
KeyNode ** table;
SIZE_T capacity;
SIZE_T size;
HashFunc hash;
EqualKey equal;
KeyNode ERROE; ValueType & get(const KeyType key){
SIZE_T index = hash(key) % capacity;
if(table[index] == NULL) return ERROE.value;
KeyNode *temp = table[index];
while(temp!= NULL){
if(equal(temp->key,key)) return temp->value;
temp = temp->next;
}
return ERROE.value;
}
//未实现
void rehash(){
//得到一个两倍左右大小的capacity(最好为素数),重新散列
}
void destroy(KeyNode ** hashtable){
for(SIZE_T i=;i<this->capacity;i++){
KeyNode * temp = hashtable[i];
while(temp != NULL){
KeyNode *del = temp;
temp = temp->next;
delete del;
}
}
delete []hashtable;
}
}; class HashFuncInt{
public:
SIZE_T operator()( int key)const{
return key;
}
}; class EqualFuncInt{
public:
bool operator()(int keyA, int keyB)const{
return keyA == keyB;
}
}; int main()
{
const int scale = ;
//内存泄露性能测试
while(false){
HashMap<int,int,HashFuncInt,EqualFuncInt> hm;
for(int i=;i<scale;i++){
hm.put(i,i*(i+));
}
hm.display();
for(int i=;i<scale;i+=)
hm.remove(i);
hm.display();
for(int i=;i<scale;i+=)
hm[i]=i*;
hm.display();
} //模板实用性测试 return ;
}

参考文献:

最新文章

  1. C#计算代码执行时间
  2. luogu10125回文数[noip1999 Day1 T1]
  3. word20161130
  4. Yii2 RBAC 用到的表
  5. java的nio之:java的nio的服务器实现模型
  6. KineticJS教程(1-2)
  7. 组合数学:容斥原理(HDU1976)
  8. 用JSON 和 Google 实现全文翻译
  9. Python 自动化脚本学习(一)
  10. Windows Server 2012 安装dll到GAC
  11. C# 禁止ALT+F4(钩子)
  12. 关于Docker中的Images与Containers
  13. python高阶函数式编程
  14. Android NDK开发三:java和C\C++交互
  15. 解决vue在ios或android中用webview打开H5链接时#号后面的参数被忽略问题angular同样适用
  16. 洛谷P5108 仰望半月的夜空(后缀数组)
  17. go-001[常用命令]
  18. 【精解】EOS TPS 多维实测
  19. Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
  20. git 录制简单实用好工具 LICEcap

热门文章

  1. webpack.config.js====插件purifycss-webpack,提炼css文件
  2. Python定时任务sched(一)
  3. They say Rome wasn&#39;t built in a day, and yet what a difference a day makes.
  4. 3D OpenGL ES
  5. POJ 1651 Multiplication Puzzle (区间DP,经典)
  6. 51nod 1101 换零钱
  7. Java的三大特性之继承
  8. [机器视觉] SIFT特征-尺度不变特征理解
  9. python 遍历list
  10. web.xml 中 resource-ref 的注意事项