gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
mp_vector.h
Go to the documentation of this file.
1#ifndef __MP_VECTOR
2#define __MP_VECTOR
3
4typedef struct MP_Vector {
5 uint32_t len;
6 uint32_t cap;
7 uint8_t ptr[];
9
10ANN static inline void mp_vector_resize(const MemPool mp, MP_Vector **ap, const uint32_t size, const uint32_t cap) {
11 MP_Vector *a = *ap;
12 *ap = (MP_Vector *)mp_realloc(mp, a, sizeof(MP_Vector) + (m_uint)(a->cap * size), sizeof(MP_Vector) + (m_uint)(cap * size));
13 (*ap)->cap = cap;
14}
15
16ANN MP_Vector *new_mp_vector(const MemPool mp, const uint32_t size, const uint32_t len);
17
18ANN static inline void free_mp_vector(const MemPool mp, const uint32_t size, MP_Vector *a) {
19 mp_free2(mp, sizeof(MP_Vector) + (m_uint)(a->cap * size), a);
20}
21
22static inline uint32_t mp_vector_len(MP_Vector *a) {
23 return a ? a->len : 0;
24}
25
26#define new_mp_vector(mp, type, data) \
27 new_mp_vector(mp, sizeof(type), data)
28
29#define free_mp_vector(mp, type, data) \
30 free_mp_vector(mp, sizeof(type), data)
31
32#define mp_vector_add(mp, a, type, data) { \
33 if (++((*a))->len >= ((*a))->cap) \
34 mp_vector_resize(mp, (a), sizeof(type), (*(a))->cap * 2); \
35 *(type*)((*(a))->ptr + ((*a)->len - 1) * sizeof(type)) = data; }\
36
37#define mp_vector_at(a, type, index) \
38 ((type*)((a)->ptr + (index) * sizeof(type)))
39
40#define mp_vector_set(a, type, index, data) \
41 *(type*)((a)->ptr + (index) * sizeof(type)) = (data)
42
43ANN static inline void mp_vector_rem(MP_Vector *const a, const uint32_t size, const uint32_t idx) {
44 if (idx >= a->len) return;
45 if (idx < a->len - 1)
46 memmove(a->ptr + idx, a->ptr + idx + OFFSET + 1,
47 (a->len - idx - 1) * size);
48}
49
50ANN static inline m_bit* mp_vector_pop(MP_Vector *const a, const uint32_t size) {
51 m_bit *data = ((a)->ptr + (a->len--) * size);
52 return data;
53}
54
55#define mp_vector_pop(a, type) \
56 ((type*)mp_vector_pop(a, sizeof(type)))
57
58#define mp_vector_rem(a, type, idx) \
59 mp_vector_rem(a, sizeof(type), idx)
60
61#define mp_vector_rem2(a, type, data) \
62 for(uint32_t i = 0; i < a->len; i++) { \
63 if(data == mp_vector_at(a, type, i)) { \
64 mp_vector_rem(a, type, i); \
65 break; \
66 } \
67 }
68
69#define mp_vector_back(a, type) \
70 ((type*)((a)->ptr + (a->len-1) * sizeof(type)))
71
72#endif
#define OFFSET
Definition container.h:9
#define ANN
Definition defs.h:19
uintptr_t m_uint
Definition gwcommon.h:11
unsigned char m_bit
Definition gwcommon.h:12
#define mp_vector_rem(a, type, idx)
Definition mp_vector.h:58
static uint32_t mp_vector_len(MP_Vector *a)
Definition mp_vector.h:22
static ANN void mp_vector_resize(const MemPool mp, MP_Vector **ap, const uint32_t size, const uint32_t cap)
Definition mp_vector.h:10
#define new_mp_vector(mp, type, data)
Definition mp_vector.h:26
#define mp_vector_pop(a, type)
Definition mp_vector.h:55
struct MP_Vector MP_Vector
#define free_mp_vector(mp, type, data)
Definition mp_vector.h:29
void * mp_realloc(MemPool mp, void *ptr, const m_uint curr, const m_uint next)
Definition mpool.c:148
#define mp_free2(p, sz, a)
Definition mpool.h:28
uint32_t cap
Definition mp_vector.h:6
uint8_t ptr[]
Definition mp_vector.h:7
uint32_t len
Definition mp_vector.h:5