source: soft/giet_vm/applications/rosenfeld/nrc2/src/nrsort2.c @ 827

Last change on this file since 827 was 826, checked in by meunier, 7 years ago
  • Mise à jour NR2 et Rosenfeld
File size: 11.0 KB
Line 
1/* ----------------- */
2/* --- nrsort2.c --- */
3/* ----------------- */
4
5/*
6 * Copyright (c) 2000-2014, Lionel Lacassagne, All rights reserved
7 * Univ Paris Sud XI, CNRS
8 *
9 * Distributed under the Boost Software License, Version 1.0
10 * see accompanying file LICENSE.txt or copy it at
11 * http://www.boost.org/LICENSE_1_0.txt
12 */
13
14
15#include <stdio.h>
16#include <stddef.h>
17#include <stdlib.h>
18
19#include "mypredef.h"
20#include "nrtype.h"
21#include "nrdef.h"
22#include "nrmacro.h"
23#include "nrkernel.h"
24
25#include "nrset1.h"
26#include "nrset2.h"
27
28#include "nrsort1.h"
29#include "nrsort2.h"
30
31
32#undef extractnz_boundaries_type_matrix
33#define extractnz_boundaries_type_matrix(t) \
34void short_name(t,extractnz_boundaries_,matrix)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t * nclnz, int32_t * nchnz) \
35{                                                                                                         \
36    int32_t a, b;                                                                                         \
37    int32_t left, right;                                                                                  \
38    short_name(t,extractnz_boundaries_,vector)(m[nrl], ncl, nch, &left, &right); /* premier intervalle */ \
39    for (int32_t i = nrl + 1; i <= nrh; i++) {                                                            \
40        short_name(t,extractnz_boundaries_,vector)(m[i], ncl, nch, &a, &b);                               \
41        if (a < left) {                                                                                   \
42            left = a; /* agrandissement de l'intervalle SI necessaire (et non le contraire) */            \
43        }                                                                                                 \
44        if (b > right) {                                                                                  \
45            right = b;                                                                                    \
46        }                                                                                                 \
47    }                                                                                                     \
48    *nclnz = left;                                                                                        \
49    *nchnz = right;                                                                                       \
50}
51
52extractnz_boundaries_type_matrix(int8_t);
53extractnz_boundaries_type_matrix(uint8_t);
54extractnz_boundaries_type_matrix(int16_t);
55extractnz_boundaries_type_matrix(uint16_t);
56extractnz_boundaries_type_matrix(int32_t);
57extractnz_boundaries_type_matrix(uint32_t);
58extractnz_boundaries_type_matrix(int64_t);
59extractnz_boundaries_type_matrix(uint64_t);
60extractnz_boundaries_type_matrix(float);
61extractnz_boundaries_type_matrix(double);
62
63
64
65void sort_si32matrix_selection2(int32_t ** m, int32_t nl, int32_t nh)
66{
67    /*
68     * sort an matrix of int32_t with the selection algorithm
69     * the key is supposed to be in row 1
70     * the flag associated, in row 0
71     */
72    int32_t x, min, pos, tmp;
73
74    for (int32_t i = nl; i < nh; i++) {
75        min = m[1][i];
76        pos = i;
77        for (int32_t j = i + 1; j <= nh; j++) {
78            x = m[1][j];
79            if (x < min) {
80                min = x;
81                pos = j;
82            }
83        }
84        m[1][pos] = m[1][i];
85        m[1][i]   = min;
86
87        tmp       = m[0][i];
88        m[0][i]   = m[0][pos];
89        m[0][pos] = tmp;
90    }
91}
92
93void sort_si32matrix_leftpart_selection2(int32_t ** m, int32_t nl, int32_t nh, int32_t len)
94{
95    /*
96     * sort an matrix of int32_t with the selection algorithm
97     * the key is supposed to be in row 1
98     * the flag associated, in row 0
99     * the sort is performed only on the len first items
100     * for selecting the len first smallest values (for Tracking algo)
101     */
102    int32_t x, min, pos, tmp;
103    int32_t ih = nl + len - 1;
104    int32_t * X;
105    int32_t * F;
106
107    F = m[0];
108    X = m[1];
109
110    for (int32_t i = nl; i <= ih; i++) {
111        min = X[i];
112        pos = i;
113        for (int32_t j = i + 1; j <= nh; j++) {
114            x = X[j];
115            if (x < min) {
116                min = x;
117                pos = j;
118            }
119        }
120        X[pos] = X[i];
121        X[i]   = min;
122
123        tmp    = F[i];
124        F[i]   = F[pos];
125        F[pos] = tmp;
126    }
127}
128
129void sort_f64matrix_selection(double * m, int32_t nl, int32_t nh)
130{
131    int32_t pos;
132    float64 x, min;
133
134    for (int32_t i = nl; i < nh; i++) {
135        min = m[i];
136        pos = i;
137        for (int32_t j = i + 1; j <= nh; j++) {
138            x = m[j];
139            if (x < min) {
140                min = x;
141                pos = j;
142            }
143        }
144        m[pos] = m[i];
145        m[i]   = min;
146    }
147}
148
149void sort_si32matrix_selection(int32_t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t nrow)
150{
151    /*
152     * sort an matrix of int32_t with the selection algorithm
153     * the key is supposed to be in row nrow
154     */
155    int32_t x, min, pos, tmp;
156
157    for (int32_t i = ncl; i < nch; i++) {
158        min = m[nrow][i];
159        pos = i;
160        for (int32_t j = i + 1; j <= nch; j++) {
161            x = m[nrow][j];
162            if (x < min) {
163                min = x;
164                pos = j;
165            }
166        }
167
168        // big swap
169        for (int32_t k = nrl; k <= nrh; k++) {
170            tmp = m[k][i];
171            m[k][i] = m[k][pos];
172            m[k][pos] = tmp;
173        }
174    }
175}
176
177void sortv_si32matrix_selection_min(int32_t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t nc)
178{
179    /*
180     * sort an matrix of int, with the selection algorithm.
181     * the key is in column nc
182     * the sort is performed, by doing a purmutation on the lines,
183     * instead of copying the lines.
184     */
185
186    int32_t x, min, pos;
187    int32_t * ptr;
188
189    for (int32_t i = nrl; i < nrh; i++) {
190        min = m[i][nc];
191        pos = i;
192        for (int32_t j = i + 1; j <= nrh; j++) {
193            x = m[j][nc];
194            if (x < min) {
195                min = x;
196                pos = j;
197            }
198        }
199
200        /* permutation des pointeurs de ligne de la matrice */
201        ptr    = m[i];
202        m[i]   = m[pos];
203        m[pos] = ptr;
204    }
205}
206
207void sortv_si32matrix_selection_kmin(int32_t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t nc, int32_t k)
208{
209    /*
210     * sort an matrix of int, with the selection algorithm.
211     * the key is in column nc
212     * the sort is performed, by doing a purmutation on the lines,
213     * instead of copying the lines.
214     */
215    int32_t il = nrl;
216    int32_t ih = nrl + k;
217
218    int32_t x, min, pos;
219    int32_t * ptr;
220
221    for (int32_t i = il; i < ih; i++) {
222        min = m[i][nc];
223        pos = i;
224        for(int32_t j = i + 1; j <= nrh; j++) {
225            x = m[j][nc];
226            if (x < min) {
227                min = x;
228                pos = j;
229            }
230        }
231
232        /* permutation des pointeurs de ligne de la matrice */
233        ptr    = m[i];
234        m[i]   = m[pos];
235        m[pos] = ptr;
236
237    }
238}
239
240void sortv_si32matrix_selection_max(int32_t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t nc)
241{
242    /*
243     * sort an matrix of int, with the selection algorithm.
244     * from max to min
245     * the key is in column nc
246     * the sort is performed, by doing a purmutation on the lines,
247     * instead of copying the lines.
248     */
249
250    int32_t x, max, pos;
251    int32_t * ptr;
252
253    for (int32_t i = nrl; i < nrh; i++) {
254        max = m[i][nc];
255        pos = i;
256        for (int32_t j = i + 1; j <= nrh; j++) {
257            x = m[j][nc];
258            if (x > max) {
259                max = x;
260                pos = j;
261            }
262        }
263
264        /* permutation des pointeurs de ligne de la matrice */
265        ptr    = m[i];
266        m[i]   = m[pos];
267        m[pos] = ptr;
268    }
269}
270
271void sortv_si32matrix_selection_kmax(int32_t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t nc, int32_t k)
272{
273    /*
274     * sort an matrix of int, with the selection algorithm.
275     * from max to min
276     * the key is in column nc
277     * the sort is performed, by doing a purmutation on the lines,
278     * instead of copying the lines.
279     */
280    int32_t x, max, pos;
281    int32_t * ptr;
282
283    for (int32_t i = nrl; i < nrh; i++) {
284        max = m[i][nc];
285        pos = i;
286        for (int32_t j = i + 1; j <= nrh; j++) {
287            x = m[j][nc];
288            if (x > max) {
289                max = x;
290                pos = j;
291            }
292        }
293
294        /* permutation des pointeurs de ligne de la matrice */
295        ptr    = m[i];
296        m[i]   = m[pos];
297        m[pos] = ptr;
298    }
299}
300
301void sort_index_si32matrix_selection_kmin(int32_t ** key, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t ** index, int32_t k)
302{
303    set_si32matrix_j(index, nrl,nrh, ncl,nch);
304    for (int32_t i = nrl; i <= nrh; i++) {
305        sort_index_ivector_selection_kmin(key[i], ncl, nch, index[i], k);
306    }
307}
308
309void sort_index_si32matrix_selection_kmax(int32_t ** key, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t ** index, int32_t k)
310{
311    set_si32matrix_j(index, nrl,nrh, ncl,nch);
312    for (int32_t i = nrl; i <= nrh; i++) {
313        sort_index_ivector_selection_kmax(key[i], ncl, nch, index[i], k);
314    }
315}
316
317void sortv_index_imatrix_selection_max(int ** key, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t * index, int32_t nc)
318{
319    int32_t pos, tmp, il, ih;
320    int32_t x, max;
321    int32_t * ptr;
322
323    il = nrl;
324    ih = nrh;
325
326    for (int32_t i = il; i <= ih; i++) {
327        index[i] = i;
328    }
329
330    for (int32_t i = il; i < ih; i++) {
331        max = key[i][nc];
332        pos = i;
333        for (int32_t j = i + 1; j <= nrh; j++) {
334            x = key[j][nc];
335            if (x > max) {
336                max = x;
337                pos = j;
338            }
339        }
340        /* permutation des pointeurs de ligne de la matrice */
341        ptr      = key[i];
342        key[i]   = key[pos];
343        key[pos] = ptr;
344
345        /* permutation des indices de l'index */
346        tmp        = index[i];
347        index[i]   = index[pos];
348        index[pos] = tmp;
349    }
350}
351
352void sortv_index_imatrix_selection_min(int32_t ** key, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, int32_t * index, int32_t nc)
353{
354    int32_t pos, tmp, il, ih;
355    int32_t x, min;
356    int32_t * ptr;
357
358    il = nrl;
359    ih = nrh;
360
361    for (int32_t i = il; i <= ih; i++) {
362        index[i] = i;
363    }
364
365    for (int32_t i = il; i < ih; i++) {
366        min = key[i][nc];
367        pos = i;
368        for (int32_t j = i + 1; j <= nrh; j++) {
369            x = key[j][nc];
370            if (x < min) {
371                min = x;
372                pos = j;
373            }
374        }
375        /* permutation des pointeurs de ligne de la matrice */
376        ptr      = key[i];
377        key[i]   = key[pos];
378        key[pos] = ptr;
379
380        /* permutation des indices de l'index */
381        tmp        = index[i];
382        index[i]   = index[pos];
383        index[pos] = tmp;
384    }
385}
386
387
388// Local Variables:
389// tab-width: 4
390// c-basic-offset: 4
391// c-file-offsets:((innamespace . 0)(inline-open . 0))
392// indent-tabs-mode: nil
393// End:
394
395// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
396
Note: See TracBrowser for help on using the repository browser.