1 /* ----------------------------------------------------- */
2 /* sort.c                       */
3 /* comparison routeins for various qsorts and bsearch's */
4 /* ----------------------------------------------------- */
5 
6 /*                              */
7 /* P. Beerli                        */
8 /*
9 Copyright 1996-2002 Peter Beerli and Joseph Felsenstein, Seattle WA
10 Copyright 2003-2004 Peter Beerli, Tallahassee FL
11 
12  Permission is hereby granted, free of charge, to any person obtaining
13  a copy of this software and associated documentation files (the
14  "Software"), to deal in the Software without restriction, including
15  without limitation the rights to use, copy, modify, merge, publish,
16  distribute, sublicense, and/or sell copies of the Software, and to
17  permit persons to whom the Software is furnished to do so, subject
18  to the following conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
27  ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
28  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 
31 
32 $Id: sort.c 2158 2013-04-29 01:56:20Z beerli $ */
33 /* ----------------------------------------------------- */
34 /*! \file sort.c */
35 
36 
37 /* include files */
38 #include "migration.h"
39 #include "sort.h"
40 /* private functions */
41 
42 /* public functions */
43 int
charcmp(const void * v1,const void * v2)44 charcmp (const void *v1, const void *v2)
45 {
46     if (*(char *) v1 < *(char *) v2)
47     {
48         return -1;
49     }
50     else
51     {
52         if (*(char *) v1 > *(char *) v2)
53         {
54             return 1;
55         }
56         else
57             return 0;
58     }
59 }
60 
61 int
stringcmp(const void * v1,const void * v2)62 stringcmp (const void *v1, const void *v2)
63 {
64     if (strcmp ((char *) v1, (char *) v2) < 0)
65     {
66         return -1;
67     }
68     else
69     {
70         if (strcmp ((char *) v1, (char *) v2) > 1)
71             return 1;
72         else
73             return 0;
74     }
75 }
76 
77 int
numcmp(const void * v1,const void * v2)78 numcmp (const void *v1, const void *v2)
79 {
80     if (*(MYREAL *) v1 < *(MYREAL *) v2)
81     {
82         return -1;
83     }
84     else
85     {
86         if (*(MYREAL *) v1 > *(MYREAL *) v2)
87         {
88             return 1;
89         }
90         else
91             return 0;
92     }
93 }
94 
95 int
paircmp(const void * v1,const void * v2)96 paircmp (const void *v1, const void *v2)
97 {
98     if (((MYREAL *) v1)[1] < ((MYREAL *) v2)[1])
99     {
100         return -1;
101     }
102     else
103     {
104         if (((MYREAL *) v1)[1] > ((MYREAL *) v2)[1])
105         {
106             return 1;
107         }
108         else
109             return 0;
110     }
111 }
112 
113 int
paircmp_first(const void * v1,const void * v2)114 paircmp_first (const void *v1, const void *v2)
115 {
116     if (((MYREAL *) v1)[0] < ((MYREAL *) v2)[0])
117     {
118         return -1;
119     }
120     else
121     {
122         if (((MYREAL *) v1)[0] > ((MYREAL *) v2)[0])
123         {
124             return 1;
125         }
126         else
127             return 0;
128     }
129 }
130 
131 
132 
paired_qsort2(pair * x,long xelem)133 void paired_qsort2(pair *x, long xelem)
134 {
135     long oldli;
136     long li;
137     long elements;
138 
139     // sort using the second element
140     qsort(x,xelem,sizeof(pair),paircmp);
141 
142     // with elements of the same histogram size sort by their first element
143     oldli = 0;
144     elements = 1;
145     for(li=1; li < xelem; li++)
146     {
147         if(x[li][1] == x[li-1][1])
148         {
149             elements++;
150             continue;
151         }
152         else
153         {
154             if(elements > 1)
155                 qsort(x+oldli,elements,sizeof(pair),paircmp_first);
156             elements=1;
157             oldli = li;
158         }
159     }
160 }
161 
162 
163 int
floatcmp(const void * v1,const void * v2)164 floatcmp (const void *v1, const void *v2)
165 {
166     if (*(float *) v1 < *(float *) v2)
167     {
168         return -1;
169     }
170     else
171     {
172         if (*(float *) v1 > *(float *) v2)
173         {
174             return 1;
175         }
176         else
177             return 0;
178     }
179 }
180 
181 
182 int
longcmp(const void * v1,const void * v2)183 longcmp (const void *v1, const void *v2)
184 {
185     if (*(long *) v1 < *(long *) v2)
186     {
187         return -1;
188     }
189     else
190     {
191         if (*(long *) v1 > *(long *) v2)
192         {
193             return 1;
194         }
195         else
196             return 0;
197     }
198 }
199 
200 int
intcmp(const void * v1,const void * v2)201 intcmp (const void *v1, const void *v2)
202 {
203     if (*(int *) v1 < *(int *) v2)
204     {
205         return -1;
206     }
207     else
208     {
209         if (*(int *) v1 > *(int *) v2)
210         {
211             return 1;
212         }
213         else
214             return 0;
215     }
216 }
217 
218 int
agecmp(const void * x,const void * y)219 agecmp (const void *x, const void *y)
220 {
221   vtlist * xx = (vtlist *) x;
222   vtlist * yy = (vtlist *) y;
223   MYREAL xa = xx->age;
224   MYREAL ya = yy->age;
225 
226   if (xa < ya)
227     {
228         return -1;
229     }
230     else
231     {
232         if (xa != ya)
233         {
234             return 1;
235         }
236         else
237             return 0;
238     }
239 }
240 
241 int
delcmp(const void * x,const void * y)242 delcmp (const void *x, const void *y)
243 {
244     if ((*((node **) x))->id < (*((node **) y))->id)
245     {
246         return -1;
247     }
248     else
249     {
250         if ((*((node **) x))->id == (*((node **) y))->id)
251         {
252             return 0;
253         }
254         else
255             return 1;
256     }
257 }
258 
259 int
migr_time_cmp(const void * x,const void * y)260 migr_time_cmp (const void *x, const void *y)
261 {
262     if (((migr_table_fmt *) x)->time < ((migr_table_fmt *) y)->time)
263     {
264         return -1;
265     }
266     else
267     {
268         if (((migr_table_fmt *) x)->time == ((migr_table_fmt *) y)->time)
269         {
270             return 0;
271         }
272         else
273             return 1;
274     }
275 }
276 
277 
278 int
searchagecmp(const void * x,const void * y)279 searchagecmp (const void *x, const void *y)
280 {
281     MYREAL xx = (MYREAL) *((MYREAL *) x);
282     MYREAL age = (MYREAL) ((vtlist *) y)->age;
283 
284     if (xx < age)
285     {
286         return -1;
287     }
288     else
289     {
290         if (xx == age)
291         {
292             return 0;
293         }
294         else
295             return 1;
296     }
297 
298 }
299 
300 
301 int
aiccmp(const void * x,const void * y)302 aiccmp (const void *x, const void *y)
303 {
304     if (((aic_fmt *) x)->aic < ((aic_fmt *) y)->aic)
305     {
306         return -1;
307     }
308     else
309     {
310         if (((aic_fmt *) x)->aic == ((aic_fmt *) y)->aic)
311         {
312             return 0;
313         }
314         else
315             return 1;
316     }
317 }
318