在 C 中寻找一个好的哈希表实现 [英] Looking for a good hash table implementation in C

查看:28
本文介绍了在 C 中寻找一个好的哈希表实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要对字符串键感兴趣.有人可以指点我去图书馆吗?

I am primarily interested in string keys. Can someone point me towards a library?

推荐答案

我有同样的需求,做了一些研究,最终使用了 libcfu

I had the same need and did some research and ended up using libcfu

它简单易读,所以如果我需要修改,我可以不用花太多时间去理解.它也是 BSD 许可证.无需更改我的结构(嵌入说下一个指针)

It's simple and readable so if I have a need to modify, I can do it without spending too much time to understand. It's also of BSD license. No need to change my structs (to embed say a next pointer)

由于以下原因(我的个人原因,YMMV),我不得不拒绝其他选项:

I had to reject the other options for following reasons (my personal reasons, YMMV):

  • sglib --> 这是一个宏迷宫,我不习惯调试/制作仅使用宏在此类代码库上进行更改
  • cbfalconer --> 很多许可红旗,网站关闭,网上关于支持/作者的不利讨论太多;不想冒险
  • google sparce-hash --> 如前所述,它用于 C++,而不是 C
  • glib (gnome hash) --> 看起来很有前途;但我找不到任何简单的方法来安装开发人员工具包;我只需要 C 例程/文件——而不是完整的开发环境
  • Judy --> 对于简单的使用来说似乎太复杂了......如果我不得不遇到任何问题,我也没有准备好自己调试
  • npsml(这里提到)--> 找不到源
  • strmap 发现非常简单和有用——键和值都必须是字符串太简单了;作为字符串的值似乎过于严格(应该接受 void *)
  • uthash --> 看起来不错(已在维基百科的哈希表上提到);发现它需要修改结构——不想这样做,因为性能并不是我真正关心的问题——它更多的是开发速度.
  • sglib --> it's a macro maze and I wasn't comfortable debugging/making changes on such a code base using just macros
  • cbfalconer --> lot of licensing redflags, and the site was down and too many unfavorable discussions on web about support/author; didn't want to take the risk
  • google sparce-hash --> as stated already, it's for C++, not C
  • glib (gnome hash) --> looked very promising; but I couldn't find any easy way to install the developer kit; I just needed the C routines/files -- not the full blown developement environment
  • Judy --> seems too complex for a simple use.. also was not ready to debug myself if I had to run into any issues
  • npsml (mentioned here) --> can't find the source
  • strmap found very simple and useful -- it's just too simplistic that both key and value must be strings; value being string seems too restrictive (should accept void *)
  • uthash --> seems good (has been mentioned on wikipedia on hashtable); found that it requires struct to be modified -- didn't want to do that as performace is not really a concern for my use --it's more of development velocity.

总而言之,对于非常简单的使用 strmap 是好的;如果您担心额外的内存使用,请使用 uthash.如果只是开发速度或易用性是主要目标,libcfu 会获胜 [注意 libcfu 在内部进行内存分配以维护节点/哈希表].令人惊讶的是,可用的简单 C 哈希实现并不多.

In summary for very simple use strmap is good; uthash if you are concerned with additional memory use. If just speed of development or ease of use is primary objective, libcfu wins [note libcfu internally does memory allocation to maintain the nodes/hashtables]. It's surprising that there aren't many simple C hash implementations available.

这篇关于在 C 中寻找一个好的哈希表实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆