Re: [gtk-list] g_[s]list_qsort suggestion
- From: Tim Janik <timj gtk org>
- To: gtk-list redhat com
- Subject: Re: [gtk-list] g_[s]list_qsort suggestion
- Date: Fri, 5 Jun 1998 22:49:15 +0200 (CEST)
On Fri, 5 Jun 1998 dramboz@imaginet.fr wrote:
>
> Hello,
>
> What about adding a function to quickly sort GLists
> and GSLists ?
>
> Here's a simple implementation using qsort :
>
>
>
> typedef gint (*GQSortFunc) ( const gpointer value1,
> const gpointer value2);
hm, as has been pointed out a few times already,
const gpointer doesn't work, we would ned tzo have some
typedef const void * gconstpointer;
> GList*
> g_list_qsort( GList *list, GQSortFunc func) {
> GList *item;
> gpointer *base, *tmp;
> guint size;
>
> g_return_val_if_fail( list != NULL, list);
> g_return_val_if_fail( func != NULL, list);
>
> size = g_list_length( list);
>
> base = tmp = g_malloc( size*sizeof( gpointer));
> g_return_val_if_fail( base != NULL, list);
>
> item = list;
> while ( item) {
> *tmp++ = item->data;
> item = item->next;
> }
>
> qsort( base, size, sizeof( gpointer), func);
>
> tmp = base;
> item = list;
> while ( item) {
> item->data = *tmp++;
> item = item->next;
> }
though just changing the ->data portion of all the GList structures is
an efficient solution, this is not always what's needed. we loose the
ability to hold GList pointers across g_list_qsort() invokations with this.
GList *sort_me;
[...]
GList *first_item;
first_item = sort_me;
sort_me = g_list_qsort (sort_me, SomeSortFunc);
[now, `first_item' still points to the first GList structure, but not to the same
->data anymore.]
>
> g_free( base);
>
> return list;
> }
could we try to get around this limitation?
---
ciaoTJ
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]