Move all hash functions to ghash.c

This matches their location in the headers.
This commit is contained in:
Matthias Clasen 2011-10-02 00:08:54 -04:00
parent e7ca67f20c
commit 6ef022bbb3
3 changed files with 227 additions and 227 deletions

View File

@ -1595,3 +1595,207 @@ g_hash_table_get_values (GHashTable *hash_table)
return retval; 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: <literal>hash = hash * 33 + c</literal>. 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;
}

View File

@ -44,83 +44,6 @@
#include "gprintf.h" #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().
*
* 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: <literal>hash = hash * 33 + c</literal>. 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;
}
#define MY_MAXSIZE ((gsize)-1)
static inline gsize
nearest_power (gsize base, gsize num)
{
if (num > MY_MAXSIZE / 2)
{
return MY_MAXSIZE;
}
else
{
gsize n = base;
while (n < num)
n <<= 1;
return n;
}
}
/** /**
* SECTION:strings * SECTION:strings
* @title: Strings * @title: Strings
@ -149,6 +72,26 @@ nearest_power (gsize base, gsize num)
*/ */
#define MY_MAXSIZE ((gsize)-1)
static inline gsize
nearest_power (gsize base, gsize num)
{
if (num > MY_MAXSIZE / 2)
{
return MY_MAXSIZE;
}
else
{
gsize n = base;
while (n < num)
n <<= 1;
return n;
}
}
static void static void
g_string_maybe_expand (GString *string, g_string_maybe_expand (GString *string,
gsize len) gsize len)

View File

@ -3490,157 +3490,10 @@ g_get_language_names (void)
return (const gchar * const *) cache->language_names; return (const gchar * const *) cache->language_names;
} }
/**
* 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: * g_nullify_pointer:
* @nullify_location: the memory address of the pointer. * @nullify_location: the memory address of the pointer.
* *
* Set the pointer at the specified location to %NULL. * Set the pointer at the specified location to %NULL.
**/ **/
void void
@ -3653,9 +3506,9 @@ g_nullify_pointer (gpointer *nullify_location)
/** /**
* g_get_codeset: * g_get_codeset:
* *
* Get the codeset for the current locale. * Get the codeset for the current locale.
* *
* Return value: a newly allocated string containing the name * Return value: a newly allocated string containing the name
* of the codeset. This string must be freed with g_free(). * of the codeset. This string must be freed with g_free().
**/ **/