forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixHa.c
43 lines (38 loc) · 1.3 KB
/
fixHa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**************************************************
*
* fixHa.c (for spread)
* This routine is to restore a 'heap' order upon
* removing a cell from the heap, which is ordered
* by the min_cost of a cell.
*
**************************************************/
#include <stdlib.h>
#include "costHa.h"
#include "local_proto.h"
struct costHa *fixHa(long go_pos, struct costHa *heap, long heap_len)
{
long vacant, smaller_child;
if (heap_len == 0)
return NULL;
vacant = go_pos;
while (2 * vacant <= heap_len) {
smaller_child = 2 * vacant;
if ((2 * vacant < heap_len) &&
(heap[2 * vacant + 1].min_cost < heap[2 * vacant].min_cost))
smaller_child++;
if (heap[heap_len].min_cost > heap[smaller_child].min_cost) {
heap[vacant].min_cost = heap[smaller_child].min_cost;
heap[vacant].angle = heap[smaller_child].angle;
heap[vacant].row = heap[smaller_child].row;
heap[vacant].col = heap[smaller_child].col;
vacant = smaller_child;
}
else
break;
}
heap[vacant].min_cost = heap[heap_len].min_cost;
heap[vacant].angle = heap[heap_len].angle;
heap[vacant].row = heap[heap_len].row;
heap[vacant].col = heap[heap_len].col;
return heap;
}