ctpv/vector.h

78 lines
3.4 KiB
C
Raw Normal View History

2022-06-05 20:56:48 +02:00
#ifndef VECTOR_H
#define VECTOR_H
#include <stdlib.h>
#define VECTOR_TYPE(name, type) \
typedef struct { \
size_t len, cap, size; \
type *buf; \
} Vector##name
#define VECTOR_SIGN(name, type, func, ret, ...) \
ret vector##name##_##func(__VA_ARGS__)
2022-06-05 20:56:48 +02:00
#define VECTOR_SIGN_V(name, type, func, ret, ...) \
VECTOR_SIGN(name, type, func, ret, \
2022-06-06 21:17:38 +02:00
Vector##name *vec __VA_OPT__(, ) __VA_ARGS__)
2022-06-05 20:56:48 +02:00
#define VECTOR_SIGN_NEW(name, type) VECTOR_SIGN(name, type, new, Vector##name *, size_t cap)
#define VECTOR_SIGN_FREE(name, type) VECTOR_SIGN_V(name, type, free, void)
2022-06-06 21:18:18 +02:00
#define VECTOR_SIGN_APPEND_ARR(name, type) VECTOR_SIGN_V(name, type, append_arr, size_t, type *arr, size_t len)
#define VECTOR_SIGN_APPEND(name, type) VECTOR_SIGN_V(name, type, append, size_t, type val)
#define VECTOR_SIGN_GET(name, type) VECTOR_SIGN_V(name, type, get, type, size_t i)
2022-06-06 21:17:38 +02:00
#define VECTOR_SIGN_RESIZE(name, type) VECTOR_SIGN_V(name, type, resize, void, size_t len)
2022-06-05 20:56:48 +02:00
#define VECTOR_GEN_SOURCE_(name, type, spec) \
inline spec VECTOR_SIGN_NEW(name, type) \
2022-06-05 20:56:48 +02:00
{ \
return (Vector##name *)vector_new(sizeof(type), cap); \
} \
inline spec VECTOR_SIGN_FREE(name, type) \
2022-06-05 20:56:48 +02:00
{ \
2022-06-06 21:17:38 +02:00
vector_free((Vector *)vec); \
2022-06-05 20:56:48 +02:00
} \
inline spec VECTOR_SIGN_APPEND_ARR(name, type) \
2022-06-05 20:56:48 +02:00
{ \
2022-06-06 21:18:18 +02:00
return vector_append_arr((Vector *)vec, arr, len); \
2022-06-05 20:56:48 +02:00
} \
inline spec VECTOR_SIGN_APPEND(name, type) \
2022-06-05 20:56:48 +02:00
{ \
2022-06-06 21:18:18 +02:00
return vector_append((Vector *)vec, &val); \
2022-06-05 20:56:48 +02:00
} \
inline spec VECTOR_SIGN_GET(name, type) \
2022-06-05 20:56:48 +02:00
{ \
2022-06-06 21:18:18 +02:00
return *(type *)vector_get((Vector *)vec, i); \
2022-06-06 21:17:38 +02:00
} \
inline spec VECTOR_SIGN_RESIZE(name, type) \
{ \
vector_resize((Vector *)vec, len); \
2022-06-05 20:56:48 +02:00
}
#define VECTOR_GEN_SOURCE(name, type) VECTOR_GEN_SOURCE_(name, type, )
#define VECTOR_GEN_SOURCE_STATIC(name, type) \
VECTOR_TYPE(name, type); \
VECTOR_GEN_SOURCE_(name, type, static __attribute__((unused)))
#define VECTOR_GEN_HEADER(name, type) \
VECTOR_TYPE(name, type); \
VECTOR_SIGN_NEW(name, type); \
VECTOR_SIGN_FREE(name, type); \
2022-06-05 20:56:48 +02:00
VECTOR_SIGN_APPEND_ARR(name, type); \
VECTOR_SIGN_APPEND(name, type); \
2022-06-06 21:17:38 +02:00
VECTOR_SIGN_GET(name, type); \
VECTOR_SIGN_RESIZE(name, type);
2022-06-05 20:56:48 +02:00
VECTOR_TYPE(, void);
Vector *vector_new(size_t size, size_t cap);
void vector_free(Vector *vec);
2022-06-06 21:18:18 +02:00
size_t vector_append_arr(Vector *vec, void *arr, size_t len);
size_t vector_append(Vector *vec, void *arr);
2022-06-05 20:56:48 +02:00
void *vector_get(Vector *vec, size_t i);
2022-06-06 21:17:38 +02:00
void vector_resize(Vector *vec, size_t len);
2022-06-05 20:56:48 +02:00
VECTOR_GEN_HEADER(Char, char)
#endif