From 779924d55dc1837b50d915bd3286fba00f39cd1b Mon Sep 17 00:00:00 2001 From: hikalium Date: Thu, 8 Jun 2017 17:24:33 +0900 Subject: [PATCH] Hide NV_Node impl from nv_relation.c --- .gitignore | 1 + Makefile | 5 +- lang/02/eval.c | 12 ++++ main.c | 23 ++++++-- nv.h | 3 + nv_dict.c | 23 ++++++++ nv_func.h | 38 ++++++------ nv_node.c | 155 +++++++++++++++++++++++++++++++++++++++---------- nv_op.c | 2 + nv_relation.c | 90 ++++++++++++++-------------- nv_static.c | 12 ++-- nv_static.h | 3 +- 12 files changed, 260 insertions(+), 107 deletions(-) diff --git a/.gitignore b/.gitignore index 1a8df1a..4e1fb9a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ html/ latex/ *.dtps/ note/ +*.txt diff --git a/Makefile b/Makefile index 31f1966..4f8fd12 100644 --- a/Makefile +++ b/Makefile @@ -6,9 +6,10 @@ SRCS= main.c \ nv.c nv_array.c nv_dict.c nv_driver.c \ nv_fix.c nv_id.c nv_node.c nv_op.c nv_static.c \ nv_variable.c nv_term.c nv_signal.c \ - nv_integer.c nv_string.c nv_relation.c nv_context.c \ + nv_integer.c nv_string.c nv_relation.c \ fnv1.c \ - lang/02/parse.c lang/02/eval.c + lang/02/parse.c lang/02/eval.c \ + nv_anchor.c HEADERS=nv.h nv_node.h nv_func.h nv_static.h CFLAGS=-Wall -Wextra -lncurses -Wunused-function CFLAGS += -DGIT_COMMIT_ID="\"$(GIT_COMMIT_ID)\"" \ diff --git a/lang/02/eval.c b/lang/02/eval.c index 7216571..13931ae 100644 --- a/lang/02/eval.c +++ b/lang/02/eval.c @@ -154,6 +154,18 @@ NV_ID NV_Lang02_OpFunc_prefixOp isAnsNotInteger = 1; NV_Dict_print(scope); *lastEvalVal = NODEID_NULL; + } else if(strcmp(opStr, "lsdep") == 0){ + isAnsNotInteger = 1; + NV_Node_printDependencyTree(&rootScope, 0); + *lastEvalVal = NODEID_NULL; + } else if(strcmp(opStr, "dump") == 0){ + isAnsNotInteger = 1; + FILE *fp = fopen("dump.txt", "w"); + if(fp){ + NV_Node_dumpAllToFile(fp); + fclose(fp); + } + *lastEvalVal = NODEID_NULL; } else if(strcmp(opStr, "+") == 0){ opR = NV_Term_getPrimNodeID(&opR, scope); ans = NV_Term_getInt32(&opR ,scope); diff --git a/main.c b/main.c index 9bd5cdb..349ae50 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,8 @@ // main // +NV_ID rootScope; + int main(int argc, char *argv[]) { NV_ID cTypeList, opDict; @@ -35,6 +37,7 @@ int main(int argc, char *argv[]) // NV_Node_initRoot(); // + /* if(filename[0]){ // restore savedata printf("Restoring from %s ...\n", filename); @@ -50,10 +53,11 @@ int main(int argc, char *argv[]) printf("fopen failed.\n"); } } + */ // NV_insertInitialNode(); // - NV_ID topLevelScope = NV_Node_createWithString("root"); + rootScope = NV_Node_createWithString("root"); // cTypeList = NV_createCharTypeList(); // @@ -61,7 +65,7 @@ int main(int argc, char *argv[]) // { NV_ID opDictName = NV_Node_createWithString("opDict"); - NV_ID opDictVar = NV_Variable_createWithName(&topLevelScope, &opDictName); + NV_ID opDictVar = NV_Variable_createWithName(&rootScope, &opDictName); NV_Variable_assign(&opDictVar, &opDict); } // @@ -83,7 +87,9 @@ int main(int argc, char *argv[]) "loop={for{#args[0]=args[1]}{#args[0]<=args[2]}{#args[0]++}{args[3]()}}"); */ // TEST CODE BEGIN vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv - /* +/* + NV_ID rootAnchor = NV_Anchor_createWithName("root"); + NV_ID list1 = NV_Array_create(); NV_ID e; e = NV_Node_createWithString("test1"); @@ -96,8 +102,13 @@ int main(int argc, char *argv[]) printf("Hash: %08X\n", NV_Term_calcHash(&list1)); printf("Hash: %08X\n", NV_Term_calcHash(&opDict)); + FILE *fp = fopen("dump.txt", "w"); + if(fp){ + NV_Node_dumpAllToFile(fp); + fclose(fp); + } - return 0; + return 0; */ // TEST CODE END ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -106,11 +117,11 @@ int main(int argc, char *argv[]) while(NV_gets(line, sizeof(line)) != NULL){ NV_ID tokenList = NV_tokenize(&cTypeList, line); NV_ID codeGraphRoot = NV_parseToCodeGraph(&tokenList, &opDict); - NV_ID result = NV_evalGraph(&codeGraphRoot, &topLevelScope); + NV_ID result = NV_evalGraph(&codeGraphRoot, &rootScope); // if(!(NV_globalExecFlag & NV_EXEC_FLAG_SUPRESS_AUTOPRINT)){ printf(" = "); - NV_ID prim = NV_Term_getPrimNodeID(&result, &topLevelScope); + NV_ID prim = NV_Term_getPrimNodeID(&result, &rootScope); NV_Term_print(&prim); printf("\n"); } else{ diff --git a/nv.h b/nv.h index cfa7864..2a9096c 100644 --- a/nv.h +++ b/nv.h @@ -77,6 +77,9 @@ struct NV_OP_POINTER { NV_ID dict; }; +// @main.c +extern NV_ID rootScope; + // @nv.c #define NV_EXEC_FLAG_VERBOSE 0x01 #define NV_EXEC_FLAG_INTERRUPT 0x02 diff --git a/nv_dict.c b/nv_dict.c index b0064e0..d71b70f 100644 --- a/nv_dict.c +++ b/nv_dict.c @@ -106,6 +106,7 @@ NV_ID NV_Dict_getAll(const NV_ID *root, const NV_ID *key) int NV_Dict_foreach (const NV_ID *dict, void *d, int (*f)(void *d, const NV_ID *rel, const NV_ID *to)) { + // ノードdictを起点とするすべての関係をたどって。そのrelationとtoをfに引き渡す。 // fの戻り値がfalseの場合はそこでループを中止する。 // 戻り値は、fを呼んだ回数である。 const NV_Node *n; @@ -123,6 +124,28 @@ int NV_Dict_foreach return count; } +int NV_Dict_foreachWithRelFilter +(const NV_ID *dict, void *d, int (*f)(void *d, const NV_ID *rel, const NV_ID *to), int (*filter)(const NV_ID *rel)) +{ + // ノードdictを起点とするすべての関係をたどって。そのrelationとtoをfに引き渡す。 + // filterがtrueを返す関係のノードのみたどる。 + // fの戻り値がfalseの場合はそこでループを中止する。 + // 戻り値は、fを呼んだ回数である。 + const NV_Node *n; + const NV_Relation *reld; + int count = 0; + for(n = nodeRoot.next; n; n = n->next){ + if(n->type == kRelation){ + reld = n->data; + if(NV_NodeID_isEqual(&reld->from, dict) && filter(&n->id)){ + count++; + if(!f(d, &reld->rel, &reld->to)) break; + } + } + } + return count; +} + NV_ID NV_Dict_getByStringKey (const NV_ID *root, const char *key) { diff --git a/nv_func.h b/nv_func.h index 1d57f00..5f7ffab 100644 --- a/nv_func.h +++ b/nv_func.h @@ -48,6 +48,8 @@ NV_ID NV_Dict_createMergedNode(const NV_ID *a, const NV_ID *b); NV_ID NV_Dict_getAll(const NV_ID *root, const NV_ID *key); int NV_Dict_foreach (const NV_ID *dict, void *d, int (*f)(void *d, const NV_ID *rel, const NV_ID *to)); +int NV_Dict_foreachWithRelFilter +(const NV_ID *dict, void *d, int (*f)(void *d, const NV_ID *rel, const NV_ID *to), int (*filter)(const NV_ID *rel)); NV_ID NV_Dict_getByStringKey (const NV_ID *root, const char *key); void NV_Dict_print(const NV_ID *root); @@ -77,26 +79,35 @@ void NV_ID_dumpIDToFile(const NV_ID *id, FILE *fp); // @nv_node.c NV_ID NV_NodeID_createNew(const NV_ID *id); void NV_Node_initRoot(); +NV_Node *NV_Node_getNextNode(const NV_Node *n); int NV_Node_getNodeCount(); NV_ID NV_NodeID_create(const NV_ID *id); NV_ID NV_Node_create(); +NV_ID NV_Node_createWithTypeAndData(NV_NodeType type, void *data, int32_t size); int NV_NodeID_isEqual(const NV_ID *a, const NV_ID *b); int NV_NodeID_isEqualInValue(const NV_ID *a, const NV_ID *b); int NV_NodeID_exists(const NV_ID *id); NV_Node *NV_NodeID_getNode(const NV_ID *id); NV_NodeType NV_Node_getType(const NV_ID *id); +const NV_Node *NV_Node_getRelCache(const NV_Node *n); +void NV_Node_setRelCache(NV_Node *n, const NV_Node *rel); int32_t NV_Node_calcHash(const NV_Node *n); int32_t NV_NodeID_calcHash(const NV_ID *id); -void *NV_Node_getDataAsType(const NV_ID *id, NV_NodeType type); +void *NV_Node_getDataAsType(const NV_Node *n, NV_NodeType type); +void *NV_NodeID_getDataAsType(const NV_ID *id, NV_NodeType type); +NV_ID NV_Node_getID(const NV_Node *n); void NV_Node_dumpAll(); void NV_Node_dumpAllToFile(FILE *fp); void NV_Node_restoreFromFile(FILE *fp); +void NV_NodeID_remove(const NV_ID *baseID); NV_ID NV_NodeID_clone(const NV_ID *baseID); -NV_ID NV_Node_restoreFromString(const char *s); void NV_Node_fdump(FILE *fp, const NV_ID *id); void NV_Node_dump(const NV_ID *id); void NV_Node_printPrimVal(const NV_ID *id); void NV_NodeID_printForDebug(const NV_ID *id); +int NV_Node_printDependencyTreeFilter(const NV_ID *rel); +int NV_Node_printDependencyTreeSub(void *d, const NV_ID *rel, const NV_ID *to); +void NV_Node_printDependencyTree(const NV_ID *root, int currentDepth); // @nv_op.c @@ -191,14 +202,12 @@ NV_ID NV_NodeID_createUniqueIDRelation (const NV_ID *from, const NV_ID *rel, const NV_ID *to); NV_ID NV_NodeID_createUniqueEqRelation (const NV_ID *from, const NV_ID *rel, const NV_ID *to); -void NV_Node_setRelation -(const NV_ID *relnid, const NV_ID *from, const NV_ID *rel, const NV_ID *to); NV_Node *NV_NodeID_Relation_getLinkFrom(const NV_ID *relnid); NV_ID NV_NodeID_Relation_getIDLinkTo(const NV_ID *relnid); NV_Node *NV_NodeID_Relation_getLinkTo(const NV_ID *relnid); NV_ID NV_NodeID_Relation_getIDLinkRel(const NV_ID *relnid); NV_Node *NV_NodeID_Relation_getLinkRel(const NV_ID *relnid); -void NV_NodeID_updateRelationTo(const NV_ID *relnid, const NV_ID *to); +NV_ID NV_NodeID_updateRelationTo(const NV_ID *relnid, const NV_ID *to); const NV_Node *NV_NodeID_getRelNodeFromWithCmp (const NV_ID *from, const NV_ID *rel, int (*cmp)(const NV_ID *p, const NV_ID *q)); NV_ID NV_NodeID_getRelationFrom(const NV_ID *from, const NV_ID *rel); @@ -207,20 +216,6 @@ NV_ID NV_NodeID_getEqRelationFrom(const NV_ID *from, const NV_ID *rel); NV_ID NV_NodeID_getEqRelatedNodeFrom(const NV_ID *from, const NV_ID *rel); -// @nv_context.c -NV_ID NV_getContextList(); -NV_ID NV_Context_create(); -NV_ID NV_Context_getEvalStack(const NV_ID *ctx); -NV_ID NV_Context_createChildScopeWithArgs(const NV_ID *ctx, const NV_ID *argsBlock); -void NV_Context_pushToEvalStack -(const NV_ID *ctx, const NV_ID *code, const NV_ID *newScope); -NV_ID NV_Context_getCurrentCode(const NV_ID *ctx); -NV_ID NV_Context_getCurrentScope(const NV_ID *ctx); -NV_ID NV_Context_getLastResult(const NV_ID *ctx); -void NV_Context_setOpDict(const NV_ID *ctx, const NV_ID *opDict); -NV_ID NV_Context_getOpDict(const NV_ID *ctx); - - // @fnv1.c uint32_t fnv_1_hash_32(uint8_t *bytes, size_t length); uint64_t fnv_1_hash_64(uint8_t *bytes, size_t length); @@ -261,3 +256,8 @@ NV_ID NV_Lang02_OpFunc_parentheses (NV_ID * const p, NV_ID * const lastEvalVal, const NV_ID *scope); NV_ID NV_evalGraph(const NV_ID *codeGraphRoot, const NV_ID *scope); + +// @nv_anchor.c +int NV_Anchor_isAnchor(const NV_ID *id); +NV_ID NV_Anchor_createWithName(const char *s); + diff --git a/nv_node.c b/nv_node.c index 63df4f7..8c104e4 100644 --- a/nv_node.c +++ b/nv_node.c @@ -24,15 +24,16 @@ NV_ID NV_NodeID_createNew(const NV_ID *id) // return n->id; } -/* -//void NV_Node_Internal_resetData(NV_Node *n) +void NV_Node_Internal_resetData(NV_Node *n) { if(n){ if(n->data){ + /* if(n->type == kRelation){ NV_Relation *reld = n->data; - NV_NodeID_release(&reld->to); + //NV_NodeID_release(&reld->to); } + */ NV_DbgInfo("Free Data type: %s", NV_NodeTypeList[n->type]); NV_free(n->data); n->data = NULL; @@ -41,9 +42,8 @@ NV_ID NV_NodeID_createNew(const NV_ID *id) n->type = kNone; } } -*/ -/* -//void NV_Node_Internal_remove(NV_Node *n) + +void NV_Node_Internal_remove(NV_Node *n) { if(n){ NV_DbgInfo_mem(n, "free"); @@ -53,9 +53,9 @@ NV_ID NV_NodeID_createNew(const NV_ID *id) if(n->next) n->next->prev = n->prev; NV_free(n); } - NV_Node_Internal_removeAllRelationFrom(&n->id); + //NV_Node_Internal_removeAllRelationFrom(&n->id); } -*/ + int NV_Node_Internal_isEqualInValue(const NV_Node *na, const NV_Node *nb) { // 2つのNodeが値として等しいか否かを返す。 @@ -84,6 +84,12 @@ void NV_Node_initRoot() 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; @@ -114,7 +120,31 @@ NV_ID NV_Node_create() return NV_NodeID_createNew(&id); } - +NV_ID NV_Node_createWithTypeAndData(NV_NodeType type, void *data, int32_t size) +{ + NV_ID id; + NV_Node *n; + id = NV_Node_create(); + n = NV_NodeID_getNode(&id); + n->type = type; + n->size = size; + n->data = data; + return id; +} +/* +//NV_ID NV_Node_createWith_ID_Type_Data +//(NV_ID *id, NV_NodeType type, void *data, int32_t size) +{ + NV_ID id; + NV_Node *n; + id = NV_Node_create(); + n = NV_NodeID_getNode(&id); + n->type = type; + n->size = size; + n->data = data; + return id; +} +*/ int NV_NodeID_isEqual(const NV_ID *a, const NV_ID *b) { int i; @@ -175,6 +205,18 @@ NV_NodeType NV_Node_getType(const NV_ID *id) 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; @@ -187,13 +229,35 @@ int32_t NV_NodeID_calcHash(const NV_ID *id) 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; +} +*/ +void *NV_Node_getDataAsType(const NV_Node *n, NV_NodeType type) +{ + if(!n || n->type != type) return NULL; + return n->data; +} -void *NV_Node_getDataAsType(const NV_ID *id, NV_NodeType type) +void *NV_NodeID_getDataAsType(const NV_ID *id, NV_NodeType type) { NV_Node *n; n = NV_NodeID_getNode(id); - if(!n || n->type != type) return NULL; - return n->data; + 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() @@ -213,7 +277,7 @@ void NV_Node_dumpAllToFile(FILE *fp) NV_Node_fdump(fp, &n->id); fputc('\n', fp); } } - +/* void NV_Node_restoreFromFile(FILE *fp) { char s[MAX_SAVE_DATA_ENTRY_SIZE]; @@ -222,15 +286,14 @@ void NV_Node_restoreFromFile(FILE *fp) NV_Node_restoreFromString(s); } } - -/* -//void NV_NodeID_remove(const NV_ID *baseID) +*/ +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, *new; @@ -247,9 +310,10 @@ NV_ID NV_NodeID_clone(const NV_ID *baseID) } -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) +//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; @@ -292,22 +356,12 @@ NV_ID NV_Node_restoreFromString(const char *s) NV_ID_setFromString(&from, &p[1]); NV_ID_setFromString(&rel, &p[ 1 + 32 + 1]); NV_ID_setFromString(&to, &p[1 + 32 + 1 + 32 + 1]); -/* - printf("rel "); - printf(" "); - NV_ID_dumpIDToFile(&from, stdout); - printf(" "); - NV_ID_dumpIDToFile(&rel, stdout); - printf(" "); - NV_ID_dumpIDToFile(&to, stdout); - printf("\n"); -*/ NV_Node_setRelation(&id, &from, &rel, &to); break; } return id; } - +*/ void NV_Node_fdump(FILE *fp, const NV_ID *id) { const NV_Node *n = NV_NodeID_getNode(id); @@ -389,3 +443,42 @@ void NV_NodeID_printForDebug(const NV_ID *id) } +typedef struct { + int currentDepth; +} NV_Node_DepthInfo; + +int NV_Node_printDependencyTreeFilter(const NV_ID *rel) +{ + if(NV_NodeID_isEqual(rel, &RELID_TERM_TYPE)){ + return 1; + } + return 0; +} + +int NV_Node_printDependencyTreeSub(void *d, 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(": "); + NV_Node_printDependencyTree(to, info->currentDepth + 1); + + return 1; +} + +void NV_Node_printDependencyTree(const NV_ID *root, int currentDepth) +{ + + NV_Node_printPrimVal(root); printf("\n"); + NV_Node_DepthInfo info; + info.currentDepth = currentDepth; + NV_Dict_foreachWithRelFilter( + root, &info, + NV_Node_printDependencyTreeSub, + NV_Node_printDependencyTreeFilter); +} + diff --git a/nv_op.c b/nv_op.c index 7d869bc..eb85cab 100644 --- a/nv_op.c +++ b/nv_op.c @@ -98,6 +98,8 @@ NV_BuiltinOpTag builtinOpList[] = { // {"print", 10, "prefix"}, {"ls", 10, "prefix"}, + {"lsdep", 10, "prefix"}, + {"dump", 10, "prefix"}, /* {"}", 10, "NV_Op_codeBlockClose"}, {"ls2", 10, "NV_Op_ls2"}, diff --git a/nv_relation.c b/nv_relation.c index 80f93b1..5551b33 100644 --- a/nv_relation.c +++ b/nv_relation.c @@ -1,5 +1,5 @@ #include "nv.h" -#include "nv_node.h" +//#include "nv_node.h" // // Internal @@ -26,9 +26,15 @@ NV_ID (*find)(const NV_ID *from, const NV_ID *rel)) NV_ID NV_NodeID_createRelation (const NV_ID *from, const NV_ID *rel, const NV_ID *to) { - NV_ID r = NV_Node_create(); - NV_Node_setRelation(&r, from, rel, to); - return r; + NV_Relation *reld; + int32_t size = sizeof(NV_Relation); + // + reld = NV_malloc(size); + reld->from = *from; + reld->rel = *rel; + reld->to = *to; + + return NV_Node_createWithTypeAndData(kRelation, reld, size); } NV_ID NV_NodeID_createUniqueIDRelation @@ -52,9 +58,9 @@ NV_ID NV_NodeID_createUniqueEqRelation return NV_NodeID_createRel_OnDupUpdate( from, rel, to, NV_NodeID_getEqRelationFrom); } - -void NV_Node_setRelation -(const NV_ID *relnid, const NV_ID *from, const NV_ID *rel, const NV_ID *to) +/* +//void NV_Node_setRelation +//(const NV_ID *relnid, const NV_ID *from, const NV_ID *rel, const NV_ID *to) { // retains to. // Relationはtoを保持し、 @@ -79,26 +85,22 @@ void NV_Node_setRelation // } } - +*/ NV_Node *NV_NodeID_Relation_getLinkFrom(const NV_ID *relnid) { - NV_Node *n; NV_Relation *reld; // - n = NV_NodeID_getNode(relnid); - if(!n || n->type != kRelation) return NULL; - reld = n->data; + reld = NV_NodeID_getDataAsType(relnid, kRelation); + if(!reld) return NULL; return NV_NodeID_getNode(&reld->from); } NV_ID NV_NodeID_Relation_getIDLinkTo(const NV_ID *relnid) { - NV_Node *n; NV_Relation *reld; // - n = NV_NodeID_getNode(relnid); - if(!n || n->type != kRelation) return NODEID_NOT_FOUND; - reld = n->data; + reld = NV_NodeID_getDataAsType(relnid, kRelation); + if(!reld) return NODEID_NOT_FOUND; return reld->to; } @@ -111,12 +113,9 @@ NV_Node *NV_NodeID_Relation_getLinkTo(const NV_ID *relnid) NV_ID NV_NodeID_Relation_getIDLinkRel(const NV_ID *relnid) { - NV_Node *n; NV_Relation *reld; // - n = NV_NodeID_getNode(relnid); - if(!n || n->type != kRelation) return NODEID_NOT_FOUND; - reld = n->data; + reld = NV_NodeID_getDataAsType(relnid, kRelation); return reld->rel; } @@ -127,18 +126,24 @@ NV_Node *NV_NodeID_Relation_getLinkRel(const NV_ID *relnid) return NV_NodeID_getNode(&id); } -void NV_NodeID_updateRelationTo(const NV_ID *relnid, const NV_ID *to) +NV_ID NV_NodeID_updateRelationTo(const NV_ID *relnid, const NV_ID *to) { + // Updateとは言っているが、内容はImmutableにしたいので、 + // 既存のものを削除して、新しいノードを生成し、以前と同一の関係性を持たせる。 + NV_ID from, rel; NV_Node *n; NV_Relation *reld; // n = NV_NodeID_getNode(relnid); - if(n){ - if(n->type != kRelation) return; - reld = n->data; - // - reld->to = *to; - } + reld = NV_Node_getDataAsType(n, kRelation); + if(!reld || !to) return NODEID_NOT_FOUND; + // 以前の関係性を取り出す + from = reld->from; + rel = reld->rel; + // 古い関係を除去 + NV_NodeID_remove(relnid); + // 新しい関係を作成して返す + return NV_NodeID_createRelation(&from, &rel, to); } #define REL_CACHE_MASK 0xFF @@ -155,9 +160,8 @@ const NV_Node *NV_NodeID_getRelNodeFromWithCmp // check cache n = relCache[HASH_REL(from)][HASH_REL(rel)]; if(n){ - reld = n->data; - if(n->type == kRelation && reld && - NV_NodeID_isEqual(&reld->from, from) && cmp(&reld->rel, rel)){ + reld = NV_Node_getDataAsType(n, kRelation); + if(reld && NV_NodeID_isEqual(&reld->from, from) && cmp(&reld->rel, rel)){ // hit! return n; } @@ -165,24 +169,22 @@ const NV_Node *NV_NodeID_getRelNodeFromWithCmp // check relCache n = NV_NodeID_getNode(from); if(n){ - n = n->relCache; - if(n && n->type == kRelation){ - reld = n->data; - if(NV_NodeID_isEqual(&reld->from, from) && cmp(&reld->rel, rel)){ - return n; - } + n = NV_Node_getRelCache(n); + reld = NV_Node_getDataAsType(n, kRelation); + if(reld && NV_NodeID_isEqual(&reld->from, from) && cmp(&reld->rel, rel)){ + return n; } } // - for(n = nodeRoot.next; n; n = n->next){ - if(n->type != kRelation) continue; - reld = n->data; + for(n = NV_Node_getNextNode(&nodeRoot); n; n = NV_Node_getNextNode(n)){ + reld = NV_Node_getDataAsType(n, kRelation); + if(!reld) continue; if(NV_NodeID_isEqual(&reld->from, from) && cmp(&reld->rel, rel)){ // relCache[HASH_REL(from)][HASH_REL(rel)] = n; NV_Node *fn = NV_NodeID_getNode(from); if(fn){ - fn->relCache = n; + NV_Node_setRelCache(fn, n); } // return n; @@ -196,7 +198,7 @@ NV_ID NV_NodeID_getRelationFrom(const NV_ID *from, const NV_ID *rel) const NV_Node *n; // n = NV_NodeID_getRelNodeFromWithCmp(from, rel, NV_NodeID_isEqual); - if(n) return n->id; + if(n) return NV_Node_getID(n); return NODEID_NOT_FOUND; } @@ -207,7 +209,7 @@ NV_ID NV_NodeID_getRelatedNodeFrom(const NV_ID *from, const NV_ID *rel) // n = NV_NodeID_getRelNodeFromWithCmp(from, rel, NV_NodeID_isEqual); if(n){ - reld = n->data; + reld = NV_Node_getDataAsType(n, kRelation); return reld->to; } return NODEID_NOT_FOUND; @@ -218,7 +220,7 @@ NV_ID NV_NodeID_getEqRelationFrom(const NV_ID *from, const NV_ID *rel) const NV_Node *n; // n = NV_NodeID_getRelNodeFromWithCmp(from, rel, NV_NodeID_isEqualInValue); - if(n) return n->id; + if(n) return NV_Node_getID(n); return NODEID_NOT_FOUND; } @@ -233,7 +235,7 @@ NV_ID NV_NodeID_getEqRelatedNodeFrom(const NV_ID *from, const NV_ID *rel) // n = NV_NodeID_getRelNodeFromWithCmp(from, rel, NV_NodeID_isEqualInValue); if(n){ - reld = n->data; + reld = NV_Node_getDataAsType(n, kRelation); return reld->to; } return NODEID_NOT_FOUND; diff --git a/nv_static.c b/nv_static.c index 56f566d..f3e2d2f 100644 --- a/nv_static.c +++ b/nv_static.c @@ -6,15 +6,15 @@ const NV_ID NODEID_NULL = {{0x00000000, 0, 0, 0}}; const NV_ID NODEID_NOT_FOUND = {{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}}; +// Term Types const NV_ID NODEID_TERM_TYPE_ARRAY = {{0x22E57D41, 0x92C74F8A, 0xA1724F15, 0x8AA67792}}; const NV_ID NODEID_TERM_TYPE_VARIABLE = {{0xACF77B34, 0x8D844B54, 0xBF55E4A5, 0x037A6523}}; const NV_ID NODEID_TERM_TYPE_OP = {{0x83E75537, 0x03CB43A8, 0x9F0B84ED, 0xEA0C635A}}; -const NV_ID NODEID_TERM_TYPE_PATH - = {{0x9F32D54F, 0xE09B4862, 0x82CC1ABF, 0xD75A12A9}}; -// +const NV_ID NODEID_TERM_TYPE_ANCHOR + = {{0x71FCAC53, 0x36144715, 0xADE0CAD2, 0x1112848C}}; const NV_ID RELID_ARRAY_NEXT = {{0x73EEE19B, 0x5086494E, 0xAACE7CD4, 0x80D9FA78}}; const NV_ID RELID_ARRAY_COUNT @@ -48,7 +48,9 @@ const NV_ID RELID_PARENT_SCOPE const NV_ID RELID_CONTEXT_LIST = {{0xC3CC02EE, 0x585B428A, 0xA965B56B, 0x57D98193}}; const NV_ID RELID_NEXT_CONTEXT - = {{0x694D5C33, 0x1D0849C0, 0x84B49D4F, 0xF10770E4}}; + = {{0x694D5C33, 0x1D0849C0, 0x84B49D4F, 0xF10770E4}}; +const NV_ID RELID_RETAINS + = {{0xA8BC9B27, 0x2D6D4428, 0x9F03C541, 0xDD753023}}; const NV_ID RELID_OP_DICT = {{0xC0F19B8B, 0x6FFF4613, 0xA238A172, 0x767CF0B6}}; @@ -136,6 +138,7 @@ void NV_Graph_initStaticNodes() void NV_insertInitialNode() { NV_Graph_initStaticNodes(); + /* // contextListが存在しなければ作成する NV_ID contextList; contextList = NV_getContextList(); @@ -144,6 +147,7 @@ void NV_insertInitialNode() NV_NodeID_createRelation(&NODEID_NV_STATIC_ROOT, &RELID_CONTEXT_LIST, &contextList); } + */ } diff --git a/nv_static.h b/nv_static.h index 78e5024..0e38fa3 100644 --- a/nv_static.h +++ b/nv_static.h @@ -4,7 +4,7 @@ extern const NV_ID NODEID_NOT_FOUND; extern const NV_ID NODEID_TERM_TYPE_ARRAY; extern const NV_ID NODEID_TERM_TYPE_VARIABLE; extern const NV_ID NODEID_TERM_TYPE_OP; -extern const NV_ID NODEID_TERM_TYPE_PATH; +extern const NV_ID NODEID_TERM_TYPE_ANCHOR; extern const NV_ID RELID_ARRAY_NEXT; extern const NV_ID RELID_ARRAY_COUNT; extern const NV_ID RELID_ARRAY_DATA; @@ -22,4 +22,5 @@ extern const NV_ID RELID_CURRENT_SCOPE; extern const NV_ID RELID_PARENT_SCOPE; extern const NV_ID RELID_CONTEXT_LIST; extern const NV_ID RELID_NEXT_CONTEXT; +extern const NV_ID RELID_RETAINS; extern const NV_ID RELID_OP_DICT;