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
- add(hash_table: dict[None, None], key: None) bool
This is a convenience function for using a
HashTable
as a set. It is equivalent to callingreplace()
withkey
as both the key and the value.In particular, this means that if
key
already exists in the hash table, then the old copy ofkey
in the hash table is freed andkey
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.
Added in version 2.32.
- Parameters:
hash_table – a
HashTable
key – a key to insert
- contains(hash_table: dict[None, None], key: None) bool
Checks if
key
is inhash_table
.Added in version 2.32.
- Parameters:
hash_table – a
HashTable
key – a key to check
- destroy(hash_table: dict[None, None]) None
Destroys all keys and values in the
HashTable
and decrements its reference count by 1. If keys and/or values are dynamically allocated, you should either free them first or create theHashTable
with 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.- Parameters:
hash_table – a
HashTable
- find(hash_table: dict[None, None], predicate: Callable[[...], bool], *user_data: Any) None
Calls the given function for key/value pairs in the
HashTable
untilpredicate
returnsTrue
. The function is passed the key and value of each pair, and the givenuser_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.
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:
hash_table – a
HashTable
predicate – function to test the key/value pairs for a certain property
user_data – user data to pass to the function
- foreach(hash_table: dict[None, None], func: Callable[[...], None], *user_data: Any) 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_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, 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:
hash_table – a
HashTable
func – the function to call for each key/value pair
user_data – user data to pass to the function
- foreach_remove(hash_table: dict[None, None], func: Callable[[...], bool], *user_data: Any) 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
HashTableIter
for an alternative way to loop over the key/value pairs in the hash table.- Parameters:
hash_table – a
HashTable
func – the function to call for each key/value pair
user_data – user data to pass to the function
- insert(hash_table: dict[None, None], key: None, value: None) bool
Inserts a new key and value into a
HashTable
.If the key already exists in the
HashTable
its current value is replaced with the new value. If you supplied avalue_destroy_func
when creating theHashTable
, the old value is freed using that function. If you supplied akey_destroy_func
when 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:
hash_table – a
HashTable
key – a key to insert
value – the value to associate with the key
- lookup(hash_table: dict[None, None], key: None) 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:
hash_table – a
HashTable
key – the key to look up
- lookup_extended(hash_table: dict[None, None], lookup_key: None) tuple[bool, None, None]
Looks up a key in the
HashTable
, returning the original key and the associated value and agboolean
which isTrue
if 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
None
forlookup_key
to test whether theNone
key exists, provided the hash and equal functions ofhash_table
areNone
-safe.- Parameters:
hash_table – a
HashTable
lookup_key – the key to look up
- new_similar(other_hash_table: dict[None, None]) dict[None, None]
Creates a new
HashTable
likenew_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.
- Parameters:
other_hash_table – Another
HashTable
- remove(hash_table: dict[None, None], key: None) bool
Removes a key and its associated value from a
HashTable
.If the
HashTable
was 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:
hash_table – a
HashTable
key – the key to remove
- Returns:
0 if the file was successfully removed, -1 if an error occurred
- remove_all(hash_table: dict[None, None]) None
Removes all keys and their associated values from a
HashTable
.If the
HashTable
was 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.
- Parameters:
hash_table – a
HashTable
- replace(hash_table: dict[None, None], key: None, value: None) bool
Inserts a new key and value into a
HashTable
similar toinsert()
. The difference is that if the key already exists in theHashTable
, it gets replaced by the new key. If you supplied avalue_destroy_func
when creating theHashTable
, the old value is freed using that function. If you supplied akey_destroy_func
when 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:
hash_table – a
HashTable
key – a key to insert
value – the value to associate with the key