gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
vector.c
Go to the documentation of this file.
1#include "gwion_util.h"
2
3ANN inline void vector_init(const Vector v) {
5 VCAP(v) = MAP_CAP;
6}
7
9 const Vector v = mp_calloc(p, Vector);
10 vector_init(v);
11 return v;
12}
13
14ANN inline void vector_release(const Vector v) { xfree(v->ptr); }
15
16ANN void free_vector(MemPool p, const Vector v) {
18 mp_free(p, Vector, v);
19}
20
21ANN void vector_add(const Vector v, const vtype data) {
23 VPTR(v, VLEN(v)++) = (vtype)data;
24}
25
26ANN inline void vector_copy2(const restrict Vector v,
27 const restrict Vector ret) {
28 ret->ptr = (m_uint *)xrealloc(ret->ptr, VCAP(v) * SZ_INT);
29 memcpy(ret->ptr, v->ptr, VCAP(v) * SZ_INT);
30}
31
33 const Vector ret = mp_calloc(p, Vector);
34 vector_copy2(v, ret);
35 return ret;
36}
37
38ANN m_int vector_find(const Vector v, const vtype data) {
39 for (vtype i = VLEN(v) + 1; --i;)
40 if (VPTR(v, i - 1) == (vtype)data) return (m_int)(i - 1);
41 return -1;
42}
43
44ANN void vector_rem(const Vector v, const vtype index) {
45 if (index >= VLEN(v)) return;
46 if (index < VLEN(v) - 1)
47 memmove(v->ptr + index + OFFSET, v->ptr + index + OFFSET + 1,
48 (VLEN(v) - index - 1) * SZ_INT);
49 if (--VLEN(v) + OFFSET < VCAP(v) / 2)
50 v->ptr = (m_uint *)xrealloc(v->ptr, (VCAP(v) /= 2) * SZ_INT);
51}
52
53ANN bool vector_rem2(const Vector v, const vtype data) {
54 const m_int index = vector_find(v, data);
55 if (index > -1) {
56 vector_rem(v, (vtype)index);
57 return true;
58 } else return false;
59}
60
62 const vtype ret = VPTR(v, VLEN(v) - 1);
63 vector_rem(v, VLEN(v) - 1);
64 return ret;
65}
66
67ANN void vector_clear(const Vector v) {
68 v->ptr = (m_uint *)xrealloc(v->ptr, (VCAP(v) = MAP_CAP) * SZ_INT);
69 VLEN(v) = 0;
70}
#define OFFSET
Definition container.h:9
#define VLEN(v)
Definition container.h:10
uintptr_t vtype
Definition container.h:14
#define MAP_CAP
Definition container.h:8
#define VCAP(v)
Definition container.h:11
#define VPTR(v, i)
Definition container.h:12
#define ANN
Definition defs.h:19
uintptr_t m_uint
Definition gwcommon.h:11
#define SZ_INT
Definition gwcommon.h:18
intptr_t m_int
Definition gwcommon.h:10
meta header (use this to include the whole library)
#define mp_calloc(p, name)
Definition mpool.h:26
#define mp_free(p, name, a)
Definition mpool.h:27
vtype * ptr
Definition vector.h:9
ANN void vector_add(const Vector v, const vtype data)
Definition vector.c:21
ANN vtype vector_pop(const Vector v)
Definition vector.c:61
Vector new_vector(MemPool p)
Definition vector.c:8
ANN bool vector_rem2(const Vector v, const vtype data)
Definition vector.c:53
ANN Vector vector_copy(MemPool p, const Vector v)
Definition vector.c:32
ANN void vector_clear(const Vector v)
Definition vector.c:67
ANN void vector_release(const Vector v)
Definition vector.c:14
ANN void vector_rem(const Vector v, const vtype index)
Definition vector.c:44
ANN void free_vector(MemPool p, const Vector v)
Definition vector.c:16
ANN void vector_copy2(const restrict Vector v, const restrict Vector ret)
Definition vector.c:26
ANN void vector_init(const Vector v)
Definition vector.c:3
ANN m_int vector_find(const Vector v, const vtype data)
Definition vector.c:38
static ANN void vector_realloc(const Vector v)
Definition vector.h:39
#define xfree(a)
Definition xalloc.h:28
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