Route *Route_alloc(Route *parent, const char *name) { Route *r = h_calloc(1, sizeof(Route)); assert_mem(r); r->name = bfromcstr(name); assert_mem(r->name); r->members = Heap_create(NULL); r->children = Heap_create(Route_children_compare); // add it to the children mapping if(parent) { hattach(r, parent); Heap_add(parent->children, r); } return r; }
Heap *Heap_from_darray(Darray *array, int type, Heap_compare cmp) { int i; Heap *heap; assert(array); heap = Heap_create(array->elementSize, type, cmp); if (heap == NULL) { return NULL; } for (i = 0; i < Darray_count(array); i++) { Heap_push(heap, Darray_get(array, i)); } return heap; }
Heap *Heap_from_list(List *list, int type, Heap_compare cmp) { Heap *heap; ListNode *node; assert(list); heap = Heap_create(list->elementSize, type, cmp); if (heap == NULL) { return NULL; } node = list->first; while (node != NULL) { Heap_push(heap, node->value); node = node->next; } return heap; }
// yikes Heap *Heap_from_array(void **array, int size, size_t elementSize, int type, Heap_compare cmp) { int i; Heap *heap; assert(array); assert(cmp); assert(size); heap = Heap_create(elementSize, type, cmp); if (heap == NULL) { return NULL; } for (i = 0; i < size; i++) { Heap_push(heap, array[i]); } return heap; }
// 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; }