From 3c09257ea1ce15069a46cb1a5610d963ac813f89 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 24 Oct 2023 14:11:04 +0200 Subject: [PATCH] glib: add internal g_uint_hash()/g_uint_equal() We have g_int_hash()/g_int_equal(), which in practice might also work with with pointers to unsigned integers. However, according to strict interpretation of C, I think it is not valid to conflate the two. Even if it were valid in all cases that we want to support, we should still have separate g_uint_{hash,equal} functions (e.g. by just #define them to their underlying g_int_{hash,equal} implementations). Add instead internal hash/equal functions for guint. --- glib/ghash.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ glib/glib-private.h | 3 +++ 2 files changed, 47 insertions(+) diff --git a/glib/ghash.c b/glib/ghash.c index 6cd1d4cf0..90e03bf9f 100644 --- a/glib/ghash.c +++ b/glib/ghash.c @@ -2552,6 +2552,50 @@ g_int_hash (gconstpointer v) return *(const gint*) v; } +/** + * g_uint_equal: + * @v1: (not nullable): a pointer to a #guint key + * @v2: (not nullable): a pointer to a #guint key to compare with @v1 + * + * Compares the two #guint values being pointed to and returns + * %TRUE if they are equal. + * It can be passed to g_hash_table_new() as the @key_equal_func + * parameter, when using non-%NULL pointers to integers as keys in a + * #GHashTable. + * + * Note that this function acts on pointers to #guint, not on #guint + * directly: if your hash table's keys are of the form + * `GUINT_TO_POINTER (n)`, use g_direct_equal() instead. + * + * Returns: %TRUE if the two keys match. + */ +gboolean +g_uint_equal (gconstpointer v1, + gconstpointer v2) +{ + return *((const guint *) v1) == *((const guint *) v2); +} + +/** + * g_uint_hash: + * @v: (not nullable): a pointer to a #guint key + * + * Converts a pointer to a #guint to a hash value. + * It can be passed to g_hash_table_new() as the @hash_func parameter, + * when using non-%NULL pointers to integer values as keys in a #GHashTable. + * + * Note that this function acts on pointers to #guint, not on #guint + * directly: if your hash table's keys are of the form + * `GUINT_TO_POINTER (n)`, use g_direct_hash() instead. + * + * Returns: a hash value corresponding to the key. + */ +guint +g_uint_hash (gconstpointer v) +{ + return *(const guint *) v; +} + /** * g_int64_equal: * @v1: (not nullable): a pointer to a #gint64 key diff --git a/glib/glib-private.h b/glib/glib-private.h index dcafddc24..dae496bfd 100644 --- a/glib/glib-private.h +++ b/glib/glib-private.h @@ -303,4 +303,7 @@ GLibPrivateVTable *glib__private__ (void); # define GLIB_DEFAULT_LOCALE "" #endif +gboolean g_uint_equal (gconstpointer v1, gconstpointer v2); +guint g_uint_hash (gconstpointer v); + #endif /* __GLIB_PRIVATE_H__ */