summaryrefslogtreecommitdiffstats
path: root/libcnary/node.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2018-11-28 03:18:39 +0100
committerGravatar Nikias Bassen2018-11-28 03:18:39 +0100
commit74536d7a6737e2a4afb006106f1ce30a9a5aeea5 (patch)
tree54c7be420a2d5bf13b96fee7ae5fc3ffca3ad6c0 /libcnary/node.c
parent2e67a01ba65ee3ac2a028dab2d636227616673ef (diff)
downloadlibplist-74536d7a6737e2a4afb006106f1ce30a9a5aeea5.tar.gz
libplist-74536d7a6737e2a4afb006106f1ce30a9a5aeea5.tar.bz2
libcnary: Remove redundant members from node_t struct
Diffstat (limited to 'libcnary/node.c')
-rw-r--r--libcnary/node.c39
1 files changed, 17 insertions, 22 deletions
diff --git a/libcnary/node.c b/libcnary/node.c
index 3bd6975..4b550dd 100644
--- a/libcnary/node.c
+++ b/libcnary/node.c
@@ -54,14 +54,11 @@ node_t* node_create(node_t* parent, void* data) {
54 memset(node, '\0', sizeof(node_t)); 54 memset(node, '\0', sizeof(node_t));
55 55
56 node->data = data; 56 node->data = data;
57 node->depth = 0;
58 node->next = NULL; 57 node->next = NULL;
59 node->prev = NULL; 58 node->prev = NULL;
60 node->count = 0; 59 node->count = 0;
61 node->isLeaf = TRUE;
62 node->isRoot = TRUE;
63 node->parent = NULL; 60 node->parent = NULL;
64 node->children = node_list_create(); 61 node->children = NULL;
65 62
66 // Pass NULL to create a root node 63 // Pass NULL to create a root node
67 if(parent != NULL) { 64 if(parent != NULL) {
@@ -80,12 +77,9 @@ node_t* node_create(node_t* parent, void* data) {
80 77
81int node_attach(node_t* parent, node_t* child) { 78int node_attach(node_t* parent, node_t* child) {
82 if (!parent || !child) return -1; 79 if (!parent || !child) return -1;
83 child->isLeaf = TRUE;
84 child->isRoot = FALSE;
85 child->parent = parent; 80 child->parent = parent;
86 child->depth = parent->depth + 1; 81 if(!parent->children) {
87 if(parent->isLeaf == TRUE) { 82 parent->children = node_list_create();
88 parent->isLeaf = FALSE;
89 } 83 }
90 int res = node_list_add(parent->children, child); 84 int res = node_list_add(parent->children, child);
91 if (res == 0) { 85 if (res == 0) {
@@ -106,12 +100,9 @@ int node_detach(node_t* parent, node_t* child) {
106int node_insert(node_t* parent, unsigned int node_index, node_t* child) 100int node_insert(node_t* parent, unsigned int node_index, node_t* child)
107{ 101{
108 if (!parent || !child) return -1; 102 if (!parent || !child) return -1;
109 child->isLeaf = TRUE;
110 child->isRoot = FALSE;
111 child->parent = parent; 103 child->parent = parent;
112 child->depth = parent->depth + 1; 104 if(!parent->children) {
113 if(parent->isLeaf == TRUE) { 105 parent->children = node_list_create();
114 parent->isLeaf = FALSE;
115 } 106 }
116 int res = node_list_insert(parent->children, node_index, child); 107 int res = node_list_insert(parent->children, node_index, child);
117 if (res == 0) { 108 if (res == 0) {
@@ -120,33 +111,37 @@ int node_insert(node_t* parent, unsigned int node_index, node_t* child)
120 return res; 111 return res;
121} 112}
122 113
123void node_debug(node_t* node) { 114static void _node_debug(node_t* node, unsigned int depth) {
124 unsigned int i = 0; 115 unsigned int i = 0;
125 node_t* current = NULL; 116 node_t* current = NULL;
126 node_iterator_t* iter = NULL; 117 node_iterator_t* iter = NULL;
127 for(i = 0; i < node->depth; i++) { 118 for(i = 0; i < depth; i++) {
128 printf("\t"); 119 printf("\t");
129 } 120 }
130 if(node->isRoot) { 121 if(!node->parent) {
131 printf("ROOT\n"); 122 printf("ROOT\n");
132 } 123 }
133 124
134 if(node->isLeaf && !node->isRoot) { 125 if(!node->children && node->parent) {
135 printf("LEAF\n"); 126 printf("LEAF\n");
136
137 } else { 127 } else {
138 if(!node->isRoot) { 128 if(node->parent) {
139 printf("NODE\n"); 129 printf("NODE\n");
140 } 130 }
141 iter = node_iterator_create(node->children); 131 iter = node_iterator_create(node->children);
142 for(current = iter->begin; current != NULL; current = iter->next(iter)) { 132 while ((current = iter->next(iter))) {
143 node_debug(current); 133 _node_debug(current, depth+1);
144 } 134 }
145 node_iterator_destroy(iter); 135 node_iterator_destroy(iter);
146 } 136 }
147 137
148} 138}
149 139
140void node_debug(node_t* node)
141{
142 _node_debug(node, 0);
143}
144
150unsigned int node_n_children(struct node_t* node) 145unsigned int node_n_children(struct node_t* node)
151{ 146{
152 if (!node) return 0; 147 if (!node) return 0;