GHashTable,使用uint64_t作为键,并使用struct作为值 [英] GHashTable that using uint64_t as key and a struct as value

查看:126
本文介绍了GHashTable,使用uint64_t作为键,并使用struct作为值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习GHashTable.尽管Stackoverflow中已经有一些示例,但它们只是一些常见情况.因此,我仍然不确定如何执行我的要求并决定寻求帮助.
我想使用uint64_t作为键,并使用struct作为值.我发现GLib中没有这样的内置哈希函数.只有一个g_int64_hash().尽管密钥是uint64_t,但大约只有52位.所以我认为gint64可以.但是我看到一些使用GINT_TO_POINTER()转换值的示例(有时没有).因此,请对此感到困惑.
非常感谢!

I am studying the GHashTable. Though there are already some examples in Stackoverflow, they are just some common case. So I am still not sure how to implement my requirements and decide to ask for help.
I want to use a uint64_t as key and a struct as value. I find that there is no such built-in hash function in GLib. There is just a g_int64_hash(). Though the key is uint64_t, it will just be about 52 bits. So I think gint64 is OK. But I see some examples using GINT_TO_POINTER() to convert the value (and sometimes they didn't). So just be confused about this.
Thanks very much!

推荐答案

请参见 ghash.c g_int64_hashg_int64_equal的实现方式:

See in ghash.c how g_int64_hash and g_int64_equal are implemented:

...
gboolean
g_int64_equal (gconstpointer v1,
               gconstpointer v2)
{
  return *((const gint64*) v1) == *((const gint64*) v2);
}
...
guint
g_int64_hash (gconstpointer v)
{
  return (guint) *(const gint64*) v;
}
...

您可以类似地写出赢取的uint64_t_hashuint64_equal:

You can write your won uint64_t_hash and uint64_equal similarly:

gboolean
uint64_t_equal (gconstpointer v1,
                gconstpointer v2)
{
  return *((const uint64_t*) v1) == *((const uint64_t*) v2);
}

guint
uint64_t_hash (gconstpointer v)
{
  return (guint) *(const uint64_t*) v;
}

查看示例:

#include <glib.h>
#include <stdio.h>
#include <inttypes.h>

/* the value structure */
typedef struct __MyStruct
{
  int a;
  int b;
} MyStruct;

/* the hash equality function */
static gboolean
uint64_t_equal (gconstpointer v1,
                gconstpointer v2)
{
  return *((const uint64_t*) v1) == *((const uint64_t*) v2);
}

/* the hash function */
static guint
uint64_t_hash (gconstpointer v)
{
  return (guint) *(const uint64_t*) v;
}

/* the hash function */
static void
print_hash(gpointer key,
           gpointer value,
           gpointer user_data)
{
  printf("%" PRIu64 " = {%d, %d}\n",
    *(uint64_t*) key, ((MyStruct *) value)->a, ((MyStruct *) value)->b);
}

int
main(int argc, char **argv)
{
  GHashTable *hash;

  /* key => value */
  uint64_t k1 = 11; MyStruct s1 = {1, 11};
  uint64_t k2 = 22; MyStruct s2 = {2, 22};
  uint64_t k3 = 33; MyStruct s3 = {3, 33};

  hash = g_hash_table_new(uint64_t_hash, uint64_t_equal);

  /* insert values */
  g_hash_table_insert(hash, &k1, &s1);
  g_hash_table_insert(hash, &k2, &s2);
  g_hash_table_insert(hash, &k3, &s3);

  /* iterate over the values in the hash table */
  g_hash_table_foreach(hash, print_hash, NULL);
  g_hash_table_destroy(hash);
  return 0;
}

这篇关于GHashTable,使用uint64_t作为键,并使用struct作为值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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