VCTR
Loading...
Searching...
No Matches
TransformedBy.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2023 by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23namespace vctr::expressions
24{
25
26template <size_t extent, class SrcType, std::invocable<ValueType<SrcType>> FunctionType>
27requires (! std::is_void_v<std::invoke_result<FunctionType, ValueType<SrcType>>>)
29{
30public:
31 using value_type = std::invoke_result_t<FunctionType, ValueType<SrcType>>;
32
33 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (TransformedBy, src)
34
35 void applyRuntimeArgs (const FunctionType& transformationFunction)
36 {
37 fn = transformationFunction;
38 }
39
40 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
41 {
42 return fn (src[i]);
43 }
44
45private:
46 using FnCopyType = std::conditional_t<std::copyable<FunctionType>, FunctionType, std::function<value_type (ValueType<SrcType>)>>;
47 FnCopyType fn;
48};
49
50} // namespace vctr::expressions
51
52namespace vctr
53{
67template <class Fn>
68constexpr auto transformedBy (Fn&& fn)
69{
70 return makeTemplateExpressionChainBuilderWithRuntimeArgs<expressions::TransformedBy, std::remove_cvref_t<Fn>> (std::forward<Fn> (fn));
71}
72
73} // namespace vctr
74
Definition: TransformedBy.h:29
constexpr auto transformedBy(Fn &&fn)
Transforms all source elements by applying fn to them.
Definition: TransformedBy.h:68
The main namespace of the VCTR project.
Definition: Array.h:24
typename detail::ValueType< std::remove_cvref_t< T > >::Type ValueType
If T is an expression template, it equals its return type, if it's a type that defines value_type as ...
Definition: Traits.h:201
The base class to every expression template.
Definition: ExpressionTemplate.h:37