Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause 2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 3 : /// @file 4 : /// @author Thibault Chatel 5 : #pragma once 6 : 7 : #include "scipp/common/overloaded.h" 8 : #include "scipp/core/element/arg_list.h" 9 : #include "scipp/core/element/comparison.h" 10 : #include "scipp/core/time_point.h" 11 : #include "scipp/core/transform_common.h" 12 : #include "scipp/core/value_and_variance.h" 13 : #include "scipp/units/unit.h" 14 : 15 : namespace scipp::core::element { 16 : 17 : namespace { 18 10 : template <class Compare> constexpr auto make_sort(Compare compare) { 19 : return overloaded{ 20 : core::element::arg_list<scipp::span<int64_t>, scipp::span<int32_t>, 21 : scipp::span<double>, scipp::span<float>, 22 : scipp::span<std::string>, 23 : scipp::span<time_point>>, 24 6 : [](units::Unit &) {}, 25 16 : [&compare](auto &range) { 26 : using T = std::decay_t<decltype(range)>; 27 10 : constexpr bool vars = is_ValueAndVariance_v<T>; 28 : if constexpr (vars) { 29 : std::vector<ValueAndVariance<typename T::value_type::value_type>> 30 4 : zipped; 31 16 : for (scipp::index i = 0; i < scipp::size(range.value); i++) { 32 12 : zipped.emplace_back(range.value[i], range.variance[i]); 33 : } 34 4 : std::sort(std::begin(zipped), std::end(zipped), compare); 35 16 : for (scipp::index i = 0; i < scipp::size(range.value); i++) { 36 12 : range.value[i] = zipped[i].value; 37 12 : range.variance[i] = zipped[i].variance; 38 : } 39 4 : } else { 40 6 : std::sort(range.begin(), range.end(), compare); 41 : } 42 10 : }}; 43 : } 44 : } // namespace 45 : 46 : auto sort_nonascending = make_sort(greater); 47 : auto sort_nondescending = make_sort(less); 48 : 49 : } // namespace scipp::core::element