Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1891600
  • 博文数量: 152
  • 博客积分: 3730
  • 博客等级: 上尉
  • 技术积分: 3710
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-02 14:36
个人简介

减肥,运动,学习,进步...

文章分类

全部博文(152)

文章存档

2016年(14)

2015年(17)

2014年(16)

2013年(4)

2012年(66)

2011年(35)

分类: C/C++

2012-08-28 11:55:55

散列是数组存储方式的一种发展,相比数组,散列的数据访问速度要高于数组,因为可以依据存储数据的部分内容找到数据在数组中的存储位置,进而能够快速实现数据的访问,理想的散列访问速度是非常迅速的,而不像在数组中的遍历过程,采用存储数组中内容的部分元素作为映射函数的输入,映射函数的输出就是存储数据的位置,这样的访问速度就省去了遍历数组的实现,因此时间复杂度可以认为为O(1),而数组遍历的时间复杂度为O(n)

 

散列是能一种快速实现访问的存储方式。通常作为检索部分的数据项是整形或者字符串,当是字符串时,字符串的数量要远远大于数组的长度,这时候就会有多个字符串映射到一个存储位置的情况,这就是所谓的冲突问题,而且冲突时肯定存在的,这时候如何实现数据的存储又是需要解决的。

目前主要的解决方式有两大类,第一种采用链表的形式,将所有冲突的数据项采用链表的形式链接起来,这样搜索数据的复杂度就包含了链表的遍历问题,特别是当所有的项都链接到一个链表下时,这时候实际上就是遍历链表,复杂度并不一定有很大的进步,但是这种链表链接的方式有很高的填充率。第二种就是充分利用没有实现的存储空间,利用探测法探测空闲的空间,进而实现数据的存储,目前有三种探测方式:线性探测法、平方探测法,以及双散列法,三种方式中平方探测法运用比较多,但是都存在各种各样的优缺点,这时候的散列搜索优势就没有理想情况下那么明显。有时候甚至比遍历数组更加的慢。但是确实不失为一种处理方式。

映射函数可选择的比较多,其实完全可以定义自己的映射函数,但是有时候为了降低冲突的概率设置了一些比较好的映射函数,比如求和取余,或者乘以一定的系数再求和取余等。

 

本文采用平方探测法解决了冲突问题,具体的实现如下所示:

1、结构体定义

点击(此处)折叠或打开

  1. #ifndef __HASHMAP_H_H_
  2. #define __HASHMAP_H_H_

  3. #include "list.h"

  4. #define TABSIZE    101

  5. /*状态变量*/
  6. typedef enum STATE{EMPTY = 0, ACTIVE = 1, DELETED = 2} State;

  7. /*键值结构体*/
  8. typedef struct _pair
  9. {
  10.     char *key;
  11.     char *value;
  12. }Pair_t, *Pair_handle_t;

  13. /*每一个实际的存储对象*/
  14. typedef struct _hashEntry
  15. {
  16.     Pair_handle_t pair;
  17.     State state;
  18. }HashEntry_t, *HashEntry_handle_t;

  19. /*哈希表结构体,便于创建*/
  20. typedef struct _hashmap
  21. {
  22.     HashEntry_t *map;
  23.     /*存储实际的存储量*/
  24.     int size;
  25.     /*容量*/
  26.     int capacity;
  27. }Hashmap_t, *Hashmap_handle_t;

  28. /*隐射函数类型定义*/
  29. typedef int(*hashfunc)(const char *, int);

  30. #ifdef __cplusplus
  31. extern "C"
  32. {
  33. #endif

  34. bool alloc_hashmap(Hashmap_handle_t *hashmap, int capacity);
  35. bool init_hashmap(Hashmap_handle_t hashmap, int capacity);
  36. bool insert_hashnode(Hashmap_handle_t hashmap, const char *key, const char *value);
  37. Pair_handle_t search_hashnode(Hashmap_handle_t hashmap, const char *key);
  38. char *GetValue(Hashmap_handle_t hashmap, const char *key);
  39. bool delete_hashnode(Hashmap_handle_t hashmap, const char *key);
  40. int Length(Hashmap_handle_t hashmap);
  41. int Capacity(Hashmap_handle_t hashmap);
  42. void delete_hashmap(Hashmap_handle_t hashmap);
  43. void free_hashmap(Hashmap_handle_t *hashmap);
  44. char *key_pair(Pair_handle_t pair);
  45. char *value_pair(Pair_handle_t pair);
  46. Hashmap_handle_t copy_hashmap(Hashmap_handle_t hashmap);
  47. bool resize(Hashmap_handle_t hashmap);

  48. #ifdef __cplusplus
  49. }
  50. #endif
  51. #endif
实现表的分配和创建,采用了动态分配的方式实现,这样可能在性能上比不上静态数据,但是为了实现数组大小的调整,我选择了动态分配的实现方式。

点击(此处)折叠或打开

  1. /*分配一个新的对象,可以实现自动分配*/
  2. bool alloc_hashmap(Hashmap_handle_t *hashmap, int capacity)
  3. {
  4.     HashEntry_handle_t temp = NULL;
  5.     Hashmap_t * map = NULL;

  6.     if(*hashmap == NULL)
  7.     {
  8.         /*分配一个散列对象*/
  9.         map = (Hashmap_handle_t)malloc(sizeof(Hashmap_t));
  10.         if(map == NULL)
  11.             return false;
  12.         /*指针指向当前对象*/
  13.         *hashmap = map;
  14.         map = NULL;

  15.         /*分配一个数组空间,大小可以控制*/
  16.         temp = (HashEntry_handle_t)malloc(
  17.             sizeof(HashEntry_t)*capacity);
  18.         if(temp != NULL)
  19.         {
  20.             /*散列对象的指针指向数组*/
  21.             (*hashmap)->map = temp;
  22.             temp = NULL;
  23.             /*设置参数*/
  24.             (*hashmap)->capacity = capacity;
  25.             (*hashmap)->size = 0;
  26.             /*初始化分配的数组空间*/
  27.             Tabinital((*hashmap)->map,capacity);
  28.             return true;
  29.         }
  30.     }
  31.     return false;
  32. }

  33. /*初始化一个新的对象,这个对象已经创建,只是没有初始化而已*/
  34. bool init_hashmap(Hashmap_handle_t hashmap, int capacity)
  35. {
  36.     HashEntry_handle_t temp = NULL;
  37.     
  38.     if(hashmap != NULL)
  39.     {
  40.         /*分配数组空间*/
  41.         temp = (HashEntry_handle_t)malloc(
  42.                 sizeof(HashEntry_t)*capacity);
  43.         if(temp != NULL)
  44.         {
  45.             /*完成对象的填充操作*/
  46.             hashmap->map = temp;
  47.             temp = NULL;
  48.             hashmap->capacity = capacity;
  49.             hashmap->size = 0;
  50.             /*初始化数组对象*/
  51.             Tabinital(hashmap->map,capacity);
  52.             return true;
  53.         }
  54.     }
  55.     return false;
  56. }
关于数组中对象的创建,和释放操作,如下所示:

点击(此处)折叠或打开

  1. /*分配一个pair对象*/
  2. static bool make_pair(Pair_handle_t *pair, const char *key, const char *value)
  3. {
  4.     Pair_handle_t newpair =(Pair_handle_t)malloc(sizeof(Pair_t));
  5.     char *newstr = NULL;

  6.     if(newpair == NULL)
  7.         return false;
  8.     
  9.     newstr = (char *)malloc(strlen(key) + 1);
  10.     if(newstr == NULL)
  11.         return false;

  12.     strcpy(newstr, key);
  13.     newstr[strlen(key)] = '\0';
  14.     newpair->key = newstr;
  15.     newstr = NULL;

  16.     newstr = (char *)malloc(strlen(value) + 1);
  17.     if(newstr == NULL)
  18.         return false;

  19.     strcpy(newstr, value);
  20.     newstr[strlen(value)] = '\0';
  21.     newpair->value = newstr;
  22.     newstr = NULL;
  23.     
  24.     (*pair) = newpair;
  25.     return true;
  26. }

  27. /*释放一个对象pair*/
  28. static void delete_pair(Pair_handle_t *pair)
  29. {
  30.     Pair_handle_t temp = NULL;
  31.     if(*pair == NULL)
  32.         return ;

  33.     temp = *pair;
  34.     free(temp->key);
  35.     temp->key = NULL;
  36.     free(temp->value);
  37.     temp->value = NULL;

  38.     free(temp);
  39.     temp = NULL;
  40.     *pair = NULL;
  41. }
数组元素的基本操作:

点击(此处)折叠或打开

  1. /*完成数组对象的初始化操作*/
  2. static void Tabinital(HashEntry_t *tab, int size)
  3. {
  4.     int i = 0;
  5.     for(; i < size; ++ i)
  6.     {
  7.         tab[i].pair = NULL;
  8.         tab[i].state = EMPTY;
  9.     }    
  10. }

  11. static void delete_array(HashEntry_handle_t *array, int size)
  12. {
  13.     int i = 0;

  14.     if(*array != NULL)
  15.     {
  16.         for(i = 0; i < size; ++ i)
  17.         {
  18.             if((*array)[i].state == ACTIVE)
  19.             {
  20.                 delete_pair(&((*array)[i].pair));
  21.                 (*array)[i].state = DELETED;
  22.             }
  23.         }
  24.         free(*array);
  25.         *array = NULL;
  26.     }
  27. }
插入元素的操作、有两个函数的创建,其中一个为了便于后期大小的调整操作。

点击(此处)折叠或打开

  1. /*插入数据到散列中,采用了二次探测的实现方式,并设置了退出条件*/
  2. static bool insert_data(Hashmap_handle_t hashmap,
  3.     const char *key, const char *value, hashfunc func)
  4. {
  5.     int hashval = func(key,hashmap->capacity);
  6.     int index = 0;
  7.     char * newstr = NULL;

  8.     Pair_handle_t newpair = NULL;

  9.     while(hashmap->map[hashval].state != EMPTY)
  10.     {
  11.         if((hashmap->map[hashval].state == ACTIVE)
  12.         && (strcmp(hashmap->map[hashval].pair->key,key) == 0))
  13.             break;

  14.         index ++;
  15.         hashval += index * index;
  16.         hashval %= hashmap->capacity;
  17.         if(index == 200)
  18.             break;
  19.     }
  20.     
  21.     if(hashmap->map[hashval].state == EMPTY)
  22.     {
  23.         if(make_pair(&newpair,key,value))
  24.         {
  25.             hashmap->map[hashval].state = ACTIVE;
  26.             hashmap->map[hashval].pair = newpair;
  27.             newpair = NULL;
  28.             hashmap->size ++;
  29.             return true;
  30.         }
  31.     }

  32.     else if((hashmap->map[hashval].state == ACTIVE)
  33.         && (strcmp(hashmap->map[hashval].pair->key, key) == 0))
  34.     {
  35.         newstr = (char *)malloc(strlen(value) + 1);
  36.         if(newstr != NULL)
  37.         {
  38.             strcpy(newstr,value);
  39.             newstr[strlen(value)] = '\0';
  40.             free(hashmap->map[hashval].pair->value);
  41.             hashmap->map[hashval].pair->value = newstr;
  42.             newstr = NULL;

  43.             return true;
  44.         }
  45.     }
  46.     return false;
  47. }

  48. static bool insert2map(HashEntry_handle_t map,
  49.     const char *key, const char *value,
  50.     hashfunc func, int size)
  51. {
  52.     int hashval = func(key,size);
  53.     int index = 0;
  54.     char *newstr = NULL;
  55.     Pair_handle_t newpair = NULL;

  56.     if(map != NULL)
  57.     {
  58.         while(map[hashval].state != EMPTY)
  59.         {
  60.             if((map[hashval].state == ACTIVE)
  61.             && (strcmp(map[hashval].pair->key, key) == 0))
  62.                 break;
  63.             
  64.             index ++;
  65.             hashval += index * index;
  66.             hashval %= size;
  67.             /*防止死循环*/
  68.             if(index == 200)
  69.                 break;
  70.         }
  71.         
  72.         if(map[hashval].state == EMPTY)
  73.         {
  74.             if(!make_pair(&newpair,key,value))
  75.                 return false;
  76.             map[hashval].pair = newpair;
  77.             map[hashval].state = ACTIVE;
  78.             newpair = NULL;
  79.             return true;
  80.         }
  81.         else if((map[hashval].state == ACTIVE) &&
  82.             (strcmp(map[hashval].pair->key, key) == 0))
  83.         {
  84.             newstr = (char *)malloc(strlen(value) +1);
  85.             if(newstr != NULL)
  86.             {
  87.                 free(map[hashval].pair->value);
  88.                 map[hashval].pair->value = NULL;
  89.                 strcpy(newstr, value);
  90.                 newstr[strlen(value)] = '\0';
  91.                 map[hashval].pair->value = newstr;
  92.                 return true;
  93.             }
  94.         }            
  95.     }
  96.     return false;
  97. }

调整大小和插入的实际操作。

点击(此处)折叠或打开

  1. bool resize(Hashmap_handle_t hashmap)
  2. {
  3.     int i = 0;
  4.     HashEntry_handle_t newarray = NULL;    

  5.     int size = next_prime(2*hashmap->capacity);

  6.     if(hashmap != NULL)
  7.     {
  8.         newarray = (HashEntry_handle_t)malloc(sizeof(HashEntry_t)
  9.                     *size);
  10.         if(newarray == NULL)
  11.             return false;

  12.         /*这一步至关重要*/
  13.         Tabinital(newarray, size);
  14.         for(; i < hashmap->capacity ; ++ i)
  15.         {
  16.             if(hashmap->map[i].state == ACTIVE)
  17.             {
  18.                 if(!insert2map(newarray,
  19.                     hashmap->map[i].pair->key,
  20.                     hashmap->map[i].pair->value,
  21.                     HashFunc, size))
  22.                     return false;
  23.             }
  24.         }
  25.         delete_array(&hashmap->map,hashmap->capacity);
  26.         hashmap->map = newarray;
  27.         hashmap->capacity = size;
  28.         return true;
  29.     }

  30.     return false;
  31. }

  32. bool insert_hashnode(Hashmap_handle_t hashmap,
  33.     const char *key, const char *value)
  34. {
  35.     if(hashmap->size < hashmap->capacity)
  36.     {
  37.         if(insert_data(hashmap,key,value,HashFunc))
  38.         {
  39.             //hashmap->size ++;
  40.         }
  41.     
  42.         return true;
  43.     }
  44.     else /*增加容量*/
  45.     {
  46.         if(!resize(hashmap))
  47.             return false;

  48.         if(insert_data(hashmap,key,value,HashFunc))
  49.         {
  50.             //hashmap->size ++;
  51.         }
  52.         return true;
  53.     }
  54.     return false;
  55. }
搜索操作

点击(此处)折叠或打开

  1. static Pair_handle_t search_data(Hashmap_handle_t hashmap, const char *key, hashfunc func)
  2. {
  3.     int hashval = func(key, hashmap->capacity);
  4.     int index = 0;
  5.     
  6.     while(hashmap->map[hashval].state != EMPTY)
  7.     {
  8.         if((hashmap->map[hashval].state == ACTIVE)
  9.         && (strcmp(hashmap->map[hashval].pair->key,key) == 0))
  10.             break;

  11.         index ++;
  12.         hashval += index * index;
  13.         hashval %= hashmap->capacity;
  14.         if(index == 200)
  15.             break;
  16.     }

  17.     if((hashmap->map[hashval].state == ACTIVE)
  18.         && (strcmp(hashmap->map[hashval].pair->key, key) == 0))
  19.     {
  20.         return hashmap->map[hashval].pair;
  21.     }
  22.     
  23.     return NULL;
  24. }

  25. Pair_handle_t search_hashnode(Hashmap_handle_t hashmap, const char * key)
  26. {
  27.     return search_data(hashmap,key,HashFunc);
  28. }
删除操作

点击(此处)折叠或打开

  1. static bool delete_node(Hashmap_handle_t hashmap,const char *key,hashfunc func)
  2. {
  3.     int hashval = func(key, hashmap->capacity);
  4.     int index = 0;
  5.     
  6.     /**********************************************
  7.       *退出循环的条件是找到空闲的单元,或者关键字匹配
  8.      *********************************************/
  9.     while(hashmap->map[hashval].state != EMPTY)
  10.     {
  11.         if((hashmap->map[hashval].state == ACTIVE)
  12.         && strcmp(hashmap->map[hashval].pair->key,key) == 0)
  13.             break;

  14.         index ++;
  15.         hashval += index * index;
  16.         hashval %= hashmap->capacity;
  17.     
  18.         if(index == 200)
  19.             break;
  20.     }

  21.     /*判断删除条件*/
  22.     if((hashmap->map[hashval].state == ACTIVE) &&
  23.         (strcmp(hashmap->map[hashval].pair->key, key) == 0))
  24.     {
  25.         hashmap->map[hashval].state = DELETED;
  26.         delete_pair(&(hashmap->map[hashval].pair));
  27.         hashmap->map[hashval].pair = NULL;    
  28.         
  29.         return true;
  30.     }

  31.     return false;
  32. }

  33. bool delete_hashnode(Hashmap_handle_t hashmap, const char *key)
  34. {
  35.     if(delete_node(hashmap, key, HashFunc))
  36.     {
  37.         hashmap->size --;
  38.         return true;
  39.     }

  40.     return false;
  41. }

参数获取函数;

点击(此处)折叠或打开

  1. int Length(Hashmap_t *map)
  2. {

  3.     return map->size;
  4. }

  5. int Capacity(Hashmap_t *map)
  6. {
  7.     return map->capacity;
  8. }

  9. void delete_hashmap(Hashmap_handle_t hashmap)
  10. {    
  11.     int i = 0;
  12.     int size = hashmap->capacity;

  13.     if(hashmap != NULL)
  14.     {
  15.         for(; i < size; ++ i)    
  16.         {
  17.             if(hashmap->map[i].state == ACTIVE)
  18.             {
  19.                 delete_pair(&(hashmap->map[i].pair));
  20.                 hashmap->map[i].state = DELETED;
  21.                 hashmap->map[i].pair = NULL;
  22.                 hashmap->size --;
  23.             }
  24.         }
  25.         free(hashmap->map);
  26.         hashmap->map = NULL;
  27.     }
  28. }

  29. void free_hashmap(Hashmap_handle_t *hashmap)
  30. {
  31.     delete_hashmap(*hashmap);
  32.     free(*hashmap);
  33.     *hashmap = NULL;
  34. }

  35. char *key_pair(Pair_handle_t pair)
  36. {
  37.     if(pair != NULL)
  38.     {
  39.         return pair->key;
  40.     }
  41.     return NULL;
  42. }

  43. char *value_pair(Pair_handle_t pair)
  44. {
  45.     if(pair != NULL)
  46.     {
  47.         return pair->value;
  48.     }
  49.     return NULL;
  50. }

  51. /*复制散列操作,相当于创建了一个新的散列对象,而不是指针复制*/
  52. Hashmap_handle_t copy_hashmap(Hashmap_handle_t hashmap)
  53. {
  54.     Hashmap_handle_t newhashmap = NULL;
  55.     int i = 0;
  56.     if(hashmap == NULL)
  57.         return NULL;
  58.     
  59.     /*采用动态分配的方式实现*/
  60.     if(!alloc_hashmap(&newhashmap,hashmap->capacity))
  61.         return NULL;
  62.     
  63.     for(; i < hashmap->capacity ; ++ i)
  64.     {
  65.         if(hashmap->map[i].state == ACTIVE)
  66.         {
  67.             /*得到当前中的值实现插入操作*/
  68.             insert_hashnode(newhashmap,
  69.                 hashmap->map[i].pair->key,
  70.                 hashmap->map[i].pair->value);
  71.         }
  72.         else if(hashmap->map[i].state == DELETED)
  73.         {
  74.             newhashmap->map[i].state = DELETED;
  75.         }
  76.     }

  77.     return newhashmap;
  78. }


测试函数:

点击(此处)折叠或打开

  1. #include "hashmap.h"
  2. #include <time.h>

  3. #define CAPACITY    13

  4. char *keys[] = {
  5. "abcd",
  6. "defh",
  7. "abcd",
  8. "12345",
  9. "a1b2c3",
  10. "12345",
  11. "a1b2c3",
  12. "23456",
  13. "hijhk",
  14. "test1",
  15. "test1",
  16. "789123",
  17. "Input",
  18. };

  19. char *values[] = {
  20. "abcd",
  21. "defh",
  22. "abcd",
  23. "12345",
  24. "a1b2c3",
  25. "12345",
  26. "a1b2c3",
  27. "23456",
  28. "hijhk",
  29. "test1",
  30. "test1",
  31. "789123",
  32. "Input",
  33. };

  34. int myhashFunc(const char *key, int Tabsize)
  35. {
  36.         int hashVal = 0;
  37.         int i = 0;

  38.         int len = strlen(key);
  39.         for(; i < len ; ++ i )
  40.                 hashVal += 37 *hashVal + key[i];
  41.     
  42.         hashVal %= Tabsize;

  43.         if(hashVal < 0)
  44.                 hashVal += Tabsize;

  45.         return hashVal;
  46. }
  47.   

  48. int main()
  49. {
  50.     int i = 0;
  51.     char str1[13];
  52.     char str2[13];

  53.     Hashmap_t mymap ;
  54.     Hashmap_handle_t map = NULL;
  55.     Hashmap_handle_t doubmap = NULL;
  56.     
  57.     Pair_handle_t pair;
  58.     /*静态分配*/
  59.     srand((int)time(0));

  60.     printf("init and alloc test:\n");
  61.     if(!init_hashmap(&mymap,13))
  62.     {
  63.         return false;
  64.     }

  65.     /*动态分配*/
  66.     if(!alloc_hashmap(&map,13))
  67.     {
  68.         return false;
  69.     }    
  70.     printf("Sucessed!\n");
  71.     /*插入测试*/
  72.     printf("insert test:\n");
  73.     for(i = 0; i < CAPACITY + 10; ++ i)
  74.     {
  75.         sprintf(str1,"%d%d",rand()%10+1,rand()%10+1);
  76.         sprintf(str2,"%d%d",rand()%10+1,rand()%10+1);
  77.         
  78.         printf("%s->-f(%s)->%d->%s\n",str1,str1,
  79.             myhashFunc(str1,CAPACITY),str2);

  80.     //    sprintf(str1,"%s",keys[i]);
  81.     //    sprintf(str2,"%s",values[i]);
  82.         if(!insert_hashnode(&mymap,str1,str2))
  83.         {
  84.             printf("i = %d, insert to mymap failed\n", i);
  85.             break;
  86.         }
  87.         if(!insert_hashnode(map,str1,str2))
  88.         {
  89.             printf("i = %d, insert to map failed\n", i);
  90.             break;
  91.         }
  92.     }    
  93.     printf("Sucessed!\n");
  94.     /*查找测试*/
  95.     printf("search test:\n");
  96.     if((pair = search_hashnode(&mymap,str1)) != NULL)
  97.     {
  98.         printf("%s->%s\n",key_pair(pair),value_pair(pair));
  99.     }
  100.     if((pair = search_hashnode(map,str1)) != NULL)
  101.     {
  102.         printf("%s->%s\n",key_pair(pair),value_pair(pair));
  103.     }
  104.     printf("Sucessed!\n");
  105.     
  106.     /*delete*/
  107.     printf("delete test:\n");
  108.     if(delete_hashnode(&mymap,str1))
  109.     {
  110.         printf("Deleted success!!\n");
  111.     }
  112.     else
  113.     {
  114.         printf("Sorry, Failed!!\n");
  115.     }
  116.     if(delete_hashnode(map,str1))
  117.     {
  118.         printf("Deleted success!!\n");
  119.     }
  120.     else
  121.     {
  122.         printf("Sorry, Failed!!\n");
  123.     }
  124.     
  125.     printf("Valid length : %d, Capacity : %d\n",
  126.         Length(&mymap),Capacity(&mymap));
  127.     printf("Valid length : %d, Capacity : %d\n",
  128.         Length(map),Capacity(map));
  129.     
  130.     /*改变长度*/
  131.     printf("resize test:\n");
  132.     if(resize(&mymap))
  133.         printf("Sucessed!\n");
  134.     if(resize(map))    
  135.         printf("Sucessed!\n");

  136.     /*长度*/
  137.     printf("Valid length : %d, Capacity : %d\n",
  138.         Length(&mymap),Capacity(&mymap));
  139.     printf("Valid length : %d, Capacity : %d\n",
  140.         Length(map),Capacity(map));

  141.     printf("Valid length : %d, Capacity : %d\n",
  142.         Length(&mymap),Capacity(&mymap));
  143.     printf("Valid length : %d, Capacity : %d\n",
  144.         Length(map),Capacity(map));
  145.     
  146.     printf("copy test:\n");
  147.     doubmap = copy_hashmap(&mymap);
  148.     printf("Valid length : %d, Capacity : %d\n",
  149.         Length(doubmap),Capacity(doubmap));
  150.     printf("Sucessed!\n");
  151.     
  152.     /*释放内存*/
  153.     printf("free test:\n");
  154.     delete_hashmap(&mymap);
  155.     free_hashmap(&map);
  156.     free_hashmap(&doubmap);
  157.     printf("Valid length : %d, Capacity : %d\n",
  158.         Length(&mymap),Capacity(&mymap));
  159.     printf("Sucessed!\n");
  160.     
  161.     return 0;
  162. }
测试结果:

点击(此处)折叠或打开

  1. [gong@Gong-Computer newversion]$ ./main
  2. init and alloc test:

  3. insert test:
  4. 48->-f(48)->4->49
  5. 108->-f(108)->5->910
  6. 78->-f(78)->1->98
  7. 87->-f(87)->12->73
  8. 36->-f(36)->3->109
  9. 59->-f(59)->4->98
  10. 32->-f(32)->12->48
  11. 210->-f(210)->10->91
  12. 105->-f(105)->2->22
  13. 41->-f(41)->10->82
  14. 1010->-f(1010)->11->69
  15. 19->-f(19)->8->64
  16. 25->-f(25)->3->45
  17. 28->-f(28)->6->104
  18. 16->-f(16)->5->83
  19. 44->-f(44)->0->86
  20. 85->-f(85)->10->72
  21. 51->-f(51)->9->27
  22. 54->-f(54)->12->57
  23. 107->-f(107)->4->210
  24. 73->-f(73)->9->27
  25. 1010->-f(1010)->11->61
  26. 63->-f(63)->10->63

  27. search test:
  28. 63->63
  29. 63->63

  30. delete test:
  31. Deleted
  32. Deleted
  33. Valid length : 21, Capacity : 29
  34. Valid length : 21, Capacity : 29
  35. resize test:


  36. Valid length : 21, Capacity : 59
  37. Valid length : 21, Capacity : 59
  38. Valid length : 21, Capacity : 59
  39. Valid length : 21, Capacity : 59
  40. copy test:
  41. Valid length : 21, Capacity : 59

  42. free test:
  43. Valid length : 0, Capacity : 59
从实验效果可知,基本上实现了散列的基本操作。








阅读(4668) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~