-
Notifications
You must be signed in to change notification settings - Fork 9
/
named_tuple.c
66 lines (56 loc) · 1.35 KB
/
named_tuple.c
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
61
62
63
64
65
66
//
//
//
#include "named_tuple.h"
#include <apr_tables.h>
#include <apr_hash.h>
typedef struct nm_tuple_t nm_tuple_t;
struct nm_tuple_t {
term_t name; // key
apr_array_header_t *fields; // term_t (atoms)
};
struct named_tuples_t {
apr_pool_t *pool;
apr_hash_t *nm_tuples; // key = name
};
named_tuples_t *named_tuples_make(apr_pool_t *pool)
{
named_tuples_t *nts = apr_palloc(pool, sizeof(*nts));
nts->pool = pool;
nts->nm_tuples = apr_hash_make(pool);
return nts;
}
int named_tuples_set(named_tuples_t *self, term_t name, term_t field)
{
nm_tuple_t *nt = apr_hash_get(self->nm_tuples, &name, sizeof(name));
if (nt != 0)
{
int i;
for (i = 0; i < nt->fields->nelts; i++)
{
if (APR_ARRAY_IDX(nt->fields, i, term_t) == field)
break;
}
if (i < nt->fields->nelts)
return i+1;
APR_ARRAY_PUSH(nt->fields, term_t) = field;
return nt->fields->nelts;
}
else
{
nt = apr_palloc(self->pool, sizeof(*nt));
nt->name = name;
nt->fields = apr_array_make(self->pool, 4, sizeof(term_t));
APR_ARRAY_PUSH(nt->fields, term_t) = field;
apr_hash_set(self->nm_tuples, &nt->name, sizeof(nt->name), nt);
return 1;
}
}
int named_tuples_arity(named_tuples_t *self, term_t name)
{
nm_tuple_t *nt = apr_hash_get(self->nm_tuples, &name, sizeof(name));
if (nt == 0)
return -1;
return nt->fields->nelts + 1; // +1 for tuple name
}
//EOF