-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnv_array.c
243 lines (223 loc) · 6.03 KB
/
nv_array.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "nv.h"
//
// Internal
//
void NV_Array_Internal_setCount(const NV_ID *array, int32_t count) {
NV_ID cn;
cn = NV_Node_createWithInt32(count);
NV_Dict_addUniqueIDKey(array, &RELID_ARRAY_COUNT, &cn);
}
int32_t NV_Array_Internal_getCount(const NV_ID *array) {
NV_ID cn;
cn = NV_Dict_getEqID(array, &RELID_ARRAY_COUNT);
return NV_NodeID_getInt32(&cn);
}
void NV_Array_Internal_updateCountRel(const NV_ID *array, int32_t diff) {
int32_t count;
count = NV_Array_Internal_getCount(array);
NV_Array_Internal_setCount(array, count + diff);
}
//
//
//
int NV_Array_isArray(const NV_ID *id) {
return NV_isTermType(id, &NODEID_TERM_TYPE_ARRAY);
}
NV_ID NV_Array_create() {
NV_ID array;
array = NV_Node_create();
NV_NodeID_createRelation(&array, &RELID_TERM_TYPE, &NODEID_TERM_TYPE_ARRAY);
//
NV_Array_Internal_setCount(&array, 0);
//
return array;
}
NV_ID NV_Array_clone(const NV_ID *base) {
NV_ID newArray, t;
int i;
//
newArray = NV_Array_create();
for (i = 0;; i++) {
t = NV_Array_getByIndex(base, i);
if (NV_NodeID_isEqual(&t, &NODEID_NOT_FOUND)) break;
NV_Array_push(&newArray, &t);
}
return newArray;
}
NV_ID NV_Array_push(const NV_ID *array, const NV_ID *data) {
NV_ID v, t, next;
//
v = NV_Node_create();
NV_NodeID_createUniqueIDRelation(&v, &RELID_ARRAY_DATA, data);
//
t = *array;
for (;;) {
next = NV_NodeID_getRelatedNodeFrom(&t, &RELID_ARRAY_NEXT);
if (NV_NodeID_isEqual(&next, &NODEID_NOT_FOUND)) break;
t = next;
}
NV_NodeID_createRelation(&t, &RELID_ARRAY_NEXT, &v);
//
NV_Array_Internal_updateCountRel(array, +1);
//
return v;
}
NV_ID NV_Array_pop(const NV_ID *array) {
NV_ID prev, t, next;
prev = *array;
t = NV_NodeID_getRelatedNodeFrom(&prev, &RELID_ARRAY_NEXT);
for (;;) {
next = NV_NodeID_getRelatedNodeFrom(&t, &RELID_ARRAY_NEXT);
if (NV_NodeID_isEqual(&next, &NODEID_NOT_FOUND)) break;
prev = t;
t = next;
}
// t is retv.
NV_ID relnid;
relnid = NV_NodeID_getRelationFrom(&prev, &RELID_ARRAY_NEXT);
// NV_NodeID_remove(&relnid);
//
if (!NV_NodeID_isEqual(&t, &NODEID_NOT_FOUND)) {
NV_Array_Internal_updateCountRel(array, -1);
}
//
return NV_NodeID_getRelatedNodeFrom(&t, &RELID_ARRAY_DATA);
}
NV_ID NV_Array_last(const NV_ID *array) {
NV_ID t;
int32_t count, i;
//
count = NV_Array_count(array);
//
t = NV_NodeID_getRelatedNodeFrom(array, &RELID_ARRAY_NEXT);
//
for (i = 0; i < count - 1; i++) {
t = NV_NodeID_getRelatedNodeFrom(&t, &RELID_ARRAY_NEXT);
}
// t is retv.
return NV_NodeID_getRelatedNodeFrom(&t, &RELID_ARRAY_DATA);
}
int32_t NV_Array_count(const NV_ID *array) {
if (!array) return 0;
if (!NV_isTermType(array, &NODEID_TERM_TYPE_ARRAY)) return 0;
return NV_Array_Internal_getCount(array);
}
NV_ID NV_Array_getAssignableByIndex(const NV_ID *array, int index) {
// indexが範囲外ならばNOT_FOUND
NV_ID t;
if (index < 0) {
return NODEID_NOT_FOUND;
}
t = NV_NodeID_getRelatedNodeFrom(array, &RELID_ARRAY_NEXT);
for (; index > 0; index--) {
t = NV_NodeID_getRelatedNodeFrom(&t, &RELID_ARRAY_NEXT);
if (NV_NodeID_isEqual(&t, &NODEID_NOT_FOUND)) return NODEID_NOT_FOUND;
}
return NV_Variable_createWithName(&t, &RELID_ARRAY_DATA);
}
NV_ID NV_Array_getByIndex(const NV_ID *array, int index) {
NV_ID v;
v = NV_Array_getAssignableByIndex(array, index);
if (NV_Term_isNotFound(&v)) return NODEID_NOT_FOUND;
return NV_Variable_getData(&v);
}
void NV_Array_removeIndex(const NV_ID *array, int index) {
NV_ID t, tn, tnn, r;
if (index < 0) return;
t = *array;
for (; index; index--) {
if (index == 0) break;
t = NV_NodeID_getRelatedNodeFrom(&t, &RELID_ARRAY_NEXT);
if (NV_NodeID_isEqual(&t, &NODEID_NOT_FOUND)) break;
}
// tのnextが削除対象。これをtnとおく。
if (!NV_NodeID_isEqual(&t, &NODEID_NOT_FOUND)) {
tn = NV_NodeID_getRelatedNodeFrom(&t, &RELID_ARRAY_NEXT);
tnn = NV_NodeID_getRelatedNodeFrom(&tn, &RELID_ARRAY_NEXT);
r = NV_NodeID_getRelationFrom(&t, &RELID_ARRAY_NEXT);
NV_NodeID_updateRelationTo(&r, &tnn);
//
NV_Array_Internal_updateCountRel(array, -1);
//
}
}
void NV_Array_writeToIndex(const NV_ID *array, int index, const NV_ID *data) {
NV_ID v;
v = NV_Array_getAssignableByIndex(array, index);
NV_Variable_assign(&v, data);
}
NV_ID NV_Array_joinWithCStr(const NV_ID *array, const char *sep) {
size_t sumLen = 1;
NV_ID t;
char *buf, *p;
const char *s;
int i;
//
for (i = 0;; i++) {
t = NV_Array_getByIndex(array, i);
if (NV_NodeID_isEqual(&t, &NODEID_NOT_FOUND)) break;
sumLen += NV_Node_String_strlen(&t);
}
sumLen += i * strlen(sep);
buf = NV_malloc(sumLen);
p = buf;
for (i = 0;; i++) {
t = NV_Array_getByIndex(array, i);
if (NV_NodeID_isEqual(&t, &NODEID_NOT_FOUND)) break;
s = NV_NodeID_getCStr(&t);
if (s) {
NV_strncpy(p, s, sumLen - (p - buf), strlen(s));
p += strlen(s);
}
NV_strncpy(p, sep, sumLen - (p - buf), strlen(sep));
p += strlen(sep);
}
*p = 0;
t = NV_Node_createWithString(buf);
NV_free(buf);
return t;
}
NV_ID NV_Array_getSorted(const NV_ID *array,
int (*f)(const void *n1, const void *n2)) {
size_t nel = NV_Array_count(array);
NV_ID *buf = NV_malloc(sizeof(NV_ID) * nel);
size_t i;
NV_ID new;
//
for (i = 0; i < nel; i++) {
buf[i] = NV_Array_getByIndex(array, i);
}
//
qsort(buf, nel, sizeof(NV_ID), f);
//
new = NV_Array_create();
for (i = 0; i < nel; i++) {
NV_Array_push(&new, &buf[i]);
}
//
NV_free(buf);
return new;
}
int32_t NV_Array_calcHash(const NV_ID *array) {
int32_t hash = 0;
NV_ID t;
int i;
for (i = 0;; i++) {
t = NV_Array_getByIndex(array, i);
if (NV_NodeID_isEqual(&t, &NODEID_NOT_FOUND)) break;
hash ^= NV_Term_calcHash(&t);
}
return hash;
}
void NV_Array_print(const NV_ID *array) {
NV_ID t;
int i;
printf("[");
for (i = 0;; i++) {
t = NV_Array_getByIndex(array, i);
if (NV_NodeID_isEqual(&t, &NODEID_NOT_FOUND)) break;
if (i != 0) printf(",");
NV_Term_print(&t);
}
printf("]");
}