* This function adds a node of certain gain into a partition **************************************************************************/
| 137 | * This function adds a node of certain gain into a partition |
| 138 | **************************************************************************/ |
| 139 | int PQueueInsert(PQueueType *queue, int node, int gain) |
| 140 | { |
| 141 | int i, j, k; |
| 142 | idxtype *locator; |
| 143 | ListNodeType *newnode; |
| 144 | KeyValueType *heap; |
| 145 | |
| 146 | if (queue->type == 1) { |
| 147 | ASSERT(gain >= -queue->ngainspan && gain <= queue->pgainspan); |
| 148 | |
| 149 | /* Allocate and add the node */ |
| 150 | queue->nnodes++; |
| 151 | newnode = queue->nodes + node; |
| 152 | |
| 153 | /* Attach this node in the doubly-linked list */ |
| 154 | newnode->next = queue->buckets[gain]; |
| 155 | newnode->prev = NULL; |
| 156 | if (newnode->next != NULL) |
| 157 | newnode->next->prev = newnode; |
| 158 | queue->buckets[gain] = newnode; |
| 159 | |
| 160 | if (queue->maxgain < gain) |
| 161 | queue->maxgain = gain; |
| 162 | } |
| 163 | else { |
| 164 | ASSERT(CheckHeap(queue)); |
| 165 | |
| 166 | heap = queue->heap; |
| 167 | locator = queue->locator; |
| 168 | |
| 169 | ASSERT(locator[node] == -1); |
| 170 | |
| 171 | i = queue->nnodes++; |
| 172 | while (i > 0) { |
| 173 | j = (i-1)/2; |
| 174 | if (heap[j].key < gain) { |
| 175 | heap[i] = heap[j]; |
| 176 | locator[heap[i].val] = i; |
| 177 | i = j; |
| 178 | } |
| 179 | else |
| 180 | break; |
| 181 | } |
| 182 | ASSERT(i >= 0); |
| 183 | heap[i].key = gain; |
| 184 | heap[i].val = node; |
| 185 | locator[node] = i; |
| 186 | |
| 187 | ASSERT(CheckHeap(queue)); |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /************************************************************************* |
no test coverage detected