| 1 |
/* |
| 2 |
# This file is part of the Astrometry.net suite. |
| 3 |
# Licensed under a 3-clause BSD style license - see LICENSE |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#ifdef _MSC_VER |
| 10 |
#include <io.h> |
| 11 |
#else |
| 12 |
#include <unistd.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <math.h> |
| 17 |
#include <assert.h> |
| 18 |
|
| 19 |
#include "permutedsort.h" |
| 20 |
#include "os-features.h" // for qsort_r |
| 21 |
|
| 22 |
#ifdef _MSC_VER |
| 23 |
#if _MSC_VER >= 1600 |
| 24 |
#include <stdint.h> |
| 25 |
#else |
| 26 |
#include <stdint_msc.h> |
| 27 |
#endif |
| 28 |
#else |
| 29 |
#include <stdint.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifdef _MSC_VER |
| 33 |
typedef long off_t; |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifdef _MSC_VER |
| 37 |
#include <float.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
int* permutation_init(int* perm, int N) { |
| 41 |
int i; |
| 42 |
if (!N) |
| 43 |
return perm; |
| 44 |
if (!perm) |
| 45 |
perm = malloc(sizeof(int) * N); |
| 46 |
for (i=0; i<N; i++) |
| 47 |
perm[i] = i; |
| 48 |
return perm; |
| 49 |
} |
| 50 |
|
| 51 |
void permutation_apply(const int* perm, int Nperm, const void* inarray, |
| 52 |
void* outarray, int elemsize) { |
| 53 |
void* temparr = NULL; |
| 54 |
int i; |
| 55 |
const char* cinput; |
| 56 |
char* coutput; |
| 57 |
|
| 58 |
if (inarray == outarray) { |
| 59 |
temparr = malloc(elemsize * Nperm); |
| 60 |
coutput = temparr; |
| 61 |
} else |
| 62 |
coutput = outarray; |
| 63 |
|
| 64 |
cinput = inarray; |
| 65 |
for (i=0; i<Nperm; i++) |
| 66 |
memcpy(coutput + i * elemsize, cinput + perm[i] * elemsize, elemsize); |
| 67 |
|
| 68 |
if (inarray == outarray) { |
| 69 |
memcpy(outarray, temparr, elemsize * Nperm); |
| 70 |
free(temparr); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
struct permuted_sort_t { |
| 75 |
int (*compare)(const void*, const void*); |
| 76 |
const void* data_array; |
| 77 |
int data_array_stride; |
| 78 |
}; |
| 79 |
typedef struct permuted_sort_t permsort_t; |
| 80 |
|
| 81 |
// This is the comparison function we use. |
| 82 |
static int QSORT_COMPARISON_FUNCTION(compare_permuted, void* user, const void* v1, const void* v2) { |
| 83 |
permsort_t* ps = user; |
| 84 |
off_t i1 = *(int*)v1; |
| 85 |
off_t i2 = *(int*)v2; |
| 86 |
const void *val1, *val2; |
| 87 |
const char* darray = ps->data_array; |
| 88 |
val1 = darray + i1 * ps->data_array_stride; |
| 89 |
val2 = darray + i2 * ps->data_array_stride; |
| 90 |
return ps->compare(val1, val2); |
| 91 |
} |
| 92 |
|
| 93 |
int* permuted_sort(const void* realarray, int array_stride, |
| 94 |
int (*compare)(const void*, const void*), |
| 95 |
int* perm, int N) { |
| 96 |
permsort_t ps; |
| 97 |
if (!perm) |
| 98 |
perm = permutation_init(perm, N); |
| 99 |
|
| 100 |
ps.compare = compare; |
| 101 |
ps.data_array = realarray; |
| 102 |
ps.data_array_stride = array_stride; |
| 103 |
|
| 104 |
QSORT_R(perm, N, sizeof(int), &ps, compare_permuted); |
| 105 |
|
| 106 |
return perm; |
| 107 |
} |
| 108 |
|
| 109 |
#ifdef _MSC_VER |
| 110 |
#define COMPARE(d1, d2, op1, op2) \ |
| 111 |
if (d1 op1 d2) return -1; \ |
| 112 |
if (d1 op2 d2) return 1; \ |
| 113 |
/* explicitly test for equality, to catch NaNs*/ \ |
| 114 |
if (d1 == d2) return 0; \ |
| 115 |
if (_isnan(d1) && _isnan(d2)) return 0; \ |
| 116 |
if (_isnan(d1)) return 1; \ |
| 117 |
if (_isnan(d2)) return -1; \ |
| 118 |
assert(0); return 0; |
| 119 |
#else |
| 120 |
#define COMPARE(d1, d2, op1, op2) \ |
| 121 |
if (d1 op1 d2) return -1; \ |
| 122 |
if (d1 op2 d2) return 1; \ |
| 123 |
/* explicitly test for equality, to catch NaNs*/ \ |
| 124 |
if (d1 == d2) return 0; \ |
| 125 |
if (isnan(d1) && isnan(d2)) return 0; \ |
| 126 |
if (isnan(d1)) return 1; \ |
| 127 |
if (isnan(d2)) return -1; \ |
| 128 |
assert(0); return 0; |
| 129 |
#endif |
| 130 |
|
| 131 |
//printf("d1=%g, d2=%g\n", d1, d2); |
| 132 |
|
| 133 |
#define INTCOMPARE(i1, i2, op1, op2) \ |
| 134 |
if (i1 op1 i2) return -1; \ |
| 135 |
if (i1 op2 i2) return 1; \ |
| 136 |
return 0; |
| 137 |
|
| 138 |
int compare_doubles_asc(const void* v1, const void* v2) { |
| 139 |
const double d1 = *(double*)v1; |
| 140 |
const double d2 = *(double*)v2; |
| 141 |
COMPARE(d1, d2, <, >); |
| 142 |
} |
| 143 |
|
| 144 |
int compare_doubles_desc(const void* v1, const void* v2) { |
| 145 |
// (note that v1,v2 are flipped) |
| 146 |
const double d1 = *(double*)v1; |
| 147 |
const double d2 = *(double*)v2; |
| 148 |
COMPARE(d1, d2, >, <); |
| 149 |
} |
| 150 |
|
| 151 |
int compare_floats_asc(const void* v1, const void* v2) { |
| 152 |
float f1 = *(float*)v1; |
| 153 |
float f2 = *(float*)v2; |
| 154 |
COMPARE(f1, f2, <, >); |
| 155 |
} |
| 156 |
|
| 157 |
int compare_floats_desc(const void* v1, const void* v2) { |
| 158 |
float f1 = *(float*)v1; |
| 159 |
float f2 = *(float*)v2; |
| 160 |
COMPARE(f1, f2, >, <); |
| 161 |
} |
| 162 |
|
| 163 |
int compare_int64_asc(const void* v1, const void* v2) { |
| 164 |
int64_t i1 = *(int64_t*)v1; |
| 165 |
int64_t i2 = *(int64_t*)v2; |
| 166 |
INTCOMPARE(i1, i2, <, >); |
| 167 |
} |
| 168 |
|
| 169 |
int compare_int64_desc(const void* v1, const void* v2) { |
| 170 |
int64_t i1 = *(int64_t*)v1; |
| 171 |
int64_t i2 = *(int64_t*)v2; |
| 172 |
INTCOMPARE(i1, i2, >, <); |
| 173 |
} |
| 174 |
|
| 175 |
// Versions for use with QSORT_R |
| 176 |
int QSORT_COMPARISON_FUNCTION(compare_floats_asc_r, |
| 177 |
void* thunk, const void* v1, const void* v2) { |
| 178 |
return compare_floats_asc(v1, v2); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
#undef COMPARE |
| 183 |
#undef INTCOMPARE |
| 184 |
|
| 185 |
int compare_ints_asc(const void* v1, const void* v2) { |
| 186 |
const int d1 = *(int*)v1; |
| 187 |
const int d2 = *(int*)v2; |
| 188 |
if (d1 < d2) |
| 189 |
return -1; |
| 190 |
if (d1 > d2) |
| 191 |
return 1; |
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
int compare_ints_desc(const void* v1, const void* v2) { |
| 196 |
return compare_ints_asc(v2, v1); |
| 197 |
} |
| 198 |
|
| 199 |
int compare_uchars_asc(const void* v1, const void* v2) { |
| 200 |
const unsigned char d1 = *(unsigned char*)v1; |
| 201 |
const unsigned char d2 = *(unsigned char*)v2; |
| 202 |
if (d1 < d2) |
| 203 |
return -1; |
| 204 |
if (d1 > d2) |
| 205 |
return 1; |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
int compare_uchars_desc(const void* v1, const void* v2) { |
| 210 |
return compare_ints_desc(v2, v1); |
| 211 |
} |