博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
glibc中的hsearch_r函数
阅读量:7174 次
发布时间:2019-06-29

本文共 2008 字,大约阅读时间需要 6 分钟。

hot3.png

hsearch_r函数

inthsearch_r (item, action, retval, htab)     ENTRY item;     ACTION action;     ENTRY **retval;     struct hsearch_data *htab;{  unsigned int hval;  unsigned int count;  unsigned int len = strlen (item.key);  unsigned int idx;  /** Compute an value for the given string. Perhaps use a better method. */  hval = len;  count = len;  while (count-- > 0)    {      hval <<= 4;      hval += item.key[count];    }  /** First hash function: simply take the modul but prevent zero. */  idx = hval % htab->size + 1;  if (htab->table[idx].used)    {      /** Further action might be required according to the action value. */      if (htab->table[idx].used == hval && strcmp (item.key, htab->table[idx].entry.key) == 0){ *retval = &htab->table[idx].entry; return 1;}      /** Second hash function, as suggested in [Knuth] */      unsigned int hval2 = 1 + hval % (htab->size - 2);      unsigned int first_idx = idx;      do{ /** Because SIZE is prime this guarantees to step through all             available indeces.  */          if (idx <= hval2)   idx = htab->size + idx - hval2; else   idx -= hval2; /** If we visited all entries leave the loop unsuccessfully.  */ if (idx == first_idx)   break;            /** If entry is found use it. */          if (htab->table[idx].used == hval     && strcmp (item.key, htab->table[idx].entry.key) == 0)   {     *retval = &htab->table[idx].entry;     return 1;   }}      while (htab->table[idx].used);    }  /** An empty bucket has been found. */  if (action == ENTER)    {      /** If table is full and another entry should be entered returnwith error.  */      if (htab->filled == htab->size){ __set_errno (ENOMEM); *retval = NULL; return 0;}      htab->table[idx].used  = hval;      htab->table[idx].entry = item;      ++htab->filled;      *retval = &htab->table[idx].entry;      return 1;    }  __set_errno (ESRCH);  *retval = NULL;  return 0;}

函数执行的时候不论action为什么,先查找,若是命中则返回有的,若是没有命中节点且action为ENTER则进行hash_add操作。

转载于:https://my.oschina.net/harvard/blog/140166

你可能感兴趣的文章
3DTouch学习笔记
查看>>
Linux下 vi 操作Found a swap file by the name
查看>>
filebeat 插件开发
查看>>
网络基础
查看>>
技术加油站:5月19日,技术大佬等你来撩
查看>>
supervisor配置详解(转)
查看>>
Confluence 6 Microsoft SQL Server 设置准备
查看>>
Nginx.conf配置文件
查看>>
EI检索期刊JA检索与CA检索有什么区别?
查看>>
人脸识别技术探讨:1:1,1:小N/大N,大姿态识别,活体识别
查看>>
面向对象程序设计
查看>>
非主从同步 mysql master slave pt-slave-delay
查看>>
【思科×××】IPsec ×××基本部署
查看>>
检验新买内存条的真假
查看>>
解密:华为的敏捷网络是SDN吗
查看>>
u16 u32 __u16 __u32 u_int16_t u_int32_t
查看>>
android: BaseAdapter和ListView简单运用(08)
查看>>
自带内存上的读写(openFileOutput和openFileInput)
查看>>
服务器搭建:3.2、openresty图片压缩之 lua调用GraphicsMagick
查看>>
bash 脚本编程 变量、变量类型 (笔记)
查看>>