[glib] GHashTable: new 'add' and 'contains' APIs



commit 752f0cac159dc84c03c42f84d9d8a8c923334d43
Author: Ryan Lortie <desrt desrt ca>
Date:   Fri Jan 6 10:09:32 2012 -0500

    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).

 docs/reference/glib/glib-sections.txt |    2 +
 glib/ghash.c                          |   48 +++++++++++++++++++++++++++++++++
 glib/ghash.h                          |    4 +++
 glib/glib.symbols                     |    2 +
 4 files changed, 56 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index bfe12c9..a3edbed 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 ace1c37..1fbaaf4 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 fdff5fd..a998a38 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 e914e86..1b01bb4 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



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]