免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6264 | 回复: 4
打印 上一主题 下一主题

linux下的C hash表測試問題,請大家幫忙 hcreate_r,hsearch_r [复制链接]

求职 : 数据库管理员
论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-06-16 10:11 |只看该作者 |倒序浏览
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 编辑 ]

求职 : 数据库管理员
论坛徽章:
0
2 [报告]
发表于 2006-06-19 11:11 |只看该作者
请高手出手,
帮帮忙,

论坛徽章:
0
3 [报告]
发表于 2006-06-22 15:41 |只看该作者
#ifndef _GUN_HASH_LIST
#define _GNU_HASH_LIST
?????????????????????

论坛徽章:
0
4 [报告]
发表于 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了

论坛徽章:
4
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11IT运维版块每日发帖之星
日期:2016-08-11 06:20:00IT运维版块每日发帖之星
日期:2016-08-15 06:20:00
5 [报告]
发表于 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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP