-
Notifications
You must be signed in to change notification settings - Fork 1
/
nv_node.c
549 lines (499 loc) · 13.3 KB
/
nv_node.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#include "nv.h"
//
// Node Internal
//
struct NV_NODE {
NV_ID id;
const void *data;
NV_Node *prev;
NV_Node *next;
NV_NodeType type;
int size; // size of data, bytes.
//
const NV_Node *relCache; // link from this node. recently referenced.
NV_ID owner;
};
#define NODE_CACHE_MASK 0xFFFF
NV_Node *nodeIDCache[NODE_CACHE_MASK + 1];
int NV_Node_Internal_getCacheIndex(const NV_ID *id) {
if (nodeIDCache[id->d[0] & NODE_CACHE_MASK]) {
if (NV_NodeID_isEqual(&nodeIDCache[id->d[0] & NODE_CACHE_MASK]->id, id)) {
// hit!
// printf("Chache hit! %08X\n", id->d[0]);
return id->d[0] & NODE_CACHE_MASK;
}
}
return -1;
}
void NV_Node_Internal_invalidateCache(const NV_ID *id) {
int cacheIndex = NV_Node_Internal_getCacheIndex(id);
if (~cacheIndex) nodeIDCache[cacheIndex] = NULL;
}
NV_ID NV_NodeID_createNew(const NV_ID *id) {
NV_Node *n;
// 新規作成
n = NV_malloc(sizeof(NV_Node));
if (!n) exit(EXIT_FAILURE);
//
n->id = *id;
n->type = kNone;
n->data = NULL;
n->size = 0;
n->relCache = NULL;
//
n->next = nodeRoot.next;
if (n->next) n->next->prev = n;
n->prev = &nodeRoot;
if (n->prev) n->prev->next = n;
//
return n->id;
}
void NV_Node_Internal_resetData(NV_Node *n) {
if (n) {
if (n->data) {
NV_DbgInfo("Free Data type: %s", NV_NodeTypeList[n->type]);
NV_free((void *)n->data);
n->data = NULL;
n->size = 0;
}
n->type = kNone;
}
}
void NV_Node_Internal_remove(NV_Node *n) {
if (n) {
NV_DbgInfo_mem(n, "free");
NV_DbgInfo("Free Node type: %s", NV_NodeTypeList[n->type]);
if (n->type != kNone) NV_Node_Internal_resetData(n);
if (n->prev) n->prev->next = n->next;
if (n->next) n->next->prev = n->prev;
NV_Node_Internal_invalidateCache(&n->id);
NV_free(n);
}
}
int NV_Node_Internal_isEqualInValue(const NV_Node *na, const NV_Node *nb) {
// 2つのNodeが値として等しいか否かを返す。
// nodei
if (!na || !nb) return 0;
if (na->type != nb->type) return 0;
if (NV_NodeID_isEqual(&na->id, &nb->id)) return 1;
if (na->type == kString) {
return (NV_Node_String_compare(&na->id, &nb->id) == 0);
}
return 0;
}
void NV_Node_Internal_set_Type_Data_Size(const NV_ID *id, NV_NodeType type,
const void *data, int32_t size) {
NV_Node *n;
n = NV_NodeID_getNode(id);
n->type = type;
n->size = size;
n->data = data;
}
//
// Node
//
NV_Node nodeRoot;
void NV_Node_initRoot() {
srand(time(NULL));
//
nodeRoot.prev = NULL;
nodeRoot.next = NULL;
nodeRoot.data = NULL;
nodeRoot.type = kNone;
}
NV_Node *NV_Node_getNextNode(const NV_Node *n) {
if (!n) return NULL;
return n->next;
}
int NV_Node_getNodeCount() {
NV_Node *n;
int i;
//
i = 0;
for (n = nodeRoot.next; n; n = n->next) {
i++;
}
return i;
}
NV_ID NV_NodeID_create(const NV_ID *id) {
NV_Node *n;
n = NV_NodeID_getNode(id);
if (n) {
printf("NodeID collision detected. abort.\n");
exit(1);
}
// 新規作成
return NV_NodeID_createNew(id);
}
NV_ID NV_Node_create() {
NV_ID id = NV_ID_generateRandom();
return NV_NodeID_createNew(&id);
}
NV_ID NV_Node_createWithTypeAndData(NV_NodeType type, const void *data,
int32_t size) {
NV_ID id = NV_Node_create();
NV_Node_Internal_set_Type_Data_Size(&id, type, data, size);
return id;
}
NV_ID NV_Node_createWith_ID_Type_Data_Size(const NV_ID *id, NV_NodeType type,
const void *data, int32_t size) {
NV_NodeID_create(id);
NV_Node_Internal_set_Type_Data_Size(id, type, data, size);
return *id;
}
int NV_NodeID_isEqual(const NV_ID *a, const NV_ID *b) {
int i;
for (i = 0; i < 4; i++) {
if (a->d[i] != b->d[i]) return 0;
}
return 1;
}
int NV_NodeID_isEqualInValue(const NV_ID *a, const NV_ID *b) {
if (NV_NodeID_isEqual(a, b)) return 1; // 等しいidのNode同士の値は常に等しい
NV_Node *na, *nb;
na = NV_NodeID_getNode(a);
nb = NV_NodeID_getNode(b);
return NV_Node_Internal_isEqualInValue(na, nb);
}
int NV_NodeID_exists(const NV_ID *id) { return NV_NodeID_getNode(id) != NULL; }
NV_Node *NV_NodeID_getNode(const NV_ID *id) {
NV_Node *n;
//
if (!id) return NULL;
// check cache
int cacheIndex = NV_Node_Internal_getCacheIndex(id);
if (~cacheIndex) return nodeIDCache[cacheIndex];
/*
if(nodeIDCache[id->d[0] & NODE_CACHE_MASK]){
if(NV_NodeID_isEqual(&nodeIDCache[id->d[0] &
NODE_CACHE_MASK]->id, id)){
// hit!
printf("Chache hit! %08X\n", id->d[0]);
return nodeIDCache[id->d[0] & NODE_CACHE_MASK];
}
}
*/
//
for (n = nodeRoot.next; n; n = n->next) {
if (NV_NodeID_isEqual(&n->id, id)) {
// found
// add to cache
nodeIDCache[id->d[0] & NODE_CACHE_MASK] = n;
//
return n;
}
}
return NULL;
}
NV_NodeType NV_Node_getType(const NV_ID *id) {
NV_Node *n;
n = NV_NodeID_getNode(id);
if (n) return n->type;
return -1;
}
const NV_Node *NV_Node_getRelCache(const NV_Node *n) {
if (!n) return NULL;
return n->relCache;
}
void NV_Node_setRelCache(NV_Node *n, const NV_Node *rel) {
if (!n) return;
n->relCache = rel;
}
int32_t NV_Node_calcHash(const NV_Node *n) {
if (!n || !n->data) return 0;
return fnv_1_hash_32(n->data, n->size);
}
int32_t NV_NodeID_calcHash(const NV_ID *id) {
NV_Node *n;
n = NV_NodeID_getNode(id);
return NV_Node_calcHash(n);
}
/*
// void NV_Node_setDataAsType(const NV_Node *n, NV_NodeType type, void *data)
{
// If .data has been set already, this func will fail.
if(!n || n->type != type) return;
if(n->data){
printf("Try to overwrite existed data. abort.\n");
return;
}
n->data = data;
}
*/
const void *NV_Node_getDataAsType(const NV_Node *n, NV_NodeType type) {
if (!n || n->type != type) return NULL;
return n->data;
}
const void *NV_NodeID_getDataAsType(const NV_ID *id, NV_NodeType type) {
NV_Node *n;
n = NV_NodeID_getNode(id);
return NV_Node_getDataAsType(n, type);
}
NV_ID NV_Node_getID(const NV_Node *n) {
if (!n) return NODEID_NOT_FOUND;
return n->id;
}
void NV_Node_dumpAll() {
NV_Node *n;
//
for (n = nodeRoot.next; n; n = n->next) {
NV_Node_dump(&n->id);
putchar('\n');
}
}
void NV_Node_dumpAllToFile(FILE *fp) {
NV_Node *n;
if (!fp) return;
for (n = nodeRoot.next; n; n = n->next) {
NV_Node_fdump(fp, &n->id);
fputc('\n', fp);
}
}
void NV_Node_restoreFromFile(FILE *fp) {
char s[MAX_SAVE_DATA_ENTRY_SIZE];
while (fgets(s, sizeof(s), fp)) {
NV_Node_restoreFromString(s);
}
}
void NV_NodeID_remove(const NV_ID *baseID) {
NV_Node *n;
n = NV_NodeID_getNode(baseID);
if (n) NV_Node_Internal_remove(n);
}
NV_ID NV_NodeID_clone(const NV_ID *baseID) {
NV_Node *base;
base = NV_NodeID_getNode(baseID);
void *data = NULL;
if (base->data) {
data = NV_malloc(base->size);
memcpy(data, base->data, base->size);
}
return NV_Node_createWithTypeAndData(base->type, data, base->size);
}
void NV_Node_Internal_setInt32ToID(const NV_ID *id, int32_t v);
void NV_Node_Internal_setStrToID(const NV_ID *id, const char *s);
NV_ID NV_Node_restoreFromString(const char *s) {
const char *p;
int n, i;
NV_ID id;
NV_ID from, rel, to;
char *buf;
//
if (NV_ID_setFromString(&id, s)) {
printf("Invalid id format.\n");
return NODEID_NULL;
}
//
p = &s[32];
n = NV_strtolSeq(&p, 10);
switch (n) {
case kString:
n = NV_strtolSeq(&p, 10);
p++;
buf = NV_malloc(n);
for (i = 0; i < n - 1; i++) {
buf[i] = c2hexTable[(int)*(p++)];
buf[i] <<= 4;
buf[i] |= c2hexTable[(int)*(p++)];
}
buf[i] = 0;
// printf("str(%d) = %s\n", n, buf);
NV_Node_createWith_ID_String(&id, buf);
NV_free(buf);
break;
case kInteger:
n = NV_strtolSeq(&p, 10);
// printf("int(%d) = ", n);
//
n = NV_strtolSeq(&p, 16);
// printf("%d\n", n);
NV_Node_createWith_ID_Int32(&id, n);
break;
case kRelation:
NV_ID_setFromString(&from, &p[1]);
NV_ID_setFromString(&rel, &p[1 + 32 + 1]);
NV_ID_setFromString(&to, &p[1 + 32 + 1 + 32 + 1]);
NV_NodeID_createRelationWithID(&id, &from, &rel, &to);
break;
default:
printf("Unknown node type :%d\n", n);
exit(EXIT_FAILURE);
}
return id;
}
void NV_Node_fdump(FILE *fp, const NV_ID *id) {
const NV_Node *n = NV_NodeID_getNode(id);
if (!n) {
fprintf(fp, "(null)");
return;
}
// n->id
NV_ID_dumpIDToFile(&n->id, fp);
// n->type
fprintf(fp, " %d ", n->type);
// n->data
if (n->type == kString) {
int i;
fprintf(fp, "%d ", n->size);
for (i = 0; ((const char *)n->data)[i]; i++) {
fprintf(fp, "%02X", ((const char *)n->data)[i]);
}
} else if (n->type == kInteger) {
if (n->size == sizeof(int32_t)) {
fprintf(fp, "%d %08X", n->size, *((int32_t *)n->data));
}
} else if (n->type == kRelation) {
const NV_Relation *e = n->data;
NV_ID_dumpIDToFile(&e->from, fp);
fputc(' ', fp);
NV_ID_dumpIDToFile(&e->rel, fp);
fputc(' ', fp);
NV_ID_dumpIDToFile(&e->to, fp);
}
}
void NV_Node_dump(const NV_ID *id) { NV_Node_fdump(stdout, id); }
void NV_Node_printPrimVal(const NV_ID *id) {
const NV_Node *n = NV_NodeID_getNode(id);
if (!n) {
printf("(null)#%08X", id->d[0]);
return;
}
if (n->type == kString) {
printf("%s", n->data);
} else if (n->type == kInteger) {
if (n->size == sizeof(int32_t)) {
printf("%d", *((int32_t *)n->data));
}
}
}
void NV_NodeID_printForDebug(const NV_ID *id) {
const NV_Node *n = NV_NodeID_getNode(id);
if (!n) {
printf("(NV_Node is NULL)");
return;
}
NV_ID_dumpIDToFile(&n->id, stdout);
printf(": ");
if (n->type == kString) {
printf("\"%s\"", n->data);
} else if (n->type == kInteger) {
if (n->size == sizeof(int32_t)) {
printf("%d", *((int32_t *)n->data));
}
} else if (n->type == kRelation) {
printf("Rel: ");
const NV_Relation *e = n->data;
NV_ID_dumpIDToFile(&e->from, stdout);
fputc(' ', stdout);
NV_ID_dumpIDToFile(&e->rel, stdout);
fputc(' ', stdout);
NV_ID_dumpIDToFile(&e->to, stdout);
}
}
typedef struct {
int currentDepth;
NV_ID owner;
} NV_Node_DepthInfo;
int NV_Node_printDependencyTreeSub(void *d, const NV_ID *reln, const NV_ID *rel,
const NV_ID *to) {
NV_Node_DepthInfo *info = d;
int i;
for (i = 0; i < info->currentDepth; i++) {
printf("│ ");
}
printf("├── ");
NV_Node_printPrimVal(rel);
printf(": ");
//
if (!NV_Node_hasOwner(reln)) NV_Node_setOwner(reln, &info->owner);
if (!NV_Node_hasOwner(rel)) NV_Node_setOwner(rel, &info->owner);
if (!NV_Node_hasOwner(to)) {
NV_Node_setOwner(to, &info->owner);
NV_Node_printDependencyTree(to, info->currentDepth + 1);
} else {
NV_Node_printPrimVal(to);
printf("\n");
}
return 1;
}
void NV_Node_setOwner(const NV_ID *id, const NV_ID *owner) {
NV_Node *n = NV_NodeID_getNode(id);
if (n) {
n->owner = *owner;
}
}
int NV_Node_hasOwner(const NV_ID *id) {
NV_Node *n = NV_NodeID_getNode(id);
if (n) {
return !NV_NodeID_isEqual(&n->owner, &NODEID_NOT_FOUND);
}
return 1;
}
void NV_Node_printDependencyTree(const NV_ID *root, int currentDepth) {
if (currentDepth == 0) {
NV_Node *n;
for (n = nodeRoot.next; n; n = n->next) {
n->owner = NODEID_NOT_FOUND;
}
NV_Node_setOwner(root, root);
}
NV_Node_printPrimVal(root);
printf("\n");
// NV_NodeID_printForDebug(root);printf("\n");
// if(currentDepth && NV_Node_hasOwner(root)) return;
NV_Node_DepthInfo info;
info.currentDepth = currentDepth;
info.owner = *root;
NV_Dict_foreach(root, &info, NV_Node_printDependencyTreeSub);
if (currentDepth == 0) {
int count = 0;
NV_Node *n;
// printf("nodes to be removed:\n");
for (n = nodeRoot.next; n; n = n->next) {
if (!NV_Node_hasOwner(&n->id)) {
// NV_NodeID_printForDebug(&n->id); NV_NodeID_printForDebug(&n->owner);
// putchar('\n'); NV_NodeID_remove(&n->id);
count++;
}
}
printf("%d nodes have no owner.\n", count);
}
}
void NV_Node_cleanup() { NV_Node_cleanDepTree(&rootScope, 0); }
int NV_Node_cleanDepTreeSub(void *d, const NV_ID *reln, const NV_ID *rel,
const NV_ID *to) {
NV_Node_DepthInfo *info = d;
//
if (!NV_Node_hasOwner(reln)) NV_Node_setOwner(reln, &info->owner);
if (!NV_Node_hasOwner(rel)) NV_Node_setOwner(rel, &info->owner);
if (!NV_Node_hasOwner(to)) {
NV_Node_setOwner(to, &info->owner);
NV_Node_cleanDepTree(to, info->currentDepth + 1);
}
return 1;
}
void NV_Node_cleanDepTree(const NV_ID *root, int currentDepth) {
if (currentDepth == 0) {
NV_Node *n;
for (n = nodeRoot.next; n; n = n->next) {
n->owner = NODEID_NOT_FOUND;
}
NV_Node_setOwner(root, root);
}
// if(currentDepth && NV_Node_hasOwner(root)) return;
NV_Node_DepthInfo info;
info.currentDepth = currentDepth;
info.owner = *root;
NV_Dict_foreach(root, &info, NV_Node_cleanDepTreeSub);
if (currentDepth == 0) {
int count = 0;
NV_Node *n;
for (n = nodeRoot.next; n; n = n->next) {
if (!NV_Node_hasOwner(&n->id)) {
NV_NodeID_remove(&n->id);
count++;
}
}
// printf("Removed %d nodes.\n", count);
}
}