csort · Section 3
csort_get_default_comparison_proc
NAME
csort_get_default_comparison_proc - get default comparison for a type
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/csort.h> ccol_comparison_proc csort_get_default_comparison_proc(dummy_var);
DESCRIPTION
This macro gives the default comparison function for a type. It works through C11 _Generic, so the type of the dummy variable decides which comparison function you get. The supported types are all the standard C integral types (char, signed char, short, int, long, long long and their unsigned variants), bool, the floating-point types float, double and long double, and the string types char* and const char* with any signed or unsigned character type. A pointer that is itself const or volatile, such as a static const char *const, counts as a string type too. The string comparator puts a stored NULL before every string that is not NULL, and a NULL is equal only to another NULL. The comparator for bool puts false before true. An enumerated type works as well. C makes every enumerated type compatible with one of the standard integer types, and the macro gives the comparison function of that compatible type. For a type that it doesn't support the macro gives NULL, and that NULL is part of its contract, because a caller with its own fallback depends on it; cvec_find, for example, then compares the element byte-wise. A caller that has no fallback must refuse the type at compile time rather than at run time, which is what cvec_sort does. A fixed-size char array (char[N]) is not a supported type, and it is a different thing from a char* pointer variable, so the macro gives NULL for it like for any other unsupported type. The bytes stored inline in such an array are not a char* value, while csort__default_string_comparison_proc dereferences a pointer, so its contract doesn't hold for an array. To sort a collection of fixed-size string buffers, write your own comparator and pass it directly to cvec_sort_with() or to csort_sort().
PARAMETERS
dummy_var A variable of the type that you want to compare. The macro does
not evaluate it.
RETURN VALUE
Gives a pointer to the right comparison function, or NULL when the macro does not support the type.
NOTES
The macro uses the variable only for its type and never evaluates it, so dummy_var can be any expression of the type you want to compare. An rvalue such as a cast or a literal is fine, and the expression does not need an address. For a real char* or const char* pointer variable the macro gives csort__default_string_comparison_proc, and that includes a pointer variable that is itself const or volatile. For a fixed-size char array (char[N]) it gives NULL, not csort__default_string_comparison_proc. For an enumerated type it gives the comparison function of the standard integer type that is compatible with it. An integral type compares three-way, as (a > b) - (a < b). For a floating-point type (float, double or long double) a NaN operand is greater than every value that is not NaN and equal only to another NaN, so the result is always a real total order. Every default comparator reads its two operands byte-wise, so the address of an operand needs neither the alignment nor the effective type of the element. That matters for a custom getter over a packed buffer.
EXAMPLES
// Get the comparator for a string char* dummy_str; ccol_comparison_proc cmp = csort_get_default_comparison_proc(dummy_str); // cmp now points to csort__default_string_comparison_proc // Get the comparator for a double double dummy_double; cmp = csort_get_default_comparison_proc(dummy_double);