Class HashTable<K,V>
GHashTable
struct is an opaque data structure to represent a
hash table. The keys and values of the Java class can be pointers
(MemorySegment
objects), strings, primitive values or native objects
(implementing the Proxy
interface).
This class is intended to help Java developers deal with native functions that require or return a GHashTable. It is not meant to be used as a replacement for Java's own HashMap.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,
V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
ConstructorDescriptionHashTable
(MemorySegment address) Create a HashTable proxy instance for the provided memory address.HashTable
(MemorySegment address, Function<MemorySegment, K> makeKey, Function<MemorySegment, V> makeValue) Create a HashTable proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionboolean
This is a convenience function for using aGHashTable
as a set.boolean
Checks ifkey
is in this GLib.HashTable.void
destroy()
Destroys all keys and values in theGHashTable
and decrements its reference count by 1.entrySet()
Calls the given function for key/value pairs in theGHashTable
untilpredicate
returnstrue
.void
Calls the given function for each of the key/value pairs in theGHashTable
.int
foreachRemove
(HRFunc func) Calls the given function for each key/value pair in theGHashTable
.int
foreachSteal
(HRFunc func) Calls the given function for each key/value pair in theGHashTable
.getKeys()
Retrieves every key inside this GLib.HashTable.Retrieves every key inside this GLib.HashTable, as an array.Retrieves every key inside this GLib.HashTable, as aGPtrArray
.static Type
getType()
Get the GType of the HashTable classRetrieves every value inside this GLib.HashTable.Retrieves every value inside this GLib.HashTable, as aGPtrArray
.handle()
Get the native memory address of the object.boolean
Inserts a new key and value into aGHashTable
.Looks up a key in aGHashTable
.boolean
Looks up a key in theGHashTable
, returning the original key and the associated value and agboolean
which istrue
if the key was found.static HashTable
<MemorySegment, MemorySegment> Creates a newGHashTable
with a reference count of 1.Creates a newGHashTable
like g_hash_table_new_full() with a reference count of 1.ref()
Atomically increments the reference count of this GLib.HashTable by one.boolean
Removes a key and its associated value from aGHashTable
.void
Removes all keys and their associated values from aGHashTable
.boolean
Inserts a new key and value into aGHashTable
similar to g_hash_table_insert().int
size()
Returns the number of elements contained in theGHashTable
.boolean
Removes a key and its associated value from aGHashTable
without calling the key and value destroy functions.void
stealAll()
Removes all keys and their associated values from aGHashTable
without calling the key and value destroy functions.Removes all keys and their associated values from aGHashTable
without calling the key destroy functions, returning the keys as aGPtrArray
with the free func set to the this GLib.HashTable key destroy function.Removes all keys and their associated values from aGHashTable
without calling the value destroy functions, returning the values as aGPtrArray
with the free func set to the this GLib.HashTable value destroy function.boolean
Looks up a key in theGHashTable
, stealing the original key and the associated value and returningtrue
if the key was found.void
unref()
Atomically decrements the reference count of this GLib.HashTable by one.Methods inherited from class java.util.AbstractMap
clear, clone, containsKey, containsValue, equals, get, hashCode, isEmpty, keySet, putAll, remove, toString, values
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
HashTable
public HashTable(MemorySegment address, Function<MemorySegment, K> makeKey, Function<MemorySegment, V> makeValue) Create a HashTable proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native objectmakeKey
- function that creates a K from a pointermakeValue
- function that creates a V from a pointer
-
HashTable
Create a HashTable proxy instance for the provided memory address. The hashtable containsMemorySegment
keys and values.- Parameters:
address
- the memory address of the native object
-
-
Method Details
-
handle
Description copied from interface:Proxy
Get the native memory address of the object. -
entrySet
-
put
-
getType
-
new_
public static HashTable<MemorySegment,MemorySegment> new_(HashFunc hashFunc, EqualFunc keyEqualFunc) Creates a newGHashTable
with a reference count of 1.Hash values returned by
hashFunc
are used to determine where keys are stored within theGHashTable
data structure. The g_direct_hash(), g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash() functions are provided for some common types of keys. IfhashFunc
isnull
, g_direct_hash() is used.keyEqualFunc
is used when looking up keys in theGHashTable
. The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal() and g_str_equal() functions are provided for the most common types of keys. IfkeyEqualFunc
isnull
, keys are compared directly in a similar fashion to g_direct_equal(), but without the overhead of a function call.keyEqualFunc
is called with the key from the hash table as its first parameter, and the user-provided key to check against as its second.- Parameters:
hashFunc
- a function to create a hash value from a keykeyEqualFunc
- a function to check two keys for equality- Returns:
- a new
GHashTable
-
add
This is a convenience function for using aGHashTable
as a set. It is equivalent to calling g_hash_table_replace() 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.
- Parameters:
key
- a key to insert- Returns:
true
if the key did not exist yet
-
contains
Checks ifkey
is in this GLib.HashTable.- Parameters:
key
- a key to check- Returns:
true
ifkey
is in this GLib.HashTable,false
otherwise.
-
destroy
public void destroy()Destroys all keys and values in theGHashTable
and decrements its reference count by 1. If keys and/or values are dynamically allocated, you should either free them first or create theGHashTable
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. -
find
Calls the given function for key/value pairs in theGHashTable
untilpredicate
returnstrue
. The function is passed the key and value of each pair, and the givenuserData
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. g_hash_table_lookup(). So code that frequently issues g_hash_table_find() or g_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).
- Parameters:
predicate
- function to test the key/value pairs for a certain property- Returns:
- The value of the first key/value pair is returned,
for which
predicate
evaluates totrue
. If no pair with the requested property is found,null
is returned.
-
foreach
Calls the given function for each of the key/value pairs in theGHashTable
. The function is passed the key and value of each pair, and the givenuserData
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 g_hash_table_foreach_remove().The order in which g_hash_table_foreach() iterates over the keys/values in the hash table is not defined.
See g_hash_table_find() for performance caveats for linear order searches in contrast to g_hash_table_lookup().
- Parameters:
func
- the function to call for each key/value pair
-
foreachRemove
Calls the given function for each key/value pair in theGHashTable
. If the function returnstrue
, then the key/value pair is removed from theGHashTable
. If you supplied key or value destroy functions when creating theGHashTable
, 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.- Parameters:
func
- the function to call for each key/value pair- Returns:
- the number of key/value pairs removed
-
foreachSteal
Calls the given function for each key/value pair in theGHashTable
. If the function returnstrue
, then the key/value pair is removed from theGHashTable
, 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.- Parameters:
func
- the function to call for each key/value pair- Returns:
- the number of key/value pairs removed.
-
getKeys
Retrieves every key inside this GLib.HashTable. The returned data is valid until changes to the hash release those keys.This iterates over every entry in the hash table to build its return value. To iterate over the entries in a
GHashTable
more efficiently, use aGHashTableIter
.- Returns:
- a
GList
containing all the keys inside the hash table. The content of the list is owned by the hash table and should not be modified or freed. Use g_list_free() when done using the list.
-
getKeysAsArray
Retrieves every key inside this GLib.HashTable, as an array.The returned array is
null
-terminated but may containnull
as a key. Uselength
to determine the true length if it's possible thatnull
was used as the value for a key.Note: in the common case of a string-keyed
GHashTable
, the return value of this function can be conveniently cast to (const gchar **).This iterates over every entry in the hash table to build its return value. To iterate over the entries in a
GHashTable
more efficiently, use aGHashTableIter
.You should always free the return result with g_free(). In the above-mentioned case of a string-keyed hash table, it may be appropriate to use g_strfreev() if you call g_hash_table_steal_all() first to transfer ownership of the keys.
- Returns:
- a
null
-terminated array containing each key from the table.
-
getKeysAsPtrArray
Retrieves every key inside this GLib.HashTable, as aGPtrArray
. The returned data is valid until changes to the hash release those keys.This iterates over every entry in the hash table to build its return value. To iterate over the entries in a
GHashTable
more efficiently, use aGHashTableIter
.You should always unref the returned array with g_ptr_array_unref().
- Returns:
- a
GPtrArray
containing each key from the table. Unref with with g_ptr_array_unref() when done.
-
getValues
Retrieves every value inside this GLib.HashTable. The returned data is valid until this GLib.HashTable is modified.This iterates over every entry in the hash table to build its return value. To iterate over the entries in a
GHashTable
more efficiently, use aGHashTableIter
.- Returns:
- a
GList
containing all the values inside the hash table. The content of the list is owned by the hash table and should not be modified or freed. Use g_list_free() when done using the list.
-
getValuesAsPtrArray
Retrieves every value inside this GLib.HashTable, as aGPtrArray
. The returned data is valid until changes to the hash release those values.This iterates over every entry in the hash table to build its return value. To iterate over the entries in a
GHashTable
more efficiently, use aGHashTableIter
.You should always unref the returned array with g_ptr_array_unref().
- Returns:
- a
GPtrArray
containing each value from the table. Unref with with g_ptr_array_unref() when done.
-
insert
Inserts a new key and value into aGHashTable
.If the key already exists in the
GHashTable
its current value is replaced with the new value. If you supplied avalueDestroyFunc
when creating theGHashTable
, the old value is freed using that function. If you supplied akeyDestroyFunc
when creating theGHashTable
, 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:
key
- a key to insertvalue
- the value to associate with the key- Returns:
true
if the key did not exist yet
-
lookup
Looks up a key in aGHashTable
. Note that this function cannot distinguish between a key that is not present and one which is present and has the valuenull
. If you need this distinction, use g_hash_table_lookup_extended().- Parameters:
key
- the key to look up- Returns:
- the associated value, or
null
if the key is not found
-
lookupExtended
public boolean lookupExtended(@Nullable K lookupKey, @Nullable @Nullable Out<K> origKey, @Nullable @Nullable Out<V> value) Looks up a key in theGHashTable
, 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 calling g_hash_table_remove().You can actually pass
null
forlookupKey
to test whether thenull
key exists, provided the hash and equal functions of this GLib.HashTable arenull
-safe.- Parameters:
lookupKey
- the key to look uporigKey
- return location for the original keyvalue
- return location for the value associated with the key- Returns:
true
if the key was found in theGHashTable
-
newSimilar
Creates a newGHashTable
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 this GLib.HashTable.
The returned hash table will be empty; it will not contain the keys or values from this GLib.HashTable.
- Returns:
- a new
GHashTable
-
ref
-
remove_
Removes a key and its associated value from aGHashTable
.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.This method calls
g_hash_table_remove()
. It does not override or replaceAbstractMap.remove(Object)
.- Parameters:
key
- the key to remove- Returns:
true
if the key was found and removed from theGHashTable
-
removeAll
public void removeAll()Removes all keys and their associated values from aGHashTable
.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. -
replace_
Inserts a new key and value into aGHashTable
similar to g_hash_table_insert(). The difference is that if the key already exists in theGHashTable
, it gets replaced by the new key. If you supplied avalueDestroyFunc
when creating theGHashTable
, the old value is freed using that function. If you supplied akeyDestroyFunc
when creating theGHashTable
, 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.
This method calls
g_hash_table_replace()
. It does not override or replaceMap.replace(Object, Object)
.- Parameters:
key
- a key to insertvalue
- the value to associate with the key- Returns:
true
if the key did not exist yet
-
size
-
steal
Removes a key and its associated value from aGHashTable
without calling the key and value destroy functions.- Parameters:
key
- the key to remove- Returns:
true
if the key was found and removed from theGHashTable
-
stealAll
public void stealAll()Removes all keys and their associated values from aGHashTable
without calling the key and value destroy functions. -
stealAllKeys
Removes all keys and their associated values from aGHashTable
without calling the key destroy functions, returning the keys as aGPtrArray
with the free func set to the this GLib.HashTable key destroy function.- Returns:
- a
GPtrArray
containing each key of the table. Unref with with g_ptr_array_unref() when done.
-
stealAllValues
Removes all keys and their associated values from aGHashTable
without calling the value destroy functions, returning the values as aGPtrArray
with the free func set to the this GLib.HashTable value destroy function.- Returns:
- a
GPtrArray
containing each value of the table. Unref with with g_ptr_array_unref() when done.
-
stealExtended
public boolean stealExtended(@Nullable K lookupKey, @Nullable @Nullable Out<K> stolenKey, @Nullable @Nullable Out<V> stolenValue) Looks up a key in theGHashTable
, stealing the original key and the associated value and returningtrue
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 g_hash_table_steal(). That is the case regardless whether
stolenKey
orstolenValue
output parameters are requested.You can pass
null
forlookupKey
, provided the hash and equal functions of this GLib.HashTable arenull
-safe.The dictionary implementation optimizes for having all values identical to their keys, for example by using g_hash_table_add(). Before 2.82, when stealing both the key and the value from such a dictionary, the value was
null
. Since 2.82, the returned value and key will be the same.- Parameters:
lookupKey
- the key to look upstolenKey
- return location for the original keystolenValue
- return location for the value associated with the key- Returns:
true
if the key was found in theGHashTable
-
unref
public void unref()Atomically decrements the reference count of this GLib.HashTable 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.
-