:right-sidebar: True HashTable =================================================================== .. currentmodule:: gi.repository.GLib .. class:: HashTable(*args, **kwargs) :no-contents-entry: The :obj:`~gi.repository.GLib.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 ------- .. rst-class:: interim-class .. class:: HashTable :no-index: .. classmethod:: add(key: ~typing.Any = None) -> bool This is a convenience function for using a :obj:`~gi.repository.GLib.HashTable` as a set. It is equivalent to calling :func:`~gi.repository.GLib.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. .. versionadded:: 2.32 :param key: a key to insert .. classmethod:: contains(key: ~typing.Any = None) -> bool Checks if ``key`` is in ``hash_table``\. .. versionadded:: 2.32 :param key: a key to check .. classmethod:: destroy() -> None Destroys all keys and values in the :obj:`~gi.repository.GLib.HashTable` and decrements its reference count by 1. If keys and/or values are dynamically allocated, you should either free them first or create the :obj:`~gi.repository.GLib.HashTable` with destroy notifiers using :func:`~gi.repository.GLib.HashTable.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(predicate: ~typing.Callable[[~typing.Any, ~typing.Any, ~typing.Any], bool], user_data: ~typing.Any = None) -> ~typing.Any | None Calls the given function for key/value pairs in the :obj:`~gi.repository.GLib.HashTable` until ``predicate`` returns :const:`True`. 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. :func:`~gi.repository.GLib.HashTable.lookup`. So code that frequently issues :func:`~gi.repository.GLib.HashTable.find` or :func:`~gi.repository.GLib.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). .. versionadded:: 2.4 :param predicate: function to test the key/value pairs for a certain property :param user_data: user data to pass to the function .. classmethod:: foreach(func: ~typing.Callable[[~typing.Any, ~typing.Any, ~typing.Any], None], user_data: ~typing.Any = None) -> None Calls the given function for each of the key/value pairs in the :obj:`~gi.repository.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, use :func:`~gi.repository.GLib.HashTable.foreach_remove`. The order in which :func:`~gi.repository.GLib.HashTable.foreach` iterates over the keys/values in the hash table is not defined. See :func:`~gi.repository.GLib.HashTable.find` for performance caveats for linear order searches in contrast to :func:`~gi.repository.GLib.HashTable.lookup`. :param func: the function to call for each key/value pair :param user_data: user data to pass to the function .. classmethod:: foreach_remove(func: ~typing.Callable[[~typing.Any, ~typing.Any, ~typing.Any], bool], user_data: ~typing.Any = None) -> int Calls the given function for each key/value pair in the :obj:`~gi.repository.GLib.HashTable`\. If the function returns :const:`True`, then the key/value pair is removed from the :obj:`~gi.repository.GLib.HashTable`\. If you supplied key or value destroy functions when creating the :obj:`~gi.repository.GLib.HashTable`\, they are used to free the memory allocated for the removed keys and values. See :obj:`~gi.repository.GLib.HashTableIter` for an alternative way to loop over the key/value pairs in the hash table. :param func: the function to call for each key/value pair :param user_data: user data to pass to the function .. classmethod:: insert(key: ~typing.Any = None, value: ~typing.Any = None) -> bool Inserts a new key and value into a :obj:`~gi.repository.GLib.HashTable`\. If the key already exists in the :obj:`~gi.repository.GLib.HashTable` its current value is replaced with the new value. If you supplied a ``value_destroy_func`` when creating the :obj:`~gi.repository.GLib.HashTable`\, the old value is freed using that function. If you supplied a ``key_destroy_func`` when creating the :obj:`~gi.repository.GLib.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. :param key: a key to insert :param value: the value to associate with the key .. classmethod:: lookup(key: ~typing.Any = None) -> ~typing.Any | None Looks up a key in a :obj:`~gi.repository.GLib.HashTable`\. Note that this function cannot distinguish between a key that is not present and one which is present and has the value :const:`None`. If you need this distinction, use :func:`~gi.repository.GLib.HashTable.lookup_extended`. :param key: the key to look up .. classmethod:: lookup_extended(lookup_key: ~typing.Any = None) -> ~typing.Tuple[bool, ~typing.Any | None, ~typing.Any | None] Looks up a key in the :obj:`~gi.repository.GLib.HashTable`\, returning the original key and the associated value and a :obj:`~gi.repository.gboolean` which is :const:`True` if the key was found. This is useful if you need to free the memory allocated for the original key, for example before calling :func:`~gi.repository.GLib.HashTable.remove`. You can actually pass :const:`None` for ``lookup_key`` to test whether the :const:`None` key exists, provided the hash and equal functions of ``hash_table`` are :const:`None`-safe. :param lookup_key: the key to look up .. classmethod:: new_similar() -> dict[~typing.Any, ~typing.Any] Creates a new :obj:`~gi.repository.GLib.HashTable` like :func:`~gi.repository.GLib.HashTable.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``\. .. versionadded:: 2.72 .. classmethod:: remove(key: ~typing.Any = None) -> bool Removes a key and its associated value from a :obj:`~gi.repository.GLib.HashTable`\. If the :obj:`~gi.repository.GLib.HashTable` was created using :func:`~gi.repository.GLib.HashTable.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. :param key: the key to remove :return: 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 :obj:`~gi.repository.GLib.HashTable`\. If the :obj:`~gi.repository.GLib.HashTable` was created using :func:`~gi.repository.GLib.HashTable.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. .. versionadded:: 2.12 .. classmethod:: replace(key: ~typing.Any = None, value: ~typing.Any = None) -> bool Inserts a new key and value into a :obj:`~gi.repository.GLib.HashTable` similar to :func:`~gi.repository.GLib.HashTable.insert`. The difference is that if the key already exists in the :obj:`~gi.repository.GLib.HashTable`\, it gets replaced by the new key. If you supplied a ``value_destroy_func`` when creating the :obj:`~gi.repository.GLib.HashTable`\, the old value is freed using that function. If you supplied a ``key_destroy_func`` when creating the :obj:`~gi.repository.GLib.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. :param key: a key to insert :param value: the value to associate with the key .. classmethod:: size() -> int Returns the number of elements contained in the :obj:`~gi.repository.GLib.HashTable`\.