|
| 1 | + |
| 2 | +// Copyright David Browne 2024-2025. |
| 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 | +// invoking lambdas with vector, matrix, and scalar values |
| 11 | +// |
| 12 | + |
| 13 | +namespace dsga |
| 14 | +{ |
| 15 | + // |
| 16 | + // support for the invoke() function |
| 17 | + // |
| 18 | + namespace invoke_detail |
| 19 | + { |
| 20 | + template <typename T> |
| 21 | + struct is_dsga_matrix : std::false_type {}; |
| 22 | + |
| 23 | + template <typename T, std::size_t C, std::size_t R> |
| 24 | + struct is_dsga_matrix<dsga::basic_matrix<T, C, R>> : std::true_type {}; |
| 25 | + |
| 26 | + template <typename T> |
| 27 | + constexpr bool is_dsga_matrix_v = is_dsga_matrix<T>::value; |
| 28 | + |
| 29 | + template <typename T> |
| 30 | + concept can_index = requires(T t, int i) |
| 31 | + { |
| 32 | + t[i]; |
| 33 | + t.size(); |
| 34 | + }; |
| 35 | + |
| 36 | + template <typename T> |
| 37 | + concept has_at = requires (T t, int i) |
| 38 | + { |
| 39 | + t.at(i); |
| 40 | + }; |
| 41 | + |
| 42 | + template <typename T> |
| 43 | + struct indexer |
| 44 | + { |
| 45 | + indexer(const T &val, std::size_t c) noexcept : t(val), C(c) |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + auto &operator []([[ maybe_unused ]] std::size_t index) |
| 50 | + { |
| 51 | + // check for valid index based on return vector size |
| 52 | + if (index >= C) |
| 53 | + { |
| 54 | + throw std::out_of_range("index out of range"); |
| 55 | + } |
| 56 | + |
| 57 | + // a matrix can technically index, but we want matrices |
| 58 | + // to be passed down whole and not just a column of a matrix |
| 59 | + if constexpr (can_index<T> && !is_dsga_matrix_v<T>) |
| 60 | + { |
| 61 | + // check for valid index based on indexable size |
| 62 | + if (index >= t.size()) |
| 63 | + { |
| 64 | + throw std::out_of_range("index out of range"); |
| 65 | + } |
| 66 | + |
| 67 | + if constexpr (has_at<T>) |
| 68 | + { |
| 69 | + return t.at(index); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + return t[index]; |
| 74 | + } |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + return t; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private: |
| 83 | + const T &t; |
| 84 | + std::size_t C; |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + // not in GLSL |
| 89 | + // return a vector created by invoking an operation element-wise to a variable number of arguments (there must be at least 1) |
| 90 | + // that are either dimensional_scalar or basic_matrix -- if an argument isn't a vector of dimensional_scalars of length C, |
| 91 | + // it can be a single scalar that will be used C times (the length of the return vector) -- similarly, if an argument is a |
| 92 | + // matrix, it will be used C times (the length of the return vector) -- usually the arguments will be vectors of the same |
| 93 | + // length C or a std::array or std::span of matrices of the same length C. |
| 94 | + // |
| 95 | + // the Op must return a dsga::dimensional_scalar type, since invoke() returns a dsga::basic_vector of the results. |
| 96 | + template <std::size_t C, typename Op, typename ...Ts> |
| 97 | + requires ((sizeof ...(Ts)) > 0) && (C > 1) |
| 98 | + [[nodiscard]] constexpr auto invoke(Op op, const Ts & ...args) noexcept |
| 99 | + { |
| 100 | + auto op_invoke = [&op, &args...](std::size_t index) { return op(invoke_detail::indexer(args, C)[index] ...); }; |
| 101 | + |
| 102 | + return [&op_invoke]<std::size_t ...Is>(std::index_sequence<Is...>) |
| 103 | + { |
| 104 | + return basic_vector(op_invoke(Is) ...); |
| 105 | + }(std::make_index_sequence<C>{}); |
| 106 | + } |
| 107 | + |
| 108 | + // not in GLSL |
| 109 | + // return a vector created by invoking an operation element-wise to a variable number of vectors (there must be at least 1) |
| 110 | + // that are all the same size, but might be of different types |
| 111 | + template <typename Op, std::size_t C, bool ...Ws, dimensional_scalar ...Ts, typename ...Ds> |
| 112 | + requires ((sizeof ...(Ts)) > 0) && (C > 1) |
| 113 | + [[nodiscard]] constexpr basic_vector<std::invoke_result_t<Op, Ts...>, C> invoke_on_vectors(Op op, const vector_base<Ws, Ts, C, Ds> & ...vectors) noexcept |
| 114 | + { |
| 115 | + return dsga::invoke<C>(op, vectors...); |
| 116 | + |
| 117 | +// auto op_invoke = [&op, &vectors ...](std::size_t index) { return op(vectors[index] ...); }; |
| 118 | +// |
| 119 | +// return [&op_invoke]<std::size_t ...Is>(std::index_sequence<Is...>) |
| 120 | +// { |
| 121 | +// return basic_vector<std::invoke_result_t<Op, Ts...>, C>{op_invoke(Is) ...}; |
| 122 | +// }(std::make_index_sequence<C>{}); |
| 123 | + } |
| 124 | +} |
0 commit comments