mcxHeap* mcxHeapNew ( mcxHeap* h , dim heapSize , dim elemSize , int (*cmp) (const void* lft, const void* rgt) ) { mcxHeap* heap = mcxHeapInit(h) ; mcxstatus status = STATUS_FAIL ; char* base ; do { if (!heap) break ; if (!(heap->base = mcxAlloc (heapSize*elemSize, RETURN_ON_FAIL))) break ; status = STATUS_OK ; } while (0) ; if (status) { mcxHeapFree(&heap) ; return NULL ; } heap->heapSize = heapSize ; heap->elemSize = elemSize ; heap->cmp = cmp ; heap->n_inserted = 0 ; base = (char*) heap->base ; return heap ; }
void handle_top ( mclx* mx , mcxTing* sa ) { long num = -1 ; dim t ; mcxHeap* hp ; if (mcxStrTol(sa->str, &num, NULL) || num < 0) { fprintf(stderr, "(error number-no-good)\n") ; return ; } if (!num || (dim) num > N_COLS(mx)) num = N_COLS(mx) /* Could use mclxColSizes instead */ ; hp = mcxHeapNew ( NULL , num , sizeof(mclp) , mclpValRevCmp ) ; for (t=0;t<N_COLS(mx);t++) { mclp ivp ; ivp.idx = mx->cols[t].vid ; ivp.val = mx->cols[t].n_ivps ; mcxHeapInsert(hp, &ivp) ; } qsort(hp->base, hp->n_inserted, hp->elemSize, hp->cmp) /* warning this destroys the heap structure */ ; for (t=0; t<hp->n_inserted;t++) { char* p = (char*) hp->base + (t*hp->elemSize) ; mclp* ivp = (void*) p ; const char* s = tab_g ? mclTabGet(tab_g, (long) ivp->idx, NULL) : NULL ; if (s) fprintf(stderr, "%20s : %6.0f\n", s, (double) ivp->val) ; else fprintf(stderr, "%20ld : %6.0f\n", (long) ivp->idx, (double) ivp->val) ; } mcxHeapFree(&hp) ; }