-
Notifications
You must be signed in to change notification settings - Fork 5
/
cons.c
154 lines (138 loc) · 3.79 KB
/
cons.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdlib.h>
#include "data/cons.h"
typedef struct {
CT_Cons* head;
CT_Cons* prev;
CT_MPool* pool;
} CT_ConsCloneState;
ct_export CT_Cons* ct_cons(void* x, CT_Cons* head, CT_MPool* mpool) {
CT_Cons* cell = CT_MP_ALLOC(mpool, CT_Cons);
CT_CHECK_MEM(cell);
CT_DEBUG("cons: %p (val: %p) -> %p", cell, x, head);
cell->value = x;
cell->next = head;
fail:
return cell;
}
static void ct_cons_iterate_clone(CT_Cons* c, void* cstate) {
CT_ConsCloneState* state = (CT_ConsCloneState*)cstate;
c = ct_cons(c->value, NULL, state->pool);
if (state->head == NULL) {
state->head = c;
state->prev = c;
} else {
state->prev->next = c;
state->prev = c;
}
}
ct_export void ct_cons_free(CT_Cons* cell, size_t freeVal) {
if (cell != NULL) {
if (freeVal) {
CT_INFO("free cons value: %p", cell->value);
free(cell->value);
}
free(cell);
}
}
ct_export void ct_cons_free_all(CT_Cons* cell, size_t freeVal) {
CT_Cons* next = NULL;
while (cell != NULL) {
next = cell->next;
ct_cons_free(cell, freeVal);
cell = next;
}
}
ct_export void* ct_cons_iterate(CT_Cons* cell,
CT_ConsVisitor visit,
void* state) {
while (cell != NULL) {
visit(cell, state);
cell = cell->next;
}
return state;
}
ct_export void* ct_cons_iterate_n(CT_Cons* cell,
CT_ConsVisitor visit,
void* state,
size_t num) {
while (cell != NULL && num--) {
visit(cell, state);
cell = cell->next;
}
return state;
}
ct_export CT_Cons* ct_cons_find(CT_Cons* cell, void* value) {
while (cell != NULL) {
if (cell->value == value) {
return cell;
}
cell = cell->next;
}
return NULL;
}
ct_export CT_Cons* ct_cons_from_array(void* values,
size_t num,
size_t stride,
CT_Cons* head,
CT_MPool* mpool) {
if (num > 0) {
values += (num - 1) * stride;
while (num--) {
head = ct_cons(values, head, mpool);
values -= stride;
}
}
return head;
}
ct_export CT_Cons* ct_cons_from_parray(void** values,
size_t num,
CT_Cons* head,
CT_MPool* mpool) {
if (num > 0) {
values += num - 1;
while (num--) {
head = ct_cons(*values--, head, mpool);
}
}
return head;
}
ct_export CT_Cons* ct_cons_concat_imm(CT_Cons* head, CT_Cons* rest) {
if (head != NULL) {
CT_Cons* hd = head;
while (1) {
//CT_INFO("search: %p, val: %s, next: %p", hd, (char*)hd->value, hd->next);
if (hd->next == NULL) {
//CT_INFO("link: %p, %p", hd, rest);
hd->next = rest;
break;
}
hd = hd->next;
}
}
return head;
}
ct_export CT_Cons* ct_cons_concat(CT_Cons* head,
CT_Cons* rest,
CT_MPool* mpool) {
CT_ConsCloneState state = {.head = NULL, .pool = mpool};
ct_cons_iterate(head, ct_cons_iterate_clone,
ct_cons_iterate(rest, ct_cons_iterate_clone, &state));
return state.head;
}
ct_export CT_Cons* ct_cons_take(CT_Cons* head, size_t num, CT_MPool* mpool) {
CT_ConsCloneState state = {.head = NULL, .pool = mpool};
ct_cons_iterate_n(head, ct_cons_iterate_clone, &state, num);
return state.head;
}
//// double-linked list
ct_export CT_ConsD* ct_consd(void* x, CT_ConsD* head, CT_MPool* mpool) {
CT_ConsD* cell = CT_MP_ALLOC(mpool, CT_ConsD);
CT_CHECK_MEM(cell);
cell->value = x;
cell->next = head;
if (head) {
head->prev = cell;
}
fail:
return cell;
}