-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoddict.h
60 lines (54 loc) · 2.36 KB
/
doddict.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef DOD_DICT_H
#define DOD_DICT_H
#include "dodvec.h"
#define doddict_new(_tk_, _tv_, _n_) doddict_newl(sizeof(_tk_), sizeof(_tv_), _n_)
#define doddict_free(_d_) \
do { \
if ((_d_) != NULL) { \
dodvec_free((_d_)->keys); \
dodvec_free((_d_)->vals); \
} \
free(_d_); \
} while (0)
#define doddict_size(_d_) ((_d_)->keys->l)
#define doddict_push_back(_d_, _k_, _v_) \
doddict_push_backl(_d_, _k_, sizeof(*_k_), _v_, sizeof(*_v_))
#define doddict_back_read(_d_, _k_, _v_) \
do { \
memcpy((_k_), dodvec_back((_d_)->keys), sizeof(*(_k_))); \
memcpy((_v_), dodvec_back((_d_)->vals), sizeof(*(_v_))); \
} while (0)
#define doddict_pop_back(_d_) \
do { \
dodvec_pop_back((_d_)->keys); \
dodvec_pop_back((_d_)->vals); \
} while (0)
typedef struct {
dodvec_t *keys;
dodvec_t *vals;
} doddict_t;
static inline doddict_t *doddict_newl(size_t ksz, size_t vsz, size_t nmemb) {
doddict_t *d = malloc(sizeof(*d));
if (d) {
d->keys = dodvec_newl(ksz, ksz * nmemb);
d->vals = dodvec_newl(vsz, vsz * nmemb);
if (!d->keys || !d->vals) {
dodvec_free(d->keys);
dodvec_free(d->vals);
free(d);
return NULL;
}
}
return d;
}
static inline dodret_t doddict_push_backl(doddict_t *d, const void *k, size_t kl,
const void *v, size_t vl) {
if (dodvec_push_backl(d->keys, k, kl) < 0) {
return DODRET_ENOMEM;
} else if (dodvec_push_backl(d->vals, v, vl) < 0) {
dodvec_pop_back(d->keys);
return DODRET_ENOMEM;
}
return DODRET_OK;
}
#endif // DOD_DICT_H