forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplaceHa.c
86 lines (68 loc) · 2.28 KB
/
replaceHa.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/***********************************************************
*
* replaceHa.c (for spread)
* This routine is to delete a cell in a heap.
* It 1) searches the cell backward and sequentially from
* the heap (if not found, returns a error message),
* 2) repalce that cell with the new min_cost and
* restore a heap order.
*
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <grass/gis.h>
#include "costHa.h"
#include "local_proto.h"
void
replaceHa(float new_min_cost, float angle, int row, int col,
struct costHa *heap, long *heap_len)
{
long i, smaller_child = 0;
G_debug(4, "in replaceHa()");
if (*heap_len < 1)
G_fatal_error("Programming ERROR: can't delete a cell from an empty list");
/* search the cell with row and col from the heap */
for (i = *heap_len; i >= 0; i--) {
if (heap[i].row == row && heap[i].col == col)
break;
}
if (i == 0)
G_fatal_error("Programming ERROR: can't find the old_cell from the list");
/* replace this cell, fix the heap */
/*take care upward */
G_debug(4, "in replaceHa() before first while");
while (i > 1 && new_min_cost < heap[i / 2].min_cost) {
heap[i].min_cost = heap[i / 2].min_cost;
heap[i].angle = heap[i / 2].angle;
heap[i].row = heap[i / 2].row;
heap[i].col = heap[i / 2].col;
i = i / 2;
}
/*take care downward */
if (2 * i <= *heap_len)
smaller_child = 2 * i;
if ((2 * i < *heap_len) &&
(heap[2 * i].min_cost > heap[2 * i + 1].min_cost))
smaller_child++;
G_debug(4, "in replaceHa() before second while. smaller_child=%ld",
smaller_child);
while ( (smaller_child <= *heap_len) && (smaller_child > 0) &&
(new_min_cost > heap[smaller_child].min_cost)) {
heap[i].min_cost = heap[smaller_child].min_cost;
heap[i].angle = heap[smaller_child].angle;
heap[i].row = heap[smaller_child].row;
heap[i].col = heap[smaller_child].col;
i = smaller_child;
smaller_child = 2 * i;
if ((2 * i < *heap_len) &&
(heap[2 * i].min_cost > heap[2 * i + 1].min_cost))
smaller_child++;
}
/*now i is the right position */
heap[i].min_cost = new_min_cost;
heap[i].angle = angle;
heap[i].row = row;
heap[i].col = col;
G_debug(4, "replaceHa() done");
return;
}