forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxsort.hpp
More file actions
562 lines (484 loc) · 19.1 KB
/
Copy pathxsort.hpp
File metadata and controls
562 lines (484 loc) · 19.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/***************************************************************************
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XTENSOR_SORT_HPP
#define XTENSOR_SORT_HPP
#include <algorithm>
#include <utility>
#include "xarray.hpp"
#include "xeval.hpp"
#include "xslice.hpp" // for xnone
#include "xmanipulation.hpp"
#include "xtensor.hpp"
namespace xt
{
namespace detail
{
constexpr std::size_t normalize_axis(std::ptrdiff_t axis, std::size_t dim)
{
return axis >= 0 ? static_cast<std::size_t>(axis) : static_cast<std::size_t>(static_cast<std::ptrdiff_t>(dim) + axis);
}
template <class E, class F>
inline void call_over_leading_axis(E& ev, F&& fct)
{
std::size_t n_iters = 1;
std::ptrdiff_t secondary_stride;
if (ev.layout() == layout_type::row_major)
{
n_iters = std::accumulate(ev.shape().begin(), ev.shape().end() - 1,
std::size_t(1), std::multiplies<>());
secondary_stride = static_cast<std::ptrdiff_t>(ev.strides()[ev.dimension() - 2]);
}
else
{
n_iters = std::accumulate(ev.shape().begin() + 1, ev.shape().end(),
std::size_t(1), std::multiplies<>());
secondary_stride = static_cast<std::ptrdiff_t>(ev.strides()[1]);
}
std::ptrdiff_t offset = 0;
for (std::size_t i = 0; i < n_iters; ++i, offset += secondary_stride)
{
fct(ev.data() + offset, ev.data() + offset + secondary_stride);
}
}
template <class E>
inline std::size_t leading_axis(const E& e)
{
if (e.layout() == layout_type::row_major)
{
return e.dimension() - 1;
}
else if (e.layout() == layout_type::column_major)
{
return 0;
}
throw std::runtime_error("Layout not supported.");
}
// get permutations to transpose and reverse-transpose array
inline std::pair<dynamic_shape<std::size_t>, dynamic_shape<std::size_t>>
get_permutations(std::size_t dim, std::size_t ax, layout_type layout)
{
dynamic_shape<std::size_t> permutation(dim);
std::iota(permutation.begin(), permutation.end(), std::size_t(0));
permutation.erase(permutation.begin() + std::ptrdiff_t(ax));
if (layout == layout_type::row_major)
{
permutation.push_back(ax);
}
else
{
permutation.insert(permutation.begin(), ax);
}
// TODO find a more clever way to get reverse permutation?
dynamic_shape<std::size_t> reverse_permutation;
for (std::size_t i = 0; i < dim; ++i)
{
auto it = std::find(permutation.begin(), permutation.end(), i);
reverse_permutation.push_back(std::size_t(std::distance(permutation.begin(), it)));
}
return std::make_pair(std::move(permutation), std::move(reverse_permutation));
}
template <class E, class R, class F>
inline auto run_lambda_over_axis(const E& e, R& res, std::size_t axis, F&& lambda)
{
if (axis != detail::leading_axis(res))
{
dynamic_shape<std::size_t> permutation, reverse_permutation;
std::tie(permutation, reverse_permutation) = get_permutations(e.dimension(), axis, e.layout());
res = transpose(e, permutation);
detail::call_over_leading_axis(res, std::forward<F>(lambda));
res = transpose(res, reverse_permutation);
}
else
{
res = e;
detail::call_over_leading_axis(res, std::forward<F>(lambda));
}
}
template <class VT>
struct flatten_sort_result_type
{
using type = VT;
};
template <class VT, std::size_t N, layout_type L>
struct flatten_sort_result_type<xtensor<VT, N, L>>
{
using type = xtensor<VT, 1, L>;
};
template <class VT, class S, layout_type L>
struct flatten_sort_result_type<xtensor_fixed<VT, S, L>>
{
using type = xtensor_fixed<VT, xshape<fixed_compute_size<S>::value>, L>;
};
template <class E, class R = typename flatten_sort_result_type<E>::type>
inline auto flat_sort_impl(const xexpression<E>& e)
{
const auto& de = e.derived_cast();
R ev;
ev.resize({de.size()});
std::copy(de.cbegin(), de.cend(), ev.begin());
std::sort(ev.begin(), ev.end());
return ev;
}
}
template <class E>
inline auto sort(const xexpression<E>& e, placeholders::xtuph /*t*/)
{
return detail::flat_sort_impl(e);
}
namespace detail
{
template <class T>
struct sort_eval_type
{
using type = typename T::temporary_type;
};
template <class T, std::size_t... I, layout_type L>
struct sort_eval_type<xtensor_fixed<T, fixed_shape<I...>, L>>
{
using type = xtensor<T, sizeof...(I), L>;
};
}
/**
* Sort xexpression (optionally along axis)
* The sort is performed using the ``std::sort`` functions.
* A copy of the xexpression is created and returned.
*
* @param e xexpression to sort
* @param axis axis along which sort is performed
*
* @return sorted array (copy)
*/
template <class E>
inline auto sort(const xexpression<E>& e, std::ptrdiff_t axis = -1)
{
using eval_type = typename detail::sort_eval_type<E>::type;
const auto& de = e.derived_cast();
if (de.dimension() == 1)
{
return detail::flat_sort_impl<std::decay_t<decltype(de)>, eval_type>(de);
}
std::size_t ax = detail::normalize_axis(axis, de.dimension());
eval_type res;
detail::run_lambda_over_axis(de, res, ax, [](auto begin, auto end) { std::sort(begin, end); });
return res;
}
namespace detail
{
template <class VT, class T>
struct rebind_value_type
{
using type = xarray<VT, xt::layout_type::dynamic>;
};
template <class VT, class EC, layout_type L>
struct rebind_value_type<VT, xarray<EC, L>>
{
using type = xarray<VT, L>;
};
template <class VT, class EC, std::size_t N, layout_type L>
struct rebind_value_type<VT, xtensor<EC, N, L>>
{
using type = xtensor<VT, N, L>;
};
template <class VT, class ET, class S, layout_type L>
struct rebind_value_type<VT, xtensor_fixed<ET, S, L>>
{
using type = xtensor_fixed<VT, S, L>;
};
template <class VT, class T>
struct flatten_rebind_value_type
{
using type = typename rebind_value_type<VT, T>::type;
};
template <class VT, class EC, std::size_t N, layout_type L>
struct flatten_rebind_value_type<VT, xtensor<EC, N, L>>
{
using type = xtensor<VT, 1, L>;
};
template <class VT, class ET, class S, layout_type L>
struct flatten_rebind_value_type<VT, xtensor_fixed<ET, S, L>>
{
using type = xtensor_fixed<VT, xshape<fixed_compute_size<S>::value>, L>;
};
template <class T>
struct argsort_result_type
{
using type = typename rebind_value_type<typename T::temporary_type::size_type,
typename T::temporary_type>::type;
};
template <class T>
struct linear_argsort_result_type
{
using type = typename flatten_rebind_value_type<typename T::temporary_type::size_type,
typename T::temporary_type>::type;
};
template <class Ed, class Ei>
inline void argsort_over_leading_axis(const Ed& data, Ei& inds)
{
std::size_t n_iters = 1;
std::ptrdiff_t data_secondary_stride;
std::ptrdiff_t inds_secondary_stride;
if (data.layout() == layout_type::row_major)
{
n_iters = std::accumulate(data.shape().begin(), data.shape().end() - 1,
std::size_t(1), std::multiplies<>());
data_secondary_stride = static_cast<std::ptrdiff_t>(data.strides()[data.dimension() - 2]);
inds_secondary_stride = static_cast<std::ptrdiff_t>(inds.strides()[inds.dimension() - 2]);
}
else
{
n_iters = std::accumulate(data.shape().begin() + 1, data.shape().end(),
std::size_t(1), std::multiplies<>());
data_secondary_stride = static_cast<std::ptrdiff_t>(data.strides()[1]);
inds_secondary_stride = static_cast<std::ptrdiff_t>(inds.strides()[1]);
}
std::ptrdiff_t data_offset = 0;
std::ptrdiff_t inds_offset = 0;
for (std::size_t i = 0; i < n_iters; ++i, data_offset += data_secondary_stride,
inds_offset += inds_secondary_stride)
{
auto comp = [&data, &data_offset](std::size_t x, std::size_t y) {
return (*(data.data() + data_offset + x) <
*(data.data() + data_offset + y));
};
std::iota(inds.data() + inds_offset, inds.data() + inds_offset + inds_secondary_stride, 0);
std::sort(inds.data() + inds_offset, inds.data() + inds_offset + inds_secondary_stride, comp);
}
}
template <class E, class R = typename detail::linear_argsort_result_type<E>::type>
inline auto flatten_argsort_impl(const xexpression<E>& e)
{
using result_type = R;
const auto& de = e.derived_cast();
result_type result;
result.resize({de.size()});
auto comp = [&de](std::size_t x, std::size_t y) {
return de[x] < de[y];
};
std::iota(result.begin(), result.end(), 0);
std::sort(result.begin(), result.end(), comp);
return result;
}
}
template <class E>
inline auto argsort(const xexpression<E>& e, placeholders::xtuph /*t*/)
{
return detail::flatten_argsort_impl(e);
}
/**
* Argsort xexpression (optionally along axis)
* Performs an indirect sort along the given axis. Returns an xarray
* of indices of the same shape as e that index data along the given axis in
* sorted order.
*
* @param e xexpression to argsort
* @param axis axis along which argsort is performed
*
* @return argsorted index array
*/
template <class E>
inline auto argsort(const xexpression<E>& e, std::ptrdiff_t axis = -1)
{
using eval_type = typename detail::sort_eval_type<E>::type;
using result_type = typename detail::argsort_result_type<eval_type>::type;
const auto& de = e.derived_cast();
std::size_t ax = detail::normalize_axis(axis, de.dimension());
if (de.dimension() == 1)
{
return detail::flatten_argsort_impl<E, result_type>(e);
}
if (ax != detail::leading_axis(de))
{
dynamic_shape<std::size_t> permutation, reverse_permutation;
std::tie(permutation, reverse_permutation) = detail::get_permutations(de.dimension(), ax, de.layout());
eval_type ev = transpose(de, permutation);
result_type res = result_type::from_shape(ev.shape());
detail::argsort_over_leading_axis(ev, res);
res = transpose(res, reverse_permutation);
return res;
}
else
{
result_type res = result_type::from_shape(de.shape());
detail::argsort_over_leading_axis(de, res);
return res;
}
}
namespace detail
{
template <class T>
struct argfunc_result_type
{
using type = xarray<std::size_t>;
};
template <class T, std::size_t N>
struct argfunc_result_type<xtensor<T, N>>
{
using type = xtensor<std::size_t, N - 1>;
};
template <class IT, class F>
inline std::size_t cmp_idx(IT iter, IT end, std::ptrdiff_t inc, F&& cmp)
{
std::size_t idx = 0;
auto min = *iter;
iter += inc;
for (std::size_t i = 1; iter < end; iter += inc, ++i)
{
if (cmp(*iter, min))
{
min = *iter;
idx = i;
}
}
return idx;
}
template <class E, class F>
inline xtensor<std::size_t, 0> arg_func_impl(const E& e, F&& f)
{
return cmp_idx(e.template begin<XTENSOR_DEFAULT_LAYOUT>(),
e.template end<XTENSOR_DEFAULT_LAYOUT>(), 1,
std::forward<F>(f));
}
template <class E, class F>
inline typename argfunc_result_type<E>::type
arg_func_impl(const E& e, std::size_t axis, F&& cmp)
{
using eval_type = typename detail::sort_eval_type<E>::type;
using value_type = typename E::value_type;
using result_type = typename argfunc_result_type<E>::type;
using result_shape_type = typename result_type::shape_type;
if (e.dimension() == 1)
{
return arg_func_impl(e, std::forward<F>(cmp));
}
result_shape_type alt_shape;
xt::resize_container(alt_shape, e.dimension() - 1);
// Excluding copy, copy all of shape except for axis
std::copy(e.shape().cbegin(), e.shape().cbegin() + std::ptrdiff_t(axis), alt_shape.begin());
std::copy(e.shape().cbegin() + std::ptrdiff_t(axis) + 1, e.shape().cend(), alt_shape.begin() + std::ptrdiff_t(axis));
result_type result = result_type::from_shape(std::move(alt_shape));
auto result_iter = result.begin();
auto arg_func_lambda = [&result_iter, &cmp](auto begin, auto end) {
std::size_t idx = 0;
value_type val = *begin;
++begin;
for (std::size_t i = 1; begin != end; ++begin, ++i)
{
if (cmp(*begin, val))
{
val = *begin;
idx = i;
}
}
*result_iter = idx;
++result_iter;
};
if (axis != detail::leading_axis(e))
{
dynamic_shape<std::size_t> permutation, reverse_permutation;
std::tie(permutation, reverse_permutation) = detail::get_permutations(e.dimension(), axis, e.layout());
// note: creating copy
eval_type input = transpose(e, permutation);
detail::call_over_leading_axis(input, arg_func_lambda);
return result;
}
else
{
auto&& input = eval(e);
detail::call_over_leading_axis(input, arg_func_lambda);
return result;
}
}
}
template <class E>
inline auto argmin(const xexpression<E>& e)
{
using value_type = typename E::value_type;
auto&& ed = eval(e.derived_cast());
return detail::arg_func_impl(ed, std::less<value_type>());
}
/**
* Find position of minimal value in xexpression
*
* @param e input xexpression
* @param axis select axis (or none)
*
* @return returns xarray with positions of minimal value
*/
template <class E>
inline auto argmin(const xexpression<E>& e, std::size_t axis)
{
using value_type = typename E::value_type;
auto&& ed = eval(e.derived_cast());
return detail::arg_func_impl(ed, axis, std::less<value_type>());
}
template <class E>
inline auto argmax(const xexpression<E>& e)
{
using value_type = typename E::value_type;
auto&& ed = eval(e.derived_cast());
return detail::arg_func_impl(ed, std::greater<value_type>());
}
/**
* Find position of maximal value in xexpression
*
* @param e input xexpression
* @param axis select axis (or none)
*
* @return returns xarray with positions of maximal value
*/
template <class E>
inline auto argmax(const xexpression<E>& e, std::size_t axis)
{
using value_type = typename E::value_type;
auto&& ed = eval(e.derived_cast());
return detail::arg_func_impl(ed, axis, std::greater<value_type>());
}
/**
* Find unique elements of a xexpression. This returns a flattened xtensor with
* sorted, unique elements from the original expression.
*
* @param e input xexpression (will be flattened)
*/
template <class E>
inline auto unique(const xexpression<E>& e)
{
auto sorted = sort(e, xnone());
auto end = std::unique(sorted.begin(), sorted.end());
std::size_t sz = static_cast<std::size_t>(std::distance(sorted.begin(), end));
// TODO check if we can shrink the vector without reallocation
using value_type = typename E::value_type;
auto result = xtensor<value_type, 1>::from_shape({sz});
std::copy(sorted.begin(), end, result.begin());
return result;
}
/**
* Find the set difference of two xexpressions. This returns a flattened xtensor with
* the sorted, unique values in ar1 that are not in ar2.
*
* @param ar1 input xexpression (will be flattened)
* @param ar2 input xexpression
*/
template <class E1, class E2>
inline auto setdiff1d(const xexpression<E1>& ar1, const xexpression<E2>& ar2)
{
using value_type = typename E1::value_type;
auto unique1 = unique(ar1);
auto unique2 = unique(ar2);
auto tmp = xtensor<value_type, 1>::from_shape({unique1.size()});
auto end = std::set_difference(
unique1.begin(), unique1.end(),
unique2.begin(), unique2.end(),
tmp.begin()
);
std::size_t sz = static_cast<std::size_t>(std::distance(tmp.begin(), end));
auto result = xtensor<value_type, 1>::from_shape({sz});
std::copy(tmp.begin(), end, result.begin());
return result;
}
}
#endif