-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
sort_cell.c
66 lines (50 loc) · 1.1 KB
/
sort_cell.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
#include <stdlib.h>
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/stats.h>
static int ascending(const void *aa, const void *bb)
{
const DCELL *a = aa, *b = bb;
if (*a < *b)
return -1;
return (*a > *b);
if (Rast_is_d_null_value((DCELL *) a) && Rast_is_d_null_value((DCELL *) b))
return 0;
if (Rast_is_d_null_value((DCELL *) a))
return 1;
if (Rast_is_d_null_value((DCELL *) b))
return -1;
return (*a < *b) ? -1 : (*a > *b) ? 1 : 0;
}
int sort_cell(DCELL * array, int n)
{
int i, j;
j = 0;
for (i = 0; i < n; i++) {
if (!Rast_is_d_null_value(&array[i])) {
array[j] = array[i];
j++;
}
}
n = j;
if (n > 0)
qsort(array, n, sizeof(DCELL), ascending);
return n;
}
int sort_cell_w(DCELL(*array)[2], int n)
{
int i, j;
j = 0;
for (i = 0; i < n; i++) {
if (!Rast_is_d_null_value(&array[i][0]) &&
!Rast_is_d_null_value(&array[i][1])) {
array[j][0] = array[i][0];
array[j][1] = array[i][1];
j++;
}
}
n = j;
if (n > 0)
qsort(array, n, 2 * sizeof(DCELL), ascending);
return n;
}