Skip to content

GLib.HashTable

record (struct)

The HashTable struct is an opaque data structure to represent a Hash Table. It should only be accessed via the following functions.

Static functions

add

@staticmethod
def add(hash_table: dict[int, int], key: int | None = ...) -> bool

This is a convenience function for using a HashTable as a set. It is equivalent to calling 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.

Parameters:

  • hash_table — a HashTable
  • key — a key to insert

contains

@staticmethod
def contains(hash_table: dict[int, int], key: int | None = ...) -> bool

Checks if key is in hash_table.

Parameters:

  • hash_table — a HashTable
  • key — a key to check

destroy

@staticmethod
def destroy(hash_table: dict[int, int]) -> 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 the HashTable with 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.

Parameters:

find

@staticmethod
def find(hash_table: dict[int, int], predicate: HRFunc) -> int | None

Calls the given function for key/value pairs in the HashTable until predicate returns 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. HashTable.lookup. So code that frequently issues HashTable.find or 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).

Parameters:

  • hash_table — a HashTable
  • predicate — function to test the key/value pairs for a certain property

foreach

@staticmethod
def foreach(hash_table: dict[int, int], func: HFunc) -> 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 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 HashTable.foreach_remove.

The order in which HashTable.foreach iterates over the keys/values in the hash table is not defined.

See HashTable.find for performance caveats for linear order searches in contrast to HashTable.lookup.

Parameters:

  • hash_table — a HashTable
  • func — the function to call for each key/value pair

foreach_remove

@staticmethod
def foreach_remove(hash_table: dict[int, int], func: HRFunc) -> int

Calls the given function for each key/value pair in the HashTable. If the function returns True, then the key/value pair is removed from the HashTable. If you supplied key or value destroy functions when creating the HashTable, 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

foreach_steal

@staticmethod
def foreach_steal(hash_table: dict[int, int], func: HRFunc) -> int

Calls the given function for each key/value pair in the HashTable. If the function returns True, then the key/value pair is removed from the HashTable, but no key or value destroy functions are called.

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

insert

@staticmethod
def insert(hash_table: dict[int, int], key: int | None = ..., value: int | 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 a value_destroy_func when creating the HashTable, the old value is freed using that function. If you supplied a key_destroy_func when creating the 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.

Parameters:

  • hash_table — a HashTable
  • key — a key to insert
  • value — the value to associate with the key

lookup

@staticmethod
def lookup(hash_table: dict[int, int], key: int | None = ...) -> int | 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 value None. If you need this distinction, use HashTable.lookup_extended.

Parameters:

  • hash_table — a HashTable
  • key — the key to look up

lookup_extended

@staticmethod
def lookup_extended(hash_table: dict[int, int], lookup_key: int | None = ...) -> tuple[bool, int, int]

Looks up a key in the HashTable, returning the original key and the associated value and a #gboolean which is 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 HashTable.remove.

You can actually pass None for lookup_key to test whether the None key exists, provided the hash and equal functions of hash_table are None-safe.

Parameters:

  • hash_table — a HashTable
  • lookup_key — the key to look up

new_similar

@staticmethod
def new_similar(other_hash_table: dict[int, int]) -> dict[int, int]

Creates a new HashTable like 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.

Parameters:

ref

@staticmethod
def ref(hash_table: dict[int, int]) -> dict[int, int]

Atomically increments the reference count of hash_table by one. This function is MT-safe and may be called from any thread.

Parameters:

remove

@staticmethod
def remove(hash_table: dict[int, int], key: int | None = ...) -> bool

Removes a key and its associated value from a HashTable.

If the HashTable was 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.

Parameters:

  • hash_table — a HashTable
  • key — the key to remove

remove_all

@staticmethod
def remove_all(hash_table: dict[int, int]) -> None

Removes all keys and their associated values from a HashTable.

If the HashTable was 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.

Parameters:

replace

@staticmethod
def replace(hash_table: dict[int, int], key: int | None = ..., value: int | None = ...) -> bool

Inserts a new key and value into a HashTable similar to HashTable.insert. The difference is that if the key already exists in the HashTable, it gets replaced by the new key. If you supplied a value_destroy_func when creating the HashTable, the old value is freed using that function. If you supplied a key_destroy_func when creating the 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.

Parameters:

  • hash_table — a HashTable
  • key — a key to insert
  • value — the value to associate with the key

size

@staticmethod
def size(hash_table: dict[int, int]) -> int

Returns the number of elements contained in the HashTable.

Parameters:

steal

@staticmethod
def steal(hash_table: dict[int, int], key: int | None = ...) -> bool

Removes a key and its associated value from a HashTable without calling the key and value destroy functions.

Parameters:

  • hash_table — a HashTable
  • key — the key to remove

steal_all

@staticmethod
def steal_all(hash_table: dict[int, int]) -> None

Removes all keys and their associated values from a HashTable without calling the key and value destroy functions.

Parameters:

steal_extended

@staticmethod
def steal_extended(hash_table: dict[int, int], lookup_key: int | None = ...) -> tuple[bool, int, int]

Looks up a key in the HashTable, stealing the original key and the associated value and returning True if the key was found. If the key was not found, False is 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 HashTable.steal. That is the case regardless whether stolen_key or stolen_value output parameters are requested.

You can pass None for lookup_key, provided the hash and equal functions of hash_table are None-safe.

The dictionary implementation optimizes for having all values identical to their keys, for example by using HashTable.add. Before 2.82, when stealing both the key and the value from such a dictionary, the value was None. Since 2.82, the returned value and key will be the same.

Parameters:

  • hash_table — a HashTable
  • lookup_key — the key to look up

unref

@staticmethod
def unref(hash_table: dict[int, int]) -> None

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.

Parameters: