From bb545b3cec36359286807096eef91175d1dd5da4 Mon Sep 17 00:00:00 2001 From: compudj Date: Tue, 25 May 2004 22:45:05 +0000 Subject: [PATCH] Direct sorting when adding git-svn-id: http://ltt.polymtl.ca/svn@543 04897980-b3bd-0310-b5e0-8ef037075253 --- ltt/branches/poly/lttv/lttv/hook.c | 58 +++++++++++++++++++++++------- ltt/branches/poly/lttv/lttv/hook.h | 7 ++-- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/ltt/branches/poly/lttv/lttv/hook.c b/ltt/branches/poly/lttv/lttv/hook.c index f92b397d..183f499a 100644 --- a/ltt/branches/poly/lttv/lttv/hook.c +++ b/ltt/branches/poly/lttv/lttv/hook.c @@ -49,27 +49,59 @@ void lttv_hooks_destroy(LttvHooks *h) void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data, LttvHookPrio p) { - LttvHookClosure c; - + LttvHookClosure *c, new_c; + guint i; + if(h == NULL)g_error("Null hook added"); - c.hook = f; - c.hook_data = hook_data; - c.prio = p; - g_array_append_val(h,c); - g_array_sort(h, (GCompareFunc)lttv_hooks_prio_compare); + new_c.hook = f; + new_c.hook_data = hook_data; + new_c.prio = p; + for(i = 0; i < h->len; i++) { + + c = &g_array_index(h, LttvHookClosure, i); + if(new_c.prio < c->prio) { + g_array_insert_val(h,i,new_c); + return; + } + } + if(i == h->len) + g_array_append_val(h,new_c); } - -void lttv_hooks_add_list(LttvHooks *h, LttvHooks *list) +/* lttv_hooks_add_list + * + * Adds a sorted list into another sorted list. + * + * Note : h->len is modified, but only incremented. This assures + * its coherence through the function. + * + * j is an index to the element following the last one added in the + * destination array. + */ +void lttv_hooks_add_list(LttvHooks *h, const LttvHooks *list) { - guint i; + guint i,j; + LttvHookClosure *c; + const LttvHookClosure *new_c; if(list == NULL) return; - for(i = 0 ; i < list->len; i++) { - g_array_append_val(h,g_array_index(list, LttvHookClosure, i)); + for(i = 0, j = 0 ; i < list->len; i++) { + new_c = &g_array_index(list, LttvHookClosure, i); + while(j < h->len) { + c = &g_array_index(h, LttvHookClosure, j); + if(new_c->prio < c->prio) { + g_array_insert_val(h,j,*new_c); + j++; + break; + } + else j++; + } + if(j == h->len) { + g_array_append_val(h,*new_c); + j++; + } } - g_array_sort(h, (GCompareFunc)lttv_hooks_prio_compare); } diff --git a/ltt/branches/poly/lttv/lttv/hook.h b/ltt/branches/poly/lttv/lttv/hook.h index bd68e096..c1814d81 100644 --- a/ltt/branches/poly/lttv/lttv/hook.h +++ b/ltt/branches/poly/lttv/lttv/hook.h @@ -41,7 +41,10 @@ typedef GArray LttvHooks; * functions. Hook removal does not change list order. */ -#define LTTV_PRIO_DEFAULT 0 +#define LTTV_PRIO_DEFAULT 50 +#define LTTV_PRIO_HIGH 0 +#define LTTV_PRIO_LOW 99 + typedef gint LttvHookPrio; /* Create and destroy a list of hooks */ @@ -58,7 +61,7 @@ void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data, LttvHookPrio p); /* Add a list of hooks to the list h */ -void lttv_hooks_add_list(LttvHooks *h, LttvHooks *list); +void lttv_hooks_add_list(LttvHooks *h, const LttvHooks *list); /* Remove a hook from the list. Return the hook data. */ -- 2.34.1