2019-09-07 16:51:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Andri Yngvason
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
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)
|
|
|
|
{
|
2019-10-20 22:13:51 +00:00
|
|
|
assert(vec->len < vec->cap);
|
|
|
|
((uint8_t*)vec->data)[vec->len++] = value;
|
2019-08-27 20:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void vec_fast_append_32(struct vec* vec, uint32_t value)
|
|
|
|
{
|
2019-10-20 22:13:51 +00:00
|
|
|
assert(vec->len + sizeof(value) <= vec->cap);
|
|
|
|
assert(vec->len % sizeof(value) == 0);
|
|
|
|
uint32_t* p = (uint32_t*)((uint8_t*)vec->data + vec->len);
|
|
|
|
*p = value;
|
|
|
|
vec->len += sizeof(value);
|
2019-08-27 20:28:12 +00:00
|
|
|
}
|
2019-08-25 16:40:59 +00:00
|
|
|
|
2019-10-20 22:13:51 +00:00
|
|
|
#define vec_for(elem, vec) \
|
|
|
|
for (elem = (vec)->data; \
|
|
|
|
((ptrdiff_t)elem - (ptrdiff_t)(vec)->data) < (ptrdiff_t)(vec)->len;\
|
2019-08-25 16:40:59 +00:00
|
|
|
++elem)
|
|
|
|
|
2019-10-20 22:13:51 +00:00
|
|
|
#define vec_for_tail(elem, vec) \
|
|
|
|
for (elem = (vec)->data, ++elem; \
|
|
|
|
((ptrdiff_t)elem - (ptrdiff_t)(vec)->data) < (ptrdiff_t)(vec)->len;\
|
2019-08-25 16:40:59 +00:00
|
|
|
++elem)
|
|
|
|
|
2019-10-20 22:13:51 +00:00
|
|
|
#define vec_for_ptr(elem, vec) \
|
|
|
|
__typeof__(elem)* ptr_; \
|
|
|
|
vec_for(ptr_, vec) \
|
|
|
|
if ((elem = *ptr_))
|