GIRAFFE Pipeline Reference Manual

giarray.c
1/*
2 * This file is part of the GIRAFFE Pipeline
3 * Copyright (C) 2002-2019 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include <string.h>
25
26#include <cxmemory.h>
27#include <cxmessages.h>
28
29#include "giarray.h"
30
31
40inline static void
41_giraffe_swap(cxdouble* a, cxdouble* b)
42{
43 register cxdouble tmp = *a;
44
45 *a = *b;
46 *b = tmp;
47
48 return;
49
50}
51
52
53inline static cxint
54_giraffe_array_heap_sort(cxdouble* array, cxsize size)
55{
56
57 register cxsize l = size >> 1;
58
59 cxsize ir = size - 1;
60
61
62 while (1) {
63
64 register cxsize i = 0;
65 register cxsize j = 0;
66
67 register cxdouble rra = 0.;
68
69 if ( l > 0) {
70 rra = array[--l]; /* Still in hiring phase. */
71 }
72 else { /* In retirement-and-promotion phase. */
73 rra = array[ir]; /* Clear a space at end of array. */
74 array[ir--] = array[0]; /* Retire the top of the heap into it. */
75 if (ir == 0) { /* Done with the last promotion. */
76 array[0] = rra; /* The least competent worker of all! */
77 return 0;
78 }
79 }
80
81 i = l; /* Whether in the hiring phase or promotion */
82 j = (l << 1) + 1; /* phase, we here set up to sift down element */
83 while (j <= ir) { /* rra to its proper level. */
84
85 /* Compare to the better underling. */
86 if (j < ir && (array[j+1] - array[j]) > DBL_EPSILON) {
87 ++j;
88 }
89
90 if (array[j] - rra > DBL_EPSILON) { /* Demote rra. */
91 array[i] = array[j];
92 j += (i = j) + 1;
93 }
94 else {
95 j = ir + 1;
96 }
97 }
98 array[i] = rra; /* Put into its slot. */
99 }
100
101}
102
103
104inline static cxdouble
105_giraffe_array_get_sorted(cxdouble* a, cxint n, cxint k)
106{
107
108 register cxint l = 0;
109 register cxint m = n - 1;
110
111
112 while (l < m) {
113
114 register cxint i = l;
115 register cxint j = m;
116
117 register cxdouble x = a[k];
118
119
120 do {
121
122 while (x - a[i] > DBL_EPSILON) {
123 ++i;
124 }
125
126 while (a [j] - x > DBL_EPSILON) {
127 --j;
128 }
129
130 if (i <= j) {
131 _giraffe_swap(&a[i], &a[j]) ;
132 ++i;
133 --j;
134 }
135
136 } while (i <= j);
137
138 if (j < k) {
139 l = i;
140 }
141
142 if (k < i) {
143 m = j;
144 }
145
146 }
147
148 return a[k];
149
150}
151
152
168cxint
169giraffe_array_sort(cxdouble* array, cxsize size)
170{
171
172 return _giraffe_array_heap_sort(array, size);
173
174}
175
176
177cxdouble
178giraffe_array_mean(const cxdouble* array, cxsize size)
179{
180 register cxsize i = 0;
181
182 register cxdouble sum = 0.;
183
184
185 for (i = 0; i < size; i++) {
186 sum += array[i];
187 }
188
189 return sum / size;
190
191}
192
193
194cxdouble
195giraffe_array_median(const cxdouble* array, cxsize size)
196{
197
198 register cxsize position = (size & 1) == 1 ? size / 2 : size / 2 - 1;
199
200 cxdouble median = 0.;
201 cxdouble* a = NULL;
202
203
204 cx_assert(array != NULL);
205
206 a = cx_calloc(size, sizeof(cxdouble));
207 memcpy(a, array, size * sizeof(cxdouble));
208
209
210 median = _giraffe_array_get_sorted(a, size, position);
211
212 cx_free(a);
213 a = NULL;
214
215 return median;
216
217}
218
cxint giraffe_array_sort(cxdouble *array, cxsize size)
Sorts an array in ascending order.
Definition: giarray.c:169

This file is part of the GIRAFFE Pipeline Reference Manual 2.18.4.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Mon Jul 7 2025 10:23:39 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2004