|
| 1 | + |
| 2 | +// Copyright David Browne 2020-2023. |
| 3 | +// Distributed under the Boost Software License, Version 1.0. |
| 4 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | +// https://www.boost.org/LICENSE_1_0.txt) |
| 6 | + |
| 7 | +#include "dsga.hxx" |
| 8 | + |
| 9 | +// |
| 10 | +// performing weighted comparisons |
| 11 | +// |
| 12 | + |
| 13 | +namespace dsga |
| 14 | +{ |
| 15 | + // default weights for comparison - x has priority over y, which is over z, which is over w |
| 16 | + template <std::size_t C> |
| 17 | + requires (C >= 1 && C <= 4) |
| 18 | + [[nodiscard]] constexpr auto default_comparison_weights() noexcept |
| 19 | + { |
| 20 | + constexpr auto weights = basic_vector<int, 4>(1, 3, 9, 27); // reverse order |
| 21 | + return[&]<std::size_t ...Is>(std::index_sequence<Is...>) noexcept |
| 22 | + { |
| 23 | + return basic_vector<int, C>(weights[C - Is - 1] ...); |
| 24 | + }(std::make_index_sequence<C>{}); |
| 25 | + } |
| 26 | + |
| 27 | + namespace detail |
| 28 | + { |
| 29 | + // helper that evaluates a binary operation lambda |
| 30 | + template <bool W1, dimensional_scalar T1, std::size_t C, typename D1, bool W2, dimensional_scalar T2, typename D2, typename BinOp> |
| 31 | + [[nodiscard]] constexpr auto binary_op(BinOp lambda, |
| 32 | + const vector_base<W1, T1, C, D1> &lhs, |
| 33 | + const vector_base<W2, T2, C, D2> &rhs) noexcept |
| 34 | + { |
| 35 | + return binary_op_execute_no_convert(std::make_index_sequence<C>{}, lhs, rhs, lambda); |
| 36 | + } |
| 37 | + |
| 38 | + // comparison lambdas that return -1 for less than, 0 for equal, and 1 for greater than. |
| 39 | + // these are for the types that don't work with the sign() function |
| 40 | + constexpr inline auto unsigned_compare_op = [](unsigned_scalar auto lhs, unsigned_scalar auto rhs) noexcept -> int |
| 41 | + { |
| 42 | + return (lhs < rhs) ? -1 : ((lhs > rhs) ? 1 : 0); |
| 43 | + }; |
| 44 | + |
| 45 | + constexpr inline auto signed_unsigned_compare_op = []<signed_scalar T1, unsigned_scalar T2>(T1 lhs, T2 rhs) noexcept -> int |
| 46 | + { |
| 47 | + return (lhs < 0) ? -1 : ((static_cast<unsigned long long>(lhs) < static_cast<unsigned long long>(rhs)) ? -1 : ((static_cast<unsigned long long>(lhs) > static_cast<unsigned long long>(rhs)) ? 1 : 0)); |
| 48 | + }; |
| 49 | + |
| 50 | + constexpr inline auto unsigned_signed_compare_op = []<unsigned_scalar T1, signed_scalar T2>(T1 lhs, T2 rhs) noexcept -> int |
| 51 | + { |
| 52 | + return (rhs < 0) ? 1 : ((static_cast<unsigned long long>(lhs) < static_cast<unsigned long long>(rhs)) ? -1 : ((static_cast<unsigned long long>(lhs) > static_cast<unsigned long long>(rhs)) ? 1 : 0)); |
| 53 | + }; |
| 54 | + |
| 55 | + template <bool W1, non_bool_scalar T1, std::size_t C, typename D1, bool W2, non_bool_scalar T2, typename D2, bool W3, numeric_integral_scalar T3, typename D3> |
| 56 | + requires (signed_scalar<T3>) |
| 57 | + [[nodiscard]] constexpr auto compare_impl(const vector_base<W1, T1, C, D1> &x, |
| 58 | + const vector_base<W2, T2, C, D2> &y, |
| 59 | + const vector_base<W3, T3, C, D3> &weights) noexcept |
| 60 | + { |
| 61 | + if constexpr (floating_point_scalar<T1> && floating_point_scalar<T2>) |
| 62 | + { |
| 63 | + return functions::innerProduct(weights, basic_vector<T3, C>(functions::sign(x - y))); |
| 64 | + } |
| 65 | + else if constexpr (signed_scalar<T1> && signed_scalar<T2>) |
| 66 | + { |
| 67 | + return functions::innerProduct(weights, basic_vector<T3, C>(functions::sign(x - y))); |
| 68 | + } |
| 69 | + else if constexpr (unsigned_scalar<T1> && unsigned_scalar<T2>) |
| 70 | + { |
| 71 | + return functions::innerProduct(weights, basic_vector<T3, C>(binary_op(unsigned_compare_op, x, y))); |
| 72 | + } |
| 73 | + else if constexpr (signed_scalar<T1> && unsigned_scalar<T2>) |
| 74 | + { |
| 75 | + return functions::innerProduct(weights, basic_vector<T3, C>(binary_op(signed_unsigned_compare_op, x, y))); |
| 76 | + } |
| 77 | + else if constexpr (unsigned_scalar<T1> && signed_scalar<T2>) |
| 78 | + { |
| 79 | + return functions::innerProduct(weights, basic_vector<T3, C>(binary_op(unsigned_signed_compare_op, x, y))); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + using commontype = std::common_type_t<T1, T2>; |
| 84 | + return compare_impl(static_cast<dsga::basic_vector<commontype, C>>(x.as_derived()), static_cast<dsga::basic_vector<commontype, C>>(y.as_derived()), weights); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // interface function for three-way comparison operator for vectors, using default weighting |
| 89 | + template <bool W1, non_bool_scalar T1, std::size_t C, typename D1, bool W2, non_bool_scalar T2, typename D2> |
| 90 | + [[nodiscard]] constexpr auto compare(const vector_base<W1, T1, C, D1> &x, |
| 91 | + const vector_base<W2, T2, C, D2> &y) noexcept |
| 92 | + { |
| 93 | + constexpr auto weights = default_comparison_weights<C>(); |
| 94 | + return compare_impl(x, y, weights); |
| 95 | + } |
| 96 | + |
| 97 | + // interface function for three-way comparison operator for vectors, using user-defined weighting |
| 98 | + template <bool W1, non_bool_scalar T1, std::size_t C, typename D1, bool W2, non_bool_scalar T2, typename D2, bool W3, numeric_integral_scalar T3, typename D3> |
| 99 | + requires signed_scalar<T3> |
| 100 | + [[nodiscard]] constexpr auto compare(const vector_base<W1, T1, C, D1> &x, |
| 101 | + const vector_base<W2, T2, C, D2> &y, |
| 102 | + const vector_base<W3, T3, C, D3> &weights) noexcept |
| 103 | + { |
| 104 | + return compare_impl(x, y, weights); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // |
| 109 | + // non-operator weighted comparison for vectors - suggest using default_comparison_weights() or a swizzle |
| 110 | + // of it that uses all the components. the weights show the priority of the dimensions in the comparison. |
| 111 | + // |
| 112 | + // returns the ordering of the the arguments based on a signed integral value, calculated using the weights |
| 113 | + // applied to the differences between the pair-wise components via compare_impl(). It is similar but different |
| 114 | + // than std::lexicographical_compare_three_way(), where that function iterates over the components, and this |
| 115 | + // function operates on all the components at once to arrive at values to compare. It is lexicographical |
| 116 | + // if the absolute value of the weights is in decreasing order, such as using default_comparison_weights<>(). |
| 117 | + // |
| 118 | + // this custom weighted comparison or the default operator <=> comparisons are important for sorting |
| 119 | + // vectors for algorithms, .e.g., convex hull, min-max, etc. Using the default_comparison_weights<>() |
| 120 | + // (or a swizzle of them where every component is used once) will give you a total ordering in the sort |
| 121 | + // (assuming no comparison is std::unordered()). |
| 122 | + // |
| 123 | + // This comparison uses exact data, not fuzzy equality within a tolerance. |
| 124 | + // |
| 125 | + |
| 126 | + template <bool W1, non_bool_scalar T1, std::size_t C, typename D1, bool W2, non_bool_scalar T2, typename D2, bool W3, numeric_integral_scalar T3, typename D3> |
| 127 | + requires signed_scalar<T3> |
| 128 | + [[nodiscard]] constexpr auto weighted_compare(const vector_base<W1, T1, C, D1> &first, |
| 129 | + const vector_base<W2, T2, C, D2> &second, |
| 130 | + const vector_base<W3, T3, C, D3> &weights) noexcept |
| 131 | + { |
| 132 | + if constexpr (std::integral<T1> && std::integral<T2>) |
| 133 | + { |
| 134 | + if (auto comp = detail::compare(first, second, weights); comp < 0) |
| 135 | + { |
| 136 | + return std::strong_ordering::less; |
| 137 | + } |
| 138 | + else if (comp > 0) |
| 139 | + { |
| 140 | + return std::strong_ordering::greater; |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + return std::strong_ordering::equal; |
| 145 | + } |
| 146 | + } |
| 147 | + else if constexpr (std::floating_point<T1> && std::floating_point<T2>) |
| 148 | + { |
| 149 | + // if any component of either inputs is a NaN, then it is unordered |
| 150 | + if (functions::any(functions::isnan(first.as_derived())) || functions::any(functions::isnan(second.as_derived()))) |
| 151 | + return std::partial_ordering::unordered; |
| 152 | + |
| 153 | + if (auto comp = detail::compare(first, second, weights); comp < 0) |
| 154 | + { |
| 155 | + return std::partial_ordering::less; |
| 156 | + } |
| 157 | + else if (comp > 0) |
| 158 | + { |
| 159 | + return std::partial_ordering::greater; |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + return std::partial_ordering::equivalent; |
| 164 | + } |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + using commontype = std::common_type_t<T1, T2>; |
| 169 | + return weighted_compare(static_cast<basic_vector<commontype, C>>(first), static_cast<basic_vector<commontype, C>>(second), weights); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // |
| 174 | + // lexicographic-like comparisons for vectors -- x has highest priority, then y, then z, then w. |
| 175 | + // all components are compared up front though, so it doesn't just stop checking when it finds the |
| 176 | + // first component that compares as not equal/equivalent. |
| 177 | + // |
| 178 | + // not in GLSL |
| 179 | + // |
| 180 | + |
| 181 | + template <bool W1, non_bool_scalar T1, std::size_t C, typename D1, bool W2, non_bool_scalar T2, typename D2> |
| 182 | + constexpr auto operator <=>(const vector_base<W1, T1, C, D1> &first, |
| 183 | + const vector_base<W2, T2, C, D2> &second) noexcept |
| 184 | + { |
| 185 | + constexpr auto weights = default_comparison_weights<C>(); |
| 186 | + return weighted_compare(first, second, weights); |
| 187 | + } |
| 188 | + |
| 189 | + // |
| 190 | + // lexicographic comparisons of vectors in matrices. |
| 191 | + // it will stop checking after first vector comparison that is not equal/equivalent. |
| 192 | + // |
| 193 | + |
| 194 | + constexpr auto mat_vec_comp_op = |
| 195 | + []<floating_point_scalar T1, std::size_t C, floating_point_scalar T2>(const basic_vector<T1, C> &v1, const basic_vector<T2, C> &v2) |
| 196 | + { |
| 197 | + return v1 <=> v2; |
| 198 | + }; |
| 199 | + |
| 200 | + template <floating_point_scalar T1, std::size_t C, std::size_t R, floating_point_scalar T2> |
| 201 | + constexpr auto operator <=>(const basic_matrix<T1, C, R> &lhs, |
| 202 | + const basic_matrix<T2, C, R> &rhs) noexcept |
| 203 | + { |
| 204 | + return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), mat_vec_comp_op); |
| 205 | + } |
| 206 | + |
| 207 | +} |
0 commit comments