From: Matthias Clasen Date: Sun, 2 Oct 2011 04:08:54 +0000 (-0400) Subject: Move all hash functions to ghash.c X-Git-Tag: 2.31.0~308 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6ef022bbb3b6dbe6f0f34858d7521a7b0ab331e3;hp=e7ca67f20c9f44478cdaf5832a91dc39086f4c38;p=platform%2Fupstream%2Fglib.git Move all hash functions to ghash.c This matches their location in the headers. --- diff --git a/glib/ghash.c b/glib/ghash.c index 8dda570..c7a9778 100644 --- a/glib/ghash.c +++ b/glib/ghash.c @@ -1595,3 +1595,207 @@ g_hash_table_get_values (GHashTable *hash_table) return retval; } + +/* Hash functions. + */ + +/** + * g_str_equal: + * @v1: a key + * @v2: a key to compare with @v1 + * + * Compares two strings for byte-by-byte equality and returns %TRUE + * if they are equal. It can be passed to g_hash_table_new() as the + * @key_equal_func parameter, when using strings as keys in a #GHashTable. + * + * Note that this function is primarily meant as a hash table comparison + * function. For a general-purpose, %NULL-safe string comparison function, + * see g_strcmp0(). + * + * Returns: %TRUE if the two keys match + */ +gboolean +g_str_equal (gconstpointer v1, + gconstpointer v2) +{ + const gchar *string1 = v1; + const gchar *string2 = v2; + + return strcmp (string1, string2) == 0; +} + +/** + * g_str_hash: + * @v: a string key + * + * Converts a string to a hash value. + * + * This function implements the widely used "djb" hash apparently posted + * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit + * unsigned hash value starts at 5381 and for each byte 'c' in the + * string, is updated: hash = hash * 33 + c. This + * function uses the signed value of each byte. + * + * It can be passed to g_hash_table_new() as the @hash_func parameter, + * when using strings as keys in a #GHashTable. + * + * Returns: a hash value corresponding to the key + */ +guint +g_str_hash (gconstpointer v) +{ + const signed char *p; + guint32 h = 5381; + + for (p = v; *p != '\0'; p++) + h = (h << 5) + h + *p; + + return h; +} + +/** + * g_direct_hash: + * @v: a #gpointer key + * + * Converts a gpointer to a hash value. + * It can be passed to g_hash_table_new() as the @hash_func parameter, + * when using pointers as keys in a #GHashTable. + * + * Returns: a hash value corresponding to the key. + */ +guint +g_direct_hash (gconstpointer v) +{ + return GPOINTER_TO_UINT (v); +} + +/** + * g_direct_equal: + * @v1: a key + * @v2: a key to compare with @v1 + * + * Compares two #gpointer arguments and returns %TRUE if they are equal. + * It can be passed to g_hash_table_new() as the @key_equal_func + * parameter, when using pointers as keys in a #GHashTable. + * + * Returns: %TRUE if the two keys match. + */ +gboolean +g_direct_equal (gconstpointer v1, + gconstpointer v2) +{ + return v1 == v2; +} + +/** + * g_int_equal: + * @v1: a pointer to a #gint key + * @v2: a pointer to a #gint key to compare with @v1 + * + * Compares the two #gint 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 pointers to integers as keys in a #HashTable. + * + * Returns: %TRUE if the two keys match. + */ +gboolean +g_int_equal (gconstpointer v1, + gconstpointer v2) +{ + return *((const gint*) v1) == *((const gint*) v2); +} + +/** + * g_int_hash: + * @v: a pointer to a #gint key + * + * Converts a pointer to a #gint to a hash value. + * It can be passed to g_hash_table_new() as the @hash_func parameter, + * when using pointers to integers values as keys in a #GHashTable. + * + * Returns: a hash value corresponding to the key. + */ +guint +g_int_hash (gconstpointer v) +{ + return *(const gint*) v; +} + +/** + * g_int64_equal: + * @v1: a pointer to a #gint64 key + * @v2: a pointer to a #gint64 key to compare with @v1 + * + * Compares the two #gint64 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 pointers to 64-bit integers as keys in a #GHashTable. + * + * Returns: %TRUE if the two keys match. + * + * Since: 2.22 + */ +gboolean +g_int64_equal (gconstpointer v1, + gconstpointer v2) +{ + return *((const gint64*) v1) == *((const gint64*) v2); +} + +/** + * g_int64_hash: + * @v: a pointer to a #gint64 key + * + * Converts a pointer to a #gint64 to a hash value. + * It can be passed to g_hash_table_new() as the @hash_func parameter, + * when using pointers to 64-bit integers values as keys in a #GHashTable. + * + * Returns: a hash value corresponding to the key. + * + * Since: 2.22 + */ +guint +g_int64_hash (gconstpointer v) +{ + return (guint) *(const gint64*) v; +} + +/** + * g_double_equal: + * @v1: a pointer to a #gdouble key + * @v2: a pointer to a #gdouble key to compare with @v1 + * + * Compares the two #gdouble 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 pointers to doubles as keys in a #GHashTable. + * + * Returns: %TRUE if the two keys match. + * + * Since: 2.22 + */ +gboolean +g_double_equal (gconstpointer v1, + gconstpointer v2) +{ + return *((const gdouble*) v1) == *((const gdouble*) v2); +} + +/** + * g_double_hash: + * @v: a pointer to a #gdouble key + * + * Converts a pointer to a #gdouble to a hash value. + * It can be passed to g_hash_table_new() as the @hash_func parameter, + * when using pointers to doubles as keys in a #GHashTable. + * + * Returns: a hash value corresponding to the key. + * + * Since: 2.22 + */ +guint +g_double_hash (gconstpointer v) +{ + return (guint) *(const gdouble*) v; +} diff --git a/glib/gstring.c b/glib/gstring.c index 8d6ef2c..9fac86c 100644 --- a/glib/gstring.c +++ b/glib/gstring.c @@ -44,62 +44,33 @@ #include "gprintf.h" -/* Hash Functions. - */ - /** - * g_str_equal: - * @v1: a key - * @v2: a key to compare with @v1 - * - * Compares two strings for byte-by-byte equality and returns %TRUE - * if they are equal. It can be passed to g_hash_table_new() as the - * @key_equal_func parameter, when using strings as keys in a #GHashTable. - * - * Note that this function is primarily meant as a hash table comparison - * function. For a general-purpose, %NULL-safe string comparison function, - * see g_strcmp0(). + * SECTION:strings + * @title: Strings + * @short_description: text buffers which grow automatically + * as text is added * - * Returns: %TRUE if the two keys match + * A #GString is an object that handles the memory management + * of a C string for you. You can think of it as similar to a + * Java StringBuffer. In addition to the string itself, GString + * stores the length of the string, so can be used for binary + * data with embedded nul bytes. To access the C string managed + * by the GString @string, simply use @string->str. */ -gboolean -g_str_equal (gconstpointer v1, - gconstpointer v2) -{ - const gchar *string1 = v1; - const gchar *string2 = v2; - - return strcmp (string1, string2) == 0; -} /** - * g_str_hash: - * @v: a string key - * - * Converts a string to a hash value. - * - * This function implements the widely used "djb" hash apparently posted - * by Daniel Bernstein to comp.lang.c some time ago. The 32 bit - * unsigned hash value starts at 5381 and for each byte 'c' in the - * string, is updated: hash = hash * 33 + c. This - * function uses the signed value of each byte. - * - * It can be passed to g_hash_table_new() as the @hash_func parameter, - * when using strings as keys in a #GHashTable. + * GString: + * @str: points to the character data. It may move as text is added. + * The @str field is null-terminated and so + * can be used as an ordinary C string. + * @len: contains the length of the string, not including the + * terminating nul byte. + * @allocated_len: the number of bytes that can be stored in the + * string before it needs to be reallocated. May be larger than @len. * - * Returns: a hash value corresponding to the key + * The GString struct contains the public fields of a GString. */ -guint -g_str_hash (gconstpointer v) -{ - const signed char *p; - guint32 h = 5381; - for (p = v; *p != '\0'; p++) - h = (h << 5) + h + *p; - - return h; -} #define MY_MAXSIZE ((gsize)-1) @@ -121,34 +92,6 @@ nearest_power (gsize base, gsize num) } } -/** - * SECTION:strings - * @title: Strings - * @short_description: text buffers which grow automatically - * as text is added - * - * A #GString is an object that handles the memory management - * of a C string for you. You can think of it as similar to a - * Java StringBuffer. In addition to the string itself, GString - * stores the length of the string, so can be used for binary - * data with embedded nul bytes. To access the C string managed - * by the GString @string, simply use @string->str. - */ - -/** - * GString: - * @str: points to the character data. It may move as text is added. - * The @str field is null-terminated and so - * can be used as an ordinary C string. - * @len: contains the length of the string, not including the - * terminating nul byte. - * @allocated_len: the number of bytes that can be stored in the - * string before it needs to be reallocated. May be larger than @len. - * - * The GString struct contains the public fields of a GString. - */ - - static void g_string_maybe_expand (GString *string, gsize len) diff --git a/glib/gutils.c b/glib/gutils.c index 12ad096..052126d 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -3491,156 +3491,9 @@ g_get_language_names (void) } /** - * g_direct_hash: - * @v: a #gpointer key - * - * Converts a gpointer to a hash value. - * It can be passed to g_hash_table_new() as the @hash_func parameter, - * when using pointers as keys in a #GHashTable. - * - * Returns: a hash value corresponding to the key. - */ -guint -g_direct_hash (gconstpointer v) -{ - return GPOINTER_TO_UINT (v); -} - -/** - * g_direct_equal: - * @v1: a key. - * @v2: a key to compare with @v1. - * - * Compares two #gpointer arguments and returns %TRUE if they are equal. - * It can be passed to g_hash_table_new() as the @key_equal_func - * parameter, when using pointers as keys in a #GHashTable. - * - * Returns: %TRUE if the two keys match. - */ -gboolean -g_direct_equal (gconstpointer v1, - gconstpointer v2) -{ - return v1 == v2; -} - -/** - * g_int_equal: - * @v1: a pointer to a #gint key. - * @v2: a pointer to a #gint key to compare with @v1. - * - * Compares the two #gint 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 pointers to integers as keys in a #GHashTable. - * - * Returns: %TRUE if the two keys match. - */ -gboolean -g_int_equal (gconstpointer v1, - gconstpointer v2) -{ - return *((const gint*) v1) == *((const gint*) v2); -} - -/** - * g_int_hash: - * @v: a pointer to a #gint key - * - * Converts a pointer to a #gint to a hash value. - * It can be passed to g_hash_table_new() as the @hash_func parameter, - * when using pointers to integers values as keys in a #GHashTable. - * - * Returns: a hash value corresponding to the key. - */ -guint -g_int_hash (gconstpointer v) -{ - return *(const gint*) v; -} - -/** - * g_int64_equal: - * @v1: a pointer to a #gint64 key. - * @v2: a pointer to a #gint64 key to compare with @v1. - * - * Compares the two #gint64 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 pointers to 64-bit integers as keys in a #GHashTable. - * - * Returns: %TRUE if the two keys match. - * - * Since: 2.22 - */ -gboolean -g_int64_equal (gconstpointer v1, - gconstpointer v2) -{ - return *((const gint64*) v1) == *((const gint64*) v2); -} - -/** - * g_int64_hash: - * @v: a pointer to a #gint64 key - * - * Converts a pointer to a #gint64 to a hash value. - * It can be passed to g_hash_table_new() as the @hash_func parameter, - * when using pointers to 64-bit integers values as keys in a #GHashTable. - * - * Returns: a hash value corresponding to the key. - * - * Since: 2.22 - */ -guint -g_int64_hash (gconstpointer v) -{ - return (guint) *(const gint64*) v; -} - -/** - * g_double_equal: - * @v1: a pointer to a #gdouble key. - * @v2: a pointer to a #gdouble key to compare with @v1. - * - * Compares the two #gdouble 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 pointers to doubles as keys in a #GHashTable. - * - * Returns: %TRUE if the two keys match. - * - * Since: 2.22 - */ -gboolean -g_double_equal (gconstpointer v1, - gconstpointer v2) -{ - return *((const gdouble*) v1) == *((const gdouble*) v2); -} - -/** - * g_double_hash: - * @v: a pointer to a #gdouble key - * - * Converts a pointer to a #gdouble to a hash value. - * It can be passed to g_hash_table_new() as the @hash_func parameter, - * when using pointers to doubles as keys in a #GHashTable. - * - * Returns: a hash value corresponding to the key. - * - * Since: 2.22 - */ -guint -g_double_hash (gconstpointer v) -{ - return (guint) *(const gdouble*) v; -} - -/** * g_nullify_pointer: * @nullify_location: the memory address of the pointer. - * + * * Set the pointer at the specified location to %NULL. **/ void @@ -3653,9 +3506,9 @@ g_nullify_pointer (gpointer *nullify_location) /** * g_get_codeset: - * + * * Get the codeset for the current locale. - * + * * Return value: a newly allocated string containing the name * of the codeset. This string must be freed with g_free(). **/