Potřebuji vytvořit sadu funkcí, které se rozprostírají glib2 GTreena:
- najít první prvek
- najít poslední
- najít nejbližší (podlahu, horní limit, největší méně než alespoň větší než)
Nalezení První z nich je snadné. Vy prostě zastavit g_tree_foreach()Calback po prvním. Ale jak najít poslední prvek bez křížení celý strom ?
Myslel jsem, že bych mohl použít g_tree_search()s zpětné volání, která udrží vrací kladnou hodnotu, až našel, ale jak mám vědět, že jsem v současné době na poslední prvek?
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <glib.h>
static
gint compare_int(gconstpointer p1, gconstpointer p2) {
int i1 = GPOINTER_TO_INT(p1);
int i2 = GPOINTER_TO_INT(p2);
//printf(%d %d\n, i1, i2);
return i1 == i2 ? 0 : i1 > i2 ? 1 : -1;
}
static
gboolean traverse(gpointer key, gpointer value, gpointer data) {
//int ikey = GPOINTER_TO_INT(key);
const char *sval = (const char *)value;
printf(%s\n, sval);
return FALSE;
}
static
gint find_last(gconstpointer p, gpointer user_data) {
return 1;
}
static inline const char *NULS(const char *s) {
return s ? s : NULL;
}
int main(int argc, char *argv[]) {
GTree *tree = g_tree_new(compare_int);
g_tree_insert(tree, GINT_TO_POINTER(10), ten);
g_tree_insert(tree, GINT_TO_POINTER(-99), minus ninety-nine);
g_tree_insert(tree, GINT_TO_POINTER(8), eight);
g_tree_foreach(tree, traverse, NULL);
printf(=======\n%s\n, NULS((const char*)g_tree_search(tree, (GCompareFunc)find_last, NULL)));
return 0;
}













