forked from rmartinjak/mex-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structlist.c
86 lines (77 loc) · 1.85 KB
/
structlist.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "mex.h"
#include "structlist.h"
static struct node *node_create(mxArray *a) {
struct node *n = mxMalloc(sizeof *n);
n->array = mxDuplicateArray(a);
n->next = NULL;
return n;
}
static void node_free(struct node *n) {
if (!n) {
return;
}
node_free(n->next);
mxFree(n);
}
void structlist_init(struct structlist *l)
{
l->head = l->tail = NULL;
l->num_fields = 0;
l->size = 0;
}
void structlist_free(struct structlist *l)
{
if (!l) {
return;
}
node_free(l->head);
l->size = 0;
}
int structlist_add(struct structlist *l, mxArray *a)
{
struct node *n = node_create(a);
if (!n) {
return -1;
}
if (!l->tail) {
l->head = l->tail = n;
l->num_fields = mxGetNumberOfFields(a);
} else {
if (mxGetNumberOfFields(a) != l->num_fields) {
node_free(n);
return -2;
}
l->tail->next = n;
l->tail = n;
}
l->size += mxGetN(a);
return 0;
}
mxArray *structlist_collapse(struct structlist *l)
{
mxArray *a = mxCreateStructMatrix(1, l->size, 0, NULL);
if (!l->size) {
return a;
}
int i, n = mxGetNumberOfFields(l->head->array);
for (i = 0; i < n; i++) {
const char *name = mxGetFieldNameByNumber(l->head->array, i);
mxAddField(a, name);
}
mwIndex index = 0;
struct node *node;
for (node = l->head; node; node = node->next) {
n = mxGetN(node->array);
for (i = 0; i < n; i++) {
int field;
for (field = 0; field < l->num_fields; field++) {
mxArray *val = mxGetFieldByNumber(node->array, i, field);
mxSetFieldByNumber(a, index, field, mxDuplicateArray(val));
}
index++;
}
mxDestroyArray(node->array);
}
structlist_free(l);
return a;
}