Chinaunix

标题: linux下的C hash表測試問題,請大家幫忙 hcreate_r,hsearch_r [打印本页]

作者: gangjh    时间: 2006-06-16 10:11
标题: linux下的C hash表測試問題,請大家幫忙 hcreate_r,hsearch_r
source如下

gnuhash.h

  1. #ifndef _GUN_HASH_LIST
  2. #define _GNU_HASH_LIST
  3. #include <string.h>
  4. #define __USE_GNU
  5. #include <search.h>


  6. #define hash_create(size, tab) do {  \
  7.         int len = sizeof(struct hsearch_data); \
  8.         tab = malloc(len);  \
  9.         memset(tab, 0, len); \
  10.         hcreate_r(size,tab); \
  11. } while (0)



  12. #define hash_createa  hcreate_r
  13. #define hash_destory  hdestroy_r



  14. ENTRY  *hash_add(char *key, void *value, struct hsearch_data *tab)
  15. {
  16.         ENTRY item, *ret ;
  17.         item.key = key ;
  18.         item.data= value  ;
  19.         int x=  hsearch_r(item, ENTER, &ret, tab);
  20.         return ret ;
  21. }

  22. ENTRY *hash_find(char *key, struct hsearch_data *tab)
  23. {
  24.         ENTRY item, *ret ;
  25.         item.key = key ;
  26.         int x= hsearch_r(item, FIND, &ret, tab);
  27.         return ret ;
  28. }
  29. ENTRY  *hash_addentry(ENTRY item, struct hsearch_data *tab)
  30. {
  31.         ENTRY *ret ;
  32.         int x =  hsearch_r(item, ENTER, &ret, tab);
  33.         return ret ;
  34. }

  35. ENTRY *hash_findentry(ENTRY item, struct hsearch_data *tab)
  36. {
  37.         ENTRY *ret ;
  38.         int x= hsearch_r(item, FIND, &ret, tab);
  39.         return ret ;
  40. }


  41. #endif
复制代码


test.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "gnuhash.h"

  5. void prtht(ENTRY *ret)
  6. {
  7.         printf("%9.9s->%s\n",
  8.                         ret ? ret->key : "NULL",
  9.                         ret ? (char *)(ret->data) : "");
  10. }

  11. int main(int argc ,char **argv) {
  12.         ENTRY e, *ret;
  13.         int i ;

  14.         struct hsearch_data *ht ;
  15.         hash_create(5, ht);
  16.         printf ("    key->value\n") ;
  17.         ret = hash_add("test1", "aaa", ht) ;
  18.         prtht(ret) ;

  19.         ret = hash_add("test2", "bbb", ht) ;
  20.         prtht(ret) ;

  21.         ret = hash_add("test3", "ccc", ht) ;
  22.         prtht(ret) ;

  23.         ret = hash_add("test1", "ddd", ht) ;
  24.         prtht(ret) ;

  25.         printf("\nfind\n") ;
  26.         ret = hash_find("test1", ht) ;
  27.         prtht(ret) ;
  28.         hash_destory(ht) ;


  29.         return (EXIT_SUCCESS);
  30. }
复制代码


編譯:
gcc -g  -pipe  -o test test.c

運行結果如下:

  1.    key->value
  2.     test1->aaa
  3.     test2->bbb
  4.     test3->ccc
  5.     test1->aaa

  6. find
  7.     test1->aaa         
复制代码


問題是 :第二次輸出test1值該為"ddd"
請各位幫忙

[ 本帖最后由 gangjh 于 2006-6-16 10:14 编辑 ]
作者: gangjh    时间: 2006-06-19 11:11
请高手出手,
帮帮忙,
作者: phoneix    时间: 2006-06-22 15:41
#ifndef _GUN_HASH_LIST
#define _GNU_HASH_LIST
?????????????????????
作者: ubuntuer    时间: 2009-03-17 15:58
The function  hsearch()  searches  the hash table for an item with the same
       key as item (where "the same" is determined using  strcmp(3)),  and  if
       successful  returns  a  pointer  to it.  The argument action determines
       what hsearch() does after an unsuccessful search.

你这里一下就successful了
作者: happy_fish100    时间: 2009-03-17 17:28
标题: 回复 #1 gangjh 的帖子
试试俺用C写的hash操作函数。对于字典类的数据,数据初始化完成后,可以调用函数hash_best_op来达到一个桶内最多只有一个key(如果hash code都不重复的话),从而查找时间为O(1)。

svn checkout http://fastdfs.googlecode.com/svn/trunk/ fastdfs-read-only

参见common/hash.h和common/hash.c




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2