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

Go to the source code of this file.

Functions

static ANN void resize (const M_Vector v, const m_uint cap)
 
void m_vector_init (const M_Vector array, const m_uint size, const m_uint len)
 
ANN void m_vector_add (const M_Vector v, const void *data)
 
ANN void m_vector_add_front (const M_Vector v, const void *data)
 
ANN void m_vector_set (const M_Vector v, const m_uint i, const void *data)
 
ANN void m_vector_rem (const M_Vector v, m_uint index)
 
ANN void m_vector_insert (const M_Vector v, m_uint index, const void *data)
 

Function Documentation

◆ m_vector_add()

ANN void m_vector_add ( const M_Vector v,
const void * data )

Definition at line 18 of file m_vector.c.

18 {
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}
uintptr_t m_uint
Definition gwcommon.h:11
static ANN void resize(const M_Vector v, const m_uint cap)
Definition m_vector.c:3
#define ARRAY_CAP(array)
Definition m_vector.h:11
#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_vector_add_front()

ANN void m_vector_add_front ( const M_Vector v,
const void * data )

Definition at line 24 of file m_vector.c.

24 {
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}

◆ m_vector_init()

void m_vector_init ( const M_Vector array,
const m_uint size,
const m_uint len )

Definition at line 8 of file m_vector.c.

8 {
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}
unsigned char m_bit
Definition gwcommon.h:12
#define ARRAY_OFFSET
Definition m_vector.h:7
m_bit * ptr
Definition m_vector.h:5
static ANEW void * xcalloc(const m_uint n, const m_uint sz)
Definition xalloc.h:18

◆ m_vector_insert()

ANN void m_vector_insert ( const M_Vector v,
m_uint index,
const void * data )

Definition at line 45 of file m_vector.c.

45 {
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}

◆ m_vector_rem()

ANN void m_vector_rem ( const M_Vector v,
m_uint index )

Definition at line 36 of file m_vector.c.

36 {
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}

◆ m_vector_set()

ANN void m_vector_set ( const M_Vector v,
const m_uint i,
const void * data )

Definition at line 31 of file m_vector.c.

31 {
32 const m_uint size = ARRAY_SIZE(v);
33 memcpy(ARRAY_PTR(v) + i * size, data, size);
34}

◆ resize()

static ANN void resize ( const M_Vector v,
const m_uint cap )
inlinestatic

Definition at line 3 of file m_vector.c.

3 {
4 v->ptr = (m_bit *)xrealloc(v->ptr, ARRAY_OFFSET + cap * ARRAY_SIZE(v));
5 ARRAY_CAP(v) = cap;
6}
static ANEW void * xrealloc(void *p, const m_uint sz)
Definition xalloc.h:23