gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
mp_vector.h File Reference

Go to the source code of this file.

Data Structures

struct  MP_Vector
 

Macros

#define new_mp_vector(mp, type, data)
 
#define free_mp_vector(mp, type, data)
 
#define mp_vector_add(mp, a, type, data)
 
#define mp_vector_at(a, type, index)
 
#define mp_vector_set(a, type, index, data)
 
#define mp_vector_pop(a, type)
 
#define mp_vector_rem(a, type, idx)
 
#define mp_vector_rem2(a, type, data)
 
#define mp_vector_back(a, type)
 
#define MK_VECTOR_TYPE(Type, type, ...)
 

Typedefs

typedef struct MP_Vector MP_Vector
 

Functions

static ANN void mp_vector_resize (const MemPool mp, MP_Vector **ap, const uint32_t size, const uint32_t cap)
 
ANN MP_Vectornew_mp_vector (const MemPool mp, const uint32_t size, const uint32_t len)
 
static ANN void free_mp_vector (const MemPool mp, const uint32_t size, MP_Vector *a)
 
static uint32_t mp_vector_len (MP_Vector *a)
 
static ANN void mp_vector_rem (MP_Vector *const a, const uint32_t size, const uint32_t idx)
 
static ANN m_bitmp_vector_pop (MP_Vector *const a, const uint32_t size)
 

Macro Definition Documentation

◆ free_mp_vector

#define free_mp_vector ( mp,
type,
data )
Value:
free_mp_vector(mp, sizeof(type), data)
#define free_mp_vector(mp, type, data)
Definition mp_vector.h:28

Definition at line 28 of file mp_vector.h.

28#define free_mp_vector(mp, type, data) \
29 free_mp_vector(mp, sizeof(type), data)

◆ MK_VECTOR_TYPE

#define MK_VECTOR_TYPE ( Type,
type,
... )

Definition at line 73 of file mp_vector.h.

73#define MK_VECTOR_TYPE(Type, type, ...) \
74typedef struct Type##List { \
75 __VA_ARGS__ ; \
76 uint32_t len; \
77 uint32_t cap; \
78 Type ptr[]; \
79} Type##List; \
80/*typedef MP_Vector Type##List;*/ \
81ANN static inline Type##List *new_##type##list(const MemPool mp, const uint32_t len) { \
82 return (Type##List*)new_mp_vector(mp, Type, len); \
83} \
84ANN static inline void free_##type##list(const MemPool mp, Type##List *v) { \
85 mp_free2(mp, sizeof(Type##List) + (m_uint)(v->cap * sizeof(Type)), v); \
86} \
87ANN static inline void type##list_resize(const MemPool mp, Type##List **ap, \
88 const uint32_t cap) { \
89 return mp_vector_resize(mp, (MP_Vector**)ap, sizeof(Type), cap); \
90} \
91ANN static inline Type type##list_at(const Type##List *v, const uint32_t index) { \
92 return v->ptr[index]; \
93} \
94ANN static inline Type* type##list_ptr_at(Type##List *v, const uint32_t index) { \
95 return v->ptr + index; \
96} \
97ANN static inline void type##list_set(Type##List *v, const uint32_t index, \
98 Type data) { \
99 v->ptr[index] = data; \
100} \
101ANN static inline void type##list_add(const MemPool mp, Type##List **v, \
102 Type data) { \
103 mp_vector_add(mp, (MP_Vector**)v, Type, data); \
104} \
105static inline uint32_t type##list_len(const Type##List *a) { \
106 return a ? a->len : 0; \
107} \
108ANN static inline Type type##list_back(const Type##List *a) { \
109 return a->ptr[a->len-1]; \
110} \
111ANN static inline void type##list_rem(Type##List *const a, const uint32_t idx) { \
112 if (idx >= a->len) return; \
113 const size_t len = a->len - idx - 1; \
114 memmove(a->ptr + idx, a->ptr + idx + OFFSET + 1, len * sizeof(Type));\
115 a->len--; \
116}

◆ mp_vector_add

#define mp_vector_add ( mp,
a,
type,
data )
Value:
{ \
if (++((*a))->len >= ((*a))->cap) \
mp_vector_resize(mp, (a), sizeof(type), (*(a))->cap * 2); \
*(type*)((*(a))->ptr + ((*a)->len - 1) * sizeof(type)) = (data); }\

Definition at line 31 of file mp_vector.h.

31#define mp_vector_add(mp, a, type, data) { \
32 if (++((*a))->len >= ((*a))->cap) \
33 mp_vector_resize(mp, (a), sizeof(type), (*(a))->cap * 2); \
34 *(type*)((*(a))->ptr + ((*a)->len - 1) * sizeof(type)) = (data); }\
35

◆ mp_vector_at

#define mp_vector_at ( a,
type,
index )
Value:
((type*)((a)->ptr + (index) * sizeof(type)))

Definition at line 36 of file mp_vector.h.

36#define mp_vector_at(a, type, index) \
37 ((type*)((a)->ptr + (index) * sizeof(type)))

◆ mp_vector_back

#define mp_vector_back ( a,
type )
Value:
((type*)((a)->ptr + (a->len-1) * sizeof(type)))

Definition at line 70 of file mp_vector.h.

70#define mp_vector_back(a, type) \
71 ((type*)((a)->ptr + (a->len-1) * sizeof(type)))

◆ mp_vector_pop

#define mp_vector_pop ( a,
type )
Value:
((type*)mp_vector_pop(a, sizeof(type)))
#define mp_vector_pop(a, type)
Definition mp_vector.h:56

Definition at line 56 of file mp_vector.h.

56#define mp_vector_pop(a, type) \
57 ((type*)mp_vector_pop(a, sizeof(type)))

◆ mp_vector_rem

#define mp_vector_rem ( a,
type,
idx )
Value:
mp_vector_rem(a, sizeof(type), idx)
#define mp_vector_rem(a, type, idx)
Definition mp_vector.h:59

Definition at line 59 of file mp_vector.h.

59#define mp_vector_rem(a, type, idx) \
60 mp_vector_rem(a, sizeof(type), idx)

◆ mp_vector_rem2

#define mp_vector_rem2 ( a,
type,
data )
Value:
for(uint32_t i = 0; i < a->len; i++) { \
if(data == mp_vector_at(a, type, i)) { \
mp_vector_rem(a, type, i); \
break; \
} \
}
#define mp_vector_at(a, type, index)
Definition mp_vector.h:36

Definition at line 62 of file mp_vector.h.

62#define mp_vector_rem2(a, type, data) \
63 for(uint32_t i = 0; i < a->len; i++) { \
64 if(data == mp_vector_at(a, type, i)) { \
65 mp_vector_rem(a, type, i); \
66 break; \
67 } \
68 }

◆ mp_vector_set

#define mp_vector_set ( a,
type,
index,
data )
Value:
*(type*)((a)->ptr + (index) * sizeof(type)) = (data)

Definition at line 39 of file mp_vector.h.

39#define mp_vector_set(a, type, index, data) \
40 *(type*)((a)->ptr + (index) * sizeof(type)) = (data)

◆ new_mp_vector

#define new_mp_vector ( mp,
type,
data )
Value:
new_mp_vector(mp, sizeof(type), data)
#define new_mp_vector(mp, type, data)
Definition mp_vector.h:25

Definition at line 25 of file mp_vector.h.

25#define new_mp_vector(mp, type, data) \
26 new_mp_vector(mp, sizeof(type), data)

Typedef Documentation

◆ MP_Vector

typedef struct MP_Vector MP_Vector

Function Documentation

◆ free_mp_vector()

static ANN void free_mp_vector ( const MemPool mp,
const uint32_t size,
MP_Vector * a )
inlinestatic

Definition at line 17 of file mp_vector.h.

17 {
18 mp_free2(mp, sizeof(MP_Vector) + (m_uint)(a->cap * size), a);
19}
uintptr_t m_uint
Definition gwcommon.h:12
#define mp_free2(p, sz, a)
Definition mpool.h:26
uint32_t cap
Definition mp_vector.h:5

◆ mp_vector_len()

static uint32_t mp_vector_len ( MP_Vector * a)
inlinestatic

Definition at line 21 of file mp_vector.h.

21 {
22 return a ? a->len : 0;
23}
uint32_t len
Definition mp_vector.h:4

◆ mp_vector_pop()

static ANN m_bit * mp_vector_pop ( MP_Vector *const a,
const uint32_t size )
inlinestatic

Definition at line 51 of file mp_vector.h.

51 {
52 m_bit *data = ((a)->ptr + (a->len--) * size);
53 return data;
54}
unsigned char m_bit
Definition gwcommon.h:13

◆ mp_vector_rem()

static ANN void mp_vector_rem ( MP_Vector *const a,
const uint32_t size,
const uint32_t idx )
inlinestatic

Definition at line 42 of file mp_vector.h.

42 {
43 if (idx >= a->len) return;
44 if (idx < a->len - 1) {
45 const size_t len = a->len - idx - 1;
46 memmove(a->ptr + idx, a->ptr + idx + OFFSET + 1,
47 len * size);
48 }
49}
#define OFFSET
Definition container.h:7
uint8_t ptr[]
Definition mp_vector.h:6

◆ mp_vector_resize()

static ANN void mp_vector_resize ( const MemPool mp,
MP_Vector ** ap,
const uint32_t size,
const uint32_t cap )
inlinestatic

Definition at line 9 of file mp_vector.h.

9 {
10 MP_Vector *a = *ap;
11 *ap = (MP_Vector *)mp_realloc(mp, a, sizeof(MP_Vector) + (m_uint)(a->cap * size), sizeof(MP_Vector) + (m_uint)(cap * size));
12 (*ap)->cap = cap;
13}
void * mp_realloc(MemPool mp, void *ptr, const m_uint curr, const m_uint next)
Definition mpool.c:148

◆ new_mp_vector()

ANN MP_Vector * new_mp_vector ( const MemPool mp,
const uint32_t size,
const uint32_t len )

Definition at line 3 of file mp_vector.c.

3 {
4 m_uint cap = 1;
5 while (cap < len) cap *= 2;
6 const size_t sz = cap * size;
7 MP_Vector *a = (MP_Vector *)mp_malloc2(mp, sizeof(MP_Vector) + sz);
8 a->cap = cap;
9 a->len = len;
10 return a;
11}
#define mp_malloc2(p, sz)
Definition mpool.h:28