ucx/tree.c

changeset 31
287484519844
parent 30
d33eaaec15da
equal deleted inserted replaced
30:d33eaaec15da 31:287484519844
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "cx/tree.h" 29 #include "cx/tree.h"
30
31 #include "cx/array_list.h"
32 30
33 #include <assert.h> 31 #include <assert.h>
34 32
35 #define CX_TREE_PTR(cur, off) (*(void**)(((char*)(cur))+(off))) 33 #define CX_TREE_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
36 #define tree_parent(node) CX_TREE_PTR(node, loc_parent) 34 #define tree_parent(node) CX_TREE_PTR(node, loc_parent)
350 iter->stack[iter->depth - 1] = next; 348 iter->stack[iter->depth - 1] = next;
351 } 349 }
352 } 350 }
353 } else { 351 } else {
354 // node has children, push the first child onto the stack and enter it 352 // node has children, push the first child onto the stack and enter it
355 cx_array_simple_add(iter->stack, children); 353 if (iter->stack_size >= iter->stack_capacity) {
354 const size_t newcap = iter->stack_capacity + 8;
355 if (cxReallocArrayDefault(&iter->stack, newcap, sizeof(void*))) {
356 // we cannot return an error in this function
357 abort(); // LCOV_EXCL_LINE
358 }
359 iter->stack_capacity = newcap;
360 }
361 iter->stack[iter->stack_size] = children;
362 iter->stack_size++;
356 iter->node = children; 363 iter->node = children;
357 iter->counter++; 364 iter->counter++;
358 } 365 }
359 } 366 }
360 367
715 return 0; 722 return 0;
716 } 723 }
717 } 724 }
718 725
719 // otherwise, create iterator and hand over to other function 726 // otherwise, create iterator and hand over to other function
720 CxIterator iter = cxIterator(src, elem_size, num, false); 727 CxIterator iter = cxIterator(src, elem_size, num);
721 return cx_tree_add_iter(cxIteratorRef(iter), num, sfunc, 728 return cx_tree_add_iter(cxIteratorRef(iter), num, sfunc,
722 cfunc, cdata, failed, root, 729 cfunc, cdata, failed, root,
723 loc_parent, loc_children, loc_last_child, 730 loc_parent, loc_children, loc_last_child,
724 loc_prev, loc_next); 731 loc_prev, loc_next);
725 } 732 }
900 } 907 }
901 908
902 size_t cxTreeInsertArray(CxTree *tree, const void *data, size_t elem_size, size_t n) { 909 size_t cxTreeInsertArray(CxTree *tree, const void *data, size_t elem_size, size_t n) {
903 if (n == 0) return 0; 910 if (n == 0) return 0;
904 if (n == 1) return 0 == cxTreeInsert(tree, data) ? 1 : 0; 911 if (n == 1) return 0 == cxTreeInsert(tree, data) ? 1 : 0;
905 CxIterator iter = cxIterator(data, elem_size, n, false); 912 CxIterator iter = cxIterator(data, elem_size, n);
906 return cxTreeInsertIter(tree, cxIteratorRef(iter), n); 913 return cxTreeInsertIter(tree, cxIteratorRef(iter), n);
907 } 914 }
908 915
909 void *cxTreeFind( CxTree *tree, const void *data) { 916 void *cxTreeFind( CxTree *tree, const void *data) {
910 return tree->cl->find(tree, tree->root, data, 0); 917 return tree->cl->find(tree, tree->root, data, 0);

mercurial