void main(){ int a[11]={0}; int size=10; int i; srand((unsigned int)time(0)); printf("Reverse\nBefore:"); for(i=1;i<11;i++){ a[i]=11-i; printf("%d ",a[i]); } printf("\nAfter:"); for(i=size/2;i>0;i--)downHeap(a,size,i); heapSort(a,size); for(i=1;i<=size;i++){ printf("%d ",a[i]); } printf("\nRandom\nBefore:"); for(i=1;i<11;i++){ a[i]=rand()%20+1; printf("%d ",a[i]); } printf("\nAfter:"); for(i=size/2;i>0;i--)downHeap(a,size,i); heapSort(a,size); for(i=1;i<=size;i++){ printf("%d ",a[i]); } }
void __declspec(dllexport) heapSort(T *array, long arrSize, int compare(T , T )) { long k = arrSize / 2 - 1; int i; for(i = k; i >= 0; i--) downHeap(array, i, arrSize, compare); T temp; for(i = arrSize - 1; i >= 0; i--) { temp = array[i]; array[i] = array[0]; array[0] = temp; downHeap(array, 0, i, compare); } }
void Sorter::SortImplementation::heapSort(int *Array, int size) { elements = Array; int i, tmp; for (i = size / 2; i >= 0; i--) downHeap(elements, i, size-1); for (i = size / 2 - 1; i >= 0; i--) downHeap(elements, i, size-1); for (i = size - 1; i > 0; i--) { tmp = elements[i]; elements[i] = elements[0]; elements[0] = tmp; downHeap(elements, 0, i - 1); } }
void heapSort(int *a, int n) { int i; int temp; for (i = n / 2 - 1; i >= 0; --i) { downHeap(a, i, n - 1); } for (i = n - 1; i > 0; --i) { temp = a[i]; a[i] = a[0]; a[0] = temp; downHeap(a, 0, i - 1); } }
void heapsort(double a[], int array_size) { int i; for (i = (array_size/2 - 1); i >= 0; --i) downHeap(a, i, array_size-1); for (i = array_size-1; i >= 0; --i) { int temp; temp = a[i]; a[i] = a[0]; a[0] = temp; downHeap(a, 0, i-1); } }
void EdgeHeap:: del_(int pos) { if(print) cerr << "del_ " << pos << endl; assert(unusedPos_); if(pos == (unusedPos_ - 1) ) { unusedPos_--; array[unusedPos_] = NULL; return; } /* move the final edge in heap to empty position */ array[pos] = array[unusedPos_ - 1]; if(!array[pos]) { error("Never get here"); return; } array[pos]->heapPos() = pos; array[unusedPos_ -1] = NULL; unusedPos_--; if(upheap(pos)) return; downHeap(pos); }
void HeapSort::sort(int array[], int size) { for (int i = ((size - 1) / 2) - 1; i >= 0; --i) downHeap(array, i, size - 1); for (int i = size - 1; i > 0; --i) { int temp = array[i]; array[i] = array[0]; array[0] = temp; downHeap(array, 0, i - 1); } return; }
void Heap::insert(hItem item) { int index; index = item.j * pdata->rows + item.i; if(item.flag == BAND) { if(item.hpos == -1) // not yet in heap { // T update and heap insertion pdata->Tfield[index].T = item.T; size = size + 1; pdata->Tfield[index].hpos = size; heap[size] = &(pdata->Tfield[index]); upHeap(size); } else // already in heap { // only T update double oldT = heap[item.hpos]->T; heap[item.hpos]->T = item.T; if(oldT > item.T) upHeap(item.hpos); else downHeap(item.hpos); } } }
void downHeap(int array[],int num,int n){ int tmp; if((array[n]<array[2*n] && 2*n <= num) || (array[n]<array[2*n+1] && 2*n+1 <=num)){ if(array[2*n]<array[2*n+1] && 2*n+1 <=num){ tmp=array[n]; array[n]=array[2*n+1]; array[2*n+1]=tmp; downHeap(array,num,2*n+1); } else { tmp=array[n]; array[n]=array[2*n]; array[2*n]=tmp; downHeap(array,num,2*n); } } }
void sortuj(size) { int x, j; for(j=size; j>0; j--) { x = Heap[j-1]; Heap[j-1] = Heap[0]; Heap[0] = x; downHeap(0,j-1); } }
void heapSort(int array[],int num){ //根の要素を削除 int tmp; while(num>1){ tmp = array[1]; array[1] = array[num]; array[num] = tmp; num-=1; downHeap(array,num,1); } }
/* * Update priority for a task in the queue */ void LpelTaskqueueUpdatePriority(taskqueue_t *tq, lpel_task_t *t, double np){ int pos = searchItem(tq, t); assert(pos > 0); double p = t->sched_info.prior; t->sched_info.prior = np; if (np > p) upHeap(tq, pos); else if (np < p) downHeap(tq, pos); }
void PriorityQueue::minPriority() { if(PQ.size() == 1) PQ.erase(PQ.begin()); else if(!PQ.empty()) { PQ.erase(PQ.begin()); PQ.insert(PQ.begin(), PQ.back()); PQ.pop_back(); downHeap(PQ); } }
void PriorityQueue::change (long itemIndex, long newValue) { if (newValue > pqList[itemIndex].key) { pqList[itemIndex].key = newValue; downHeap(itemIndex); } else if (newValue < pqList[itemIndex].key) { pqList[itemIndex].key = newValue; upHeap(itemIndex); } }
void EdgeHeap:: downHeap(int pos) { if(print) cerr << "downHeap " << pos << endl; if(pos >= unusedPos_-1) return; assert(pos < HeapSize); Edge* par = array[pos]; assert(par->heapPos() == pos); double merit = par->merit(); int lc = left_child(pos); int rc = right_child(pos); int largec; int lcthere = 0; Edge* lct=NULL; if(lc < unusedPos_) { assert(lc < HeapSize); lct = array[lc]; if(lct) { lcthere = 1; assert(lct->heapPos() == lc); } } int rcthere = 0; Edge* rct=NULL; if(rc < unusedPos_) { rct = array[rc]; if(rct) { rcthere = 1; assert(rct->heapPos() == rc); } } if(!lcthere && !rcthere) return; assert(lcthere); if(!rcthere || (lct->merit() > rct->merit())) largec = lc; else largec = rc; Edge* largeEdg = array[largec]; if(merit >= largeEdg->merit()) { if(print) cerr << "downheap of " << merit << " stopped by " << *largeEdg << " " << largeEdg->merit() << endl; return; } array[pos] = largeEdg; largeEdg->heapPos() = pos; array[largec] = par; par->heapPos() = largec; downHeap(largec); }
hItem Heap::extract() { hItem ret; //heap[0]->flag = KNOWN; ret = *heap[0]; heap[0] = heap[size]; heap[0]->hpos = 0; size = size - 1; downHeap(0); return ret; }
void heap_sort(PSTPoint* array, int begin, int end) { // First arrange the array into a heap (root element is always higher // than both child elements buildHeap(array,begin,end); while(end > begin) { // Since the highest is first, move it to the end PSTArray::swap(array,begin,end); // Now that the highest element is last, it is sorted so don't // touch it again end--; // Since the smallest element is now first, rebuild the heap downHeap(array,0,begin,end); } }
void PriorityQueue::chgPriority(QueueElement QE) { for(int i = 0; i < PQ.size(); i++) if(PQ[i].node == QE.node) { if(PQ[i].value > QE.value) { PQ[i].value = QE.value; upHeap(PQ, i); } else { PQ[i].value = QE.value; downHeap(PQ, i); } break; } }
double Heap :: getSmallest(int *Ind) { // Smallest element is the first of the heap double heapRoot = Keys [ 0 ]; // Set 'pointer' to the Ind in T of the min heap element * Ind = H2T [ 0 ]; // Replace root by the last heap element swapElements(0, heapCount - 1); // Decrement heapCount. // NB: Important to do this before downHeap is run! heapCount--; // Run downHeap from root. downHeap(0); return heapRoot; }
void downHeap(int i, int size) { int x, y; y = i; if (prawy(i) < size) { if(Heap[prawy(i)] > Heap[lewy(i)]) { y = prawy(i); } else { y = lewy(i); } } else if (lewy(i)+1==size) { y=lewy(i); } if(Heap[i] < Heap[y]) { x = Heap[y]; Heap[y] = Heap[i]; Heap[i] = x; downHeap(y, size); } }
void AnsTreeHeap:: downHeap(int pos) { if(print) cerr << "downHeap " << pos << endl; if(pos >= unusedPos_-1) return; AnsTreePair* par = array[pos]; double merit = par->first; int lc = left_child(pos); int rc = right_child(pos); int largec; int lcthere = 0; AnsTreePair* lct; if(lc < unusedPos_) { lct = array[lc]; if(lct) lcthere = 1; } int rcthere = 0; AnsTreePair* rct; if(rc < unusedPos_) { rct = array[rc]; if(rct) rcthere = 1; } if(!lcthere && !rcthere) return; assert(lcthere); if(!rcthere || (lct->first > rct->first)) largec = lc; else largec = rc; AnsTreePair* largeatp = array[largec]; if(merit <= largeatp->first) { if(print) cerr << "downheap of " << merit << " stopped by " << " " << largeatp->first << endl; return; } array[pos] = largeatp; array[largec] = par; downHeap(largec); }
void PriorityQueue::remove (PQNode& item) { item = pqList[1]; pqList[1] = pqList[numItems--]; downHeap(1); }
int phraseQuery(PostingsState* subs, unsigned char *liveDocsBytes, double *termScoreCache, float termWeight, int maxDoc, int topN, int numScorers, int docBase, unsigned int *filled, int *docIDs, unsigned int *coords, float *topScores, int *topDocIDs, float *normTable, unsigned char *norms, int *posOffsets) { bool failed = false; unsigned int *posCounts = 0; unsigned int *positions = 0; int docUpto = 0; int hitCount = 0; int countUpto = 1; float scoreToBeat = 0.0; posCounts = (unsigned int *) calloc(POS_CHUNK, sizeof(int)); if (posCounts == 0) { failed = true; goto end; } for(int i=0;i<numScorers;i++) { subs[i].tfSums = (unsigned long *) malloc(CHUNK * sizeof(long)); if (subs[i].tfSums == 0) { failed = true; goto end; } subs[i].tfs = (unsigned int *) malloc(CHUNK * sizeof(int)); if (subs[i].tfs == 0) { failed = true; goto end; } } while (docUpto < maxDoc) { #ifdef DEBUG printf("cycle docUpto=%d\n", docUpto); #endif int endDoc = docUpto + CHUNK; if (liveDocsBytes != 0) { orFirstMustChunkWithDeletes(&subs[0], endDoc, docIDs, coords, liveDocsBytes); } else { orFirstMustChunk(&subs[0], endDoc, docIDs, coords); } for(int i=1;i<numScorers-1;i++) { orMustChunk(&subs[i], endDoc, docIDs, coords, i); } // TODO: we could check positions inside orLastMustChunk // instead, saves one pass: int numFilled = orLastMustChunk(&subs[numScorers-1], endDoc, filled, docIDs, coords, numScorers-1); #ifdef DEBUG printf(" numFilled=%d\n", numFilled); #endif int docChunkBase = docBase + docUpto; for(int fill=0;fill<numFilled;fill++) { int slot = filled[fill]; #ifdef DEBUG printf("\ncheck positions slot=%d docID=%d\n", slot, docIDs[slot]); #endif //printf(" docID=%d\n", //docIDs[slot]);fflush(stdout); // This doc has all of the terms, now check their // positions: // Seek/init pos so we are positioned at posDeltas // for this document: bool done = false; unsigned int minTF = 4294967295; for(int j=0;j<numScorers;j++) { PostingsState *sub = subs+j; #ifdef DEBUG printf(" scorer[%d]: skip %d positions, %d pos in doc, tfSum=%d, posBlockLastRead=%d\n", j, sub->tfSums[slot] - sub->posUpto, sub->tfs[slot], sub->tfSums[slot], sub->posBlockLastRead); #endif skipPositions(sub, sub->tfSums[slot]); #ifdef DEBUG printf(" posBlockLastRead=%d posDelta=%d nextPosDelta=%d\n", sub->posBlockLastRead, sub->posDeltas[sub->posBlockLastRead], sub->posDeltas[sub->posBlockLastRead+1]); #endif sub->nextPos = posOffsets[j] + sub->posDeltas[sub->posBlockLastRead]; int skipped = 0; sub->posLeftInDoc = sub->tfs[slot]; while (sub->nextPos < 0) { skipped++; if (--sub->posLeftInDoc == 0) { sub->posBlockLastRead++; done = true; break; } if (sub->posBlockLastRead == sub->posBlockEnd) { nextPosBlock(sub); sub->posBlockLastRead = -1; } sub->nextPos += sub->posDeltas[++sub->posBlockLastRead]; } if (sub->posLeftInDoc < minTF) { minTF = sub->posLeftInDoc; } #ifdef DEBUG printf(" posLeftInDoc=%d posUpto=%d\n", sub->posLeftInDoc, sub->posUpto); #endif } if (done) { for(int j=0;j<numScorers;j++) { PostingsState *sub = subs+j; sub->posUpto += sub->tfs[slot] - sub->posLeftInDoc; } continue; } bool doStopAfterFirstPhrase; bool doSkipCollect; if (topScores == 0) { doStopAfterFirstPhrase = true; doSkipCollect = false; } else { // Invert the score of the bottom of the queue: float f = (scoreToBeat / normTable[norms[docIDs[slot]]]) / termWeight; unsigned int phraseFreqToBeat = (unsigned int) (f*f); //printf("scoreToBeat=%g phraseFreqToBeat=%d\n", scoreToBeat, phraseFreqToBeat); doStopAfterFirstPhrase = doSkipCollect = minTF < phraseFreqToBeat; //if (doStopAfterFirstPhrase) { //printf("do stop: %d vs %d\n", minTF, phraseFreqToBeat); //} } // nocommit doStopAfterFirstPhrase = false; doSkipCollect = false; int phraseFreq = 0; // Find all phrase matches, in windows of POS_CHUNK: int posUpto = 0; //bool dbg = docIDs[slot] == 46260; while (true) { #ifdef DEBUG printf(" cycle posUpto=%d countUpto=%d\n", posUpto, countUpto); #endif if (countUpto + numScorers + 1 < countUpto) { // Wrap-around countUpto = 1; memset(posCounts, 0, POS_CHUNK*sizeof(int)); } PostingsState *sub = subs; int endPos = posUpto + POS_CHUNK; // First terms in phrase: int posBlockLastRead = sub->posBlockLastRead; unsigned int *posDeltas = sub->posDeltas; unsigned int posBlockEnd = sub->posBlockEnd; int pos = sub->nextPos; while (pos < endPos) { int posSlot = pos & POS_MASK; posCounts[posSlot] = countUpto; #ifdef DEBUG printf("scorer[0] nextPos=%d\n", pos); #endif if (--sub->posLeftInDoc == 0) { #ifdef DEBUG printf(" done: no more positions\n"); #endif posBlockLastRead++; done = true; break; } if (posBlockLastRead == posBlockEnd) { #ifdef DEBUG printf(" nextPosBlock: posBlockEnd=%d\n", posBlockEnd); #endif nextPosBlock(sub); posBlockLastRead = -1; posBlockEnd = sub->posBlockEnd; } #ifdef DEBUG printf(" add posDeltas[%d]=%d\n", 1+posBlockLastRead, posDeltas[1+posBlockLastRead]);fflush(stdout); #endif pos += posDeltas[++posBlockLastRead]; } sub->posBlockLastRead = posBlockLastRead; sub->nextPos = pos; // Middle terms in phrase: for(int i=1;i<numScorers-1;i++) { sub = subs + i; posBlockLastRead = sub->posBlockLastRead; posDeltas = sub->posDeltas; posBlockEnd = sub->posBlockEnd; pos = sub->nextPos; while (pos < endPos) { #ifdef DEBUG printf("scorer[%d] nextPos=%d\n", i, pos); #endif int posSlot = pos & POS_MASK; if (posCounts[posSlot] == countUpto) { posCounts[posSlot]++; } if (--sub->posLeftInDoc == 0) { posBlockLastRead++; done = true; break; } if (posBlockLastRead == posBlockEnd) { nextPosBlock(sub); posBlockLastRead = -1; posBlockEnd = sub->posBlockEnd; } pos += posDeltas[++posBlockLastRead]; } sub->posBlockLastRead = posBlockLastRead; sub->nextPos = pos; countUpto++; } // Last term in phrase: sub = subs + (numScorers-1); posBlockLastRead = sub->posBlockLastRead; posDeltas = sub->posDeltas; posBlockEnd = sub->posBlockEnd; pos = sub->nextPos; while (pos < endPos) { #ifdef DEBUG printf("scorer[%d] nextPos=%d\n", numScorers-1, pos);fflush(stdout); #endif int posSlot = pos & POS_MASK; if (posCounts[posSlot] == countUpto) { #ifdef DEBUG printf(" match pos=%d slot=%d count=%d\n", pos, posSlot, posCounts[posSlot]);fflush(stdout); #endif phraseFreq++; if (doStopAfterFirstPhrase) { // ConstantScoreQuery(PhraseQuery), so we can // stop & collect hit as soon as we find one // phrase match done = true; break; } } if (--sub->posLeftInDoc == 0) { posBlockLastRead++; done = true; break; } if (posBlockLastRead == posBlockEnd) { #ifdef DEBUG printf(" nextPosBlock: posBlockEnd=%d\n", posBlockEnd); #endif nextPosBlock(sub); posBlockLastRead = -1; posBlockEnd = sub->posBlockEnd; } #ifdef DEBUG printf(" add posDeltas[%d]=%d\n", 1+posBlockLastRead, posDeltas[1+posBlockLastRead]);fflush(stdout); #endif pos += posDeltas[++posBlockLastRead]; } countUpto++; sub->posBlockLastRead = posBlockLastRead; sub->nextPos = pos; if (done) { break; } posUpto += POS_CHUNK; } for(int j=0;j<numScorers;j++) { PostingsState *sub = subs+j; #ifdef DEBUG printf("add posUpto[%d]: %d; posLeftInDoc %d\n", j, sub->tfs[slot] - sub->posLeftInDoc, sub->posLeftInDoc); #endif sub->posUpto += sub->tfs[slot] - sub->posLeftInDoc; } if (phraseFreq != 0) { //printf(" freq=%d\n", phraseFreq);fflush(stdout); #ifdef DEBUG printf(" phraseFreq=%d topScores=%lx\n", phraseFreq, topScores);fflush(stdout); #endif hitCount++; if (doSkipCollect) { continue; } int docID = docChunkBase + slot; #ifdef DEBUG printf(" now collect\n");fflush(stdout); #endif // collect if (topScores == 0) { // TODO: we can stop collecting, and tracking filled, // after chunk once queue is full if (docID < topDocIDs[1]) { // Hit is competitive topDocIDs[1] = docID; downHeapNoScores(topN, topDocIDs); } } else { float score; if (phraseFreq < TERM_SCORES_CACHE_SIZE) { score = termScoreCache[phraseFreq]; } else { score = sqrt(phraseFreq) * termWeight; } //printf(" freq=%d\n", phraseFreq);fflush(stdout); score *= normTable[norms[docIDs[slot]]]; if (score > topScores[1] || (score == topScores[1] && docID < topDocIDs[1])) { // Hit is competitive topDocIDs[1] = docID; topScores[1] = score; downHeap(topN, topDocIDs, topScores); #ifdef DEBUG printf(" ** score=%g phraseFreq=%d norm=%g\n", score, phraseFreq, normTable[norms[docIDs[slot]]]);fflush(stdout); #endif scoreToBeat = topScores[1]; } } } } docUpto += CHUNK; } end: if (posCounts != 0) { free(posCounts); } for(int i=0;i<numScorers;i++) { if (subs[i].tfSums != 0) { free(subs[i].tfSums); } if (subs[i].tfs != 0) { free(subs[i].tfs); } } if (failed) { return -1; } else { return hitCount; } }
// builds a heap from bottom up, starting at right-most lowest level // and traversing up the heap in reverse order (right to left, bottom to top) void buildHeap(PSTPoint* array, int begin, int end) { int n = 1 + end - begin; for(int v = n/2-1; v >= 0; v--) downHeap(array,v,begin,end); }
/* * remove the head (highest priority) of the queue */ void LpelTaskqueueRemoveHead(taskqueue_t *tq){ /* pull last item to top, then down heap. */ tq->count--; tq->heap[1] = tq->heap[tq->count]; downHeap(tq, 1); }
void kopcuj(int size) { int i; for(i=size/2; i>=0; i--) { downHeap(i, size); } }
int booleanQueryShouldMustMustNot(PostingsState* subs, unsigned char *liveDocsBytes, double **termScoreCache, float *termWeights, int maxDoc, int topN, int numScorers, int docBase, int numMust, int numMustNot, unsigned int *filled, int *docIDs, float *scores, unsigned int *coords, float *topScores, int *topDocIDs, float *coordFactors, float *normTable, unsigned char *norms, PostingsState *dsSubs, unsigned int *dsCounts, unsigned int *dsMissingDims, unsigned int dsNumDims, unsigned int *dsTotalHits, unsigned int *dsTermsPerDim, unsigned long *dsHitBits, unsigned long **dsNearMissBits) { int docUpto = 0; int hitCount = 0; while (docUpto < maxDoc) { int endDoc = docUpto + CHUNK; //printf("cycle endDoc=%d dels=%lx\n", endDoc, liveDocBytes);fflush(stdout); // MUST_NOT: for(int i=0;i<numMustNot;i++) { orMustNotChunk(&subs[i], endDoc, docIDs, coords); } // MUST: int numFilled; if (liveDocsBytes != 0) { numFilled = orFirstMustChunkWithDeletes(&subs[numMustNot], termScoreCache[numMustNot], termWeights[numMustNot], endDoc, filled, docIDs, scores, coords, liveDocsBytes); } else { numFilled = orFirstMustChunk(&subs[numMustNot], termScoreCache[numMustNot], termWeights[numMustNot], endDoc, filled, docIDs, scores, coords); } //printf("numFilled=%d\n", numFilled); for(int i=numMustNot+1;i<numMustNot + numMust;i++) { numFilled = orMustChunk(&subs[i], termScoreCache[i], termWeights[i], endDoc, filled, docIDs, scores, coords, i-numMustNot); } if (topScores != 0) { // SHOULD for(int i=numMustNot + numMust;i<numScorers;i++) { orShouldChunk(&subs[i], termScoreCache[i], termWeights[i], endDoc, docIDs, scores, coords, numMust); } } int docChunkBase = docBase + docUpto; if (dsNumDims > 0) { hitCount += drillSidewaysCollect(topN, docBase, topDocIDs, topScores, normTable, norms, coordFactors, coords, filled, numFilled, docIDs, scores, dsCounts, dsMissingDims, docUpto, dsSubs, dsNumDims, dsTermsPerDim, dsTotalHits, dsHitBits, dsNearMissBits); } else if (topScores == 0) { hitCount += numFilled; for(int i=0;i<numFilled;i++) { int slot = filled[i]; int docID = docChunkBase + slot; // TODO: we can stop collecting, and tracking filled, // after chunk once queue is full if (docID < topDocIDs[1]) { // Hit is competitive topDocIDs[1] = docID; downHeapNoScores(topN, topDocIDs); //printf(" **\n");fflush(stdout); } } } else { hitCount += numFilled; // Collect: //printf("collect:\n"); for(int i=0;i<numFilled;i++) { int slot = filled[i]; float score = scores[slot] * coordFactors[coords[slot]] * normTable[norms[docIDs[slot]]]; int docID = docChunkBase + slot; //printf(" docBase=%d doc=%d score=%.5f coord=%d cf=%.5f\n", //docBase, docID, score, coords[slot], coordFactors[coords[slot]]); if (score > topScores[1] || (score == topScores[1] && docID < topDocIDs[1])) { // Hit is competitive topDocIDs[1] = docID; topScores[1] = score; downHeap(topN, topDocIDs, topScores); //printf(" **\n");fflush(stdout); } } } docUpto += CHUNK; } return hitCount; }