Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
Add g_hash_table_get_keys() and g_hash_table_get_values(), API to ret…
Browse files Browse the repository at this point in the history
…rieve

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
  • Loading branch information
ebassi authored and Emmanuele Bassi committed Apr 11, 2007
1 parent e542f52 commit db8642a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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
Expand Down
62 changes: 62 additions & 0 deletions glib/ghash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions glib/ghash.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define __G_HASH_H__

#include <glib/gtypes.h>
#include <glib/glist.h>

G_BEGIN_DECLS

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions glib/glib.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion tests/hash-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ main (int argc,
GHashTable *hash_table;
gint i;
gint value = 120;
gint *pvalue;
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++)
Expand All @@ -353,6 +355,22 @@ main (int argc,
pvalue = g_hash_table_find (hash_table, find_first, &value);
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);

Expand Down

0 comments on commit db8642a

Please sign in to comment.