2019-08-25 16:40:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2019-08-27 20:28:12 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <assert.h>
|
2019-08-25 16:40:59 +00:00
|
|
|
|
|
|
|
struct vec {
|
|
|
|
void* data;
|
|
|
|
size_t len;
|
|
|
|
size_t cap;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void vec_clear(struct vec* vec)
|
|
|
|
{
|
|
|
|
vec->len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int vec_init(struct vec* vec, size_t cap);
|
|
|
|
void vec_destroy(struct vec* vec);
|
|
|
|
|
|
|
|
int vec_reserve(struct vec* vec, size_t size);
|
|
|
|
|
|
|
|
void vec_bzero(struct vec* vec);
|
|
|
|
|
|
|
|
int vec_assign(struct vec* vec, const void* data, size_t size);
|
|
|
|
int vec_append(struct vec* vec, const void* data, size_t size);
|
|
|
|
void* vec_append_zero(struct vec* vec, size_t size);
|
|
|
|
|
2019-08-27 20:28:12 +00:00
|
|
|
static inline void vec_fast_append_8(struct vec* vec, uint8_t value)
|
|
|
|
{
|
|
|
|
assert(vec->len < vec->cap);
|
|
|
|
((uint8_t*)vec->data)[vec->len++] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void vec_fast_append_32(struct vec* vec, uint32_t value)
|
|
|
|
{
|
|
|
|
assert(vec->len + sizeof(value) <= vec->cap);
|
|
|
|
assert(vec->len % sizeof(value) == 0);
|
|
|
|
((uint32_t*)vec->data)[vec->len] = value;
|
|
|
|
vec->len += sizeof(value);
|
|
|
|
}
|
2019-08-25 16:40:59 +00:00
|
|
|
|
|
|
|
#define vec_for(elem, vec) \
|
|
|
|
for (elem = (vec)->data; \
|
|
|
|
((ptrdiff_t)elem - (ptrdiff_t)(vec)->data) < (ptrdiff_t)(vec)->len; \
|
|
|
|
++elem)
|
|
|
|
|
|
|
|
#define vec_for_tail(elem, vec) \
|
|
|
|
for (elem = (vec)->data, ++elem; \
|
|
|
|
((ptrdiff_t)elem - (ptrdiff_t)(vec)->data) < (ptrdiff_t)(vec)->len; \
|
|
|
|
++elem)
|
|
|
|
|
|
|
|
#define vec_for_ptr(elem, vec) \
|
|
|
|
__typeof__(elem)* ptr_; \
|
|
|
|
vec_for(ptr_, vec) \
|
|
|
|
if ((elem = *ptr_))
|