void Route_dump(Route *parent, int indent) { int i = 0; int x = 0; if(indent == 1) { fprintf(stderr, "ELEMENTS in parent '%s'\n", bdata(parent->name)); } HEAP_ITERATE(parent->children, indx, Route *, r, for(x = 0; x < indent; x++) fprintf(stderr, " "); fprintf(stderr, "(%p) %d: %s [%zu]\n", r, i++, bdata(r->name), Heap_count(r->members)); Route_dump(r, indent+1); );
// there is probably some genius heap merging algorithm, look it up later // appropriates the left->cmp for use in the new heap // and the left->type Heap *Heap_merge(Heap *left, Heap *right) { int i; Heap *newHeap; assert(left); assert(right); assert(left->elementSize == right->elementSize); newHeap = Heap_create(left->elementSize, left->type, left->cmp); if (newHeap == NULL) { return NULL; } for (i = 0; i < Heap_count(left); i++) { Heap_push(newHeap, Darray_get(left->contents, i)); } for (i = 0; i < Heap_count(right); i++) { Heap_push(newHeap, Darray_get(right->contents, i)); } return newHeap; }
// sift the last element up to where it belongs void Heap_sift_up(Heap *heap) { int i; int p; int cmp; void *child; void *parent; assert(heap); if (Heap_count(heap) <= 1) { return; } for (i = Heap_end(heap) - 1; i > 0; i = (i - 1) / 2) { // Darray_debug(heap->contents); p = (i - 1) / 2; // printf("i: %d, p: %d\n", i, p); child = Heap_get(heap, i); parent = Heap_get(heap, p); cmp = heap->cmp(parent, child); // max-heap // parent is less than child, so move child up if (cmp > 0 && heap->type == 0) { // printf("swapping %d with %d\n", *(int *)parent, *(int *)child); Heap_set(heap, i, parent); Heap_set(heap, p, child); // min-heap // parent is greater than child, so move child up } else if (cmp <= 0 && heap->type != 0) { // printf("swapping\n"); Heap_set(heap, i, parent); Heap_set(heap, p, child); } else { break; } } }
// sift the first element down to where it belongs void Heap_sift_down(Heap *heap) { int i; int p; int cmp; int end; void *child1; void *child2; void *greaterChild; void *parent; assert(heap); if (Heap_count(heap) <= 1) { return; } end = Heap_end(heap); i = 0; while (i < end) { p = i * 2 + 1; parent = Heap_get(heap, i); // parent has no children, end of the line if (p >= end) { break; // parent has 1 child } else if (p + 1 >= end) { greaterChild = Heap_get(heap, p); // parent has 2 children // figure out which one needs to be compared to the parent } else { child1 = Heap_get(heap, p); child2 = Heap_get(heap, p + 1); cmp = heap->cmp(child1, child2); if (heap->type == 0) { if (cmp > 0) { greaterChild = child2; p = p + 1; } else { greaterChild = child1; } } else { if (cmp > 0) { greaterChild = child2; p = p + 1; } else { greaterChild = child1; } } } cmp = heap->cmp(parent, greaterChild); // max-heap // parent is less than child if (cmp > 0 && heap->type == 0) { Heap_set(heap, i, greaterChild); Heap_set(heap, p, parent); // min-heap // parent is greater than child } else if (cmp <= 0 && heap->type != 0) { Heap_set(heap, i, greaterChild); Heap_set(heap, p, parent); // element is where it needs to be } else { break; } i = p; } }