find


Description:

[ Version ( since = "2.4" ) ]
public unowned V find (HRFunc<K,V> predicate)

Calls the given function for key/value pairs in the GenericSet 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. @get . So code that frequently issues find or for_each (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).

Example: Predicate based search:

public static int main (string[] args) {
HashTable<int, string> table = new HashTable<int, string> (direct_hash, direct_equal);
table.insert (1, "first string");
table.insert (2, "second string");
table.insert (3, "third string");

// Output: ``second string``
unowned string? str = table.find ((k, v) => {
return v.has_prefix ("second");
});
print ("%s\n", str);

return 0;
}

valac --pkg glib-2.0 GLib.HashTable.find.vala

Parameters:

predicate

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

hash_table

a GenericSet

user_data

user data to pass to the function

Returns:

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