[glib] Add g_hash_table_get_keys_as_array()
- From: Ryan Lortie <desrt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] Add g_hash_table_get_keys_as_array()
- Date: Sun, 27 Oct 2013 16:29:47 +0000 (UTC)
commit 288076770264c7b34d57c985d827c3a3d2710209
Author: Ryan Lortie <desrt desrt ca>
Date: Sat Oct 26 18:48:42 2013 -0700
Add g_hash_table_get_keys_as_array()
Returns a %NULL-terminated array of the keys of a hashtable.
In the case that the hash table has strings for keys, this is actually a
gchar**.
https://bugzilla.gnome.org/show_bug.cgi?id=710964
docs/reference/glib/glib-sections.txt | 1 +
glib/ghash.c | 46 +++++++++++++++++++++++++++++++++
glib/ghash.h | 3 ++
3 files changed, 50 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 0414ed9..9613a5f 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -2296,6 +2296,7 @@ g_hash_table_remove_all
g_hash_table_steal_all
g_hash_table_get_keys
g_hash_table_get_values
+g_hash_table_get_keys_as_array
GHRFunc
g_hash_table_freeze
g_hash_table_thaw
diff --git a/glib/ghash.c b/glib/ghash.c
index 9150f32..9fcdcc7 100644
--- a/glib/ghash.c
+++ b/glib/ghash.c
@@ -1646,6 +1646,52 @@ g_hash_table_get_keys (GHashTable *hash_table)
}
/**
+ * g_hash_table_get_keys_as_array:
+ * @hash_table: a #GHashTable
+ * @length: (out): the length of the returned array
+ *
+ * Retrieves every key inside @hash_table, as an array.
+ *
+ * The returned array is %NULL-terminated but may contain %NULL as a
+ * key. Use @length to determine the true length if it's possible that
+ * %NULL was used as the value for a key.
+ *
+ * Note: in the common case of a string-keyed #GHashTable, the return
+ * value of this function can be conveniently cast to (gchar **).
+ *
+ * You should always free the return result with g_free(). In the
+ * above-mentioned case of a string-keyed hash table, it may be
+ * appropriate to use g_strfreev() if you call g_hash_table_steal_all()
+ * first to transfer ownership of the keys.
+ *
+ * Returns: (array length=length) (transfer container): a
+ * %NULL-terminated array containing each key from the table.
+ *
+ * Since: 2.40
+ **/
+gpointer *
+g_hash_table_get_keys_as_array (GHashTable *hash_table,
+ guint *length)
+{
+ gpointer *result;
+ guint i, j = 0;
+
+ result = g_new (gpointer, hash_table->nnodes + 1);
+ for (i = 0; i < hash_table->size; i++)
+ {
+ if (HASH_IS_REAL (hash_table->hashes[i]))
+ result[j++] = hash_table->keys[i];
+ }
+ g_assert_cmpint (j, ==, hash_table->nnodes);
+ result[j] = NULL;
+
+ if (length)
+ *length = j;
+
+ return result;
+}
+
+/**
* g_hash_table_get_values:
* @hash_table: a #GHashTable
*
diff --git a/glib/ghash.h b/glib/ghash.h
index 19054bb..796f744 100644
--- a/glib/ghash.h
+++ b/glib/ghash.h
@@ -119,6 +119,9 @@ GLIB_AVAILABLE_IN_ALL
GList * g_hash_table_get_keys (GHashTable *hash_table);
GLIB_AVAILABLE_IN_ALL
GList * g_hash_table_get_values (GHashTable *hash_table);
+GLIB_AVAILABLE_IN_2_40
+gpointer * g_hash_table_get_keys_as_array (GHashTable *hash_table,
+ guint *length);
GLIB_AVAILABLE_IN_ALL
void g_hash_table_iter_init (GHashTableIter *iter,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]