cvector · Section 3
cvec_find
NAME
cvec_find - find the first element with a given value (type-inferred)
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cvector.h> size_t idx = cvec_find(v, elem);
DESCRIPTION
Type-inferred wrapper for cvector_find() that accepts any value, an rvalue or a literal included. The needle is converted to the vector's declared element type and the elements are compared with that type's default comparison procedure, so equality means equality of the type, not of its object representation. chashmap and cbstmap use the same idea of equality for a key of the same type: -0.0 and 0.0 are one value, for example, and a long double compares by value rather than through the padding bytes its representation carries on some targets. A char*, signed char* or unsigned char* element (uint8_t* is an unsigned char*) is a NUL-terminated string to this library, and the comparison reads it with strcmp(). Don't search a vector of pointers to binary buffers this way, because a buffer without a NUL byte is read past its end. Search such a vector with cvector_find(3) and a comparator that compares the pointers, or declare the element type as void*, whose byte-wise equality is the identity of the pointer. For an element type that is an array, such as char[64], elem is an array or a string literal. The macro zero-fills its copy to the element size and copies the bytes of elem into it, up to the smaller of the two sizes, exactly the way cvec_push(3) stores such a value. Passing a pointer as elem for an array element type is a compile error, since the macro can't see the size of what it points at. A char* element can be NULL, and so can the needle. The default string comparison puts NULL before every non-NULL string and treats NULL as equal only to another NULL, so searching for NULL in a sparse char* vector gives the index of the first empty slot. Nothing is dereferenced. A struct, a union, a pointer to anything other than char and a fixed-size char array have no default comparison procedure. For those the comparison falls back to byte-wise equality over the whole representation of the element, the padding bytes of a struct included. With GCC 11 or later the macro zeroes the padding bytes of its own copy of elem, and cvec_push(3) does the same for each element it stores, so a struct that went in through cvec_push(3) is found with any value whose members are equal, whatever that value's padding holds. An element that went in through cvector_push_back(3), cvector_append_array(3) or an assignment through cvec_at(3) keeps whatever padding bytes the caller wrote. With a compiler that has no __builtin_clear_padding, such as Clang, the padding bytes of the macro's copies are unspecified; search for a padded struct with cvector_find(3) and a comparator that reads the members. cvec_find compiles for every element type, but cvec_sort is different on purpose: An order can't come from the bytes of an element the way equality can, so cvec_sort has no fallback and rejects such an element type at compile time. Call cvector_find() directly when you need a custom comparator, or when you want byte-wise equality for a type that does have a default comparison procedure.
PARAMETERS
v Vector to search elem Element value to search for. It can be an rvalue or an lvalue.
RETURN VALUE
Zero-based index of the first element that matches. ccol_invalid_size when there is no match.
NOTES
O(n). The comparison is the default comparison procedure of the element type, and a NULL char* element or a NULL needle for a char* vector is ordered without dereferencing either of them. A type with no default comparison procedure falls back to byte-wise equality, so a struct with padding bytes compares correctly only when both copies carry zero padding; see DESCRIPTION. The macro compiles for every element type, which cvec_sort doesn't.
EXAMPLES
cvec_construct(vec, int); cvec_push(vec, 10); cvec_push(vec, 20); cvec_push(vec, 30); size_t idx = cvec_find(vec, 20); // idx == 1 size_t nf = cvec_find(vec, 99); // nf == ccol_invalid_size cvec_destroy(vec);