34 lines
1,007 B
C
34 lines
1,007 B
C
#ifndef NS_VEC_H
|
|
#define NS_VEC_H
|
|
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
|
|
#define ns_vec_gen(type) \
|
|
struct Vec_##type##_T { \
|
|
type *arr; \
|
|
size_t len; \
|
|
size_t cap; \
|
|
}; \
|
|
typedef struct Vec_##type##_T Vec_##type;
|
|
|
|
#define ns_vec_init(type, vec, cap_) \
|
|
vec.len = 0; \
|
|
vec.cap = cap_; \
|
|
vec.arr = malloc(vec.cap * sizeof(type)); \
|
|
assert(vec.arr != NULL);
|
|
|
|
#define ns_vec_push(type, vec, obj) \
|
|
if (vec.len + 1 > vec.cap) { \
|
|
vec.cap += 8; \
|
|
vec.arr = realloc(vec.arr, vec.cap * sizeof(type)); \
|
|
assert(vec.arr != NULL); \
|
|
} \
|
|
vec.arr[vec.len] = obj; \
|
|
vec.len++;
|
|
|
|
#define ns_vec_destroy(vec) \
|
|
free(vec.arr); \
|
|
vec.arr = NULL;
|
|
|
|
#endif
|