/** Elimina del heap el elemento situado en index */ void heap_remove_at(Heap* heap, size_t index) { /* Hacemos subir al nodo a la cabeza del heap */ heap_update_key(heap, index, INT_MAX); /* Ponemos el último elemento en la cabeza del heap */ heap_swap(heap, 0, --heap -> count); /* Lo hacemos bajar a donde le correspnde */ heap_sift_down(heap, 0); }
/* get the coordinate pair with the minimum A* score from the heap */ static uint32 heap_get(TCOD_path_data_t *path,TCOD_list_t heap) { /* return the first value of the heap (minimum score) */ uintptr *array=(uintptr *)TCOD_list_begin(heap); int end=TCOD_list_size(heap)-1; uint32 off=(uint32)(array[0]); /* take the last element and put it at first position (heap root) */ array[0] = array[end]; TCOD_list_pop(heap); /* and bubble it down to its real position */ heap_sift_down(path,heap); return off; }
/** Extrae el elemento de mayor key del heap. Retorna NULL si esta vacio */ void* heap_extract(Heap* heap) { /* Si no tiene elementos, retorna NULL */ if(!heap -> count) return NULL; /* Obtenemos el puntero a retornar */ void* max = heap -> array[0].content; /* Actualizamos el contador */ heap -> count--; /* Ponemos el último elemento en la cabeza del heap */ heap_swap(heap, 0, heap -> count); /* Lo hacemos bajar a donde le correspnde */ heap_sift_down(heap, 0); /* Retornamos el puntero con mayor key */ return max; }
/** Actualiza la key y posicion del nodo en index */ void heap_update_key(Heap* heap, size_t index, int key) { /* Si la key es la misma, no hay nada que hacer */ if(heap -> array[index].key == key) return; /* Si la nueva es mayor a la antigua */ if(key > heap -> array[index].key) { heap -> array[index].key = key; heap_sift_up(heap, index); } /* Si la nueva es menor */ else { heap -> array[index].key = key; heap_sift_down(heap, index); } }
/** * @brief 弹出堆顶元素. * @param[in] h 堆对象的指针 * @return 无 */ void heap_pop(heap_t *h) { h->elems[0] = h->elems[h->size - 1]; h->size --; heap_sift_down(h, 0); }