gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
m_vector.c
Go to the documentation of this file.
1#include "gwion_util.h"
2
3ANN static inline void resize(const M_Vector v, const m_uint cap) {
4 v->ptr = (m_bit *)xrealloc(v->ptr, ARRAY_OFFSET + cap * ARRAY_SIZE(v));
5 ARRAY_CAP(v) = cap;
6}
7
8void m_vector_init(const M_Vector array, const m_uint size, const m_uint len) {
9 m_uint cap = 2;
10 while (cap < len) cap *= 2;
11 const size_t sz = ARRAY_OFFSET + cap * size;
12 array->ptr = (m_bit *)xcalloc(1, sz);
13 ARRAY_CAP(array) = cap;
14 ARRAY_SIZE(array) = size;
15 ARRAY_LEN(array) = len;
16}
17
18ANN void m_vector_add(const M_Vector v, const void *data) {
19 const m_uint size = ARRAY_SIZE(v);
20 if (++ARRAY_LEN(v) >= ARRAY_CAP(v)) resize(v, ARRAY_CAP(v) *= 2);
21 memcpy(ARRAY_PTR(v) + (ARRAY_LEN(v) - 1) * size, data, size);
22}
23
24ANN void m_vector_add_front(const M_Vector v, const void *data) {
25 const m_uint size = ARRAY_SIZE(v);
26 if (++ARRAY_LEN(v) >= ARRAY_CAP(v)) resize(v, ARRAY_CAP(v) *= 2);
27 memmove(ARRAY_PTR(v) + size, ARRAY_PTR(v), ARRAY_LEN(v) * size);
28 memmove(ARRAY_PTR(v), data, size);
29}
30
31ANN void m_vector_set(const M_Vector v, const m_uint i, const void *data) {
32 const m_uint size = ARRAY_SIZE(v);
33 memcpy(ARRAY_PTR(v) + i * size, data, size);
34}
35
36ANN void m_vector_rem(const M_Vector v, m_uint index) {
37 const m_uint size = ARRAY_SIZE(v);
38 if (index < ARRAY_LEN(v) - 1)
39 memmove(ARRAY_PTR(v) + index * size, ARRAY_PTR(v) + (index + 1) * size,
40 (ARRAY_LEN(v) - index - 1) * size);
41 --ARRAY_LEN(v);
42 if (ARRAY_LEN(v) < ARRAY_CAP(v) / 2) resize(v, ARRAY_CAP(v) / 2);
43}
44
45ANN void m_vector_insert(const M_Vector v, m_uint index, const void *data) {
46 const size_t size = ARRAY_SIZE(v);
47 if (++ARRAY_LEN(v) >= ARRAY_CAP(v)) resize(v, ARRAY_CAP(v) *= 2);
48 memmove(ARRAY_PTR(v) + (index + 1) * size, ARRAY_PTR(v) + index * size,
49 (ARRAY_LEN(v) - index + 1) * size);
50 memcpy(ARRAY_PTR(v) + index * size, data, size);
51}
#define ANN
Definition defs.h:19
uintptr_t m_uint
Definition gwcommon.h:11
unsigned char m_bit
Definition gwcommon.h:12
meta header (use this to include the whole library)
ANN void m_vector_add(const M_Vector v, const void *data)
Definition m_vector.c:18
ANN void m_vector_add_front(const M_Vector v, const void *data)
Definition m_vector.c:24
ANN void m_vector_set(const M_Vector v, const m_uint i, const void *data)
Definition m_vector.c:31
ANN void m_vector_rem(const M_Vector v, m_uint index)
Definition m_vector.c:36
static ANN void resize(const M_Vector v, const m_uint cap)
Definition m_vector.c:3
ANN void m_vector_insert(const M_Vector v, m_uint index, const void *data)
Definition m_vector.c:45
void m_vector_init(const M_Vector array, const m_uint size, const m_uint len)
Definition m_vector.c:8
#define ARRAY_CAP(array)
Definition m_vector.h:11
#define ARRAY_OFFSET
Definition m_vector.h:7
#define ARRAY_LEN(array)
Definition m_vector.h:9
#define ARRAY_SIZE(array)
Definition m_vector.h:10
#define ARRAY_PTR(array)
Definition m_vector.h:8
m_bit * ptr
Definition m_vector.h:5
static ANEW void * xcalloc(const m_uint n, const m_uint sz)
Definition xalloc.h:18
static ANEW void * xrealloc(void *p, const m_uint sz)
Definition xalloc.h:23