source: soft/giet_vm/applications/rosenfeld/nrc2/src/nrsort1.c @ 826

Last change on this file since 826 was 826, checked in by meunier, 7 years ago
  • Mise à jour NR2 et Rosenfeld
File size: 9.6 KB
Line 
1/* ----------------- */
2/* --- nrsort1.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 "nrsort1.h"
27
28
29#undef extractnz_boundaries_type_vector
30#define extractnz_boundaries_type_vector(t) \
31void short_name(t,extractnz_boundaries_,vector)(t * v, int32_t nl, int32_t nh, int32_t * nlnz, int32_t * nhnz) \
32{                                      \
33    int32_t left = nl;                 \
34    int32_t right = nh;                \
35    while (!v[left] && left <= nh) {   \
36        left++;                        \
37    }                                  \
38    while (!v[right] && right >= nl) { \
39        right--;                       \
40    }                                  \
41    *nlnz = left;                      \
42    *nhnz = right;                     \
43}
44
45
46extractnz_boundaries_type_vector(int8_t);
47extractnz_boundaries_type_vector(uint8_t);
48extractnz_boundaries_type_vector(int16_t);
49extractnz_boundaries_type_vector(uint16_t);
50extractnz_boundaries_type_vector(int32_t);
51extractnz_boundaries_type_vector(uint32_t);
52extractnz_boundaries_type_vector(int64_t);
53extractnz_boundaries_type_vector(uint64_t);
54extractnz_boundaries_type_vector(float);
55extractnz_boundaries_type_vector(double);
56
57
58
59void sort_index_f64vector_selection(double * key, int32_t nl, int32_t nh, int32_t * index)
60{
61    int32_t pos, tmp;
62    double x, min;
63
64    for (int32_t i = nl; i<=nh; i++) {
65        index[i] = i - nl;
66    }
67
68    for (int32_t i = nl; i < nh; i++) {
69        min = key[i];
70        pos = i;
71        for (int32_t j = i + 1; j <= nh; j++) {
72            x = key[j];
73            if (x < min) {
74                min = x;
75                pos = j;
76            }
77        }
78        key[pos] = key[i];
79        key[i]   = min;
80
81        tmp        = index[i];
82        index[i]   = index[pos];
83        index[pos] = tmp;
84    }
85}
86
87void sort_index_ivector_selection_min(int32_t * key, int32_t nl, int32_t nh, int32_t * index)
88{
89    int32_t pos, tmp;
90    int32_t x, min;
91
92    for (int32_t i = nl; i <= nh; i++) {
93        index[i] = i;
94    }
95
96    for (int32_t i = nl; i < nh; i++) {
97        min = key[i];
98        pos = i;
99        for (int32_t j = i + 1; j <= nh; j++) {
100            x = key[j];
101            if (x < min) {
102                min = x;
103                pos = j;
104            }
105        }
106        key[pos] = key[i];
107        key[i]   = min;
108
109        tmp        = index[i];
110        index[i]   = index[pos];
111        index[pos] = tmp;
112    }
113}
114
115void sort_index_ivector_selection_max(int32_t * key, int32_t nl, int32_t nh, int32_t * index)
116{
117    int32_t pos, tmp;
118    int32_t x, max;
119
120    for (int32_t i = nl; i <= nh; i++) {
121        index[i] = i;
122    }
123
124    for (int32_t i = nl; i < nh; i++) {
125        max = key[i];
126        pos = i;
127        for (int32_t j = i + 1; j <= nh; j++) {
128            x = key[j];
129            if (x > max) {
130                max = x;
131                pos = j;
132            }
133        }
134        key[pos] = key[i];
135        key[i]   = max;
136
137        tmp        = index[i];
138        index[i]   = index[pos];
139        index[pos] = tmp;
140    }
141}
142
143void sort_index_vector_selection_min(float * key, int32_t nl, int32_t nh, int32_t * index)
144{
145    int32_t pos, tmp;
146    float x, min;
147
148    for (int32_t i = nl; i <= nh; i++) {
149        index[i] = i;
150    }
151
152    for (int32_t i = nl; i < nh; i++) {
153        min = key[i];
154        pos = i;
155        for (int32_t j = i + 1; j <= nh; j++) {
156            x = key[j];
157            if (x < min) {
158                min = x;
159                pos = j;
160            }
161        }
162        key[pos] = key[i];
163        key[i]   = min;
164
165        tmp        = index[i];
166        index[i]   = index[pos];
167        index[pos] = tmp;
168    }
169}
170
171void sort_index_vector_selection_max(float * key, int32_t nl, int32_t nh, int32_t * index)
172{
173    int32_t pos, tmp;
174    float x, max;
175
176    for (int32_t i = nl; i <= nh; i++) {
177        index[i] = i;
178    }
179
180    for (int32_t i = nl; i < nh; i++) {
181        max = key[i];
182        pos = i;
183        for (int32_t j = i + 1; j <= nh; j++) {
184            x = key[j];
185            if (x > max) {
186                max = x;
187                pos = j;
188            }
189        }
190        key[pos] = key[i];
191        key[i]   = max;
192
193        tmp        = index[i];
194        index[i]   = index[pos];
195        index[pos] = tmp;
196    }
197}
198
199void sort_index_vector_selection_kmin(float * key, int32_t nl, int32_t nh, int32_t * index, int32_t k)
200{
201    /*
202     * ATTENTION, le tableau index DOIT etre initialise
203     * @QM : pourquoi est-ce commenté alors ?
204     */
205    int32_t pos, tmp, il, ih;
206    float x, min;
207
208    il = nl;
209    ih = il + k;
210
211    /*
212    for(int32_t i = il; i <= ih; i++) {
213        index[i] = i - il;
214    }
215    */
216
217    for (int32_t i = il; i < ih; i++) {
218        min = key[i];
219        pos = i;
220        for (int32_t j = i + 1; j <= nh; j++) {
221            x = key[j];
222            if (x < min) {
223                min = x;
224                pos = j;
225            }
226        }
227        key[pos] = key[i];
228        key[i]   = min;
229
230        tmp        = index[i];
231        index[i]   = index[pos];
232        index[pos] = tmp;
233    }
234}
235
236void sort_index_ivector_selection_kmin(int32_t * key, int32_t nl, int32_t nh, int32_t * index, int32_t k)
237{
238    /*
239     * ATTENTION, le tableau index DOIT etre initialise
240     */
241    int32_t pos, tmp, il, ih;
242    int32_t x, min;
243
244    il = nl;
245    ih = il + k;
246
247    /*
248    for(int32_t i = il; i <= ih; i++) {
249        index[i] = i - il;
250    }
251    */
252
253    for (int32_t i = il; i < ih; i++) {
254        min = key[i];
255        pos = i;
256        for (int32_t j = i + 1; j <= nh; j++) {
257            x = key[j];
258            if (x < min) {
259                min = x;
260                pos = j;
261            }
262        }
263        key[pos] = key[i];
264        key[i]   = min;
265
266        tmp        = index[i];
267        index[i]   = index[pos];
268        index[pos] = tmp;
269    }
270}
271
272void sort_index_vector_selection_kmax(float * key, int32_t nl, int32_t nh, int32_t * index, int32_t k)
273{
274    /*
275     * ATTENTION, le tableau index DOIT etre initialise
276     */
277    int32_t pos, tmp, il, ih;
278    float x, max;
279
280    il = nl;
281    ih = il + k;
282
283    /*
284    for (int32_t i = il; i <= ih; i++) {
285        index[i] = i - il;
286    }
287    */
288
289    for (int32_t i = il; i < ih; i++) {
290        max = key[i];
291        pos = i;
292        for (int32_t j = i + 1; j <= nh; j++) {
293            x = key[j];
294            if (x > max) {
295                max = x;
296                pos = j;
297            }
298        }
299        key[pos] = key[i];
300        key[i]   = max;
301
302        tmp        = index[i];
303        index[i]   = index[pos];
304        index[pos] = tmp;
305    }
306}
307
308void sort_index_ivector_selection_kmax(int32_t * key, int32_t nl, int32_t nh, int32_t * index, int32_t k)
309{
310    /*
311     * ATTENTION, le tableau index DOIT etre initialise
312     */
313    int32_t pos, tmp, il, ih;
314    int32_t x, max;
315
316    il = nl;
317    ih = il + k;
318
319    /*
320    for (int32_t i = i l; i <= ih; i++) {
321        index[i] = i - il;
322    }
323    */
324
325    for (int32_t i = il; i < ih; i++) {
326        max = key[i];
327        pos = i;
328        for (int32_t j = i + 1; j <= nh; j++) {
329            x = key[j];
330            if (x > max) {
331                max = x;
332                pos = j;
333            }
334        }
335        key[pos] = key[i];
336        key[i]   = max;
337
338        tmp        = index[i];
339        index[i]   = index[pos];
340        index[pos] = tmp;
341    }
342}
343
344
345void sort_bvector_selection_min(int8_t * v, int32_t nl, int32_t nh)
346{
347    int8_t x, min;
348    int32_t pos;
349
350    for (int32_t i = nl; i < nh; i++) {
351        min = v[i];
352        pos = i;
353        for (int32_t j = i + 1; j <= nh; j++) {
354            x = v[j];
355            if (x < min) {
356                min = x;
357                pos = j;
358            }
359        }
360        v[pos] = v[i];
361        v[i]   = min;
362    }
363}
364
365int8_t select_bvector(int8_t * v, int32_t nl, int32_t nh, int32_t k)
366{
367    int32_t il, ih;
368    int8_t x, min, pos;
369
370    il = nl;
371    ih = il + k;
372
373    for (int32_t i = il; i < ih; i++) {
374        min = v[i];
375        pos = i;
376        for (int32_t j = i + 1; j <= nh; j++) {
377            x = v[j];
378            if (x < min) {
379                min = x;
380                pos = j;
381            }
382        }
383        v[pos] = v[i];
384        v[i]   = min;
385    }
386    return v[ih];
387}
388
389rgb8 select_rgb8vector(rgb8 * v, int32_t nl, int32_t nh, int32_t k)
390{
391    int32_t il, ih;
392    int32_t rpos, gpos, bpos;
393
394    rgb8 xi, xj;
395    int8_t r, g, b;
396    int8_t rmin, gmin, bmin;
397
398    il = nl;
399    ih = il + k;
400
401    for (int32_t i = il; i < ih; i++) {
402        xi = v[i];
403        rmin = xi.r;
404        gmin = xi.g;
405        bmin = xi.b;
406        rpos = gpos = bpos = i;
407
408        for (int32_t j = i + 1; j <= nh; j++) {
409            xj = v[j];
410            r = xj.r;
411            g = xj.g;
412            b = xj.b;
413            if (r < rmin) {
414                rmin = r;
415                rpos = j;
416            }
417            if (g < gmin) {
418                gmin = r;
419                gpos = j;
420            }
421            if (b < bmin) {
422                bmin = r;
423                bpos = j;
424            }
425        }
426        v[rpos].r = v[i].r;
427        v[i].r = rmin;
428        v[rpos].g = v[i].g;
429        v[i].g = gmin;
430        v[rpos].b = v[i].b;
431        v[i].b = bmin;
432    }
433    return v[ih];
434}
435
436// Local Variables:
437// tab-width: 4
438// c-basic-offset: 4
439// c-file-offsets:((innamespace . 0)(inline-open . 0))
440// indent-tabs-mode: nil
441// End:
442
443// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
444
Note: See TracBrowser for help on using the repository browser.