gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
Functions
vector.c File Reference
#include "gwion_util.h"

Go to the source code of this file.

Functions

ANN void vector_init (const Vector v)
 
Vector new_vector (MemPool p)
 
ANN void vector_release (const Vector v)
 
ANN void free_vector (MemPool p, const Vector v)
 
ANN void vector_add (const Vector v, const vtype data)
 
ANN void vector_copy2 (const restrict Vector v, const restrict Vector ret)
 
ANN Vector vector_copy (MemPool p, const Vector v)
 
ANN m_int vector_find (const Vector v, const vtype data)
 
ANN void vector_rem (const Vector v, const vtype index)
 
ANN bool vector_rem2 (const Vector v, const vtype data)
 
ANN vtype vector_pop (const Vector v)
 
ANN void vector_clear (const Vector v)
 

Function Documentation

◆ free_vector()

ANN void free_vector ( MemPool p,
const Vector v )

Definition at line 16 of file vector.c.

16 {
18 mp_free(p, Vector, v);
19}
#define mp_free(p, name, a)
Definition mpool.h:27
ANN void vector_release(const Vector v)
Definition vector.c:14

◆ new_vector()

Vector new_vector ( MemPool p)

Definition at line 8 of file vector.c.

8 {
9 const Vector v = mp_calloc(p, Vector);
10 vector_init(v);
11 return v;
12}
#define mp_calloc(p, name)
Definition mpool.h:26
ANN void vector_init(const Vector v)
Definition vector.c:3

◆ vector_add()

ANN void vector_add ( const Vector v,
const vtype data )

Definition at line 21 of file vector.c.

21 {
23 VPTR(v, VLEN(v)++) = (vtype)data;
24}
#define VLEN(v)
Definition container.h:10
uintptr_t vtype
Definition container.h:14
#define VPTR(v, i)
Definition container.h:12
static ANN void vector_realloc(const Vector v)
Definition vector.h:39

◆ vector_clear()

ANN void vector_clear ( const Vector v)

Definition at line 67 of file vector.c.

67 {
68 v->ptr = (m_uint *)xrealloc(v->ptr, (VCAP(v) = MAP_CAP) * SZ_INT);
69 VLEN(v) = 0;
70}
#define MAP_CAP
Definition container.h:8
#define VCAP(v)
Definition container.h:11
uintptr_t m_uint
Definition gwcommon.h:11
#define SZ_INT
Definition gwcommon.h:18
vtype * ptr
Definition vector.h:9
static ANEW void * xrealloc(void *p, const m_uint sz)
Definition xalloc.h:23

◆ vector_copy()

ANN Vector vector_copy ( MemPool p,
const Vector v )

Definition at line 32 of file vector.c.

32 {
33 const Vector ret = mp_calloc(p, Vector);
34 vector_copy2(v, ret);
35 return ret;
36}
ANN void vector_copy2(const restrict Vector v, const restrict Vector ret)
Definition vector.c:26

◆ vector_copy2()

ANN void vector_copy2 ( const restrict Vector v,
const restrict Vector ret )
inline

Definition at line 26 of file vector.c.

27 {
28 ret->ptr = (m_uint *)xrealloc(ret->ptr, VCAP(v) * SZ_INT);
29 memcpy(ret->ptr, v->ptr, VCAP(v) * SZ_INT);
30}

◆ vector_find()

ANN m_int vector_find ( const Vector v,
const vtype data )

Definition at line 38 of file vector.c.

38 {
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}
intptr_t m_int
Definition gwcommon.h:10

◆ vector_init()

ANN void vector_init ( const Vector v)
inline

Definition at line 3 of file vector.c.

3 {
5 VCAP(v) = MAP_CAP;
6}
static ANEW void * xcalloc(const m_uint n, const m_uint sz)
Definition xalloc.h:18

◆ vector_pop()

ANN vtype vector_pop ( const Vector v)

Definition at line 61 of file vector.c.

61 {
62 const vtype ret = VPTR(v, VLEN(v) - 1);
63 vector_rem(v, VLEN(v) - 1);
64 return ret;
65}
ANN void vector_rem(const Vector v, const vtype index)
Definition vector.c:44

◆ vector_release()

ANN void vector_release ( const Vector v)
inline

Definition at line 14 of file vector.c.

14{ xfree(v->ptr); }
#define xfree(a)
Definition xalloc.h:28

◆ vector_rem()

ANN void vector_rem ( const Vector v,
const vtype index )

Definition at line 44 of file vector.c.

44 {
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}
#define OFFSET
Definition container.h:9

◆ vector_rem2()

ANN bool vector_rem2 ( const Vector v,
const vtype data )

Definition at line 53 of file vector.c.

53 {
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}
ANN m_int vector_find(const Vector v, const vtype data)
Definition vector.c:38