#ifndef UCX_ARRAY_LIST_H
#define UCX_ARRAY_LIST_H
#include "list.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const unsigned cx_array_swap_sbo_size;
#define CX_ARRAY_DECLARE_SIZED(type, name, size_type) \
type * name; \
size_type name##_size; \
size_type name##_capacity
#define CX_ARRAY_DECLARE(type, name)
CX_ARRAY_DECLARE_SIZED(type, name,
size_t)
#define cx_array_initialize(array, capacity) \
array##_capacity = capacity; \
array##_size =
0; \
array = malloc(
sizeof(array[
0]) * capacity)
#define cx_array_initialize_a(allocator, array, capacity) \
array##_capacity = capacity; \
array##_size =
0; \
array = cxMalloc(allocator,
sizeof(array[
0]) * capacity)
struct cx_array_reallocator_s {
cx_attr_nodiscard
cx_attr_nonnull_arg(
4)
cx_attr_allocsize(
2,
3)
void *(*realloc)(
void *array,
size_t capacity,
size_t elem_size,
struct cx_array_reallocator_s *alloc
);
void *ptr1;
void *ptr2;
size_t int1;
size_t int2;
};
typedef struct cx_array_reallocator_s CxArrayReallocator;
extern CxArrayReallocator *cx_array_default_reallocator;
CxArrayReallocator cx_array_reallocator(
const struct cx_allocator_s *allocator,
const void *stackmem
);
cx_attr_nonnull_arg(
1,
2,
3)
int cx_array_reserve(
void **array,
void *size,
void *capacity,
unsigned width,
size_t elem_size,
size_t elem_count,
CxArrayReallocator *reallocator
);
cx_attr_nonnull_arg(
1,
2,
3,
6)
int cx_array_copy(
void **target,
void *size,
void *capacity,
unsigned width,
size_t index,
const void *src,
size_t elem_size,
size_t elem_count,
CxArrayReallocator *reallocator
);
#define cx_array_simple_copy_a(reallocator, array, index, src, count) \
cx_array_copy((
void**)&(array), &(array##_size), &(array##_capacity), \
sizeof(array##_size), index, src,
sizeof((array)[
0]), count, \
reallocator)
#define cx_array_simple_copy(array, index, src, count) \
cx_array_simple_copy_a(
NULL, array, index, src, count)
#define cx_array_simple_reserve_a(reallocator, array, count) \
cx_array_reserve((
void**)&(array), &(array##_size), &(array##_capacity), \
sizeof(array##_size),
sizeof((array)[
0]), count, \
reallocator)
#define cx_array_simple_reserve(array, count) \
cx_array_simple_reserve_a(
NULL, array, count)
#define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \
cx_array_copy((
void**)(target), size, capacity,
sizeof(*(size)), \
*(size), elem, elem_size,
1, reallocator)
#define cx_array_simple_add_a(reallocator, array, elem) \
cx_array_simple_copy_a(reallocator, array, array##_size, &(elem),
1)
#define cx_array_simple_add(array, elem) \
cx_array_simple_add_a(cx_array_default_reallocator, array, elem)
cx_attr_nonnull_arg(
1,
2,
3,
5)
int cx_array_insert_sorted(
void **target,
size_t *size,
size_t *capacity,
cx_compare_func cmp_func,
const void *src,
size_t elem_size,
size_t elem_count,
CxArrayReallocator *reallocator
);
#define cx_array_add_sorted(target, size, capacity, elem_size, elem, cmp_func, reallocator) \
cx_array_insert_sorted((
void**)(target), size, capacity, cmp_func, elem, elem_size,
1, reallocator)
#define cx_array_simple_add_sorted_a(reallocator, array, elem, cmp_func) \
cx_array_add_sorted(&array, &(array##_size), &(array##_capacity), \
sizeof((array)[
0]), &(elem), cmp_func, reallocator)
#define cx_array_simple_add_sorted(array, elem, cmp_func) \
cx_array_simple_add_sorted_a(
NULL, array, elem, cmp_func)
#define cx_array_simple_insert_sorted_a(reallocator, array, src, n, cmp_func) \
cx_array_insert_sorted((
void**)(&array), &(array##_size), &(array##_capacity), \
cmp_func, src,
sizeof((array)[
0]), n, reallocator)
#define cx_array_simple_insert_sorted(array, src, n, cmp_func) \
cx_array_simple_insert_sorted_a(
NULL, array, src, n, cmp_func)
cx_attr_nonnull
size_t cx_array_binary_search_inf(
const void *arr,
size_t size,
size_t elem_size,
const void *elem,
cx_compare_func cmp_func
);
cx_attr_nonnull
size_t cx_array_binary_search(
const void *arr,
size_t size,
size_t elem_size,
const void *elem,
cx_compare_func cmp_func
);
cx_attr_nonnull
size_t cx_array_binary_search_sup(
const void *arr,
size_t size,
size_t elem_size,
const void *elem,
cx_compare_func cmp_func
);
cx_attr_nonnull
void cx_array_swap(
void *arr,
size_t elem_size,
size_t idx1,
size_t idx2
);
cx_attr_nodiscard
cx_attr_malloc
cx_attr_dealloc(cxListFree,
1)
CxList *cxArrayListCreate(
const CxAllocator *allocator,
cx_compare_func comparator,
size_t elem_size,
size_t initial_capacity
);
#define cxArrayListCreateSimple(elem_size, initial_capacity) \
cxArrayListCreate(
NULL,
NULL, elem_size, initial_capacity)
#ifdef __cplusplus
}
#endif
#endif