HashTable#
- class HashTable(*args, **kwargs)#
The HashTable struct is an opaque data structure to represent a
[Hash Table][glib-Hash-Tables]. It should only be accessed via the
following functions.
Methods#
- class HashTable
- classmethod add(key: Any = None) bool#
This is a convenience function for using a
HashTableas a set. It is equivalent to callingreplace()withkeyas both the key and the value.In particular, this means that if
keyalready exists in the hash table, then the old copy ofkeyin the hash table is freed andkeyreplaces 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.
Added in version 2.32.
- Parameters:
key – a key to insert
- classmethod contains(key: Any = None) bool#
Checks if
keyis inhash_table.Added in version 2.32.
- Parameters:
key – a key to check
- classmethod destroy() None#
Destroys all keys and values in the
HashTableand decrements its reference count by 1. If keys and/or values are dynamically allocated, you should either free them first or create theHashTablewith destroy notifiers usingnew_full(). In the latter case the destroy functions you supplied will be called on all keys and values during the destruction phase.
- classmethod find(predicate: Callable[[Any, Any, Any], bool], user_data: Any = None) Any | None#
Calls the given function for key/value pairs in the
HashTableuntilpredicatereturnsTrue. The function is passed the key and value of each pair, and the givenuser_dataparameter. 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.
lookup(). So code that frequently issuesfind()orforeach()(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).Added in version 2.4.
- Parameters:
predicate – function to test the key/value pairs for a certain property
user_data – user data to pass to the function
- classmethod foreach(func: Callable[[Any, Any, Any], None], user_data: Any = None) None#
Calls the given function for each of the key/value pairs in the
HashTable. The function is passed the key and value of each pair, and the givenuser_dataparameter. The hash table may not be modified while iterating over it (you can’t add/remove items). To remove all items matching a predicate, useforeach_remove().The order in which
foreach()iterates over the keys/values in the hash table is not defined.See
find()for performance caveats for linear order searches in contrast tolookup().- Parameters:
func – the function to call for each key/value pair
user_data – user data to pass to the function
- classmethod foreach_remove(func: Callable[[Any, Any, Any], bool], user_data: Any = None) int#
Calls the given function for each key/value pair in the
HashTable. If the function returnsTrue, then the key/value pair is removed from theHashTable. If you supplied key or value destroy functions when creating theHashTable, they are used to free the memory allocated for the removed keys and values.See
HashTableIterfor an alternative way to loop over the key/value pairs in the hash table.- Parameters:
func – the function to call for each key/value pair
user_data – user data to pass to the function
- classmethod insert(key: Any = None, value: Any = None) bool#
Inserts a new key and value into a
HashTable.If the key already exists in the
HashTableits current value is replaced with the new value. If you supplied avalue_destroy_funcwhen creating theHashTable, the old value is freed using that function. If you supplied akey_destroy_funcwhen creating theHashTable, 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.
- Parameters:
key – a key to insert
value – the value to associate with the key
- classmethod lookup(key: Any = None) Any | None#
Looks up a key in a
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, uselookup_extended().- Parameters:
key – the key to look up
- classmethod lookup_extended(lookup_key: Any = None) tuple[bool, Any | None, Any | None]#
Looks up a key in the
HashTable, returning the original key and the associated value and agbooleanwhich isTrueif the key was found. This is useful if you need to free the memory allocated for the original key, for example before callingremove().You can actually pass
Noneforlookup_keyto test whether theNonekey exists, provided the hash and equal functions ofhash_tableareNone-safe.- Parameters:
lookup_key – the key to look up
- classmethod new_similar() dict[Any, Any]#
Creates a new
HashTablelikenew_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.Added in version 2.72.
- classmethod remove(key: Any = None) bool#
Removes a key and its associated value from a
HashTable.If the
HashTablewas created usingnew_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.- Parameters:
key – the key to remove
- Returns:
0 if the file was successfully removed, -1 if an error occurred
- classmethod remove_all() None#
Removes all keys and their associated values from a
HashTable.If the
HashTablewas created usingnew_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.Added in version 2.12.
- classmethod replace(key: Any = None, value: Any = None) bool#
Inserts a new key and value into a
HashTablesimilar toinsert(). The difference is that if the key already exists in theHashTable, it gets replaced by the new key. If you supplied avalue_destroy_funcwhen creating theHashTable, the old value is freed using that function. If you supplied akey_destroy_funcwhen creating theHashTable, 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.
- Parameters:
key – a key to insert
value – the value to associate with the key