-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathsort.cpp
More file actions
278 lines (249 loc) · 9.77 KB
/
sort.cpp
File metadata and controls
278 lines (249 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <backend.hpp>
#include <common/ArrayInfo.hpp>
#include <common/err_common.hpp>
#include <copy.hpp>
#include <handle.hpp>
#include <sort.hpp>
#include <sort_by_key.hpp>
#include <sort_index.hpp>
#include <af/algorithm.h>
#include <af/array.h>
#include <af/defines.h>
#include <cstdio>
using af::dim4;
using detail::Array;
using detail::cdouble;
using detail::cfloat;
using detail::createEmptyArray;
using detail::intl;
using detail::schar;
using detail::uchar;
using detail::uint;
using detail::uintl;
using detail::ushort;
template<typename T>
static inline af_array sort(const af_array in, const unsigned dim,
const bool isAscending) {
const Array<T> &inArray = getArray<T>(in);
return getHandle(sort<T>(inArray, dim, isAscending));
}
af_err af_sort(af_array *out, const af_array in, const unsigned dim,
const bool isAscending) {
try {
const ArrayInfo &info = getInfo(in);
af_dtype type = info.getType();
if (info.elements() == 0) { return af_retain_array(out, in); }
DIM_ASSERT(1, info.elements() > 0);
af_array val;
switch (type) {
case f32: val = sort<float>(in, dim, isAscending); break;
case f64: val = sort<double>(in, dim, isAscending); break;
case s32: val = sort<int>(in, dim, isAscending); break;
case u32: val = sort<uint>(in, dim, isAscending); break;
case s16: val = sort<short>(in, dim, isAscending); break;
case u16: val = sort<ushort>(in, dim, isAscending); break;
case s64: val = sort<intl>(in, dim, isAscending); break;
case u64: val = sort<uintl>(in, dim, isAscending); break;
case s8: val = sort<schar>(in, dim, isAscending); break;
case u8: val = sort<uchar>(in, dim, isAscending); break;
case b8: val = sort<char>(in, dim, isAscending); break;
default: TYPE_ERROR(1, type);
}
std::swap(*out, val);
}
CATCHALL;
return AF_SUCCESS;
}
template<typename T>
static inline void sort_index(af_array *val, af_array *idx, const af_array in,
const unsigned dim, const bool isAscending) {
const Array<T> &inArray = getArray<T>(in);
// Initialize Dummy Arrays
Array<T> valArray = createEmptyArray<T>(af::dim4());
Array<uint> idxArray = createEmptyArray<uint>(af::dim4());
sort_index<T>(valArray, idxArray, inArray, dim, isAscending);
*val = getHandle(valArray);
*idx = getHandle(idxArray);
}
af_err af_sort_index(af_array *out, af_array *indices, const af_array in,
const unsigned dim, const bool isAscending) {
try {
const ArrayInfo &info = getInfo(in);
af_dtype type = info.getType();
if (info.elements() <= 0) {
AF_CHECK(af_create_handle(out, 0, nullptr, type));
AF_CHECK(af_create_handle(indices, 0, nullptr, type));
return AF_SUCCESS;
}
af_array val;
af_array idx;
switch (type) {
case f32:
sort_index<float>(&val, &idx, in, dim, isAscending);
break;
case f64:
sort_index<double>(&val, &idx, in, dim, isAscending);
break;
case s32: sort_index<int>(&val, &idx, in, dim, isAscending); break;
case u32: sort_index<uint>(&val, &idx, in, dim, isAscending); break;
case s16:
sort_index<short>(&val, &idx, in, dim, isAscending);
break;
case u16:
sort_index<ushort>(&val, &idx, in, dim, isAscending);
break;
case s64: sort_index<intl>(&val, &idx, in, dim, isAscending); break;
case u64:
sort_index<uintl>(&val, &idx, in, dim, isAscending);
break;
case s8: sort_index<schar>(&val, &idx, in, dim, isAscending); break;
case u8: sort_index<uchar>(&val, &idx, in, dim, isAscending); break;
case b8: sort_index<char>(&val, &idx, in, dim, isAscending); break;
default: TYPE_ERROR(1, type);
}
std::swap(*out, val);
std::swap(*indices, idx);
}
CATCHALL;
return AF_SUCCESS;
}
template<typename Tk, typename Tv>
static inline void sort_by_key(af_array *okey, af_array *oval,
const af_array ikey, const af_array ival,
const unsigned dim, const bool isAscending) {
const Array<Tk> &ikeyArray = getArray<Tk>(ikey);
const Array<Tv> &ivalArray = getArray<Tv>(ival);
// Initialize Dummy Arrays
Array<Tk> okeyArray = createEmptyArray<Tk>(af::dim4());
Array<Tv> ovalArray = createEmptyArray<Tv>(af::dim4());
sort_by_key<Tk, Tv>(okeyArray, ovalArray, ikeyArray, ivalArray, dim,
isAscending);
*okey = getHandle(okeyArray);
*oval = getHandle(ovalArray);
}
template<typename Tk>
void sort_by_key_tmplt(af_array *okey, af_array *oval, const af_array ikey,
const af_array ival, const unsigned dim,
const bool isAscending) {
const ArrayInfo &info = getInfo(ival);
af_dtype vtype = info.getType();
switch (vtype) {
case f32:
sort_by_key<Tk, float>(okey, oval, ikey, ival, dim, isAscending);
break;
case f64:
sort_by_key<Tk, double>(okey, oval, ikey, ival, dim, isAscending);
break;
case c32:
sort_by_key<Tk, cfloat>(okey, oval, ikey, ival, dim, isAscending);
break;
case c64:
sort_by_key<Tk, cdouble>(okey, oval, ikey, ival, dim, isAscending);
break;
case s32:
sort_by_key<Tk, int>(okey, oval, ikey, ival, dim, isAscending);
break;
case u32:
sort_by_key<Tk, uint>(okey, oval, ikey, ival, dim, isAscending);
break;
case s16:
sort_by_key<Tk, short>(okey, oval, ikey, ival, dim, isAscending);
break;
case u16:
sort_by_key<Tk, ushort>(okey, oval, ikey, ival, dim, isAscending);
break;
case s64:
sort_by_key<Tk, intl>(okey, oval, ikey, ival, dim, isAscending);
break;
case u64:
sort_by_key<Tk, uintl>(okey, oval, ikey, ival, dim, isAscending);
break;
case s8:
sort_by_key<Tk, schar>(okey, oval, ikey, ival, dim, isAscending);
break;
case u8:
sort_by_key<Tk, uchar>(okey, oval, ikey, ival, dim, isAscending);
break;
case b8:
sort_by_key<Tk, char>(okey, oval, ikey, ival, dim, isAscending);
break;
default: TYPE_ERROR(1, vtype);
}
}
af_err af_sort_by_key(af_array *out_keys, af_array *out_values,
const af_array keys, const af_array values,
const unsigned dim, const bool isAscending) {
try {
const ArrayInfo &kinfo = getInfo(keys);
af_dtype ktype = kinfo.getType();
const ArrayInfo &vinfo = getInfo(values);
DIM_ASSERT(4, kinfo.dims() == vinfo.dims());
if (kinfo.elements() == 0) {
AF_CHECK(af_create_handle(out_keys, 0, nullptr, ktype));
AF_CHECK(af_create_handle(out_values, 0, nullptr, ktype));
return AF_SUCCESS;
}
TYPE_ASSERT(kinfo.isReal());
af_array oKey;
af_array oVal;
switch (ktype) {
case f32:
sort_by_key_tmplt<float>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
case f64:
sort_by_key_tmplt<double>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
case s32:
sort_by_key_tmplt<int>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
case u32:
sort_by_key_tmplt<uint>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
case s16:
sort_by_key_tmplt<short>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
case u16:
sort_by_key_tmplt<ushort>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
case s64:
sort_by_key_tmplt<intl>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
case u64:
sort_by_key_tmplt<uintl>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
case s8:
sort_by_key_tmplt<schar>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
case u8:
sort_by_key_tmplt<uchar>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
case b8:
sort_by_key_tmplt<char>(&oKey, &oVal, keys, values, dim,
isAscending);
break;
default: TYPE_ERROR(1, ktype);
}
std::swap(*out_keys, oKey);
std::swap(*out_values, oVal);
}
CATCHALL;
return AF_SUCCESS;
}