* This function initializes the data structures of the priority queue **************************************************************************/
| 21 | * This function initializes the data structures of the priority queue |
| 22 | **************************************************************************/ |
| 23 | void PQueueInit(CtrlType *ctrl, PQueueType *queue, int maxnodes, int maxgain) |
| 24 | { |
| 25 | int i, j, ncore; |
| 26 | |
| 27 | queue->nnodes = 0; |
| 28 | queue->maxnodes = maxnodes; |
| 29 | |
| 30 | queue->buckets = NULL; |
| 31 | queue->nodes = NULL; |
| 32 | queue->heap = NULL; |
| 33 | queue->locator = NULL; |
| 34 | |
| 35 | if (maxgain > PLUS_GAINSPAN || maxnodes < 500) |
| 36 | queue->type = 2; |
| 37 | else |
| 38 | queue->type = 1; |
| 39 | |
| 40 | if (queue->type == 1) { |
| 41 | queue->pgainspan = amin(PLUS_GAINSPAN, maxgain); |
| 42 | queue->ngainspan = amin(NEG_GAINSPAN, maxgain); |
| 43 | |
| 44 | j = queue->ngainspan+queue->pgainspan+1; |
| 45 | |
| 46 | ncore = 2 + (sizeof(ListNodeType)/sizeof(idxtype))*maxnodes + (sizeof(ListNodeType *)/sizeof(idxtype))*j; |
| 47 | |
| 48 | if (WspaceAvail(ctrl) > ncore) { |
| 49 | queue->nodes = (ListNodeType *)idxwspacemalloc(ctrl, (sizeof(ListNodeType)/sizeof(idxtype))*maxnodes); |
| 50 | queue->buckets = (ListNodeType **)idxwspacemalloc(ctrl, (sizeof(ListNodeType *)/sizeof(idxtype))*j); |
| 51 | queue->mustfree = 0; |
| 52 | } |
| 53 | else { /* Not enough memory in the wspace, allocate it */ |
| 54 | queue->nodes = (ListNodeType *)idxmalloc((sizeof(ListNodeType)/sizeof(idxtype))*maxnodes, "PQueueInit: queue->nodes"); |
| 55 | queue->buckets = (ListNodeType **)idxmalloc((sizeof(ListNodeType *)/sizeof(idxtype))*j, "PQueueInit: queue->buckets"); |
| 56 | queue->mustfree = 1; |
| 57 | } |
| 58 | |
| 59 | for (i=0; i<maxnodes; i++) |
| 60 | queue->nodes[i].id = i; |
| 61 | |
| 62 | for (i=0; i<j; i++) |
| 63 | queue->buckets[i] = NULL; |
| 64 | |
| 65 | queue->buckets += queue->ngainspan; /* Advance buckets by the ngainspan proper indexing */ |
| 66 | queue->maxgain = -queue->ngainspan; |
| 67 | } |
| 68 | else { |
| 69 | queue->heap = (KeyValueType *)idxwspacemalloc(ctrl, (sizeof(KeyValueType)/sizeof(idxtype))*maxnodes); |
| 70 | queue->locator = idxwspacemalloc(ctrl, maxnodes); |
| 71 | idxset(maxnodes, -1, queue->locator); |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /************************************************************************* |
no test coverage detected