152 *result = (void*)root; |
152 *result = (void*)root; |
153 return ret; |
153 return ret; |
154 } |
154 } |
155 |
155 |
156 // create a working stack |
156 // create a working stack |
157 CX_ARRAY_DECLARE(void const*, work); |
157 CX_ARRAY_DECLARE(const void *, work); |
158 cx_array_initialize(work, 32); |
158 cx_array_initialize(work, 32); |
159 |
159 |
160 // add the children of root to the working stack |
160 // add the children of root to the working stack |
161 { |
161 { |
162 void *c = tree_children(root); |
162 void *c = tree_children(root); |
172 int ret_candidate = ret; |
172 int ret_candidate = ret; |
173 |
173 |
174 // process the working stack |
174 // process the working stack |
175 while (work_size > 0) { |
175 while (work_size > 0) { |
176 // pop element |
176 // pop element |
177 void const *elem = work[--work_size]; |
177 const void *elem = work[--work_size]; |
178 |
178 |
179 // apply the search function |
179 // apply the search function |
180 ret = sfunc(elem, node); |
180 ret = sfunc(elem, node); |
181 |
181 |
182 if (ret == 0) { |
182 if (ret == 0) { |
225 (cx_tree_search_func) sfunc, |
225 (cx_tree_search_func) sfunc, |
226 result, |
226 result, |
227 loc_children, loc_next); |
227 loc_children, loc_next); |
228 } |
228 } |
229 |
229 |
230 static bool cx_tree_iter_valid(void const *it) { |
230 static bool cx_tree_iter_valid(const void *it) { |
231 struct cx_tree_iterator_s const *iter = it; |
231 const struct cx_tree_iterator_s *iter = it; |
232 return iter->node != NULL; |
232 return iter->node != NULL; |
233 } |
233 } |
234 |
234 |
235 static void *cx_tree_iter_current(void const *it) { |
235 static void *cx_tree_iter_current(const void *it) { |
236 struct cx_tree_iterator_s const *iter = it; |
236 const struct cx_tree_iterator_s *iter = it; |
237 return iter->node; |
237 return iter->node; |
238 } |
238 } |
239 |
239 |
240 static void cx_tree_iter_next(void *it) { |
240 static void cx_tree_iter_next(void *it) { |
241 struct cx_tree_iterator_s *iter = it; |
241 struct cx_tree_iterator_s *iter = it; |
349 iter.base.current = cx_tree_iter_current; |
349 iter.base.current = cx_tree_iter_current; |
350 |
350 |
351 return iter; |
351 return iter; |
352 } |
352 } |
353 |
353 |
354 static bool cx_tree_visitor_valid(void const *it) { |
354 static bool cx_tree_visitor_valid(const void *it) { |
355 struct cx_tree_visitor_s const *iter = it; |
355 const struct cx_tree_visitor_s *iter = it; |
356 return iter->node != NULL; |
356 return iter->node != NULL; |
357 } |
357 } |
358 |
358 |
359 static void *cx_tree_visitor_current(void const *it) { |
359 static void *cx_tree_visitor_current(const void *it) { |
360 struct cx_tree_visitor_s const *iter = it; |
360 const struct cx_tree_visitor_s *iter = it; |
361 return iter->node; |
361 return iter->node; |
362 } |
362 } |
363 |
363 |
364 __attribute__((__nonnull__)) |
364 __attribute__((__nonnull__)) |
365 static void cx_tree_visitor_enqueue_siblings( |
365 static void cx_tree_visitor_enqueue_siblings( |
495 // add new node as new child |
495 // add new node as new child |
496 cx_tree_link(parent, node, cx_tree_ptr_locations); |
496 cx_tree_link(parent, node, cx_tree_ptr_locations); |
497 } |
497 } |
498 |
498 |
499 int cx_tree_add( |
499 int cx_tree_add( |
500 void const *src, |
500 const void *src, |
501 cx_tree_search_func sfunc, |
501 cx_tree_search_func sfunc, |
502 cx_tree_node_create_func cfunc, |
502 cx_tree_node_create_func cfunc, |
503 void *cdata, |
503 void *cdata, |
504 void **cnode, |
504 void **cnode, |
505 void *root, |
505 void *root, |
558 // iter not valid? cancel... |
558 // iter not valid? cancel... |
559 if (!iter->valid(iter)) return 0; |
559 if (!iter->valid(iter)) return 0; |
560 |
560 |
561 size_t processed = 0; |
561 size_t processed = 0; |
562 void *current_node = root; |
562 void *current_node = root; |
563 void const *elem; |
563 const void *elem; |
564 |
564 |
565 for (void **eptr; |
565 for (void **eptr; |
566 iter->valid(iter) && (eptr = iter->current(iter)) != NULL; |
566 iter->valid(iter) && (eptr = iter->current(iter)) != NULL; |
567 iter->next(iter)) { |
567 iter->next(iter)) { |
568 elem = *eptr; |
568 elem = *eptr; |