Skip to content

GLib.HashTableIter

record (struct)

A GHashTableIter structure represents an iterator that can be used to iterate over the elements of a HashTable. GHashTableIter structures are typically allocated on the stack and then initialized with HashTableIter.init.

The iteration order of a HashTableIter over the keys/values in a hash table is not defined.

Methods

get_hash_table

def get_hash_table(self) -> dict[int, int]

Returns the HashTable associated with iter.

init

def init(self, hash_table: dict[int, int]) -> None

Initializes a key/value pair iterator and associates it with hash_table. Modifying the hash table after calling this function invalidates the returned iterator.

The iteration order of a HashTableIter over the keys/values in a hash table is not defined.

GHashTableIter iter;
gpointer key, value;

g_hash_table_iter_init (&iter, hash_table);
while (g_hash_table_iter_next (&iter, &key, &value))
  {
    // do something with key and value
  }

Parameters:

next

def next(self) -> tuple[bool, int, int]

Advances iter and retrieves the key and/or value that are now pointed to as a result of this advancement. If False is returned, key and value are not set, and the iterator becomes invalid.

remove

def remove(self) -> None

Removes the key/value pair currently pointed to by the iterator from its associated HashTable. Can only be called after HashTableIter.next returned True, and cannot be called more than once for the same key/value pair.

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.

It is safe to continue iterating the HashTable afterward:

while (g_hash_table_iter_next (&iter, &key, &value))
  {
    if (condition)
      g_hash_table_iter_remove (&iter);
  }

replace

def replace(self, value: int | None = ...) -> None

Replaces the value currently pointed to by the iterator from its associated HashTable. Can only be called after HashTableIter.next returned True.

If you supplied a value_destroy_func when creating the HashTable, the old value is freed using that function.

Parameters:

  • value — the value to replace with

steal

def steal(self) -> None

Removes the key/value pair currently pointed to by the iterator from its associated HashTable, without calling the key and value destroy functions. Can only be called after HashTableIter.next returned True, and cannot be called more than once for the same key/value pair.

Properties

dummy1

dummy1: int  # read/write

dummy2

dummy2: int  # read/write

dummy3

dummy3: int  # read/write

dummy4

dummy4: int  # read/write

dummy5

dummy5: bool  # read/write

dummy6

dummy6: int  # read/write