GRASS GIS 8 Programmer's Manual 8.2.1(2023)-exported
sort_cell.c
Go to the documentation of this file.
1#include <stdlib.h>
2
3#include <grass/gis.h>
4#include <grass/raster.h>
5#include <grass/stats.h>
6
7static int ascending(const void *aa, const void *bb)
8{
9 const DCELL *a = aa, *b = bb;
10
11 if (*a < *b)
12 return -1;
13 return (*a > *b);
14
15
16 if (Rast_is_d_null_value((DCELL *) a) && Rast_is_d_null_value((DCELL *) b))
17 return 0;
18
19 if (Rast_is_d_null_value((DCELL *) a))
20 return 1;
21
22 if (Rast_is_d_null_value((DCELL *) b))
23 return -1;
24
25 return (*a < *b) ? -1 : (*a > *b) ? 1 : 0;
26}
27
28int sort_cell(DCELL * array, int n)
29{
30 int i, j;
31
32 j = 0;
33 for (i = 0; i < n; i++) {
34 if (!Rast_is_d_null_value(&array[i])) {
35 array[j] = array[i];
36 j++;
37 }
38 }
39 n = j;
40
41 if (n > 0)
42 qsort(array, n, sizeof(DCELL), ascending);
43
44 return n;
45}
46
47int sort_cell_w(DCELL(*array)[2], int n)
48{
49 int i, j;
50
51 j = 0;
52 for (i = 0; i < n; i++) {
53 if (!Rast_is_d_null_value(&array[i][0]) &&
54 !Rast_is_d_null_value(&array[i][1])) {
55 array[j][0] = array[i][0];
56 array[j][1] = array[i][1];
57 j++;
58 }
59 }
60 n = j;
61
62 if (n > 0)
63 qsort(array, n, 2 * sizeof(DCELL), ascending);
64
65 return n;
66}
double b
int sort_cell(DCELL *array, int n)
Definition: sort_cell.c:28
int sort_cell_w(DCELL(*array)[2], int n)
Definition: sort_cell.c:47