summaryrefslogtreecommitdiffstats
path: root/libcnary/node.c
diff options
context:
space:
mode:
Diffstat (limited to 'libcnary/node.c')
-rw-r--r--libcnary/node.c49
1 files changed, 27 insertions, 22 deletions
diff --git a/libcnary/node.c b/libcnary/node.c
index 6d68f6e..8d3708b 100644
--- a/libcnary/node.c
+++ b/libcnary/node.c
@@ -27,11 +27,12 @@
27#include "node.h" 27#include "node.h"
28#include "node_list.h" 28#include "node_list.h"
29 29
30void node_destroy(node_t* node) { 30void node_destroy(node_t node)
31{
31 if(!node) return; 32 if(!node) return;
32 33
33 if (node->children && node->children->count > 0) { 34 if (node->children && node->children->count > 0) {
34 node_t* ch; 35 node_t ch;
35 while ((ch = node->children->begin)) { 36 while ((ch = node->children->begin)) {
36 node_list_remove(node->children, ch); 37 node_list_remove(node->children, ch);
37 node_destroy(ch); 38 node_destroy(ch);
@@ -43,10 +44,11 @@ void node_destroy(node_t* node) {
43 free(node); 44 free(node);
44} 45}
45 46
46node_t* node_create(node_t* parent, void* data) { 47node_t node_create(node_t parent, void* data)
48{
47 int error = 0; 49 int error = 0;
48 50
49 node_t* node = (node_t*)calloc(1, sizeof(node_t)); 51 node_t node = (node_t)calloc(1, sizeof(struct node));
50 if (node == NULL) { 52 if (node == NULL) {
51 return NULL; 53 return NULL;
52 } 54 }
@@ -73,7 +75,8 @@ node_t* node_create(node_t* parent, void* data) {
73 return node; 75 return node;
74} 76}
75 77
76int node_attach(node_t* parent, node_t* child) { 78int node_attach(node_t parent, node_t child)
79{
77 if (!parent || !child) return -1; 80 if (!parent || !child) return -1;
78 child->parent = parent; 81 child->parent = parent;
79 if(!parent->children) { 82 if(!parent->children) {
@@ -86,7 +89,8 @@ int node_attach(node_t* parent, node_t* child) {
86 return res; 89 return res;
87} 90}
88 91
89int node_detach(node_t* parent, node_t* child) { 92int node_detach(node_t parent, node_t child)
93{
90 if (!parent || !child) return -1; 94 if (!parent || !child) return -1;
91 int node_index = node_list_remove(parent->children, child); 95 int node_index = node_list_remove(parent->children, child);
92 if (node_index >= 0) { 96 if (node_index >= 0) {
@@ -95,7 +99,7 @@ int node_detach(node_t* parent, node_t* child) {
95 return node_index; 99 return node_index;
96} 100}
97 101
98int node_insert(node_t* parent, unsigned int node_index, node_t* child) 102int node_insert(node_t parent, unsigned int node_index, node_t child)
99{ 103{
100 if (!parent || !child) return -1; 104 if (!parent || !child) return -1;
101 child->parent = parent; 105 child->parent = parent;
@@ -109,9 +113,10 @@ int node_insert(node_t* parent, unsigned int node_index, node_t* child)
109 return res; 113 return res;
110} 114}
111 115
112static void _node_debug(node_t* node, unsigned int depth) { 116static void _node_debug(node_t node, unsigned int depth)
117{
113 unsigned int i = 0; 118 unsigned int i = 0;
114 node_t* current = NULL; 119 node_t current = NULL;
115 for(i = 0; i < depth; i++) { 120 for(i = 0; i < depth; i++) {
116 printf("\t"); 121 printf("\t");
117 } 122 }
@@ -132,23 +137,23 @@ static void _node_debug(node_t* node, unsigned int depth) {
132 137
133} 138}
134 139
135void node_debug(node_t* node) 140void node_debug(node_t node)
136{ 141{
137 _node_debug(node, 0); 142 _node_debug(node, 0);
138} 143}
139 144
140unsigned int node_n_children(struct node_t* node) 145unsigned int node_n_children(node_t node)
141{ 146{
142 if (!node) return 0; 147 if (!node) return 0;
143 return node->count; 148 return node->count;
144} 149}
145 150
146node_t* node_nth_child(struct node_t* node, unsigned int n) 151node_t node_nth_child(node_t node, unsigned int n)
147{ 152{
148 if (!node || !node->children || !node->children->begin) return NULL; 153 if (!node || !node->children || !node->children->begin) return NULL;
149 unsigned int node_index = 0; 154 unsigned int node_index = 0;
150 int found = 0; 155 int found = 0;
151 node_t *ch; 156 node_t ch;
152 for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { 157 for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
153 if (node_index++ == n) { 158 if (node_index++ == n) {
154 found = 1; 159 found = 1;
@@ -161,30 +166,30 @@ node_t* node_nth_child(struct node_t* node, unsigned int n)
161 return ch; 166 return ch;
162} 167}
163 168
164node_t* node_first_child(struct node_t* node) 169node_t node_first_child(node_t node)
165{ 170{
166 if (!node || !node->children) return NULL; 171 if (!node || !node->children) return NULL;
167 return node->children->begin; 172 return node->children->begin;
168} 173}
169 174
170node_t* node_prev_sibling(struct node_t* node) 175node_t node_prev_sibling(node_t node)
171{ 176{
172 if (!node) return NULL; 177 if (!node) return NULL;
173 return node->prev; 178 return node->prev;
174} 179}
175 180
176node_t* node_next_sibling(struct node_t* node) 181node_t node_next_sibling(node_t node)
177{ 182{
178 if (!node) return NULL; 183 if (!node) return NULL;
179 return node->next; 184 return node->next;
180} 185}
181 186
182int node_child_position(struct node_t* parent, node_t* child) 187int node_child_position(node_t parent, node_t child)
183{ 188{
184 if (!parent || !parent->children || !parent->children->begin || !child) return -1; 189 if (!parent || !parent->children || !parent->children->begin || !child) return -1;
185 int node_index = 0; 190 int node_index = 0;
186 int found = 0; 191 int found = 0;
187 node_t *ch; 192 node_t ch;
188 for (ch = node_first_child(parent); ch; ch = node_next_sibling(ch)) { 193 for (ch = node_first_child(parent); ch; ch = node_next_sibling(ch)) {
189 if (ch == child) { 194 if (ch == child) {
190 found = 1; 195 found = 1;
@@ -198,17 +203,17 @@ int node_child_position(struct node_t* parent, node_t* child)
198 return node_index; 203 return node_index;
199} 204}
200 205
201node_t* node_copy_deep(node_t* node, copy_func_t copy_func) 206node_t node_copy_deep(node_t node, copy_func_t copy_func)
202{ 207{
203 if (!node) return NULL; 208 if (!node) return NULL;
204 void *data = NULL; 209 void *data = NULL;
205 if (copy_func) { 210 if (copy_func) {
206 data = copy_func(node->data); 211 data = copy_func(node->data);
207 } 212 }
208 node_t* copy = node_create(NULL, data); 213 node_t copy = node_create(NULL, data);
209 node_t* ch; 214 node_t ch;
210 for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) { 215 for (ch = node_first_child(node); ch; ch = node_next_sibling(ch)) {
211 node_t* cc = node_copy_deep(ch, copy_func); 216 node_t cc = node_copy_deep(ch, copy_func);
212 node_attach(copy, cc); 217 node_attach(copy, cc);
213 } 218 }
214 return copy; 219 return copy;