1.10 <GHashTable>

Structure: <GHashTable>

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

Procedures

Procedure: hash-table:add? hash-table [key] ⇒ %return

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

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

a key to insert

%return

Returned: a boolean.

#t if the key did not exist yet

Procedure: hash-table:contains? hash-table [key] ⇒ %return

Checks if key is in hash-table.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

a key to check

%return

Returned: a boolean.

#t if key is in hash-table, #f otherwise.

Procedure: hash-table:destroy hash-table

Destroys all keys and values in the <GHashTable> and decrements its reference count by 1. If keys and/or values are dynamically allocated, you should either free them first or create the <GHashTable> 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.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

Procedure: hash-table:find hash-table predicate ⇒ %return

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

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

predicate

Passed: a procedure.

function to test the key/value pairs for a certain property

See GHRFunc for how it is called.

%return

Returned: a foreign pointer, or #f.

The value of the first key/value pair is returned, for which predicate evaluates to #t. If no pair with the requested property is found, #f is returned.

user-data

Not passed: guile-gi supplies it.

user data to pass to the function

Procedure: hash-table:foreach hash-table func

Calls the given function for each of the key/value pairs in the <GHashTable>. 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 hash-table-foreach-remove.

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

See hash-table-find for performance caveats for linear order searches in contrast to hash-table-lookup.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

func

Passed: a procedure.

the function to call for each key/value pair

See GHFunc for how it is called.

user-data

Not passed: guile-gi supplies it.

user data to pass to the function

Procedure: hash-table:foreach-remove hash-table func ⇒ %return

Calls the given function for each key/value pair in the <GHashTable>. If the function returns #t, then the key/value pair is removed from the <GHashTable>. If you supplied key or value destroy functions when creating the <GHashTable>, they are used to free the memory allocated for the removed keys and values.

See GHashTableIter for an alternative way to loop over the key/value pairs in the hash table.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

func

Passed: a procedure.

the function to call for each key/value pair

See GHRFunc for how it is called.

%return

Returned: an unsigned 32-bit integer.

the number of key/value pairs removed

user-data

Not passed: guile-gi supplies it.

user data to pass to the function

Procedure: hash-table:foreach-steal hash-table func ⇒ %return

Calls the given function for each key/value pair in the <GHashTable>. If the function returns #t, then the key/value pair is removed from the <GHashTable>, but no key or value destroy functions are called.

See GHashTableIter for an alternative way to loop over the key/value pairs in the hash table.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

func

Passed: a procedure.

the function to call for each key/value pair

See GHRFunc for how it is called.

%return

Returned: an unsigned 32-bit integer.

the number of key/value pairs removed.

user-data

Not passed: guile-gi supplies it.

user data to pass to the function

Procedure: hash-table:insert? hash-table [key] [value] ⇒ %return

Inserts a new key and value into a <GHashTable>.

If the key already exists in the <GHashTable> its current value is replaced with the new value. If you supplied a value-destroy-func when creating the <GHashTable>, the old value is freed using that function. If you supplied a key-destroy-func when creating the <GHashTable>, 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.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

a key to insert

value

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the value to associate with the key

%return

Returned: a boolean.

#t if the key did not exist yet

Procedure: hash-table:lookup hash-table [key] ⇒ %return

Looks up a key in a <GHashTable>. Note that this function cannot distinguish between a key that is not present and one which is present and has the value #f. If you need this distinction, use hash-table-lookup-extended.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to look up

%return

Returned: a foreign pointer, or #f.

the associated value, or #f if the key is not found

Procedure: hash-table:lookup-extended hash-table [lookup-key] ⇒ %return orig-key value

Looks up a key in the <GHashTable>, returning the original key and the associated value and a gboolean which is #t if the key was found. This is useful if you need to free the memory allocated for the original key, for example before calling hash-table-remove?.

You can actually pass #f for lookup-key to test whether the #f key exists, provided the hash and equal functions of hash-table are #f-safe.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

lookup-key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to look up

%return

Returned: a boolean.

#t if the key was found in the <GHashTable>

orig-key

Returned: a foreign pointer, or #f.

return location for the original key

value

Returned: a foreign pointer, or #f.

return location for the value associated with the key

Procedure: hash-table:new-similar other-hash-table ⇒ %return

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

other-hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

Another <GHashTable>

%return

Returned: a hash table, each key a foreign pointer and each value a foreign pointer.

a new <GHashTable>

Procedure: hash-table:ref hash-table ⇒ %return

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

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a valid <GHashTable>

%return

Returned: a hash table, each key a foreign pointer and each value a foreign pointer.

the passed in <GHashTable>

Procedure: hash-table:remove-all hash-table

Removes all keys and their associated values from a <GHashTable>.

If the <GHashTable> 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.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

Procedure: hash-table:remove? hash-table [key] ⇒ %return

Removes a key and its associated value from a <GHashTable>.

If the <GHashTable> 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.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to remove

%return

Returned: a boolean.

#t if the key was found and removed from the <GHashTable>

Procedure: hash-table:replace? hash-table [key] [value] ⇒ %return

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

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

a key to insert

value

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the value to associate with the key

%return

Returned: a boolean.

#t if the key did not exist yet

Procedure: hash-table:size hash-table ⇒ %return

Returns the number of elements contained in the <GHashTable>.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

%return

Returned: an unsigned 32-bit integer.

the number of key/value pairs in the <GHashTable>.

Procedure: hash-table:steal-all hash-table

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

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

Procedure: hash-table:steal-extended hash-table [lookup-key] ⇒ %return stolen-key stolen-value

Looks up a key in the <GHashTable>, stealing the original key and the associated value and returning #t if the key was found. If the key was not found, #f 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 hash-table-steal?. That is the case regardless whether stolen-key or stolen-value output parameters are requested.

You can pass #f for lookup-key, provided the hash and equal functions of hash-table are #f-safe.

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

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

lookup-key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to look up

%return

Returned: a boolean.

#t if the key was found in the <GHashTable>

stolen-key

Returned: a foreign pointer, or #f.

return location for the original key

stolen-value

Returned: a foreign pointer, or #f.

return location for the value associated with the key

Procedure: hash-table:steal? hash-table [key] ⇒ %return

Removes a key and its associated value from a <GHashTable> without calling the key and value destroy functions.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a <GHashTable>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to remove

%return

Returned: a boolean.

#t if the key was found and removed from the <GHashTable>

Procedure: hash-table:unref hash-table

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.

hash-table

Passed: a hash table, each key a foreign pointer and each value a foreign pointer.

a valid <GHashTable>