gqsort: Add g_sort_array() and deprecate g_qsort_with_data()

The latter only accepts a `gint` as the number of elements in the array,
which means that its use in `GArray` (and related array implementations)
truncates at least half the potential array size.

So, introduce a replacement for it which uses `size_t` for the number of
elements. This is inline with what `qsort()` (or `qsort_r()`) actually
does. Unfortunately we can’t directly use `qsort_r()` because it’s not
guaranteed to be a stable sort.

This fixes some `-Wsign-conversion` warnings (when building GLib with
that enabled).

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3405
This commit is contained in:
Philip Withnall
2024-06-28 15:22:33 +01:00
parent 616749c1e2
commit b32e1b63ee
5 changed files with 91 additions and 33 deletions

View File

@@ -364,9 +364,9 @@ g_value_array_sort_with_data (GValueArray *value_array,
g_return_val_if_fail (compare_func != NULL, NULL);
if (value_array->n_values)
g_qsort_with_data (value_array->values,
value_array->n_values,
sizeof (value_array->values[0]),
compare_func, user_data);
g_sort_array (value_array->values,
value_array->n_values,
sizeof (value_array->values[0]),
compare_func, user_data);
return value_array;
}