gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
map.c
Go to the documentation of this file.
1#include "gwion_util.h"
2
3ANN void map_clear(const Map v) {
4 if (VCAP(v) != MAP_CAP)
5 v->ptr = (m_uint *)xrealloc(v->ptr, (VCAP(v) = MAP_CAP) * SZ_INT);
6 VLEN(v) = 0;
7}
8
9ANN inline void map_init(const Map a) {
11 VCAP(a) = MAP_CAP;
12}
13
15 const Map map = mp_calloc(p, Map);
16 map_init(map);
17 return map;
18}
19
20ANN vtype map_get(const Map map, const vtype key) {
21 for (vtype i = VLEN(map) + 1; --i;)
22 if (VKEY(map, i - 1) == key) return VVAL(map, i - 1);
23 return 0;
24}
25
26ANN m_int map_index(const Map map, const vtype key) {
27 for (vtype i = VLEN(map) + 1; --i;)
28 if (VKEY(map, i - 1) == key) return i - 1;
29 return -1;
30}
31
32ANN void map_set(const Map map, const vtype key, const vtype ptr) {
33 for (vtype i = VLEN(map) + 1; --i;) {
34 if (VKEY(map, i - 1) == key) {
35 VVAL(map, i - 1) = ptr;
36 return;
37 }
38 }
40 VKEY(map, VLEN(map)) = key;
41 VVAL(map, VLEN(map)++) = ptr;
42}
43
44ANN void map_remove(const Map map, const vtype key) {
45 const vtype len = VLEN(map);
46 for (vtype i = 0, j = 0; i < len; ++i) {
47 if (VKEY(map, i) != key) {
48 VKEY(map, j) = VKEY(map, i);
49 VVAL(map, j) = VVAL(map, i);
50 ++j;
51 } else
52 --VLEN(map);
53 }
54}
55
56ANN void map_commit(const restrict Map map, const restrict Map commit) {
57 for (m_uint i = 0; i < map_size(commit); ++i)
58 map_set(map, VKEY(commit, i), VVAL(commit, i));
59}
60
61ANN void map_release(const Map map) { xfree(map->ptr); }
62
63ANN void free_map(MemPool p, const Map map) {
64 map_release(map);
65 mp_free(p, Map, map);
66}
#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 ANN
Definition defs.h:19
#define ANEW
Definition defs.h:22
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)
ANN void map_init(const Map a)
Definition map.c:9
ANN void map_release(const Map map)
Definition map.c:61
ANN m_int map_index(const Map map, const vtype key)
Definition map.c:26
ANN void map_remove(const Map map, const vtype key)
Definition map.c:44
ANN void free_map(MemPool p, const Map map)
Definition map.c:63
ANN vtype map_get(const Map map, const vtype key)
Definition map.c:20
ANN void map_set(const Map map, const vtype key, const vtype ptr)
Definition map.c:32
ANN void map_clear(const Map v)
Definition map.c:3
ANEW Map new_map(MemPool p)
Definition map.c:14
ANN void map_commit(const restrict Map map, const restrict Map commit)
Definition map.c:56
static ANN vtype map_size(const Map map)
Definition map.h:28
#define VVAL(v, i)
Definition map.h:12
#define VKEY(v, i)
Definition map.h:11
#define mp_calloc(p, name)
Definition mpool.h:26
#define mp_free(p, name, a)
Definition mpool.h:27
Definition map.h:7
vtype * ptr
Definition map.h:8
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