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).
This commit is contained in:
Ryan Lortie 2012-01-06 10:09:32 -05:00
parent b87f6f9f8c
commit 752f0cac15
4 changed files with 56 additions and 0 deletions

View File

@ -2228,6 +2228,8 @@ GHashFunc
GEqualFunc GEqualFunc
g_hash_table_insert g_hash_table_insert
g_hash_table_replace g_hash_table_replace
g_hash_table_add
g_hash_table_contains
g_hash_table_size g_hash_table_size
g_hash_table_lookup g_hash_table_lookup
g_hash_table_lookup_extended g_hash_table_lookup_extended

View File

@ -123,6 +123,9 @@
* } * }
* </programlisting> * </programlisting>
* </example> * </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_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: * g_hash_table_remove_internal:
* @hash_table: our #GHashTable * @hash_table: our #GHashTable

View File

@ -68,6 +68,8 @@ void g_hash_table_insert (GHashTable *hash_table,
void g_hash_table_replace (GHashTable *hash_table, void g_hash_table_replace (GHashTable *hash_table,
gpointer key, gpointer key,
gpointer value); gpointer value);
void g_hash_table_add (GHashTable *hash_table,
gpointer key);
gboolean g_hash_table_remove (GHashTable *hash_table, gboolean g_hash_table_remove (GHashTable *hash_table,
gconstpointer key); gconstpointer key);
void g_hash_table_remove_all (GHashTable *hash_table); 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); void g_hash_table_steal_all (GHashTable *hash_table);
gpointer g_hash_table_lookup (GHashTable *hash_table, gpointer g_hash_table_lookup (GHashTable *hash_table,
gconstpointer key); gconstpointer key);
gboolean g_hash_table_has (GHashTable *hash_table,
gconstpointer lookup_key);
gboolean g_hash_table_lookup_extended (GHashTable *hash_table, gboolean g_hash_table_lookup_extended (GHashTable *hash_table,
gconstpointer lookup_key, gconstpointer lookup_key,
gpointer *orig_key, gpointer *orig_key,

View File

@ -379,6 +379,8 @@ g_file_open_tmp_utf8
g_file_test_utf8 g_file_test_utf8
g_mkstemp_utf8 g_mkstemp_utf8
#endif #endif
g_hash_table_add
g_hash_table_contains
g_hash_table_destroy g_hash_table_destroy
g_hash_table_unref g_hash_table_unref
g_hash_table_ref g_hash_table_ref