C99 中的 HashTable 和类似 Vector 的数据结构
我想在C99中创建一个依赖于独立向量数据结构的哈希表。我可以在 OO 的帮助下在 C++ 中做到这一点,但我不确定如何使用结构和联合来实现这一点。
我希望任何链接的示例都不包含具有高度复杂的哈希函数的哈希表实现。我并不特别关心碰撞或存储效率。我只想要有关如何进行的建议,或者一个简单的示例来举例说明各个数据结构的形式而不是功能。
I want to create a hash table that relies on an independent vector data structure in C99. I can do this in C++ with the help of OO, but I'm unsure how to approach this using structs and unions.
I would prefer that any linked examples do not include hash table implementations that have highly complex hashing functions. I do not particularly care about collisions or efficiency of storage. I just want either advice as to how to proceed or a simple example that exemplifies the form rather than function of the respective data structures.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确地推断您希望以完全通用的方式实现不断增长的哈希表,那么您将需要大量
void
指针。向量并不太难,只是需要大量输入:请记住,通用哈希函数将具有原型
size_t hash(void const *, size_t)
,即您需要传入尺寸。(旁注:您会错过的不是 C++ 的 OOP 功能;而是模板、他们购买的类型安全性以及运算符重载等语法糖。看看 OpenBSD 的
ohash
库了解更多示例。)If I infer correctly that you want to implement growing hash tables in a fully generic way, then you'll need a lot of
void
pointers. A vector isn't too hard, it just takes a lot of typing:Keep in mind that a generic hash function would have prototype
size_t hash(void const *, size_t)
, i.e. you need to pass in the size.(Side note: it's not C++'s OOP features that you're going to miss; it's templates, the type safety that they buy, and syntactic sugar such as operator overloading. Take a look at OpenBSD's
ohash
library for more examples.)下面的书可能对 C 中使用结构的链表和哈希表进行了最好的描述:
http: //en.wikipedia.org/wiki/The_C_Programming_Language_(book)
它也实现了一个简单的哈希算法。
另一个简单但均匀分布的哈希算法是 cdb。此处定义的算法:
http://cr.yp.to/cdb/cdb.txt
The following book has probably the best description of both linked lists and a hash table in C using structs:
http://en.wikipedia.org/wiki/The_C_Programming_Language_(book)
It implements a simple hashing algorithm as well.
Another simple, yet uniformly distributed hashing algorithm is the cdb algorithm as defined here:
http://cr.yp.to/cdb/cdb.txt