static void mkheap_heapify(MKHeap *mkheap, bool doLv0) { int i; MKEntry tmp; /* Build heap, expanded already at lv 0 */ if(doLv0) { for(i=PARENT(mkheap->maxentry); i >= 0; --i) { tmp = mkheap->p[i]; mkheap_siftdown(mkheap, i, &tmp); } } /* Populate deeper levels */ while(mke_get_lv(mkheap->p) < mkheap->mkctxt->total_lv - 1) { int nlv = mke_get_lv(mkheap->p) + 1; Assert(nlv < mkheap->mkctxt->total_lv); mkheap_save_lvtop(mkheap); mkheap_prep_siftdown_lv(mkheap, nlv, 0, mkheap->mkctxt); } #ifdef USE_ASSERT_CHECKING if(gp_mk_sort_check) mkheap_verify_heap(mkheap, 0); #endif }
/* * Prepare heap (expand lv) at postion cur. */ static void mkheap_prep_siftdown_lv(MKHeap *mkheap, int lv, int cur, MKContext *mkctxt) { bool expchild = false; int c; Assert(cur < mkheap->maxentry); Assert(cur >= 0); /* If necessary, expand right */ if (RIGHT(cur) < mkheap->maxentry) { c = mkheap_compare(mkheap, mkheap->p + cur, mkheap->p + RIGHT(cur)); if (c == 0) { mkheap_prep_siftdown_lv(mkheap, lv, RIGHT(cur), mkctxt); expchild = true; } } /* If necessary, expand left */ if (LEFT(cur) < mkheap->maxentry) { c = mkheap_compare(mkheap, mkheap->p + cur, mkheap->p + LEFT(cur)); if (c == 0) { mkheap_prep_siftdown_lv(mkheap, lv, LEFT(cur), mkctxt); expchild = true; } } Assert(mke_get_lv(mkheap->p + cur) == lv - 1); if (mkheap->mkctxt->fetchForPrep) tupsort_prepare(mkheap->p + cur, mkctxt, lv); mke_set_lv(mkheap->p + cur, lv); if (expchild) { MKEntry tmp = mkheap->p[cur]; mkheap_siftdown(mkheap, cur, &tmp); } }