gpt4 book ai didi

c - 二进制数据的 glib 散列函数

转载 作者:太空宇宙 更新时间:2023-11-04 02:00:22 24 4
gpt4 key购买 nike

我正在尝试使用 glib 构建哈希表,键将是一些二进制字符串。

我不能使用内置函数 g_str_hash (),因为它用于 C 字符串(以空值终止)。也不能使用内置函数 g_direct_hash (),因为它只需要一个指针。没有办法指定 key 的长度,这就是为什么我也不能提供我自己的散列函数版本(因为它需要两个参数:指向 key 的指针和生成散列的长度,这是反对函数指针类型 GHashFunc)。

有什么想法吗?

最佳答案

使用g_bytes_new (或类似的)将您的数据和长度放入 GBytes struct并使用提供的 g_bytes_hash()g_bytes_equal()initialize the hash table .

A GBytes can come from many different origins that may have different procedures for freeing the memory region. Examples are memory from g_malloc(), from memory slices, from a GMappedFile or memory from other allocators.

GBytes work well as keys in GHashTable. Use g_bytes_equal() and g_bytes_hash() as parameters to g_hash_table_new() or g_hash_table_new_full(). GBytes can also be used as keys in a GTree by passing the g_bytes_compare() function to g_tree_new().

如果你的数据是固定宽度的并且额外的内存困扰你,你可以自己写GHashFuncGEqualFunc作为薄包装器,临时将您的数据放入 GBytes 结构中,以便使用 g_bytes_hash() 和 g_bytes_equal()。

关于c - 二进制数据的 glib 散列函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27946095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com