search_key


Description:

[ CCode ( cname = "g_tree_search" ) ]
public unowned V search_key (CompareFunc<K> search_func, K key)

Searches a Tree using search_func.

The search_func is called with a pointer to the key of a key/value pair in the tree, and the passed in user_data. If search_func returns 0 for a key/value pair, then the corresponding value is returned as the result of search_key. If search_func returns -1, searching will proceed among the key/value pairs that have a smaller key; if search_func returns 1, searching will proceed among the key/value pairs that have a larger key.

Example: Search a Tree using a search-func:

public static int main (string[] args) {
Tree<string, string> tree = new Tree<string, string> ((a, b) => { return strcmp (a, b); });
tree.insert ("key1", "val1");
tree.insert ("key2", "val2");
tree.insert ("key3", "val3");

// Output:
// ``visit: key2, key3``
// ``visit: key3, key3``
string val = tree.search_key ((key, needle) => {
// key: one of {"key1", "key2", "key3"}
// needle: KEY2
print ("visit: %s, %s\n", key, needle);

// param order is significant!
return strcmp (needle, key);
}, "key3");

// Output:
// ``key=KEY2, value=val3``
print ("Selected: %s\n", val);

return 0;
}

valac --pkg glib-2.0 GLib.Tree.search_key.vala

Parameters:

this

a Tree

search_func

a function used to search the Tree

user_data

the data passed as the second argument to search_func

Returns:

the value corresponding to the found key, or null if the key was not found