[PATCH]: g_tree_get_keys and g_tree_get_values
Submitted by Jukka-Pekka Iivonen
Link to original bug (#520786)
Description
Please describe the problem: GTree is missing an API to get the keys and values into a list. A similar API is provided in GHashTable and it has been very useful. Since GTree is order preserving we might want to specify if we want ascending or descending order in the generated list (see the patch below).
<------- patch -------->
static gboolean cb_tree_get_keys(gpointer key, gpointer value, GList **list) { *list = g_list_prepend( *list, key );
return FALSE;
}
/**
- g_tree_get_keys:
- Retrieves every key inside a balanced binary tree. The returned data is valid until tree is modified.
- tree : a GTree
- ascending : TRUE to get the keys in ascending order, FALSE if descending order prefered
- Returns : a GList containing all the keys inside the tree. 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.
/ GList g_tree_get_keys(GTree *tree, gboolean ascending) { GList *list = NULL;
g_tree_foreach( tree, (GTraverseFunc) cb_tree_get_keys, &list );
return (ascending) ? g_list_reverse( list ) : list;
}
static gboolean cb_tree_get_values(gpointer key, gpointer value, GList **list) { *list = g_list_prepend( *list, value );
return FALSE;
}
/**
- g_tree_get_values:
- Retrieves every value inside a balanced binary tree. The returned data is valid until tree is modified.
- tree : a GTree
- ascending : TRUE to get the values in ascending order of keys mapped to the values, FALSE if descending order
-
prefered
- Returns : a GList containing all the values inside the tree. The content of the list is owned by the tree and
-
should not be modified or freed. Use g_list_free() when done using the list.
/ GList g_tree_get_values(GTree *tree, gboolean ascending) { GList *list = NULL;
g_tree_foreach( tree, (GTraverseFunc) cb_tree_get_values, &list );
return (ascending) ? g_list_reverse( list ) : list;
}
Steps to reproduce:
Actual results:
Expected results:
Does this happen every time?
Other information: