1 /*
2 * Copyright (c) 2007 - 2015 Joseph Gaeddert
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 //
24 // optim.common.c
25 //
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include "liquid.internal.h"
31
32
33 // optimization threshold switch
34 // _u0 : first utility
35 // _u1 : second utility
36 // _minimize : minimize flag
37 //
38 // returns:
39 // (_u0 > _u1) if (_minimize == 1)
40 // (_u0 < _u1) otherwise
optim_threshold_switch(float _u0,float _u1,int _minimize)41 int optim_threshold_switch(float _u0,
42 float _u1,
43 int _minimize)
44 {
45 return _minimize ? _u0 > _u1 : _u0 < _u1;
46 }
47
48 // sort values by index
49 // _v : input values [size: _len x 1]
50 // _rank : output rank array (indices) [size: _len x 1]
51 // _len : length of input array
52 // _descending : descending/ascending
optim_sort(float * _v,unsigned int * _rank,unsigned int _len,int _descending)53 void optim_sort(float *_v,
54 unsigned int * _rank,
55 unsigned int _len,
56 int _descending)
57 {
58 unsigned int i, j, tmp_index;
59
60 for (i=0; i<_len; i++)
61 _rank[i] = i;
62
63 for (i=0; i<_len; i++) {
64 for (j=_len-1; j>i; j--) {
65 //if (_v[_rank[j]]>_v[_rank[j-1]]) {
66 if ( optim_threshold_switch(_v[_rank[j]], _v[_rank[j-1]], _descending) ) {
67 // swap elements
68 tmp_index = _rank[j];
69 _rank[j] = _rank[j-1];
70 _rank[j-1] = tmp_index;
71 }
72 }
73 }
74 }
75
76
77