mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-26 12:12:10 +01:00
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:
parent
b87f6f9f8c
commit
752f0cac15
@ -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
|
||||
|
48
glib/ghash.c
48
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
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user