ZE-Standard-Libraries/tree/tree_expand.c

228 lines
5.8 KiB
C
Raw Normal View History

2018-06-13 08:04:51 +00:00
#include "tree_expand.h"
TNode *tnodeWithInt(int temp) {
TNode *p_tnode = initTNode();
int *p_temp = (int *)malloc(sizeof(int));
*p_temp = temp;
2018-07-23 04:59:16 +00:00
initMallocValueForTNode(p_tnode, INT, p_temp);
2018-06-13 08:04:51 +00:00
return p_tnode;
}
TNode *tnodeWithDouble(double temp) {
TNode *p_tnode = initTNode();
double *p_temp = (double *)malloc(sizeof(double));
*p_temp = temp;
2018-07-23 04:59:16 +00:00
initMallocValueForTNode(p_tnode, DOUBLE, p_temp);
2018-06-13 08:04:51 +00:00
return p_tnode;
}
TNode *tnodeWithString(char *temp) {
TNode *p_tnode = initTNode();
char *p_temp = (char *)malloc(sizeof(temp));
2018-07-23 04:59:16 +00:00
strcpy(p_temp, temp);
initMallocValueForTNode(p_tnode, STRING, p_temp);
2018-06-13 08:04:51 +00:00
return p_tnode;
}
TNode *tnodeWithPointer(void *temp) {
TNode *p_tnode = initTNode();
2018-07-23 04:59:16 +00:00
initMallocValueForTNode(p_tnode, POINTER, temp);
2018-06-13 08:04:51 +00:00
return p_tnode;
}
int getValueByIntForTree(TNode *p_tnode) {
2018-07-23 04:59:16 +00:00
if (p_tnode->type == INT) {
2018-06-13 08:04:51 +00:00
return *(int *)p_tnode->value;
}
return -1;
}
double getValueByDoubleForTree(TNode *p_tnode) {
2018-07-23 04:59:16 +00:00
if (p_tnode->type == DOUBLE) {
2018-06-13 08:04:51 +00:00
return *(double *)p_tnode->value;
}
return -1;
}
char *getValueByStringForTree(TNode *p_tnode) {
2018-07-23 04:59:16 +00:00
if (p_tnode->type == STRING) {
2018-06-13 08:04:51 +00:00
return (char *)p_tnode->value;
}
return NULL;
}
void *getValueByPointerForTree(TNode *p_tnode) {
2018-07-23 04:59:16 +00:00
if (p_tnode->type == POINTER) {
2018-06-13 08:04:51 +00:00
return p_tnode->value;
}
return NULL;
}
int printTNode(TNode *p_tnode, int priority) {
int i;
2018-06-13 08:32:39 +00:00
if (p_tnode != NULL) {
for (i = 0; i < priority; i++) printf(" ");
2018-06-13 08:32:39 +00:00
if (priority == 0) printf("###");
else printf("#");
2018-07-30 10:18:58 +00:00
printf("TNode(id: %s)\n", s_idToASCIIString(p_tnode->s_id));
for (i = 0; i < priority + 1; i++) printf(" ");
2018-06-13 08:32:39 +00:00
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (i = 0; i < priority + 1; i++) printf(" ");
2018-07-23 04:59:16 +00:00
printf("Value(type: %d): ", p_tnode->type);
if (p_tnode->type == INT) {
2018-06-13 08:32:39 +00:00
printf("%d\n", *(int *)(p_tnode->value));
}
2018-07-23 04:59:16 +00:00
else if (p_tnode->type == DOUBLE) {
2018-06-13 08:32:39 +00:00
printf("%a\n", *(double *)(p_tnode->value));
}
2018-07-23 04:59:16 +00:00
else if (p_tnode->type == STRING) {
2018-06-13 08:32:39 +00:00
printf("%s\n", (char *)(p_tnode->value));
}
2018-07-23 04:59:16 +00:00
else if (p_tnode->type == POINTER) {
2018-06-13 08:32:39 +00:00
printf("%p\n", (char *)(p_tnode->value));
}
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
else printf("NO\n");
if (p_tnode->child_num > 0) {
for (i = 0; i < priority + 1; i++) printf(" ");
2018-06-13 08:32:39 +00:00
printf("Child number: %llu\n", p_tnode->child_num);
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
return 0;
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
return -1;
2018-06-13 08:04:51 +00:00
}
int printTNodeWithHome(TNode *p_tnode,int priority) {
int i;
List *p_home;
Node *p_node;
2018-06-13 08:32:39 +00:00
if (p_tnode != NULL) {
if (priority == 0) printf("###");
else printf("#");
2018-07-30 10:18:58 +00:00
printf("TNode(id: %s)\n", s_idToASCIIString(p_tnode->s_id));
for (i = 0; i < priority + 1; i++) printf(" ");
2018-06-13 08:32:39 +00:00
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (i = 0; i < priority + 1; i++) printf(" ");
2018-07-23 04:59:16 +00:00
printf("Value(type: %d): ", p_tnode->type);
if (p_tnode->type == INT) {
2018-06-13 08:32:39 +00:00
printf("%d\n", *(int *)(p_tnode->value));
}
2018-07-23 04:59:16 +00:00
else if (p_tnode->type == DOUBLE) {
2018-06-13 08:32:39 +00:00
printf("%a\n", *(double *)(p_tnode->value));
}
2018-07-23 04:59:16 +00:00
else if (p_tnode->type == STRING) {
2018-06-13 08:32:39 +00:00
printf("%s\n", (char *)(p_tnode->value));
}
2018-07-23 04:59:16 +00:00
else if (p_tnode->type == POINTER) {
2018-06-13 08:32:39 +00:00
printf("%p\n", (char *)(p_tnode->value));
}
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
else printf("NO\n");
if (p_tnode->father != NULL) {
for (i = 0; i < priority + 1; i++) printf(" ");
2018-07-30 10:18:58 +00:00
printf("Father id: %s\n", s_idToASCIIString(p_tnode->father->s_id));
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
else
{
for (i = 0; i < priority + 1; i++) printf(" ");
2018-06-13 08:32:39 +00:00
printf("Father: NO\n");
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
if (p_tnode->child_num > 0) {
for (i = 0; i < priority + 1; i++) printf(" ");
2018-06-13 08:32:39 +00:00
printf("Child(number: %llu):\n", p_tnode->child_num);
}
2018-06-13 08:04:51 +00:00
p_home = p_tnode->home;
p_node = p_home->head;
2018-06-13 08:32:39 +00:00
while (p_node != NULL) {
printTNode((TNode *)p_node->value, priority + 2);
p_node = p_node->next;
}
return 0;
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
return -1;
2018-06-13 08:04:51 +00:00
}
int printTNodeWithFamily(TNode *p_tnode, int priority) {
int i;
List *p_home;
Node *p_node;
2018-06-13 08:32:39 +00:00
if (p_tnode != NULL) {
for (i = 0; i < priority; i++) printf(" ");
2018-06-13 08:32:39 +00:00
if (priority == 0) printf("###");
else printf("#");
2018-07-30 10:18:58 +00:00
printf("TNode(id: %s)\n", s_idToASCIIString(p_tnode->s_id));
for (i = 0; i < priority + 1; i++) printf(" ");
2018-06-13 08:32:39 +00:00
printf("ifMalloc: ");
if (p_tnode->if_malloc) {
printf("YES\n");
for (i = 0; i < priority + 1; i++) printf(" ");
2018-07-23 04:59:16 +00:00
printf("Value(type: %d): ", p_tnode->type);
if (p_tnode->type == INT) {
2018-06-13 08:32:39 +00:00
printf("%d\n", *(int *)(p_tnode->value));
}
2018-07-23 04:59:16 +00:00
else if (p_tnode->type == DOUBLE) {
2018-06-13 08:32:39 +00:00
printf("%a\n", *(double *)(p_tnode->value));
}
2018-07-23 04:59:16 +00:00
else if (p_tnode->type == STRING) {
2018-06-13 08:32:39 +00:00
printf("%s\n", (char *)(p_tnode->value));
}
2018-07-23 04:59:16 +00:00
else if (p_tnode->type == POINTER) {
2018-06-13 08:32:39 +00:00
printf("%p\n", (char *)(p_tnode->value));
}
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
else printf("NO\n");
if (p_tnode->father != NULL) {
for (i = 0; i < priority + 1; i++) printf(" ");
2018-07-30 10:18:58 +00:00
printf("Father id: %s\n", s_idToASCIIString(p_tnode->father->s_id));
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
else
{
for (i = 0; i < priority + 1; i++) printf(" ");
2018-06-13 08:32:39 +00:00
printf("Father: NO\n");
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
if (p_tnode->child_num > 0) {
for (i = 0; i < priority + 1; i++) printf(" ");
2018-06-13 08:32:39 +00:00
printf("Child(number: %llu):\n", p_tnode->child_num);
}
2018-06-13 08:04:51 +00:00
p_home = p_tnode->home;
p_node = p_home->head;
2018-06-13 08:32:39 +00:00
while (p_node != NULL) {
printTNodeWithFamily((TNode *)p_node->value, priority + 2);
p_node = p_node->next;
}
return 0;
2018-06-13 08:04:51 +00:00
}
2018-06-13 08:32:39 +00:00
return -1;
2018-06-13 08:04:51 +00:00
}
int printTree(Tree *p_tree) {
printf("###");
2018-07-30 10:18:58 +00:00
printf("Tree(id: %s)",s_idToASCIIString(p_tree->s_id));
2018-06-13 08:04:51 +00:00
printTNodeWithFamily(p_tree->root,0);
return 0;
2018-07-23 05:47:31 +00:00
}
static unsigned long long tnodes_size = 0LL;
unsigned long long calTreeMemory(Tree *p_tree){
tnodes_size = 0LL;
TreeThroughDown(p_tree, _doCalTreeMemory);
return sizeof(p_tree) + tnodes_size;
}
int _doCalTreeMemory(TNode *p_tnode, unsigned long long height){
tnodes_size += sizeof(p_tnode) + sizeof(*p_tnode->value);
return 0;
}