cvector · Section 3
cvec_sort
NAME
cvec_sort - sort a vector with the default comparison for its type
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cvector.h> cvec_sort(v);
DESCRIPTION
Sorts the vector in place with the default comparison function for the element type, which comes from the csort library; the type decides which comparator is used. A char* vector can hold NULL elements. The default string comparison puts NULL before every non-NULL string and treats NULL as equal only to another NULL, so all NULL elements end up at the front of the sorted vector and the other elements follow in strcmp() order. An element type with no default comparison procedure is a compile-time error rather than a run-time one. Whether a type has a default comparison procedure depends on the type alone, so cvec_sort carries a _Static_assert and such a call never builds. The diagnostic names the vector and points at cvec_sort_with. These element types sort: Every standard integer type in both signednesses, bool (false before true), every enumerated type, float, double, long double, char*, const char*, signed char* and unsigned char*. Each of the char pointer types sorts as a NUL-terminated string in strcmp() order, and uint8_t* is an unsigned char* that sorts the same way. Sort a vector of pointers to binary buffers with cvec_sort_with and a comparator that compares the pointers. A char pointer that is itself const or volatile, such as const char *const, also sorts, by the content of its string. An enumerated type is compatible with one of the standard integer types, and the macro uses the comparison procedure of that type. The types refused at compile time are the ones with no default comparison procedure: A struct, a union, a pointer to anything other than char, and a fixed-size char array such as char[64]. A vector of an enum type isn't one of them and sorts normally. Sort a refused element type with cvec_sort_with and an explicit comparison procedure.
PARAMETERS
v Vector to sort
NOTES
The sort is an in-place, stable mergesort that moves the elements inside the backing array directly, with no call per element access. It takes O(n log n) time and O(n) space and uses the default comparison for the element type. A NULL char* element is ordered without being dereferenced. The code doesn't compile when the element type has no default comparison procedure; the diagnostic names the vector and points at cvec_sort_with. cvec_find differs on purpose: It accepts every element type and falls back to byte-wise equality, because equality can come from the bytes of an element but an order can't. When the internal temporary buffer of the sort can't be allocated, the macro calls ccol__fatal_err(), as every other type-inferred macro in cvector.h that changes a vector does (cvec_push, cvec_reserve, cvec_append_array and the others). The vector is then left completely unsorted.
EXAMPLES
cvec_construct(vec, int); cvec_push(vec, 3); cvec_push(vec, 1); cvec_push(vec, 2); cvec_sort(vec); // vec is now [1, 2, 3] cvec_destroy(vec);
SEE ALSO
cvec_sort_with(3), cvec_find(3), csort_get_default_comparison_proc(3)