| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | //====================================================================================================================== | ||
| 2 | /* | ||
| 3 | POLYFLOAT - Extended precision floating points | ||
| 4 | Copyright : POLYFLOAT Contributors & Maintainers | ||
| 5 | SPDX-License-Identifier: BSL-1.0 | ||
| 6 | */ | ||
| 7 | //====================================================================================================================== | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | #include <polyfloat/details/callable.hpp> | ||
| 11 | #include <polyfloat/types/concepts.hpp> | ||
| 12 | #include <polyfloat/types/traits.hpp> | ||
| 13 | #include <type_traits> | ||
| 14 | #include <polyfloat/module/core/cumfun.hpp> | ||
| 15 | |||
| 16 | namespace plf | ||
| 17 | { | ||
| 18 | |||
| 19 | template<typename Options> | ||
| 20 | struct cumsum_t : eve::strict_tuple_callable<cumsum_t, Options, raw_option, pedantic_option> | ||
| 21 | { | ||
| 22 | template<typename T> using return_type = T; | ||
| 23 | |||
| 24 | template<typename... Ts> | ||
| 25 | using result = kumi::result::fill_t<sizeof...(Ts), return_type<plf::as_polyfloat_like_t<Ts...>>>; | ||
| 26 | |||
| 27 | template<eve::product_type Tup> | ||
| 28 | using tuple_result = kumi::result::fill_t<Tup::size(), return_type<kumi::apply_traits_t<as_polyfloat_like, Tup>>>; | ||
| 29 | |||
| 30 | template<concepts::polyfloat_like... Zs> | ||
| 31 | 92 | POLYFLOAT_ABI result<Zs...> constexpr operator()(Zs const&... zs) const noexcept | |
| 32 | { | ||
| 33 | 92 | return POLYFLOAT_CALL(zs...); | |
| 34 | } | ||
| 35 | |||
| 36 | 9 | template<eve::non_empty_product_type PT> POLYFLOAT_ABI constexpr tuple_result<PT> operator()(PT t) const noexcept | |
| 37 | { | ||
| 38 | 9 | return POLYFLOAT_CALL(t); | |
| 39 | } | ||
| 40 | |||
| 41 | 102 | POLYFLOAT_CALLABLE_OBJECT(cumsum_t, cumsum_); | |
| 42 | }; | ||
| 43 | //====================================================================================================================== | ||
| 44 | //! @addtogroup core | ||
| 45 | //! @{ | ||
| 46 | //! @var cumsum | ||
| 47 | //! @brief convert a pack of values into | ||
| 48 | //! a tuple of the cumulative sum of its values | ||
| 49 | //! | ||
| 50 | //! @groupheader{Header file} | ||
| 51 | //! | ||
| 52 | //! @code | ||
| 53 | //! #include <polyfloat/module/core.hpp> | ||
| 54 | //! @endcode | ||
| 55 | //! | ||
| 56 | //! @groupheader{Callable Signatures} | ||
| 57 | //! | ||
| 58 | //! @code | ||
| 59 | //! namespace plf | ||
| 60 | //! { | ||
| 61 | //! // Regular overloads | ||
| 62 | //! constexpr auto cumsum(auto ... xs) noexcept; // 1 | ||
| 63 | //! constexpr auto cumsum(non_empty_product_type tup) noexcept; // 2 | ||
| 64 | //! | ||
| 65 | //! } | ||
| 66 | //! @endcode | ||
| 67 | //! | ||
| 68 | //! **Parameters** | ||
| 69 | //! | ||
| 70 | //! * `xs...`: [polyfloat like](@ref plf::concepts::polyfloat_like) arguments. | ||
| 71 | //! * `tup`: kumi tuple of values. | ||
| 72 | //! | ||
| 73 | //! **Return value** | ||
| 74 | //! | ||
| 75 | //! 1. return a kumi tuple of the values of the cumulated sums of all `xs` converted to | ||
| 76 | //! the element type of the common polyfloat like value of the `xs`. | ||
| 77 | //! 2. same as 1., using the tuple elements. | ||
| 78 | //! | ||
| 79 | //! @note currently cumsum can only be applied with `f` being one of these POLYFLOAT sumctors : | ||
| 80 | //! `add`, `mul`, `min`, `max` that define abelian monoids and each possess | ||
| 81 | //! a well defined neutral element. | ||
| 82 | //! | ||
| 83 | //! @groupheader{Example} | ||
| 84 | //! @godbolt{doc/core/cumsum.cpp} | ||
| 85 | //================================================================================================ | ||
| 86 | inline constexpr auto cumsum = eve::functor<cumsum_t>; | ||
| 87 | //================================================================================================ | ||
| 88 | //! @} | ||
| 89 | //================================================================================================ | ||
| 90 | } | ||
| 91 | |||
| 92 | namespace plf::_ | ||
| 93 | { | ||
| 94 | template<eve::product_type PT, eve::callable_options O> | ||
| 95 | constexpr auto cumsum_(POLYFLOAT_DELAY(), O const& o, PT tup) noexcept | ||
| 96 | { | ||
| 97 | return plf::cumfun[o](plf::add, tup); | ||
| 98 | } | ||
| 99 | |||
| 100 | template<typename... Ts, eve::callable_options O> | ||
| 101 | 102 | constexpr auto cumsum_(POLYFLOAT_DELAY(), O const& o, Ts... ts) noexcept | |
| 102 | { | ||
| 103 | 204 | return plf::cumfun[o](plf::add, kumi::make_tuple(ts...)); | |
| 104 | } | ||
| 105 | } | ||
| 106 |