summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Lortie <desrt@desrt.ca>2012-01-06 10:09:32 -0500
committerRyan Lortie <desrt@desrt.ca>2012-01-06 10:18:41 -0500
commit752f0cac159dc84c03c42f84d9d8a8c923334d43 (patch)
tree8ca78bf3987db283803070d52c8eb676a9832df6
parentb87f6f9f8cb789a645b8792cf197328fb686ba55 (diff)
downloadglib-752f0cac159dc84c03c42f84d9d8a8c923334d43.tar.gz
GHashTable: new 'add' and 'contains' APIs
These are both convenience APIs that make it slightly nicer to use GHashTable as a set (which is something we document as officially supported).
-rw-r--r--docs/reference/glib/glib-sections.txt2
-rw-r--r--glib/ghash.c48
-rw-r--r--glib/ghash.h4
-rw-r--r--glib/glib.symbols2
4 files changed, 56 insertions, 0 deletions
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index bfe12c99c..a3edbedc4 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -2228,6 +2228,8 @@ GHashFunc
GEqualFunc
g_hash_table_insert
g_hash_table_replace
+g_hash_table_add
+g_hash_table_contains
g_hash_table_size
g_hash_table_lookup
g_hash_table_lookup_extended
diff --git a/glib/ghash.c b/glib/ghash.c
index ace1c372b..1fbaaf464 100644
--- a/glib/ghash.c
+++ b/glib/ghash.c
@@ -123,6 +123,9 @@
* }
* </programlisting>
* </example>
+ *
+ * As of version 2.32, there is also a g_hash_table_add() function to
+ * add a key to a #GHashTable that is being used as a set.
*/
/**
@@ -1182,6 +1185,51 @@ g_hash_table_replace (GHashTable *hash_table,
g_hash_table_insert_internal (hash_table, key, value, TRUE);
}
+/**
+ * g_hash_table_add:
+ * @hash_table: a #GHashTable
+ * @key: a key to insert
+ *
+ * This is a convenience function for using a #GHashTable as a set. It
+ * is equivalent to calling g_hash_table_replace() with @key as both the
+ * key and the value.
+ *
+ * When a hash table only ever contains keys that have themselves as the
+ * corresponding value it is able to be stored more efficiently. See
+ * the discussion in the section description.
+ *
+ * Since: 2.32
+ **/
+void
+g_hash_table_add (GHashTable *hash_table,
+ gpointer key)
+{
+ g_hash_table_insert_internal (hash_table, key, key, TRUE);
+}
+
+/**
+ * g_hash_table_contains:
+ * @hash_table: a #GHashTable
+ * @key: a key to check
+ *
+ * Checks if @key is in @hash_table.
+ *
+ * Since: 2.32
+ **/
+gboolean
+g_hash_table_contains (GHashTable *hash_table,
+ gconstpointer key)
+{
+ guint node_index;
+ guint node_hash;
+
+ g_return_val_if_fail (hash_table != NULL, FALSE);
+
+ node_index = g_hash_table_lookup_node (hash_table, key, &node_hash);
+
+ return HASH_IS_REAL (hash_table->hashes[node_index]);
+}
+
/*
* g_hash_table_remove_internal:
* @hash_table: our #GHashTable
diff --git a/glib/ghash.h b/glib/ghash.h
index fdff5fd6e..a998a3832 100644
--- a/glib/ghash.h
+++ b/glib/ghash.h
@@ -68,6 +68,8 @@ void g_hash_table_insert (GHashTable *hash_table,
void g_hash_table_replace (GHashTable *hash_table,
gpointer key,
gpointer value);
+void g_hash_table_add (GHashTable *hash_table,
+ gpointer key);
gboolean g_hash_table_remove (GHashTable *hash_table,
gconstpointer key);
void g_hash_table_remove_all (GHashTable *hash_table);
@@ -76,6 +78,8 @@ gboolean g_hash_table_steal (GHashTable *hash_table,
void g_hash_table_steal_all (GHashTable *hash_table);
gpointer g_hash_table_lookup (GHashTable *hash_table,
gconstpointer key);
+gboolean g_hash_table_has (GHashTable *hash_table,
+ gconstpointer lookup_key);
gboolean g_hash_table_lookup_extended (GHashTable *hash_table,
gconstpointer lookup_key,
gpointer *orig_key,
diff --git a/glib/glib.symbols b/glib/glib.symbols
index e914e86de..1b01bb4bd 100644
--- a/glib/glib.symbols
+++ b/glib/glib.symbols
@@ -379,6 +379,8 @@ g_file_open_tmp_utf8
g_file_test_utf8
g_mkstemp_utf8
#endif
+g_hash_table_add
+g_hash_table_contains
g_hash_table_destroy
g_hash_table_unref
g_hash_table_ref