哈希表的实现

Hash表这种数据结构在许多语言中是原生的一个集合对象,在实际中用途极广,主要有这么几个特点:

访问速度快
大小不受限制
按键进行索引,没有重复对象

用字符串(id:string)检索对象(object)


C实现Hash表

今天整理以前在学校写的一些算法,翻出来一个hash表的实现,就贴出来,自己也温习温习。

先看看头文件,也就是数据结构的定义,相当于java中的接口的概念:

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2.   
  3. #define    HASHSIZE 256  
  4.   
  5. //定义hash表中的节点的类型  
  6. struct    nlist{  
  7.     struct    nlist    *next;  
  8.     char    *name;  
  9.     char    *defn;  
  10. };  
  11.   
  12. //定义接口中的函数,也就是对外来说,这个程序可以做什么  
  13. unsigned    hash(char *s);//计算一个串的hash值  
  14. struct    nlist    *lookup(char *s);//查找一个value,根据key  
  15. struct    nlist    *install(char *name,char *defn);//插入一个key=value的对象  
然后是具体实现:

[cpp]  view plain  copy
  1. #include <string.h>  
  2. #include "list.h"  
  3.   
  4. static struct nlist *hashtab[HASHSIZE];  
  5.   
  6. unsigned    hash(char *s)  
  7. {  
  8.     unsigned    hashval;  
  9.   
  10.     for(hashval = 0; *s != '\0';s++)  
  11.             hashval = *s + 31 * hashval;  
  12.     return hashval % HASHSIZE;  
  13. }  
  14.   
  15. struct    nlist    *lookup(char *s)  
  16. {  
  17.     struct    nlist    *np;  
  18.   
  19.     for(np = hashtab[hash(s)];  
  20.         np != NULL;  
  21.         np = np->next)  
  22.             if(strcmp(s,np->name) == 0)  
  23.                     return np;  
  24.     return NULL;  
  25. }  
  26.   
  27. struct    nlist    *install(char *name,char *defn)  
  28. {  
  29.     struct    nlist    *np;  
  30.     unsigned    hashval;  
  31.   
  32.     if((np = lookup(name)) == NULL){  
  33.         np = (struct nlist *)malloc(sizeof(struct nlist));  
  34.         if(np == NULL || (np->name = strdup(name)) == NULL)  
  35.                 return NULL;  
  36.         hashval = hash(name);  
  37.         np->next= hashtab[hashval];  
  38.         hashtab[hashval] = np;  
  39.     }else  
  40.         free((void *)np->defn);  
  41.     if((np->defn = strdup(defn)) == NULL)  
  42.             return NULL;  
  43.     return np;  
  44. }  
很简单,只有两个外部接口,

install(key, value),用来插入一个新的节点
lookup(key),根据一个键来进行搜索,并返回节点
代码很简单,主要用到的hash算法跟java中的String的hashcode()方法中用到的算法一样,使用:

[cpp]  view plain  copy
  1. unsigned    hash(char *s)  
  2. {  
  3.     unsigned    hashval;  
  4.   
  5.     for(hashval = 0; *s != '\0';s++)  
  6.             hashval = *s + 31 * hashval;  
  7.     return hashval % HASHSIZE;  
  8. }  
这里的31并非随意,乃是一个经验值,选取它的目的在于减少冲突,当然,hash冲突这个问题是不能根本避免的。这里只是一个人们在测试中发现的可以相对减少hash冲突的一个数字,可能以后会发现更好的数值来。

C++  STL的hash表用法

0 为什么需要hash_map
用过map吧?map提供一个很常用的功能,那就是提供key-value的存储和查找功能。例如,我要记录一个人名和相应的存储,而且随时增加,要快速查找和修改:

岳不群-华山派掌门人,人称君子剑
张三丰-武当掌门人,太极拳创始人
东方不败-第一高手,葵花宝典
...
这些信息如果保存下来并不复杂,但是找起来比较麻烦。例如我要找"张三丰"的信息,最傻的方法就是取得所有的记录,然后按照名字一个一个比较。如果要速度快,就需要把这些记录按照字母顺序排列,然后按照二分法查找。但是增加记录的时候同时需要保持记录有序,因此需要插入排序。考虑到效率,这就需要用到二叉树。讲下去会没完没了,如果你使用STL 的map容器,你可以非常方便的实现这个功能,而不用关心其细节。关于map的数据结构细节,感兴趣的朋友可以参看学习STL map, STL set之数据结构基础。看看map的实现:

[cpp]  view plain  copy
  1. #include <map>  
  2. #include <string>  
  3. using namespace std;  
  4. ...  
  5. map<string, string> namemap;  
  6.   
  7. //增加。。。  
  8. namemap["岳不群"] = "华山派掌门人,人称君子剑";  
  9. namemap["张三丰"] = "武当掌门人,太极拳创始人";  
  10. namemap["东方不败"] = "第一高手,葵花宝典";  
  11. ...  
  12.   
  13. //查找。。  
  14. if(namemap.find("岳不群") != namemap.end()){  
  15.         ...  
  16. }  
不觉得用起来很easy吗?而且效率很高,100万条记录,最多也只要20次的string.compare的比较,就能找到你要找的记录;200万条记录事,也只要用21次的比较。
速度永远都满足不了现实的需求。如果有100万条记录,我需要频繁进行搜索时,20次比较也会成为瓶颈,要是能降到一次或者两次比较是否有可能?而且当记录数到200万的时候也是一次或者两次的比较,是否有可能?而且还需要和map一样的方便使用。

答案是肯定的。这时你需要has_map. 虽然hash_map目前并没有纳入C++ 标准模板库中,但几乎每个版本的STL都提供了相应的实现。而且应用十分广泛。在正式使用hash_map之前,先看看hash_map的原理。

1 数据结构:hash_map原理
这是一节让你深入理解hash_map的介绍,如果你只是想囫囵吞枣,不想理解其原理,你倒是可以略过这一节,但我还是建议你看看,多了解一些没有坏处。

hash_map基于hash table(哈希表)。 哈希表最大的优点,就是把数据的存储和查找消耗的时间大大降低,几乎可以看成是常数时间;而代价仅仅是消耗比较多的内存。然而在当前可利用内存越来越多的 情况下,用空间换时间的做法是值得的。另外,编码比较容易也是它的特点之一。

其基本原理是:使用一个下标范围比较大的数组来存储元素。可以设计一个函数(哈希函数,也叫做散列函数),使得每个元素的关键字都与一个函数值(即 数组下标,hash值)相对应,于是用这个数组单元来存储这个元素;也可以简单的理解为,按照关键字为每一个元素“分类”,然后将这个元素存储在相应 “类”所对应的地方,称为桶。

但是,不能够保证每个元素的关键字与函数值是一一对应的,因此极有可能出现对于不同的元素,却计算出了相同的函数值,这样就产生了“冲突”,换句话说,就是把不同的元素分在了相同的“类”之中。 总的来说,“直接定址”与“解决冲突”是哈希表的两大特点。

hash_map,首先分配一大片内存,形成许多桶。是利用hash函数,对key进行映射到不同区域(桶)进行保存。其插入过程是:

得到key
通过hash函数得到hash值
得到桶号(一般都为hash值对桶数求模)
存放key和value在桶内。
其取值过程是:

得到key
通过hash函数得到hash值
得到桶号(一般都为hash值对桶数求模)
比较桶的内部元素是否与key相等,若都不相等,则没有找到。
取出相等的记录的value。
hash_map中直接地址用hash函数生成,解决冲突用比较函数解决。这里可以看出,如果每个桶内部只有一个元素,那么查找的时候只有一次比较。当许多桶内没有值时,许多查询就会更快了(指查不到的时候).

由此可见,要实现哈希表, 和用户相关的是:hash函数和比较函数。这两个参数刚好是我们在使用hash_map时需要指定的参数。

2 hash_map 使用
2.1 一个简单实例
不要着急如何把"岳不群"用hash_map表示,我们先看一个简单的例子:随机给你一个ID号和ID号相应的信息,ID号的范围是1~2的31次方。如何快速保存查找。

[cpp]  view plain  copy
  1. #include <hash_map>  
  2. #include <string>  
  3. using namespace std;  
  4. int main(){  
  5. hash_map<int, string> mymap;  
  6. mymap[9527] = "唐伯虎点秋香";  
  7. mymap[1000000] = "百万富翁的生活";  
  8. mymap[10000] = "白领的工资底线";  
  9. ...  
  10. if(mymap.find(10000) != mymap.end()){  
  11. ...  
  12. }  
够简单,和map使用方法一样。这时你或许会问?hash函数和比较函数呢?不是要指定么?你说对了,但是在你没有指定hash函数和比较函数的时候,你会有一个缺省的函数,看看hash_map的声明,你会更加明白。下面是SGI STL的声明:
[cpp]  view plain  copy
  1. template <class _Key, class _Tp, class _HashFcn = hash<_Key>,  
  2. class _EqualKey = equal_to<_Key>,  
  3. class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >  
  4. class hash_map  
  5. {  
  6. ...  
  7. }  
也就是说,在上例中,有以下等同关系:
...
hash_map<int, string> mymap;
//等同于:
hash_map<int, string, hash<int>, equal_to<int> > mymap;
Alloc我们就不要取关注太多了(希望深入了解Allocator的朋友可以参看标准库 STL :Allocator能做什么)
2.2 hash_map 的 hash函数
hash< int>到底是什么样子?看看源码:

struct hash<int> {
size_t operator()(int __x) const { return __x; }
};
原来是个函数对象。在SGI STL中,提供了以下hash函数:
struct hash<char*>
struct hash<const char*>
struct hash<char> 
struct hash<unsigned char> 
struct hash<signed char>
struct hash<short>
struct hash<unsigned short> 
struct hash<int> 
struct hash<unsigned int>
struct hash<long> 
struct hash<unsigned long>
也就是说,如果你的key使用的是以上类型中的一种,你都可以使用缺省的hash函数。当然你自己也可以定义自己的hash函数。对于自定义变量,你只能如此,例如对于string,就必须自定义hash函数。例如:

[cpp]  view plain  copy
  1. struct hash_string{  
  2. size_t operator()(const string& str) const  
  3. {  
  4.     unsigned long __h = 0;  
  5.     for (size_t i = 0 ; i < str.size() ; i ++)  
  6. <span style="white-space:pre">  </span>__h = 5*__h + str[i];  
  7. <span style="white-space:pre">  </span>return size_t(__h);  
  8. }  
  9. };  
//如果你希望利用系统定义的字符串hash函数,你可以这样写:
[cpp]  view plain  copy
  1. struct hash_string{  
  2. size_t operator()(const string& str) const  
  3. {  
  4.     return __stl_hash_string(str.c_str());  
  5. }  
  6. };  
在Visual Studio下,hash function 和 equal function 在一个结构中,不想SGI的是分开的。
[cpp]  view plain  copy
  1. struct hash_string  
  2. {  
  3.     static const size_t bucket_size = 4;   
  4.     static const size_t min_buckets = 8;  
  5.     // 1. define the hash function  
  6.     size_t operator()(const string& str) const  
  7.     {  
  8. <span style="white-space:pre">  </span>unsigned long __h = 0;  
  9. <span style="white-space:pre">  </span>for (size_t i = 0 ; i < str.size() ; i ++)  
  10. <span style="white-space:pre">  </span>    __h = 5*__h + str[i];  
  11. <span style="white-space:pre">  </span>    return size_t(__h);  
  12. <span style="white-space:pre">  </span>}  
  13.      1. define the hash function  
  14.     //size_t operator()(const string& str) const  
  15.     //{  
  16. <span style="white-space:pre">  </span>// return __stl_hash_string(str.c_str());  
  17.     //}  
  18.     // 2. define the equal function  
  19.     bool operator()(const string& p1, const string& p2) const{  
  20. <span style="white-space:pre">  </span>return p1 == p2;  
  21.     }  
  22. };  
在声明自己的哈希函数时要注意以下几点:
使用struct,然后重载operator();
返回是size_t;
参数是你要hash的key的类型;
函数是const类型的。
如果这些比较难记,最简单的方法就是照猫画虎,找一个函数改改就是了。

现在可以对开头的"岳不群"进行哈希化了 . 直接替换成下面的声明即可:

map<string, string> namemap; 
//改为:
hash_map<string, string, hash_string> namemap;
其他用法都不用边。当然不要忘了吧hash_string的声明以及头文件改为hash_map。
你或许会问:比较函数呢?别着急,这里就开始介绍hash_map中的比较函数。

2.3 hash_map 的 比较函数
在map中的比较函数,需要提供less函数。如果没有提供,缺省的也是less< Key> 。在hash_map中,要比较桶内的数据和key是否相等,因此需要的是是否等于的函数:equal_to< Key> 。先看看equal_to的源码:

//本代码可以从SGI STL
//先看看binary_function 函数声明,其实只是定义一些类型而已。

[cpp]  view plain  copy
  1. template <class _Arg1, class _Arg2, class _Result>  
  2. struct binary_function {  
  3.     typedef _Arg1 first_argument_type;  
  4.     typedef _Arg2 second_argument_type;  
  5.     typedef _Result result_type;  
  6. };  
  7. //看看equal_to的定义:  
  8. template <class _Tp>  
  9. struct equal_to : public binary_function<_Tp,_Tp,bool>  
  10. {  
  11.     bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }  
  12. };  
如果你使用一个自定义的数据类型,如struct mystruct, 或者const char* 的字符串,如何使用比较函数?
使用比较函数,有两种方法.

第一种是:重载==操作符,利用equal_to;看看下面的例子:

struct mystruct{
int iID;
int len;
bool operator==(const mystruct & my) const{
return (iID==my.iID) && (len==my.len) ;
}
};
这样,就可以使用equal_to< mystruct>作为比较函数了。
另一种方法就是:使用函数对象。自定义一个比较函数体:

struct compare_str{
bool operator()(const char* p1, const char*p2) const{
return strcmp(p1,p2)==0;
}
};
有了compare_str,就可以使用hash_map了。
typedef hash_map<const char*, string, hash<const char*>, compare_str> StrIntMap;
StrIntMap namemap;
namemap["岳不群"]="华山派掌门人,人称君子剑";
namemap["张三丰"]="武当掌门人,太极拳创始人";
namemap["东方不败"]="第一高手,葵花宝典";
2.4 hash_map 函数
hash_map的函数和map的函数差不多。具体函数的参数和解释,请参看:STL 编程手册:Hash_map,这里主要介绍几个常用函数。

hash_map(size_type n) 如果讲究效率,这个参数是必须要设置的。n 主要用来设置hash_map 容器中hash桶的个数。桶个数越多,hash函数发生冲突的概率就越小,重新申请内存的概率就越小。n越大,效率越高,但是内存消耗也越大。
const_iterator find(const key_type& k) const. 用查找,输入为键值,返回为迭代器。
data_type& operator[](const key_type& k) . 这是我最常用的一个函数。因为其特别方便,可像使用数组一样使用。不过需要注意的是,当你使用[key ]操作符时,如果容器中没有key元素,这就相当于自动增加了一个key元素。因此当你只是想知道容器中是否有key元素时,你可以使用find。如果你希望插入该元素时,你可以直接使用[]操作符。
insert 函数。在容器中不包含key值时,insert函数和[]操作符的功能差不多。但是当容器中元素越来越多,每个桶中的元素会增加,为了保证效率,hash_map会自动申请更大的内存,以生成更多的桶。因此在insert以后,以前的iterator有可能是不可用的。
erase 函数。在insert的过程中,当每个桶的元素太多时,hash_map可能会自动扩充容器的内存。但在sgi stl中是erase并不自动回收内存。因此你调用erase后,其他元素的iterator还是可用的。


3 相关hash容器
hash 容器除了hash_map之外,还有hash_set, hash_multimap, has_multiset, 这些容器使用起来和set, multimap, multiset的区别与hash_map和map的区别一样,我想不需要我一一细说了吧。

4 其他
这里列几个常见问题,应该对你理解和使用hash_map比较有帮助。

4.1 hash_map 和 map的区别在哪里?
构造函数。hash_map需要hash函数,等于函数;map只需要比较函数(小于函数).
存储结构。hash_map采用hash表存储,map一般采用红黑树(RB Tree)实现。因此其memory数据结构是不一样的。
4.2 什么时候需要用hash_map,什么时候需要用map?
总体来说,hash_map 查找速度会比map快,而且查找速度基本和数据数据量大小,属于常数级别;而map的查找速度是log(n)级别。并不一定常数就比log(n)小,hash还有hash函数的耗时,明白了吧,如果你考虑效率,特别是在元素达到一定数量级时,考虑考虑hash_map。但若你对内存使用特别严格,希望程序尽可能少消耗内存,那么一定要小心,hash_map可能会让你陷入尴尬,特别是当你的hash_map对象特别多时,你就更无法控制了,而且hash_map的构造速度较慢。

现在知道如何选择了吗?权衡三个因素: 查找速度, 数据量, 内存使用。

这里还有个关于hash_map和map的小故事,看看:http://dev.csdn.net/Develop/article/14/14019.shtm



4.3 如何在hash_map中加入自己定义的类型?
你只要做两件事, 定义hash函数,定义等于比较函数。下面的代码是一个例子:

-bash-2.05b$ cat my.cpp
[python]  view plain  copy
  1. #include <string>  
  2. #include <iostream>  
  3. using namespace std;  
  4. // just for "#include <hash_map>" in linux  
  5. #if __GNUC__>2  
  6. #include <ext/hash_map>  
  7. using __gnu_cxx::hash_map;  
  8. #else  
  9. #include <hash_map>  
  10. #endif  
  11. // 0 define the class  
  12. class ClassA{  
  13. public:  
  14. ClassA(int a):c_a(a){}  
  15. int getvalue()const { return c_a;}  
  16. void setvalue(int a){c_a = a;}  
  17. private:  
  18. int c_a;  
  19. };  
  20.   
  21. // 1 define the hash function  
  22. struct hash_A{  
  23. size_t operator()(const class ClassA & A)const{  
  24. // return hash<int>(classA.getvalue());  
  25. return A.getvalue();  
  26. }  
  27. };  
  28.   
  29. // 2 define the equal function  
  30. struct equal_A{  
  31. bool operator()(const class ClassA & a1, const class ClassA & a2)const{  
  32. return a1.getvalue() == a2.getvalue();  
  33. }  
  34. };  
  35.   
  36. int main()  
  37. {  
  38. hash_map<ClassA, string, hash_A, equal_A> hmap;  
  39. ClassA a1(12);  
  40. hmap[a1]="I am 12";  
  41. ClassA a2(198877);  
  42. hmap[a2]="I am 198877";  
  43.   
  44. cout<<hmap[a1]<<endl;  
  45. cout<<hmap[a2]<<endl;  
  46. return 0;  
  47. }  


-bash-2.05b$ make my
c++ -O -pipe -march=pentiumpro my.cpp -o my
-bash-2.05b$ ./my
I am 12
I am 198877

在Visual Studio下,hash function 和 equal function 在一个结构中,不想SGI的是分开的。
class MyClass 

[cpp]  view plain  copy
  1. {   
  2. ....   
  3. };   
  4.   
  5. struct my_hash   
  6. {   
  7.     static const size_t bucket_size = 4;   
  8.     static const size_t min_buckets = 8;   
  9.     size_t operator()(const MyClass& Key) const   
  10.     {   
  11. <span style="white-space:pre">  </span>size_t hash = 999;   
  12. <span style="white-space:pre">  </span>for (size_t i = 0; i < 100000; i++)   
  13. <span style="white-space:pre">  </span>    hash = "hash function";   
  14. <span style="white-space:pre">  </span>    return hash;   
  15.     }   
  16.   
  17.     bool operator()(const MyClass& c1, const MyClass& c2) const   
  18.     {   
  19. <span style="white-space:pre">  </span>return "equal function";   
  20.     }   
  21. };   
  22.   
  23. int main()   
  24. {   
  25.     hash_map<MyClass, int, my_hash> my;   
  26.     ......   
  27. }  
4.4如何用hash_map替换程序中已有的map容器?
这个很容易,但需要你有良好的编程风格。建议你尽量使用typedef来定义你的类型:

typedef map<Key, Value> KeyMap;
当你希望使用hash_map来替换的时候,只需要修改:
typedef hash_map<Key, Value> KeyMap;
其他的基本不变。当然,你需要注意是否有Key类型的hash函数和比较函数。
4.5为什么hash_map不是标准的?
具体为什么不是标准的,我也不清楚,有个解释说在STL加入标准C++之时,hash_map系列当时还没有完全实现,以后应该会成为标准。如果谁知道更合理的解释,也希望告诉我。但我想表达的是,正是因为hash_map不是标准的,所以许多平台上安装了g++编译器,不一定有hash_map的实现。我就遇到了这样的例子。因此在使用这些非标准库的时候,一定要事先测试。另外,如果考虑到平台移植,还是少用为佳。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值