GLib.HashTable¶
Fields¶
None
Methods¶
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
class |
|
Details¶
- class GLib.HashTable¶
The
GLib.HashTablestruct is an opaque data structure to represent a Hash Table. It should only be accessed via the following functions.- classmethod add(hash_table, key)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable
- Returns:
Trueif the key did not exist yet- Return type:
This is a convenience function for using a
GLib.HashTableas a set. It is equivalent to callingGLib.HashTable.replace() with key as both the key and the value.In particular, this means that if key already exists in the hash table, then the old copy of key in the hash table is freed and key replaces it in the table.
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.
Starting from GLib 2.40, this function returns a boolean value to indicate whether the newly added value was already in the hash table or not.
New in version 2.32.
- classmethod contains(hash_table, key)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable
- Returns:
- Return type:
Checks if key is in hash_table.
New in version 2.32.
- classmethod destroy(hash_table)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable
Destroys all keys and values in the
GLib.HashTableand decrements its reference count by 1. If keys and/or values are dynamically allocated, you should either free them first or create theGLib.HashTablewith destroy notifiers using g_hash_table_new_full(). In the latter case the destroy functions you supplied will be called on all keys and values during the destruction phase.
- classmethod find(hash_table, predicate, *user_data)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTablepredicate (
GLib.HRFunc) – function to test the key/value pairs for a certain propertyuser_data (
objectorNone) – user data to pass to the function
- Returns:
The value of the first key/value pair is returned, for which predicate evaluates to
True. If no pair with the requested property is found,Noneis returned.- Return type:
Calls the given function for key/value pairs in the
GLib.HashTableuntil predicate returnsTrue. The function is passed the key and value of each pair, and the given user_data parameter. The hash table may not be modified while iterating over it (you can’t add/remove items).Note, that hash tables are really only optimized for forward lookups, i.e.
GLib.HashTable.lookup(). So code that frequently issuesGLib.HashTable.find() orGLib.HashTable.foreach() (e.g. in the order of once per every entry in a hash table) should probably be reworked to use additional or different data structures for reverse lookups (keep in mind that an O(n) find/foreach operation issued for all n values in a hash table ends up needing O(n*n) operations).New in version 2.4.
- classmethod foreach(hash_table, func, *user_data)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTablefunc (
GLib.HFunc) – the function to call for each key/value pairuser_data (
objectorNone) – user data to pass to the function
Calls the given function for each of the key/value pairs in the
GLib.HashTable. The function is passed the key and value of each pair, and the given user_data parameter. The hash table may not be modified while iterating over it (you can’t add/remove items). To remove all items matching a predicate, useGLib.HashTable.foreach_remove().The order in which
GLib.HashTable.foreach() iterates over the keys/values in the hash table is not defined.See
GLib.HashTable.find() for performance caveats for linear order searches in contrast toGLib.HashTable.lookup().
- classmethod foreach_remove(hash_table, func, *user_data)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTablefunc (
GLib.HRFunc) – the function to call for each key/value pairuser_data (
objectorNone) – user data to pass to the function
- Returns:
the number of key/value pairs removed
- Return type:
Calls the given function for each key/value pair in the
GLib.HashTable. If the function returnsTrue, then the key/value pair is removed from theGLib.HashTable. If you supplied key or value destroy functions when creating theGLib.HashTable, they are used to free the memory allocated for the removed keys and values.See
GLib.HashTableIterfor an alternative way to loop over the key/value pairs in the hash table.
- classmethod foreach_steal(hash_table, func, *user_data)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTablefunc (
GLib.HRFunc) – the function to call for each key/value pairuser_data (
objectorNone) – user data to pass to the function
- Returns:
the number of key/value pairs removed.
- Return type:
Calls the given function for each key/value pair in the
GLib.HashTable. If the function returnsTrue, then the key/value pair is removed from theGLib.HashTable, but no key or value destroy functions are called.See
GLib.HashTableIterfor an alternative way to loop over the key/value pairs in the hash table.
- classmethod insert(hash_table, key, value)[source]¶
- Parameters:
- Returns:
Trueif the key did not exist yet- Return type:
Inserts a new key and value into a
GLib.HashTable.If the key already exists in the
GLib.HashTableits current value is replaced with the new value. If you supplied a value_destroy_func when creating theGLib.HashTable, the old value is freed using that function. If you supplied a key_destroy_func when creating theGLib.HashTable, the passed key is freed using that function.Starting from GLib 2.40, this function returns a boolean value to indicate whether the newly added value was already in the hash table or not.
- classmethod lookup(hash_table, key)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable
- Returns:
the associated value, or
Noneif the key is not found- Return type:
Looks up a key in a
GLib.HashTable. Note that this function cannot distinguish between a key that is not present and one which is present and has the valueNone. If you need this distinction, useGLib.HashTable.lookup_extended().
- classmethod lookup_extended(hash_table, lookup_key)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable
- Returns:
Trueif the key was found in theGLib.HashTable- orig_key:
return location for the original key
- value:
return location for the value associated with the key
- Return type:
Looks up a key in the
GLib.HashTable, returning the original key and the associated value and aboolwhich isTrueif the key was found. This is useful if you need to free the memory allocated for the original key, for example before callingGLib.HashTable.remove().You can actually pass
Nonefor lookup_key to test whether theNonekey exists, provided the hash and equal functions of hash_table areNone-safe.
- classmethod new_similar(other_hash_table)[source]¶
- Parameters:
other_hash_table ({
object:object}) – AnotherGLib.HashTable- Returns:
a new
GLib.HashTable- Return type:
Creates a new
GLib.HashTablelike g_hash_table_new_full() with a reference count of 1.It inherits the hash function, the key equal function, the key destroy function, as well as the value destroy function, from other_hash_table.
The returned hash table will be empty; it will not contain the keys or values from other_hash_table.
New in version 2.72.
- classmethod ref(hash_table)[source]¶
- Parameters:
hash_table ({
object:object}) – a validGLib.HashTable- Returns:
the passed in
GLib.HashTable- Return type:
Atomically increments the reference count of hash_table by one. This function is MT-safe and may be called from any thread.
New in version 2.10.
- classmethod remove(hash_table, key)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable
- Returns:
Trueif the key was found and removed from theGLib.HashTable- Return type:
Removes a key and its associated value from a
GLib.HashTable.If the
GLib.HashTablewas created using g_hash_table_new_full(), the key and value are freed using the supplied destroy functions, otherwise you have to make sure that any dynamically allocated values are freed yourself.
- classmethod remove_all(hash_table)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable
Removes all keys and their associated values from a
GLib.HashTable.If the
GLib.HashTablewas created using g_hash_table_new_full(), the keys and values are freed using the supplied destroy functions, otherwise you have to make sure that any dynamically allocated values are freed yourself.New in version 2.12.
- classmethod replace(hash_table, key, value)[source]¶
- Parameters:
- Returns:
Trueif the key did not exist yet- Return type:
Inserts a new key and value into a
GLib.HashTablesimilar toGLib.HashTable.insert(). The difference is that if the key already exists in theGLib.HashTable, it gets replaced by the new key. If you supplied a value_destroy_func when creating theGLib.HashTable, the old value is freed using that function. If you supplied a key_destroy_func when creating theGLib.HashTable, the old key is freed using that function.Starting from GLib 2.40, this function returns a boolean value to indicate whether the newly added value was already in the hash table or not.
- classmethod size(hash_table)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable- Returns:
the number of key/value pairs in the
GLib.HashTable.- Return type:
Returns the number of elements contained in the
GLib.HashTable.
- classmethod steal(hash_table, key)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable
- Returns:
Trueif the key was found and removed from theGLib.HashTable- Return type:
Removes a key and its associated value from a
GLib.HashTablewithout calling the key and value destroy functions.
- classmethod steal_all(hash_table)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable
Removes all keys and their associated values from a
GLib.HashTablewithout calling the key and value destroy functions.New in version 2.12.
- classmethod steal_extended(hash_table, lookup_key)[source]¶
- Parameters:
hash_table ({
object:object}) – aGLib.HashTable
- Returns:
Trueif the key was found in theGLib.HashTable- stolen_key:
return location for the original key
- stolen_value:
return location for the value associated with the key
- Return type:
Looks up a key in the
GLib.HashTable, stealing the original key and the associated value and returningTrueif the key was found. If the key was not found,Falseis returned.If found, the stolen key and value are removed from the hash table without calling the key and value destroy functions, and ownership is transferred to the caller of this method, as with
GLib.HashTable.steal(). That is the case regardless whether stolen_key or stolen_value output parameters are requested.You can pass
Nonefor lookup_key, provided the hash and equal functions of hash_table areNone-safe.The dictionary implementation optimizes for having all values identical to their keys, for example by using
GLib.HashTable.add(). Before 2.82, when stealing both the key and the value from such a dictionary, the value wasNone. Since 2.82, the returned value and key will be the same.New in version 2.58.
- classmethod unref(hash_table)[source]¶
- Parameters:
hash_table ({
object:object}) – a validGLib.HashTable
Atomically decrements the reference count of hash_table by one. If the reference count drops to 0, all keys and values will be destroyed, and all memory allocated by the hash table is released. This function is MT-safe and may be called from any thread.
New in version 2.10.