g_[s]list_qsort suggestion




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); 

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;
    }
    
    g_free( base);
    
    return list;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]