Fix type type.

This commit is contained in:
Saturneic 2018-07-23 12:59:16 +08:00
parent 733a304b62
commit 6aa9b8d708
15 changed files with 159 additions and 152 deletions

Binary file not shown.

Binary file not shown.

View File

@ -5,7 +5,7 @@ Node *nodeWithInt(int m_int) {
int *p_int = (int *)malloc(sizeof(int)); int *p_int = (int *)malloc(sizeof(int));
*p_int = m_int; *p_int = m_int;
p_node = initNode(); p_node = initNode();
initMallocValueForNode(p_node, "int", (void *)p_int); initMallocValueForNode(p_node, INT, (void *)p_int);
return p_node; return p_node;
} }
@ -14,28 +14,28 @@ Node *nodeWithDouble(double m_double) {
double *p_double = (double *)malloc(sizeof(double)); double *p_double = (double *)malloc(sizeof(double));
*p_double = m_double; *p_double = m_double;
p_node = initNode(); p_node = initNode();
initMallocValueForNode(p_node, "double", (void *)p_double); initMallocValueForNode(p_node, DOUBLE, (void *)p_double);
return p_node; return p_node;
} }
Node *nodeWithString(const char *m_string) { Node *nodeWithString(const char *m_string) {
Node *p_node; Node *p_node;
char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string) + 1)); char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string) + 1));
strcpy_s(p_string, sizeof(p_string), m_string); strcpy(p_string, m_string);
p_node = initNode(); p_node = initNode();
initMallocValueForNode(p_node, "string", (void *)p_string); initMallocValueForNode(p_node, STRING, (void *)p_string);
return p_node; return p_node;
} }
Node *nodeWithPointer(void *m_pointer) { Node *nodeWithPointer(void *m_pointer) {
Node *p_node = initNode(); Node *p_node = initNode();
initMallocValueForNode(p_node, "pointer", m_pointer); initMallocValueForNode(p_node, POINTER, m_pointer);
return p_node; return p_node;
} }
Node *nodeWithComplex(void) { Node *nodeWithComplex(void) {
Node *p_node = initNode(); Node *p_node = initNode();
p_node->type = "list"; p_node->type = LIST;
p_node->value = initList(); p_node->value = initList();
p_node->if_malloc = 1; p_node->if_malloc = 1;
return p_node; return p_node;
@ -50,7 +50,7 @@ Node *findByIndexForNode(List *p_list, unsigned long long m_index) {
return p_node; return p_node;
} }
int listThrough(List *p_list, int(*p_func)(const char *, void *)) { int listThrough(List *p_list, int(*p_func)(int, void *)) {
Node *p_node = p_list->head; Node *p_node = p_list->head;
while (p_node != NULL) { while (p_node != NULL) {
if (p_node->if_malloc == 1) { if (p_node->if_malloc == 1) {
@ -70,22 +70,22 @@ int listThrough(List *p_list, int(*p_func)(const char *, void *)) {
} }
int getByIntForNode(Node *p_node) { int getByIntForNode(Node *p_node) {
if (!strcmp(p_node->type, "int")) return *(int *)(p_node->value); if (p_node->type == INT) return *(int *)(p_node->value);
else return -1; else return -1;
} }
char *getByStringForNode(Node *p_node) { char *getByStringForNode(Node *p_node) {
if (!strcmp(p_node->type, "string")) return (char *)(p_node->value); if (p_node->type == STRING) return (char *)(p_node->value);
else return NULL; else return NULL;
} }
double getByDoubleForNode(Node *p_node) { double getByDoubleForNode(Node *p_node) {
if (!strcmp(p_node->type, "double")) return *(double *)(p_node->value); if (p_node->type == DOUBLE) return *(double *)(p_node->value);
else return -1; else return -1;
} }
void *getByPointerForNode(Node *p_node) { void *getByPointerForNode(Node *p_node) {
if (!strcmp(p_node->type, "pointer")) return (void *)(p_node->value); if (p_node->type == POINTER) return (void *)(p_node->value);
else return NULL; else return NULL;
} }
@ -115,19 +115,19 @@ void printList(List *p_list) {
printf("["); printf("[");
while (p_node != NULL) { while (p_node != NULL) {
if (!if_nearLast && p_node->next == NULL) if_nearLast = 1; if (!if_nearLast && p_node->next == NULL) if_nearLast = 1;
if (!strcmp(p_node->type, "int")) { if (p_node->type == INT) {
printf("%d", *(int *)(p_node->value)); printf("%d", *(int *)(p_node->value));
} }
else if (!strcmp(p_node->type, "double")) { else if (p_node->type == DOUBLE) {
printf("%a", *(double *)(p_node->value)); printf("%a", *(double *)(p_node->value));
} }
else if (!strcmp(p_node->type, "string")) { else if (p_node->type == STRING) {
printf("%s", (char *)(p_node->value)); printf("%s", (char *)(p_node->value));
} }
else if (!strcmp(p_node->type, "pointer")) { else if (p_node->type == POINTER) {
printf("%p", (char *)(p_node->value)); printf("%p", (char *)(p_node->value));
} }
else if (!strcmp(p_node->type, "list")) { else if (p_node->type == LIST) {
printList((List *)p_node->value); printList((List *)p_node->value);
} }
if (!if_nearLast) { if (!if_nearLast) {
@ -144,23 +144,23 @@ void printNodeInfo(Node *p_node, int priority) {
printf("#NODE(location:%p, id:%llu){\n", p_node, p_node->id); printf("#NODE(location:%p, id:%llu){\n", p_node, p_node->id);
for (i = 0; i < priority + 1; i++) printf(" "); for (i = 0; i < priority + 1; i++) printf(" ");
printf("NEXT->%p / LAST->%p / MALLOC:%d\n", p_node->next, p_node->last, p_node->if_malloc); printf("NEXT->%p / LAST->%p / MALLOC:%d\n", p_node->next, p_node->last, p_node->if_malloc);
if (!strcmp(p_node->type, "int")) { if (p_node->type == INT) {
for (i = 0; i < priority + 1; i++) printf(" "); for (i = 0; i < priority + 1; i++) printf(" ");
printf("VALUE(int):%d\n", *(int *)(p_node->value)); printf("VALUE(int):%d\n", *(int *)(p_node->value));
} }
else if (!strcmp(p_node->type, "double")) { else if (p_node->type == DOUBLE) {
for (i = 0; i < priority + 1; i++) printf(" "); for (i = 0; i < priority + 1; i++) printf(" ");
printf("VALUE(double):%a\n", *(double *)(p_node->value)); printf("VALUE(double):%a\n", *(double *)(p_node->value));
} }
else if (!strcmp(p_node->type, "string")) { else if (p_node->type == STRING) {
for (i = 0; i < priority + 1; i++) printf(" "); for (i = 0; i < priority + 1; i++) printf(" ");
printf("VALUE(string):%s\n", (char *)(p_node->value)); printf("VALUE(string):%s\n", (char *)(p_node->value));
} }
else if (!strcmp(p_node->type, "pointer")) { else if (p_node->type == POINTER) {
for (i = 0; i < priority + 1; i++) printf(" "); for (i = 0; i < priority + 1; i++) printf(" ");
printf("VALUE(pointer):%s\n", (char *)(p_node->value)); printf("VALUE(pointer):%s\n", (char *)(p_node->value));
} }
else if (!strcmp(p_node->type, "list")) { else if (p_node->type == LIST) {
for (i = 0; i < priority + 1; i++) printf(" "); for (i = 0; i < priority + 1; i++) printf(" ");
printf("VALUE(List):\n"); printf("VALUE(List):\n");
printListInfo((List *)p_node->value, priority + 2); printListInfo((List *)p_node->value, priority + 2);
@ -173,26 +173,26 @@ void printNode(Node *p_node) {
int i; int i;
printf("#NODE(location:%p, id:%llu){\n", p_node, p_node->id); printf("#NODE(location:%p, id:%llu){\n", p_node, p_node->id);
printf(" "); printf(" ");
printf("NEXT->%p / LAST->%p\n", p_node->next, p_node->last, p_node->if_malloc); printf("NEXT->%p / LAST->%p\n", p_node->next, p_node->last);
for (i = 0; i < 1; i++) printf(" "); for (i = 0; i < 1; i++) printf(" ");
printf("ifMalloc: "); printf("ifMalloc: ");
if (p_node->if_malloc) { if (p_node->if_malloc) {
printf("YES\n"); printf("YES\n");
for (i = 0; i < 1; i++) printf(" "); for (i = 0; i < 1; i++) printf(" ");
printf("Value(type: %s): ", p_node->type); printf("Value(type: %d): ", p_node->type);
if (!strcmp(p_node->type, "int")) { if (p_node->type == INT) {
printf("%d", *(int *)(p_node->value)); printf("%d", *(int *)(p_node->value));
} }
else if (!strcmp(p_node->type, "double")) { else if (p_node->type == DOUBLE) {
printf("%a\n", *(double *)(p_node->value)); printf("%a\n", *(double *)(p_node->value));
} }
else if (!strcmp(p_node->type, "string")) { else if (p_node->type == STRING) {
printf("%s\n", (char *)(p_node->value)); printf("%s\n", (char *)(p_node->value));
} }
else if (!strcmp(p_node->type, "pointer")) { else if (p_node->type == POINTER) {
printf("%s\n", (char *)(p_node->value)); printf("%p\n", (char *)(p_node->value));
} }
else if (!strcmp(p_node->type, "list")) { else if (p_node->type == LIST) {
printList((List *)p_node->value); printList((List *)p_node->value);
} }
} }
@ -206,7 +206,7 @@ Node *findByIntForNode(List *p_list, int target) {
Node *t_node; Node *t_node;
int *p_target = (int *)malloc(sizeof(int)); int *p_target = (int *)malloc(sizeof(int));
*p_target = target; *p_target = target;
t_node = findByValue(p_list, "int", p_target); t_node = findByValue(p_list, INT, p_target);
free(p_target); free(p_target);
return t_node; return t_node;
} }
@ -215,7 +215,7 @@ Node *findByDoubleForNode(List *p_list, double target) {
Node *t_node; Node *t_node;
double *p_target = (double *)malloc(sizeof(double)); double *p_target = (double *)malloc(sizeof(double));
*p_target = target; *p_target = target;
t_node = findByValue(p_list, "double", p_target); t_node = findByValue(p_list, DOUBLE, p_target);
free(p_target); free(p_target);
return t_node; return t_node;
} }
@ -223,21 +223,21 @@ Node *findByDoubleForNode(List *p_list, double target) {
Node *findByStringForNode(List *p_list, char *target) { Node *findByStringForNode(List *p_list, char *target) {
Node *t_node; Node *t_node;
char *p_temp = (char *)malloc(sizeof(char)*(strlen(target) + 1)); char *p_temp = (char *)malloc(sizeof(char)*(strlen(target) + 1));
strcpy_s(p_temp, sizeof(p_temp), target); strcpy(p_temp, target);
t_node = findByValue(p_list, "string", p_temp); t_node = findByValue(p_list, STRING, p_temp);
free(p_temp); free(p_temp);
return t_node; return t_node;
} }
Node *findByPointerForNode(List *p_list, void *target) { Node *findByPointerForNode(List *p_list, void *target) {
Node *t_node = findByValue(p_list, "pointer", target); Node *t_node = findByValue(p_list, POINTER, target);
return t_node; return t_node;
} }
int addValueForComplex(Node * p_node, char *type, void *value) { int addValueForComplex(Node * p_node, int type, void *value) {
List *c_list; List *c_list;
Node *c_node; Node *c_node;
if (!strcmp(p_node->type, "list")) { if (p_node->type == LIST) {
c_list = (List *)p_node->value; c_list = (List *)p_node->value;
c_node = initNode(); c_node = initNode();
initMallocValueForNode(c_node, type, value); initMallocValueForNode(c_node, type, value);
@ -248,38 +248,38 @@ int addValueForComplex(Node * p_node, char *type, void *value) {
} }
int addIntForComplex(Node *p_node, int temp) { int addIntForComplex(Node *p_node, int temp) {
if (!strcmp(p_node->type, "list")) { if (p_node->type == LIST) {
int *p_temp = (int *)malloc(sizeof(int)); int *p_temp = (int *)malloc(sizeof(int));
*p_temp = temp; *p_temp = temp;
addValueForComplex(p_node, "int", p_temp); addValueForComplex(p_node, INT, p_temp);
return 0; return 0;
} }
return -1; return -1;
} }
int addDoubleForComplex(Node *p_node, double temp) { int addDoubleForComplex(Node *p_node, double temp) {
if (!strcmp(p_node->type, "list")) { if (p_node->type == LIST) {
double *p_temp = (double *)malloc(sizeof(double)); double *p_temp = (double *)malloc(sizeof(double));
*p_temp = temp; *p_temp = temp;
addValueForComplex(p_node, "double", p_temp); addValueForComplex(p_node, DOUBLE, p_temp);
return 0; return 0;
} }
return -1; return -1;
} }
int addStringForComplex(Node *p_node, char *temp) { int addStringForComplex(Node *p_node, char *temp) {
if (!strcmp(p_node->type, "list")) { if (p_node->type == LIST) {
char *p_temp = (char *)malloc(sizeof(strlen(temp) + 1)); char *p_temp = (char *)malloc(sizeof(strlen(temp) + 1));
strcpy_s(p_temp, sizeof(p_temp), temp); strcpy(p_temp, temp);
addValueForComplex(p_node, "string", p_temp); addValueForComplex(p_node, STRING, p_temp);
return 0; return 0;
} }
return -1; return -1;
} }
int addPointerForComplex(Node *p_node, void *temp) { int addPointerForComplex(Node *p_node, void *temp) {
if (!strcmp(p_node->type, "list")) { if (p_node->type == LIST) {
addValueForComplex(p_node, "pointer", temp); addValueForComplex(p_node, POINTER, temp);
return 0; return 0;
} }
return -1; return -1;
@ -289,7 +289,7 @@ List *m_findByInt(List* p_list, int temp) {
int *p_temp = (int *)malloc(sizeof(int)); int *p_temp = (int *)malloc(sizeof(int));
List *t_list; List *t_list;
*p_temp = temp; *p_temp = temp;
t_list = mply_findByValue(p_list, "int", (void *)p_temp); t_list = mply_findByValue(p_list, INT, (void *)p_temp);
free(p_temp); free(p_temp);
return t_list; return t_list;
} }
@ -298,7 +298,7 @@ List *m_findByDouble(List* p_list, double temp) {
List *t_list; List *t_list;
double *p_temp = (double *)malloc(sizeof(double)); double *p_temp = (double *)malloc(sizeof(double));
*p_temp = temp; *p_temp = temp;
t_list = mply_findByValue(p_list, "double", (void *)p_temp); t_list = mply_findByValue(p_list, DOUBLE, (void *)p_temp);
free(p_temp); free(p_temp);
return t_list; return t_list;
} }
@ -306,14 +306,14 @@ List *m_findByDouble(List* p_list, double temp) {
List *m_findByString(List* p_list, char *temp) { List *m_findByString(List* p_list, char *temp) {
List *t_list; List *t_list;
char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1)); char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1));
strcpy_s(p_temp, sizeof(p_temp), temp); strcpy(p_temp, temp);
t_list = mply_findByValue(p_list, "string", (void *)p_temp); t_list = mply_findByValue(p_list, STRING, (void *)p_temp);
free(p_temp); free(p_temp);
return t_list; return t_list;
} }
List *m_findByPointer(List* p_list, void *temp) { List *m_findByPointer(List* p_list, void *temp) {
List *t_list = mply_findByValue(p_list, "double", (void *)temp); List *t_list = mply_findByValue(p_list, DOUBLE, (void *)temp);
return t_list; return t_list;
} }
@ -331,21 +331,21 @@ unsigned long long getIndexByNode(List *p_list, Node *p_node) {
List *m_findByIntForNode(List* p_list, int temp) { List *m_findByIntForNode(List* p_list, int temp) {
int *p_temp = (int *)malloc(sizeof(int)); int *p_temp = (int *)malloc(sizeof(int));
*p_temp = temp; *p_temp = temp;
return mply_findByValue(p_list, "int", (void *)p_temp); return mply_findByValue(p_list, INT, (void *)p_temp);
} }
List *m_findByDoubleForNode(List* p_list, double temp) { List *m_findByDoubleForNode(List* p_list, double temp) {
double *p_temp = (double *)malloc(sizeof(double)); double *p_temp = (double *)malloc(sizeof(double));
*p_temp = temp; *p_temp = temp;
return mply_findByValue(p_list, "double", (void *)p_temp); return mply_findByValue(p_list, DOUBLE, (void *)p_temp);
} }
List *m_findByStringForNode(List* p_list, char *temp) { List *m_findByStringForNode(List* p_list, char *temp) {
char *p_temp = (char *)malloc(sizeof(char) * (strlen(temp) + 1)); char *p_temp = (char *)malloc(sizeof(char) * (strlen(temp) + 1));
strcpy_s(p_temp, sizeof(p_temp), temp); strcpy(p_temp, temp);
return mply_findByValue(p_list, "string", (void *)p_temp); return mply_findByValue(p_list, STRING, (void *)p_temp);
} }
List *m_findByPointerForNode(List* p_list, void *temp) { List *m_findByPointerForNode(List* p_list, void *temp) {
return mply_findByValue(p_list, "pointer", (void *)temp); return mply_findByValue(p_list, POINTER, (void *)temp);
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -4,7 +4,7 @@ SNode *snodeWithInt(int temp) {
SNode *p_snode = initSNode(); SNode *p_snode = initSNode();
int *p_temp = (int *)malloc(sizeof(int)); int *p_temp = (int *)malloc(sizeof(int));
*p_temp = temp; *p_temp = temp;
initMallocValueForSNode(p_snode, "int", p_temp); initMallocValueForSNode(p_snode, INT, p_temp);
return p_snode; return p_snode;
} }
@ -12,40 +12,40 @@ SNode *snodeWithDouble(double temp) {
SNode *p_snode = initSNode(); SNode *p_snode = initSNode();
double *p_temp = (double *)malloc(sizeof(double)); double *p_temp = (double *)malloc(sizeof(double));
*p_temp = temp; *p_temp = temp;
initMallocValueForSNode(p_snode, "double", p_temp); initMallocValueForSNode(p_snode, DOUBLE, p_temp);
return p_snode; return p_snode;
} }
SNode *snodeWithString(char *temp) { SNode *snodeWithString(char *temp) {
SNode *p_snode = initSNode(); SNode *p_snode = initSNode();
char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1)); char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1));
strcpy_s(p_temp, strlen(p_temp), temp); strcpy(p_temp, temp);
initMallocValueForSNode(p_snode, "string", p_temp); initMallocValueForSNode(p_snode, STRING, p_temp);
return p_snode; return p_snode;
} }
SNode *snodeWithPointer(void *temp) { SNode *snodeWithPointer(void *temp) {
SNode *p_snode = initSNode(); SNode *p_snode = initSNode();
initMallocValueForSNode(p_snode, "pointer", temp); initMallocValueForSNode(p_snode, POINTER, temp);
return p_snode; return p_snode;
} }
int getValueByIntForSNode(SNode *p_snode) { int getValueByIntForSNode(SNode *p_snode) {
if (!strcmp(p_snode->type, "int")) return *(int *)p_snode->value; if (p_snode->type == INT) return *(int *)p_snode->value;
else return -1; else return -1;
} }
double getValueByDoubleForSNode(SNode *p_snode) { double getValueByDoubleForSNode(SNode *p_snode) {
if (!strcmp(p_snode->type, "double")) return *(double *)p_snode->value; if (p_snode->type == DOUBLE) return *(double *)p_snode->value;
else return -1; else return -1;
} }
char *getValueByStringForSNode(SNode *p_snode) { char *getValueByStringForSNode(SNode *p_snode) {
if (!strcmp(p_snode->type, "int")) return (char *)p_snode->value; if (p_snode->type == STRING) return (char *)p_snode->value;
else return NULL; else return NULL;
} }
void *getValueByPointerForSNode(SNode *p_snode) { void *getValueByPointerForSNode(SNode *p_snode) {
if (!strcmp(p_snode->type, "int")) return (void *)p_snode->value; if (p_snode->type == POINTER) return (void *)p_snode->value;
else return NULL; else return NULL;
} }

Binary file not shown.

3
test.c
View File

@ -2,7 +2,6 @@
int list(void) { int list(void) {
int i;
List *t_list, *m_list;; List *t_list, *m_list;;
init_rand(); init_rand();
safeModeForNode(1); safeModeForNode(1);
@ -20,7 +19,7 @@ int list(void) {
insertInTail(t_list,t_node); insertInTail(t_list,t_node);
initMalllocValue(t_node,(void *)"there");*/ initMalllocValue(t_node,(void *)"there");*/
for (i = 0; i < 12; i++) { for (int i = 0; i < 12; i++) {
insertInHead(t_list, nodeWithInt(i)); insertInHead(t_list, nodeWithInt(i));
insertInTail(t_list, nodeWithInt(i)); insertInTail(t_list, nodeWithInt(i));
} }

1
test.h
View File

@ -13,4 +13,3 @@ int list(void);
int tree(void); int tree(void);
#endif // TEST_H #endif // TEST_H

View File

@ -1,8 +1,8 @@
#include"tree.h" #include"tree.h"
unsigned long long target_id = 0; static unsigned long long target_id = 0;
TNode *target_value_id = NULL; static TNode *target_value_id = NULL;
int if_safeModeForTree = 0; static int if_safeModeForTree = 0;
int safeModeForTree(int ifon) { int safeModeForTree(int ifon) {
if (ifon == 1) { if (ifon == 1) {
@ -36,21 +36,21 @@ TNode *initTNode(void) {
p_tnode->father = NULL; p_tnode->father = NULL;
p_tnode->if_malloc = 0; p_tnode->if_malloc = 0;
p_tnode->value = NULL; p_tnode->value = NULL;
p_tnode->type = NULL; p_tnode->type = VOID;
p_tnode->home = initList(); p_tnode->home = initList();
p_tnode->room = NULL; p_tnode->room = NULL;
if (if_safeModeForTree) { if (if_safeModeForTree) {
if (if_safeModeForNode) { if (if_safeModeForNode) {
if_safeModeForNode = 0; if_safeModeForNode = 0;
s_node = initNode(); s_node = initNode();
initMallocValueForNode(s_node, "pointer", (void *)p_tnode); initMallocValueForNode(s_node, POINTER, (void *)p_tnode);
insertInTail(tnode_list, s_node); insertInTail(tnode_list, s_node);
if_safeModeForNode = 1; if_safeModeForNode = 1;
} }
else else
{ {
s_node = initNode(); s_node = initNode();
initMallocValueForNode(s_node, "pointer", (void *)p_tnode); initMallocValueForNode(s_node, POINTER, (void *)p_tnode);
insertInTail(tnode_list, s_node); insertInTail(tnode_list, s_node);
} }
} }
@ -66,21 +66,21 @@ Tree *initTree(void) {
if (if_safeModeForNode) { if (if_safeModeForNode) {
if_safeModeForNode = 0; if_safeModeForNode = 0;
s_node = initNode(); s_node = initNode();
initMallocValueForNode(s_node, "pointer", (void *)p_tree); initMallocValueForNode(s_node, POINTER, (void *)p_tree);
if_safeModeForNode = 1; if_safeModeForNode = 1;
insertInTail(tree_list, s_node); insertInTail(tree_list, s_node);
} }
else else
{ {
s_node = initNode(); s_node = initNode();
initMallocValueForNode(s_node, "pointer", (void *)p_tree); initMallocValueForNode(s_node, POINTER, (void *)p_tree);
insertInTail(tree_list, s_node); insertInTail(tree_list, s_node);
} }
} }
return p_tree; return p_tree;
} }
int *initMallocValueForTNode(TNode *p_tnode, char *type, void *value) { int *initMallocValueForTNode(TNode *p_tnode, int type, void *value) {
p_tnode->type = type; p_tnode->type = type;
p_tnode->value = value; p_tnode->value = value;
p_tnode->if_malloc = 1; p_tnode->if_malloc = 1;
@ -89,7 +89,7 @@ int *initMallocValueForTNode(TNode *p_tnode, char *type, void *value) {
int addChildInLeft(TNode *f_tnode, TNode *c_tnode) { int addChildInLeft(TNode *f_tnode, TNode *c_tnode) {
Node *p_node = initNode(); Node *p_node = initNode();
initMallocValueForNode(p_node, "pointer", c_tnode); initMallocValueForNode(p_node, POINTER, c_tnode);
insertInHead(f_tnode->home, p_node); insertInHead(f_tnode->home, p_node);
c_tnode->father = f_tnode; c_tnode->father = f_tnode;
c_tnode->room = p_node; c_tnode->room = p_node;
@ -99,7 +99,7 @@ int addChildInLeft(TNode *f_tnode, TNode *c_tnode) {
int addChildInRight(TNode *f_tnode, TNode *c_tnode) { int addChildInRight(TNode *f_tnode, TNode *c_tnode) {
Node *p_node = initNode(); Node *p_node = initNode();
initMallocValueForNode(p_node, "pointer", c_tnode); initMallocValueForNode(p_node, POINTER, c_tnode);
insertInTail(f_tnode->home, p_node); insertInTail(f_tnode->home, p_node);
c_tnode->father = f_tnode; c_tnode->father = f_tnode;
c_tnode->room = p_node; c_tnode->room = p_node;
@ -155,8 +155,8 @@ TNode *getChildById(TNode *p_tnode, unsigned long long id) {
return NULL; return NULL;
} }
int _dogetChildById(const char *type, void *value) { int _dogetChildById(int type, void *value) {
if (!strcmp(type, "pointer")) { if (type == POINTER) {
TNode *p_tode = (TNode *)value; TNode *p_tode = (TNode *)value;
if (p_tode->id == target_id) { if (p_tode->id == target_id) {
target_value_id = p_tode; target_value_id = p_tode;
@ -166,12 +166,11 @@ int _dogetChildById(const char *type, void *value) {
return 0; return 0;
} }
char *target_type = NULL; static int target_type = VOID;
void *target_value = NULL; static void *target_value = NULL;
TNode *target_value_value = NULL; static TNode *target_value_value = NULL;
int _dogetChildByValue(const char *type, void *value);
TNode *getChildByValue(TNode *p_tnode, char *type, void *value) { TNode *getChildByValue(TNode *p_tnode, int type, void *value) {
List *p_home = p_tnode->home; List *p_home = p_tnode->home;
target_value = value; target_value = value;
target_type = type; target_type = type;
@ -183,17 +182,17 @@ TNode *getChildByValue(TNode *p_tnode, char *type, void *value) {
return NULL; return NULL;
} }
int _dogetChildByValue(const char *type, void *value) { int _dogetChildByValue(int type, void *value) {
if (!strcmp(type, target_type)) { if (type == target_type) {
TNode *p_tode = (TNode *)value; TNode *p_tode = (TNode *)value;
if (!strcmp((char *)target_value, "int")) { if (target_type == INT) {
if (*(int *)p_tode->value == *(int *)target_value) if (*(int *)p_tode->value == *(int *)target_value)
{ {
target_value_value = p_tode; target_value_value = p_tode;
return -1; return -1;
} }
} }
else if (!strcmp((char *)target_value, "double")) else if (target_type == DOUBLE)
{ {
if (*(double *)p_tode->value == *(double *)target_value) if (*(double *)p_tode->value == *(double *)target_value)
{ {
@ -201,7 +200,7 @@ int _dogetChildByValue(const char *type, void *value) {
return -1; return -1;
} }
} }
else if (!strcmp((char *)target_value, "string")) else if (target_type == STRING)
{ {
if (!strcmp((char *)p_tode->value, (char *)target_value)) if (!strcmp((char *)p_tode->value, (char *)target_value))
{ {
@ -209,7 +208,7 @@ int _dogetChildByValue(const char *type, void *value) {
return -1; return -1;
} }
} }
else if (!strcmp((char *)target_value, "pointer")) else if (target_type == POINTER)
{ {
if (p_tode->value == target_value) if (p_tode->value == target_value)
{ {
@ -235,7 +234,7 @@ int removeChildById(TNode *p_tnode, unsigned long long id) {
return -1; return -1;
} }
int removeChildByValue(TNode *p_tnode, char *type, void *value) { int removeChildByValue(TNode *p_tnode, int type, void *value) {
TNode *t_tnode = getChildByValue(p_tnode, type, value); TNode *t_tnode = getChildByValue(p_tnode, type, value);
if (t_tnode != NULL) { if (t_tnode != NULL) {
TNode *p_fnode = t_tnode->father; TNode *p_fnode = t_tnode->father;
@ -376,8 +375,8 @@ int releaseTNode(TNode *p_tnode) {
if (p_tnode->father != NULL) { if (p_tnode->father != NULL) {
removeChildById(p_tnode->father, p_tnode->id); removeChildById(p_tnode->father, p_tnode->id);
} }
if (strcmp(p_tnode->type, "pointer")) { if (p_tnode->type != POINTER) {
if (!strcmp(p_tnode->type, "list")) { if (p_tnode->type == LIST) {
releaseList((List *)p_tnode->value); releaseList((List *)p_tnode->value);
} }
else { else {
@ -385,7 +384,7 @@ int releaseTNode(TNode *p_tnode) {
} }
} }
p_tnode->value = NULL; p_tnode->value = NULL;
p_tnode->type = NULL; p_tnode->type = VOID;
p_tnode->id = 0; p_tnode->id = 0;
p_tnode->if_malloc = 0; p_tnode->if_malloc = 0;
free(p_tnode); free(p_tnode);
@ -416,8 +415,8 @@ int releaseOnlyTree(Tree *p_tree) {
int releaseOnlyTNode(TNode *p_tnode) { int releaseOnlyTNode(TNode *p_tnode) {
releaseList(p_tnode->home); releaseList(p_tnode->home);
if (p_tnode->if_malloc) { if (p_tnode->if_malloc) {
if (strcmp(p_tnode->type, "pointer")) { if (p_tnode->type != STRING) {
if (!strcmp(p_tnode->type, "list")) { if (p_tnode->type == LIST) {
releaseList((List *)p_tnode->value); releaseList((List *)p_tnode->value);
} }
else { else {
@ -426,7 +425,7 @@ int releaseOnlyTNode(TNode *p_tnode) {
} }
} }
p_tnode->value = NULL; p_tnode->value = NULL;
p_tnode->type = NULL; p_tnode->type = VOID;
p_tnode->id = 0; p_tnode->id = 0;
p_tnode->if_malloc = 0; p_tnode->if_malloc = 0;
free(p_tnode); free(p_tnode);

View File

@ -1,19 +1,9 @@
<<<<<<< HEAD
//
// tree.h
// ZE-Standard-Libraries
//
// Created by 胡一兵 on 2018/6/11.
// Copyright © 2018年 ZE. All rights reserved.
//
#ifndef tree_h
#define tree_h
#include <stdio.h>
#include "../list/list_expand.h"
#ifndef TREE_H #ifndef TREE_H
#define TREE_H #define TREE_H
#include <stdio.h>
#include "../list/list_expand.h"
typedef struct tree_node typedef struct tree_node
{ {
unsigned long long id; unsigned long long id;
@ -21,7 +11,7 @@ typedef struct tree_node
struct tree_node *father; struct tree_node *father;
Node *room; Node *room;
unsigned long long child_num; unsigned long long child_num;
char *type; int type;
void *value; void *value;
int if_malloc; int if_malloc;
}TNode; }TNode;
@ -38,7 +28,7 @@ int releaseAllForTree(void);
TNode *initTNode(void); TNode *initTNode(void);
Tree *initTree(void); Tree *initTree(void);
int *initMallocValueForTNode(TNode *p_tnode, char *type, void *value); int *initMallocValueForTNode(TNode *p_tnode, int type, void *value);
int addChildInLeft(TNode *f_tnode, TNode *c_tnode); int addChildInLeft(TNode *f_tnode, TNode *c_tnode);
int addChildInRight(TNode *f_tnode, TNode *c_tnode); int addChildInRight(TNode *f_tnode, TNode *c_tnode);
@ -47,30 +37,30 @@ TNode *getBrotherInRight(TNode *p_node);
int removeChildInLeft(TNode *p_tnode); int removeChildInLeft(TNode *p_tnode);
int removeChildInRight(TNode *p_tnode); int removeChildInRight(TNode *p_tnode);
TNode *getChildById(TNode *p_tnode, unsigned long long id); TNode *getChildById(TNode *p_tnode, unsigned long long id);
TNode *getChildByValue(TNode *p_tnode, char *type, void *value); TNode *getChildByValue(TNode *p_tnode, int type, void *value);
TNode *getChildByIndex(TNode *p_tnode, unsigned long long index); TNode *getChildByIndex(TNode *p_tnode, unsigned long long index);
unsigned long long getIndexByChild(TNode *f_tnode, TNode *c_tnode); unsigned long long getIndexByChild(TNode *f_tnode, TNode *c_tnode);
int removeChildById(TNode *p_tnode, unsigned long long id); int removeChildById(TNode *p_tnode, unsigned long long id);
int removeChildByIndex(TNode *p_tnode, unsigned long long index); int removeChildByIndex(TNode *p_tnode, unsigned long long index);
int removeChildByValue(TNode *p_tnode, char *type, void *value); int removeChildByValue(TNode *p_tnode, int type, void *value);
int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height));
int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height));
int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height));
static int _dogetChildById(const char *type, void *value); int _dogetChildById(int type, void *value);
static int _dogetChildByValue(const char *type, void *value); int _dogetChildByValue(int type, void *value);
static int _doreleaseTree(TNode *p_tnode, unsigned long long height); int _doreleaseTree(TNode *p_tnode, unsigned long long height);
static int _doTreeThroughDown(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)); int _doTreeThroughDown(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height));
static int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)); int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height));
int releaseTree(Tree *p_tree); int releaseTree(Tree *p_tree);
int releaseOnlyTree(Tree *p_tree); int releaseOnlyTree(Tree *p_tree);
int releaseTNode(TNode *p_tnode); int releaseTNode(TNode *p_tnode);
static int releaseOnlyTNode(TNode *p_tnode); int releaseOnlyTNode(TNode *p_tnode);
int setRoot(Tree *p_tree, TNode *p_tnode); int setRoot(Tree *p_tree, TNode *p_tnode);
static char *target_type; static int target_type;
static void *target_value; static void *target_value;
static TNode *target_value_value; static TNode *target_value_value;
@ -83,4 +73,3 @@ static int if_safeModeForTree;
#endif #endif
>>>>>>> 30b15314d8173f7cdfb24ab7f9250435ae2db90c

View File

@ -4,7 +4,7 @@ TNode *tnodeWithInt(int temp) {
TNode *p_tnode = initTNode(); TNode *p_tnode = initTNode();
int *p_temp = (int *)malloc(sizeof(int)); int *p_temp = (int *)malloc(sizeof(int));
*p_temp = temp; *p_temp = temp;
initMallocValueForTNode(p_tnode, "int", p_temp); initMallocValueForTNode(p_tnode, INT, p_temp);
return p_tnode; return p_tnode;
} }
@ -12,46 +12,46 @@ TNode *tnodeWithDouble(double temp) {
TNode *p_tnode = initTNode(); TNode *p_tnode = initTNode();
double *p_temp = (double *)malloc(sizeof(double)); double *p_temp = (double *)malloc(sizeof(double));
*p_temp = temp; *p_temp = temp;
initMallocValueForTNode(p_tnode, "double", p_temp); initMallocValueForTNode(p_tnode, DOUBLE, p_temp);
return p_tnode; return p_tnode;
} }
TNode *tnodeWithString(char *temp) { TNode *tnodeWithString(char *temp) {
TNode *p_tnode = initTNode(); TNode *p_tnode = initTNode();
char *p_temp = (char *)malloc(sizeof(temp)); char *p_temp = (char *)malloc(sizeof(temp));
strcpy_s(p_temp, sizeof(p_temp), temp); strcpy(p_temp, temp);
initMallocValueForTNode(p_tnode, "double", p_temp); initMallocValueForTNode(p_tnode, STRING, p_temp);
return p_tnode; return p_tnode;
} }
TNode *tnodeWithPointer(void *temp) { TNode *tnodeWithPointer(void *temp) {
TNode *p_tnode = initTNode(); TNode *p_tnode = initTNode();
initMallocValueForTNode(p_tnode, "pointer", temp); initMallocValueForTNode(p_tnode, POINTER, temp);
return p_tnode; return p_tnode;
} }
int getValueByIntForTree(TNode *p_tnode) { int getValueByIntForTree(TNode *p_tnode) {
if (!strcmp(p_tnode->type, "int")) { if (p_tnode->type == INT) {
return *(int *)p_tnode->value; return *(int *)p_tnode->value;
} }
return -1; return -1;
} }
double getValueByDoubleForTree(TNode *p_tnode) { double getValueByDoubleForTree(TNode *p_tnode) {
if (!strcmp(p_tnode->type, "double")) { if (p_tnode->type == DOUBLE) {
return *(double *)p_tnode->value; return *(double *)p_tnode->value;
} }
return -1; return -1;
} }
char *getValueByStringForTree(TNode *p_tnode) { char *getValueByStringForTree(TNode *p_tnode) {
if (!strcmp(p_tnode->type, "string")) { if (p_tnode->type == STRING) {
return (char *)p_tnode->value; return (char *)p_tnode->value;
} }
return NULL; return NULL;
} }
void *getValueByPointerForTree(TNode *p_tnode) { void *getValueByPointerForTree(TNode *p_tnode) {
if (!strcmp(p_tnode->type, "pointer")) { if (p_tnode->type == POINTER) {
return p_tnode->value; return p_tnode->value;
} }
return NULL; return NULL;
@ -70,17 +70,17 @@ int printTNode(TNode *p_tnode, int priority) {
if (p_tnode->if_malloc) { if (p_tnode->if_malloc) {
printf("YES\n"); printf("YES\n");
for (i = 0; i < priority + 1; i++) printf(" "); for (i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type); printf("Value(type: %d): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) { if (p_tnode->type == INT) {
printf("%d\n", *(int *)(p_tnode->value)); printf("%d\n", *(int *)(p_tnode->value));
} }
else if (!strcmp(p_tnode->type, "double")) { else if (p_tnode->type == DOUBLE) {
printf("%a\n", *(double *)(p_tnode->value)); printf("%a\n", *(double *)(p_tnode->value));
} }
else if (!strcmp(p_tnode->type, "string")) { else if (p_tnode->type == STRING) {
printf("%s\n", (char *)(p_tnode->value)); printf("%s\n", (char *)(p_tnode->value));
} }
else if (!strcmp(p_tnode->type, "pointer")) { else if (p_tnode->type == POINTER) {
printf("%p\n", (char *)(p_tnode->value)); printf("%p\n", (char *)(p_tnode->value));
} }
} }
@ -108,17 +108,17 @@ int printTNodeWithHome(TNode *p_tnode,int priority) {
if (p_tnode->if_malloc) { if (p_tnode->if_malloc) {
printf("YES\n"); printf("YES\n");
for (i = 0; i < priority + 1; i++) printf(" "); for (i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type); printf("Value(type: %d): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) { if (p_tnode->type == INT) {
printf("%d\n", *(int *)(p_tnode->value)); printf("%d\n", *(int *)(p_tnode->value));
} }
else if (!strcmp(p_tnode->type, "double")) { else if (p_tnode->type == DOUBLE) {
printf("%a\n", *(double *)(p_tnode->value)); printf("%a\n", *(double *)(p_tnode->value));
} }
else if (!strcmp(p_tnode->type, "string")) { else if (p_tnode->type == STRING) {
printf("%s\n", (char *)(p_tnode->value)); printf("%s\n", (char *)(p_tnode->value));
} }
else if (!strcmp(p_tnode->type, "pointer")) { else if (p_tnode->type == POINTER) {
printf("%p\n", (char *)(p_tnode->value)); printf("%p\n", (char *)(p_tnode->value));
} }
} }
@ -164,17 +164,17 @@ int printTNodeWithFamily(TNode *p_tnode, int priority) {
if (p_tnode->if_malloc) { if (p_tnode->if_malloc) {
printf("YES\n"); printf("YES\n");
for (i = 0; i < priority + 1; i++) printf(" "); for (i = 0; i < priority + 1; i++) printf(" ");
printf("Value(type: %s): ", p_tnode->type); printf("Value(type: %d): ", p_tnode->type);
if (!strcmp(p_tnode->type, "int")) { if (p_tnode->type == INT) {
printf("%d\n", *(int *)(p_tnode->value)); printf("%d\n", *(int *)(p_tnode->value));
} }
else if (!strcmp(p_tnode->type, "double")) { else if (p_tnode->type == DOUBLE) {
printf("%a\n", *(double *)(p_tnode->value)); printf("%a\n", *(double *)(p_tnode->value));
} }
else if (!strcmp(p_tnode->type, "string")) { else if (p_tnode->type == STRING) {
printf("%s\n", (char *)(p_tnode->value)); printf("%s\n", (char *)(p_tnode->value));
} }
else if (!strcmp(p_tnode->type, "pointer")) { else if (p_tnode->type == POINTER) {
printf("%p\n", (char *)(p_tnode->value)); printf("%p\n", (char *)(p_tnode->value));
} }
} }

21
type/type.h Normal file
View File

@ -0,0 +1,21 @@
//
// type.h
// ZE-Standard-Libraries
//
// Created by 胡一兵 on 2018/7/23.
// Copyright © 2018年 ZE. All rights reserved.
//
#ifndef type_h
#define type_h
#define VOID 0
#define INT 1
#define DOUBLE 2
#define STRING 3
#define POINTER 4
#define LIST 5
#endif /* type_h */