void NHeap<T>::heapifyup(unsigned p) { while((p>0)&&(p<occupation)&&(heap[FATHER(p)].key>heap[p].key)) { swap(heap[FATHER(p)],heap[p]); p = FATHER(p); } }
/** * HeapPopWorst * * Remove the largest item from the heap. * * @param Heap ptr to heap whose top is to be removed and returned * @param Key place to put key of top heap item * @param out_ptr place to put data of top heap item */ int HeapPopWorst(HEAP *Heap, FLOAT32 *Key, void *out_ptr) { inT32 Index; /*current index */ inT32 Hole; FLOAT32 HoleKey; inT32 Father; void *HoleData; void **Data = (void **) out_ptr; if (Heap->FirstFree <= 1) return (EMPTY); HoleKey = Heap->Entry[1].Key; Hole = 1; Heap->FirstFree--; for (Index = Heap->FirstFree, Father = FATHER (Index); Index > Father; Index--) if (Heap->Entry[Index].Key > HoleKey) { /*find biggest */ HoleKey = Heap->Entry[Index].Key; Hole = Index; } *Key = HoleKey; *Data = Heap->Entry[Hole].Data; HoleKey = Heap->Entry[Heap->FirstFree].Key; Heap->Entry[Hole].Key = HoleKey; HoleData = Heap->Entry[Heap->FirstFree].Data; Heap->Entry[Hole].Data = HoleData; /* now sift last entry to its rightful place */ Father = FATHER (Hole); /*father of hole */ while (Hole > 1 && Heap->Entry[Father].Key > HoleKey) { /*swap entries */ Heap->Entry[Hole].Key = Heap->Entry[Father].Key; Heap->Entry[Hole].Data = Heap->Entry[Father].Data; Heap->Entry[Father].Data = HoleData; Heap->Entry[Father].Key = HoleKey; Hole = Father; Father = FATHER (Hole); } return (TESS_HEAP_OK); } /* HeapPop */
/** * This routine stores Entry into Heap. The heap is * maintained in such a way that the item with the lowest key * is always at the top of the heap. * * Globals: * - None * * @param Heap ptr to heap to store new item in * @param Entry ptr to item to be stored in Heap * @note Exceptions: * - HEAPFULL error if heap size is exceeded * @note History: 3/13/89, DSJ, Created. */ void HeapStore(HEAP *Heap, HEAPENTRY *Entry) { inT32 Item; inT32 Father; if (Heap->FirstFree > Heap->Size) DoError (HEAPFULL, "Heap size exceeded"); Item = Heap->FirstFree; Heap->FirstFree++; while (Item != 1) { Father = FATHER (Item); if (Heap->Entry[Father].Key > Entry->Key) { Heap->Entry[Item].Key = Heap->Entry[Father].Key; Heap->Entry[Item].Data = Heap->Entry[Father].Data; Item = Father; } else break; } Heap->Entry[Item].Key = Entry->Key; Heap->Entry[Item].Data = Entry->Data; } /* HeapStore */
/** * This routine stores Data into Heap and associates it * with Key. The heap is * maintained in such a way that the item with the lowest key * is always at the top of the heap. * * Globals: * - None * * @param Heap ptr to heap to store new item in * @param Key numeric key associated with new item * @param Data ptr to data contents of new item * * @note Exceptions: * - HEAPFULL error if heap size is exceeded * * @note History: 5/10/91, DSJ, Created (Modified version of HeapStore). */ void HeapPush(HEAP *Heap, FLOAT32 Key, void *Data) { inT32 Item; inT32 Father; if (Heap->FirstFree > Heap->Size) DoError (HEAPFULL, "Heap size exceeded"); Item = Heap->FirstFree; Heap->FirstFree++; while (Item != 1) { Father = FATHER (Item); if (Heap->Entry[Father].Key > Key) { Heap->Entry[Item].Key = Heap->Entry[Father].Key; Heap->Entry[Item].Data = Heap->Entry[Father].Data; Item = Father; } else break; } Heap->Entry[Item].Key = Key; Heap->Entry[Item].Data = Data; } /* HeapPush */