mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-03-03 14:42:10 +01:00
Add g_hash_table_get_keys() and g_hash_table_get_values(), API to retrieve
2007-04-11 Emmanuele Bassi <ebassi@gnome.org> * glib/ghash.[ch]: Add g_hash_table_get_keys() and g_hash_table_get_values(), API to retrieve the keys and values inside an hash table in list form. (#413133) * glib/glib.symbols: Update symbols. * tests/hash-test.c: Exercise newly added functions. svn path=/trunk/; revision=5444
This commit is contained in:
parent
e542f521ef
commit
db8642a56c
10
ChangeLog
10
ChangeLog
@ -1,3 +1,13 @@
|
||||
2007-04-11 Emmanuele Bassi <ebassi@gnome.org>
|
||||
|
||||
* glib/ghash.[ch]: Add g_hash_table_get_keys() and
|
||||
g_hash_table_get_values(), API to retrieve the keys
|
||||
and values inside an hash table in list form. (#413133)
|
||||
|
||||
* glib/glib.symbols: Update symbols.
|
||||
|
||||
* tests/hash-test.c: Exercise newly added functions.
|
||||
|
||||
2007-04-11 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* configure.in: Use CFLAGS/LDFLAGS in addition to
|
||||
|
62
glib/ghash.c
62
glib/ghash.c
@ -728,6 +728,68 @@ g_hash_table_size (GHashTable *hash_table)
|
||||
return hash_table->nnodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_hash_table_get_keys:
|
||||
* @hash_table: a #GHashTable
|
||||
*
|
||||
* Retrieves every key inside @hash_table. The returned data is valid
|
||||
* until @hash_table is modified.
|
||||
*
|
||||
* Return value: a #GList containing all the keys inside the hash
|
||||
* table. The content of the list is owned by the hash table and
|
||||
* should not be modified or freed. Use g_list_free() when done
|
||||
* using the list.
|
||||
*
|
||||
* Since: 2.14
|
||||
*/
|
||||
GList *
|
||||
g_hash_table_get_keys (GHashTable *hash_table)
|
||||
{
|
||||
GHashNode *node;
|
||||
gint i;
|
||||
GList *retval;
|
||||
|
||||
g_return_val_if_fail (hash_table != NULL, NULL);
|
||||
|
||||
retval = NULL;
|
||||
for (i = 0; i < hash_table->size; i++)
|
||||
for (node = hash_table->nodes[i]; node; node = node->next)
|
||||
retval = g_list_prepend (retval, node->key);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* g_hash_table_get_values:
|
||||
* @hash_table: a #GHashTable
|
||||
*
|
||||
* Retrieves every value inside @hash_table. The returned data is
|
||||
* valid until @hash_table is modified.
|
||||
*
|
||||
* Return value: a #GList containing all the values inside the hash
|
||||
* table. The content of the list is owned by the hash table and
|
||||
* should not be modified or freed. Use g_list_free() when done
|
||||
* using the list.
|
||||
*
|
||||
* Since: 2.14
|
||||
*/
|
||||
GList *
|
||||
g_hash_table_get_values (GHashTable *hash_table)
|
||||
{
|
||||
GHashNode *node;
|
||||
gint i;
|
||||
GList *retval;
|
||||
|
||||
g_return_val_if_fail (hash_table != NULL, NULL);
|
||||
|
||||
retval = NULL;
|
||||
for (i = 0; i < hash_table->size; i++)
|
||||
for (node = hash_table->nodes[i]; node; node = node->next)
|
||||
retval = g_list_prepend (retval, node->value);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
static void
|
||||
g_hash_table_resize (GHashTable *hash_table)
|
||||
{
|
||||
|
@ -28,6 +28,7 @@
|
||||
#define __G_HASH_H__
|
||||
|
||||
#include <glib/gtypes.h>
|
||||
#include <glib/glist.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
@ -77,6 +78,8 @@ guint g_hash_table_foreach_steal (GHashTable *hash_table,
|
||||
GHRFunc func,
|
||||
gpointer user_data);
|
||||
guint g_hash_table_size (GHashTable *hash_table);
|
||||
GList * g_hash_table_get_keys (GHashTable *hash_table);
|
||||
GList * g_hash_table_get_values (GHashTable *hash_table);
|
||||
|
||||
/* keeping hash tables alive */
|
||||
GHashTable* g_hash_table_ref (GHashTable *hash_table);
|
||||
|
@ -356,6 +356,8 @@ g_hash_table_find
|
||||
g_hash_table_foreach
|
||||
g_hash_table_foreach_remove
|
||||
g_hash_table_foreach_steal
|
||||
g_hash_table_get_keys
|
||||
g_hash_table_get_values
|
||||
g_hash_table_insert
|
||||
g_hash_table_lookup
|
||||
g_hash_table_lookup_extended
|
||||
|
@ -343,6 +343,8 @@ main (int argc,
|
||||
gint i;
|
||||
gint value = 120;
|
||||
gint *pvalue;
|
||||
GList *keys, *values;
|
||||
gint keys_len, values_len;
|
||||
|
||||
hash_table = g_hash_table_new (my_hash, my_hash_equal);
|
||||
for (i = 0; i < 10000; i++)
|
||||
@ -354,6 +356,22 @@ main (int argc,
|
||||
if (!pvalue || *pvalue != value)
|
||||
g_assert_not_reached();
|
||||
|
||||
keys = g_hash_table_get_keys (hash_table);
|
||||
if (!keys)
|
||||
g_assert_not_reached ();
|
||||
|
||||
values = g_hash_table_get_values (hash_table);
|
||||
if (!values)
|
||||
g_assert_not_reached ();
|
||||
|
||||
keys_len = g_list_length (keys);
|
||||
values_len = g_list_length (values);
|
||||
if (values_len != keys_len && keys_len != g_hash_table_size (hash_table))
|
||||
g_assert_not_reached ();
|
||||
|
||||
g_list_free (keys);
|
||||
g_list_free (values);
|
||||
|
||||
g_hash_table_foreach (hash_table, my_hash_callback, NULL);
|
||||
|
||||
for (i = 0; i < 10000; i++)
|
||||
|
Loading…
x
Reference in New Issue
Block a user