cusel/compiler/vector.h
2026-11-26 16:23:34 +02:00

194 lines
5.1 KiB
C

// This header file defines macros for working with bounds-checked arrays.
// The type definitions are in vector_internal.h.
//
// Table of contents
// 1. Vectors (growable owning array type)
// 2. Spans (borrowing subslice-of-array type)
// 3. Creating a span from a vector
// 4. Operations that work with either
// For size_t, NULL
#include <stddef.h>
// For struct panic_location, PANIC_LOCATION, panic_at
#include "error_internal.h"
#include "vector_internal.h"
// ------------------------------------------------------------
// 1. Vectors (growable owning array type)
// ------------------------------------------------------------
void vec_resize(
struct type_erased_vector *vec,
size_t count,
size_t element_size,
struct panic_location loc
);
#define VEC_RESIZE(vec, count) \
vec_resize( \
&(vec).data, (count), sizeof *(vec).dummy_pointer, PANIC_LOCATION \
)
void vec_ensure_free_space(
struct type_erased_vector *vec,
size_t element_size,
struct panic_location loc
);
#define VEC_ENSURE_FREE_SPACE(vec) \
vec_ensure_free_space( \
&(vec).data, sizeof *(vec).dummy_pointer, PANIC_LOCATION \
)
void vec_free(struct type_erased_vector *vec);
#define VEC_FREE(vec) vec_free(&(vec).data)
void vec_push(
struct type_erased_vector *vec,
void *element,
size_t element_size,
struct panic_location loc
);
#define VEC_PUSH(vec, element) \
vec_push( \
&(vec).data, &(element), sizeof(1 ? *(vec).dummy_pointer : (element)), \
PANIC_LOCATION \
)
// ------------------------------------------------------------
// 2. Spans (borrowing subslice-of-array type)
// ------------------------------------------------------------
#define MAKE_SPAN(type, array, length) \
((SPAN(type)) {.data = {.ptr = (array), .size = (length) * sizeof(type)}})
static inline struct type_erased_span span_slice(
struct type_erased_span span,
size_t start,
size_t end,
size_t element_size,
struct panic_location loc
) {
size_t length = span.size / element_size;
if (end < start) {
panic_at(
loc, "start index %zu is larger than end index %zu when slicing",
start, end
);
} else if (length < end) {
panic_at(
loc, "out-of-bounds index range %zu-%zu (length %zu)", start, end,
length
);
}
return (struct type_erased_span) {
.ptr = ((char *)span.ptr) + start * element_size,
.size = (end - start) * element_size
};
}
// This is not generic over VEC/SPAN because for a VEC, we'd need to
// manually pass in the type, while for a SPAN it's not necessary. As
// this'll mainly be used with SPANs[citation needed], prioritize the
// ergonomics of that over slicing VECs'. If you need to slice a VEC,
// it's `SLICE(VEC_AS_SPAN(type, vec), start, end)`.
#define SLICE(span, start, end) \
((typeof(span)) { \
.data = span_slice( \
(span).data, (start), (end), sizeof *(span).dummy_pointer, \
PANIC_LOCATION \
) \
})
// ------------------------------------------------------------
// 3. Creating a span from a vector
// ------------------------------------------------------------
static inline struct type_erased_span
vec_as_span(struct type_erased_vector vec) {
return (struct type_erased_span) {
.ptr = vec.ptr,
.size = vec.size,
};
}
#define VEC_AS_SPAN(type, vec) \
_Generic( \
(vec).dummy_pointer, \
type *: (SPAN(type)) {.data = vec_as_span((vec).data)} \
)
static inline struct type_erased_span
vec_free_space(struct type_erased_vector vec) {
if (!vec.ptr) {
// Doing any arithmetic on a NULL pointer is UB, even adding 0
// to it, so we need to handle this separately.
return (struct type_erased_span) {.ptr = NULL, .size = 0};
}
// We can't do pointer arithmetic portably on void*, but char* has
// the correct semantics aiui universally.
char *ptr = vec.ptr;
return (struct type_erased_span) {
.ptr = ptr + vec.size,
.size = vec.capacity - vec.size,
};
}
#define VEC_FREE_SPACE(type, vec) \
_Generic( \
(vec).dummy_pointer, \
type *: (SPAN(type)) {.data = vec_free_space((vec).data)} \
)
// ------------------------------------------------------------
// 4. Operations that work with either
// ------------------------------------------------------------
#define LENGTH(span_or_vec) \
((span_or_vec).data.size / sizeof *(span_or_vec).dummy_pointer)
static inline void *generic_at(
void *ptr,
size_t size,
size_t index,
size_t element_size,
struct panic_location loc
) {
size_t length = size / element_size;
if (index < length) {
size_t offset = index * element_size;
return ((char *)ptr) + offset;
} else {
panic_at(loc, "out-of-bounds index %zu (length %zu)", index, length);
}
}
static inline void *vec_at(
struct type_erased_vector vec,
size_t index,
size_t element_size,
struct panic_location loc
) {
return generic_at(vec.ptr, vec.size, index, element_size, loc);
}
static inline void *span_at(
struct type_erased_span span,
size_t index,
size_t element_size,
struct panic_location loc
) {
return generic_at(span.ptr, span.size, index, element_size, loc);
}
#define AT(span_or_vec, index) \
(*((typeof((span_or_vec).dummy_pointer))_Generic((span_or_vec).data, struct type_erased_vector: vec_at, struct type_erased_span: span_at)( \
(span_or_vec).data, (index), sizeof *(span_or_vec).dummy_pointer, \
PANIC_LOCATION \
)))