g_array_get_element_size constness & g_array_find function



   
Hi,

I'm Paul. Hope I might be able to contribute to this project. Well, since GList appears to have a bug (as far as I see it: https://bugzilla.gnome.org/show_bug.cgi?id=699137 ) I decided to switch to GArray in the meantime. Since I needed some kind of a find-function that scans through the data structure and checks whether a value exists within it, I decided to write my own function for GArray, since AFAIK, it doesn't exist. You might have had a reason for this, that I can't see from my (unfortunately) quite inexperienced point of view. Anyways, I don't know if this little function might come in handy in the next release...

/**
 * Searches for the given value in a GArray and returns the index of the first occurence.
 * @param array GArray to search
 * @param value item to search for
 * @param start position from which to start searching for
 * @return first index with the given value or -1 in case it wasn't found
 */
int g_array_find( GArray *array, gconstpointer value, guint start ) {
    if( value == NULL || array == NULL ) return -1;

    guint element_size = g_array_get_element_size( array );

    for( ; start < array->len; ++ start )
        if( !memcmp( array->data + element_size * start, value, element_size ) )
            return start;

    return -1;
}

I'd like to make the first parameter const, but for some reason g_array_get_element_size requires a non-const pointer. This seems very odd to me. Is there a reason for this?
Anyways, if You think the above code is useful, I'd like to have something similar in glib. Maybe a reverse search could be implemented as well. Personally, I don't need it and would just sort the array in a descending order before searching.

Regards,
Paul

PS: Never used doxygen, don't know whether the comment style is correct or not ...


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