12static void * __cstl_vector_at(
15 return (
void *)((uintptr_t)v->elem.base + i * v->elem.size);
25 return __cstl_vector_at(v, i);
34static void cstl_vector_set_capacity(
45 assert(sz >= v->count);
52 e = realloc(v->elem.base, (sz + 1) * v->elem.size);
62 cstl_vector_set_capacity(v, sz);
68 if (v->cap > v->count) {
69 cstl_vector_set_capacity(v, v->count);
75 void *
const priv = v->elem.xtor.priv;
89 xtor = v->elem.xtor.cons;
90 }
else if (v->count > sz) {
91 xtor = v->elem.xtor.dest;
96 }
else if (v->count < sz) {
98 xtor(__cstl_vector_at(v, v->count++), priv);
99 }
while (v->count < sz);
100 }
else if (v->count > sz) {
102 xtor(__cstl_vector_at(v, --v->count), priv);
103 }
while (v->count > sz);
122 v->elem.base, v->count, v->elem.size,
124 swap, __cstl_vector_at(v, v->cap),
129 const void *
const e,
134 v->count, v->elem.size,
140 const void *
const e,
145 v->count, v->elem.size,
154 v->count, v->elem.size,
155 swap, __cstl_vector_at(v, v->cap));
167#include "internal/check.h"
171static int int_cmp(
const void *
const a,
const void *
const b,
175 return *(
int *)a - *(
int *)b;
180 static size_t n = 71;
198 for (i = 0; i <
sizeof(algo) /
sizeof(*algo); i++) {
201 for (j = 0; j < n; j++) {
205 for (j = 1; j < n; j++) {
215START_TEST(invalid_access)
230 static size_t n = 63;
236 for (i = 0; i < n; i++) {
240 for (i = 0; i < n; i++) {
245 for (i = 0; i < n; i++) {
256 static size_t n = 27;
262 for (i = 0; i < n; i++) {
267 for (i = 1; i < n; i++) {
276static void int_cons(
void *
const i,
void *
const p)
283static void int_dest(
void *
const i,
void *
const p)
296 int_cons, int_dest, NULL);
302 ck_assert_int_eq(i, 10);
304 ck_assert_int_eq(*(
int *)__cstl_vector_at(&v, i), -1);
306 ck_assert_int_eq(i, 10);
312 ck_assert_int_eq(i, 3);
314 ck_assert_int_eq(*(
int *)__cstl_vector_at(&v, i), -1);
316 ck_assert_int_eq(i, 10);
322 ck_assert_int_eq(i, 6);
324 ck_assert_int_eq(*(
int *)__cstl_vector_at(&v, i), -1);
326 ck_assert_int_eq(i, 10);
336Suite * vector_suite(
void)
338 Suite *
const s = suite_create(
"vector");
342 tc = tcase_create(
"vector");
343 tcase_add_test(tc, invalid_access);
344 tcase_add_test(tc, sort);
345 tcase_add_test(tc, search);
346 tcase_add_test(tc, reverse);
347 tcase_add_test(tc, complex);
348 suite_add_tcase(s, tc);
cstl_sort_algorithm_t
Enumeration indicating the desired sort algorithm.
@ CSTL_SORT_ALGORITHM_QUICK_M
Median-of-three quicksort.
@ CSTL_SORT_ALGORITHM_QUICK
Quicksort.
@ CSTL_SORT_ALGORITHM_HEAP
Heapsort.
@ CSTL_SORT_ALGORITHM_QUICK_R
Randomized quicksort.
static void cstl_swap(void *const x, void *const y, void *const t, const size_t sz)
Swap values at two memory locations via use of a third.
void cstl_xtor_func_t(void *obj, void *priv)
Type for functions called to construct, clear, or destroy an object.
void cstl_swap_func_t(void *a, void *b, void *t, size_t len)
Type of function called to swap two objects.
int cstl_compare_func_t(const void *obj1, const void *obj2, void *priv)
Function type for comparing (in)equality of two objects.
ssize_t cstl_raw_array_find(const void *arr, size_t count, size_t size, const void *ex, cstl_compare_func_t *cmp, void *priv)
Perform a linear search of the array.
void cstl_raw_array_sort(void *arr, size_t count, size_t size, cstl_compare_func_t *cmp, void *priv, cstl_swap_func_t *swap, void *tmp, cstl_sort_algorithm_t algo)
Sort the array using the specified algorithm.
ssize_t cstl_raw_array_search(const void *arr, size_t count, size_t size, const void *ex, cstl_compare_func_t *cmp, void *priv)
Perform a binary search of the array.
void cstl_raw_array_reverse(void *arr, size_t count, size_t size, cstl_swap_func_t *swap, void *tmp)
Reverse the contents of an array.
void cstl_vector_resize(struct cstl_vector *const v, const size_t sz)
Change the number of valid elements in the vector.
void cstl_vector_reserve(struct cstl_vector *const v, const size_t sz)
Request to increase the capacity of the vector.
void * cstl_vector_at(struct cstl_vector *const v, const size_t i)
Get a pointer to an element in the vector.
#define DECLARE_CSTL_VECTOR(NAME, TYPE)
(Statically) declare and initialize a vector
static size_t cstl_vector_size(const struct cstl_vector *const v)
Get the number of elements in the vector.
static void cstl_vector_init_complex(struct cstl_vector *const v, const size_t sz, cstl_xtor_func_t *const cons, cstl_xtor_func_t *const dest, void *const priv)
Initialize a vector object.
static size_t cstl_vector_capacity(const struct cstl_vector *const v)
Get the number of elements the vector can hold.
void cstl_vector_swap(struct cstl_vector *const a, struct cstl_vector *const b)
Swap the vector objects at the two given locations.
void cstl_vector_shrink_to_fit(struct cstl_vector *const v)
Request to decrease the capacity of the vector.
ssize_t cstl_vector_search(const struct cstl_vector *const v, const void *const e, cstl_compare_func_t *const cmp, void *const priv)
Perform a binary search of the vector.
const void * cstl_vector_at_const(const struct cstl_vector *const v, const size_t i)
Get a const pointer to an element from a const vector.
void cstl_vector_clear(struct cstl_vector *const v)
Return a vector to its initialized state.
static void cstl_vector_reverse(struct cstl_vector *const v)
Reverse the current order of the elements.
ssize_t cstl_vector_find(const struct cstl_vector *const v, const void *const e, cstl_compare_func_t *const cmp, void *const priv)
Perform a linear search of the vector.
void __cstl_vector_reverse(struct cstl_vector *const v, cstl_swap_func_t *const swap)
Reverse the current order of the elements.
void __cstl_vector_sort(struct cstl_vector *const v, cstl_compare_func_t *const cmp, void *const priv, cstl_swap_func_t *const swap, const cstl_sort_algorithm_t algo)
Sort the elements in the vector.