开发者

Using g_array_sort function

开发者 https://www.devze.com 2023-02-20 00:05 出处:网络
I need to use the function g_array_sort(GArray *array,开发者_高级运维 GCompareFunc *func) but I do not understand the second parameter.

I need to use the function g_array_sort(GArray *array,开发者_高级运维 GCompareFunc *func) but I do not understand the second parameter.

Please show me how it should be called and if possible please attach a sample example....


The second argument of g_array_sort () is a pointer to a function. If you look at the documentation for GCompareFunc you will see that it is a function that takes two pointers and returns an int:

gint (*GCompareFunc) (gconstpointer a, gconstpointer b);

The documentation also tells you what this function should do:

The function should return a negative integer if the first value comes before the second, 0 if they are equal, or a positive integer if the first value comes after the second.

As you didn't specify what datatypes you're storing in your array, I'll just go with strings. Your sort function would look something like this:

int my_string_sort_function (gconstpointer a, gconstpointer b)
{
    char *str_a = (char *)a;
    char *str_b = (char *)b;

    return strcmp (str_a, str_b);
}

If you were storing numbers in your array you could do something like

int my_int_sort_function (gconstpointer a, gconstpointer b)
{
    int int_a = GPOINTER_TO_INT (a);
    int int_b = GPOINTER_TO_INT (b);

    return int_a - int_b;
}

To use these functions with g_array_sort

g_array_sort (my_array, my_int_sort_function);


This is an open source library, so you could take a look at the code of g_array_sort() itself. Just type in g_array_sort into Google's code search and you'll get the code.

There you can see that this function actually calls libc's qsort(3) and passes the function you are interested in to qsort unchanged.

Now, Linux's qsort man page has a good example of qsort use.


Add an example for iain's answer.

This is an example to show the effect of GArray functions including 'g_array_sort()'. You can see that after the sort, the int array is listed in an ascending order:

/*
 * file: garray_test.c
 * compile: gcc -o g_array garray_test.c `pkg-config --cflags --libs glib-2.0`
 */

#include <glib.h>

void display_array(GArray *array, int len, const char *prompt)
{
    int i = 0;

    printf("%s: \n", prompt);
    for (i = 0; i < len; i++) {
        printf("%d ", g_array_index(array, int, i));
    }
    printf("\n");
}

int my_int_sort_function (gconstpointer a, gconstpointer b)
{
    int * int_a = (int *) a;
    int * int_b = (int *) b;

    return (*int_a) - (*int_b);
}

int main(int argc, char *argv[])
{
    GArray *array = NULL;
    int i = 0;
    int cur_arr_len = 0;
    int len = 0;

    array = g_array_new(FALSE, TRUE, sizeof(int));
    printf("Current length of array is %d\n", array->len);

    len = 0;
    for (i = 10; i < 15; i++) {
        g_array_append_val(array, i);
        len++;
    }
    cur_arr_len += len;
    display_array(array, cur_arr_len, "Create array");

    int app_data[] = {30, 40, 50, 60};
    int app_len = sizeof(app_data) / sizeof(int);

    g_array_append_vals(array, app_data, app_len);
    cur_arr_len += app_len;
    display_array(array, cur_arr_len, "After append values 30 40 50 60");

    len = 0;
    for (i = 1; i < 4; i++) {
        g_array_prepend_val(array, i);
        len++;
    }
    cur_arr_len += len;
    display_array(array, cur_arr_len, "After prepend value 1 2 3");

    int prepend_data[] = {-10, -20, -30, -40};
    int prepend_len = sizeof(prepend_data) / sizeof(int);

    g_array_prepend_vals(array, prepend_data, prepend_len);
    cur_arr_len += prepend_len;
    display_array(array, cur_arr_len, "After prepend values -10 -20 -30 -40");

    int data = 100;
    g_array_insert_val(array, 5, data);
    cur_arr_len += 1;
    display_array(array, cur_arr_len, "After insert 100 at index 5");

    g_array_remove_index(array, 5);
    cur_arr_len -= 1;
    display_array(array, cur_arr_len, "After remove value at index 5");

    g_array_remove_index_fast(array, 10);
    cur_arr_len -= 1;
    display_array(array, cur_arr_len, "After remove value at index 10 fast");

    g_array_sort (array, my_int_sort_function);
    display_array(array, cur_arr_len, "Try to sort the array");

    g_array_free(array, TRUE);
}

Result of the code above:

Current length of array is 0
Create array: 
10 11 12 13 14 
After append values 30 40 50 60: 
10 11 12 13 14 30 40 50 60 
After prepend value 1 2 3: 
3 2 1 10 11 12 13 14 30 40 50 60 
After prepend values -10 -20 -30 -40: 
-10 -20 -30 -40 3 2 1 10 11 12 13 14 30 40 50 60 
After insert 100 at index 5: 
-10 -20 -30 -40 3 100 2 1 10 11 12 13 14 30 40 50 60 
After remove value at index 5: 
-10 -20 -30 -40 3 2 1 10 11 12 13 14 30 40 50 60 
After remove value at index 10 fast: 
-10 -20 -30 -40 3 2 1 10 11 12 60 14 30 40 50 
Try to sort the array: 
-40 -30 -20 -10 1 2 3 10 11 12 14 30 40 50 60 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号