寻找 C 中的数组(与链表)哈希表实现

发布于 2024-08-30 19:33:36 字数 240 浏览 5 评论 0原文

我正在寻找 C 中的哈希表实现,它将其对象存储在(二维)数组而不是链接列表中。 即,如果发生冲突,导致冲突的对象将存储在下一个空闲行索引中,而不是推送到链表的头部和第一个元素。

另外,对象本身必须复制到哈希表中,而不是通过指针引用。 (对象不会在程序的整个生命周期中存在,但表却存在)。

我知道这样的实现可能会存在严重的效率缺陷,并且不是“散列的标准方式”,但当我致力于一个非常特殊的系统架构时,我需要这些特征。

谢谢

I'm looking for a hashtable implementation in C that stores its objects in (twodimensional) arrays rather than linked lists.
i.e. if a collision happens, the object that is causing the collision will be stored in the next free row index rather than pushed to the head and first element of a linked list.

plus, the objects themselves must be copied to the hashtable, rather than referenced by pointers. (the objects do not live for the whole lifetime of the program but the table does).

I know that such an implementation might have serious efficiency drawbacks and is not the "standard way of hashing" but as I work on a very special system-architecture i need those characteristics.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

jJeQQOZ5 2024-09-06 19:33:36

一个超级简单的实现:

char hashtable[MAX_KEY][MAX_MEMORY];
int counts[MAX_KEY] = {0}; 

/* Inserting something into the table */
SomeStruct* some_struct;
int hashcode = compute_code(some_struct);
int size = sizeof(SomeStruct); 
memcpy(hashtable[hashcode] + counts[hashcode] * size, some_struct, size);
++counts[hashcode];

不要忘记检查MAX_MEMORY

A super simple implementation:

char hashtable[MAX_KEY][MAX_MEMORY];
int counts[MAX_KEY] = {0}; 

/* Inserting something into the table */
SomeStruct* some_struct;
int hashcode = compute_code(some_struct);
int size = sizeof(SomeStruct); 
memcpy(hashtable[hashcode] + counts[hashcode] * size, some_struct, size);
++counts[hashcode];

Don't forget to check against MAX_MEMORY.

苯莒 2024-09-06 19:33:36

我的猜测是您的系统不允许动态内存分配。因此,您需要预先定义适合您的数据的数组边界(对象总数和最大预期冲突),并另外为您的对象定义一个自定义哈希函数,因此最好实现您自己的哈希表。

My guess is your system does not allow for dynamic memory allocation. Therefore you will need to define up front array bounds that are reasonable for your data (number of total objects and maximum expected collisions) and additionally a custom hash function for your objects so it might be best to implement your own hash table.

放肆 2024-09-06 19:33:36

它不是用 C 编写的,而是用 C++ 编写的,但请看一下 Google Sparse Hash -可能会给你一些想法。关键要求是所存储的对象有办法成为null

It's not in C but in C++, but take a look at Google Sparse Hash - might give you some ideas. The key requirement is that the object being stored has a way to be null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文