TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
map.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2022 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Olivier Parcollet, Nils Wentzell
16
22#pragma once
23
24#include "./concepts.hpp"
25#include "./layout/range.hpp"
26#include "./macros.hpp"
27#include "./traits.hpp"
28
29#include <cstddef>
30#include <utility>
31#include <tuple>
32
33namespace nda {
34
36 // Forward declarations.
37 template <typename F, Array... A>
38 struct expr_call;
39
40 template <class F>
41 struct mapped;
43
44 namespace detail {
45
46 // Implementation of the nda::get_algebra trait for function call expressions.
47 template <typename... Char>
48 constexpr char _impl_find_common_algebra(char x0, Char... x) {
49 return (((x == x0) && ...) ? x0 : 'N');
50 }
51
52 } // namespace detail
53
63 template <typename F, Array... As>
64 constexpr char get_algebra<expr_call<F, As...>> = detail::_impl_find_common_algebra(get_algebra<As>...);
65
89 template <typename F, Array... As>
90 struct expr_call {
92 F f;
93
95 std::tuple<const As...> a;
96
97 private:
98 // Implementation of the function call operator.
99 template <size_t... Is, typename... Args>
100 [[gnu::always_inline]] [[nodiscard]] auto _call(std::index_sequence<Is...>, Args const &...args) const {
101 // if args contains a range, we need to return an expr_call on the resulting slice
102 if constexpr ((is_range_or_ellipsis<Args> or ... or false)) {
103 return mapped<F>{f}(std::get<Is>(a)(args...)...);
104 } else {
105 return f(std::get<Is>(a)(args...)...);
106 }
107 }
108
109 // Implementation of the subscript operator.
110 template <size_t... Is, typename Arg>
111 [[gnu::always_inline]] auto _call_bra(std::index_sequence<Is...>, Arg const &arg) const {
112 return f(std::get<Is>(a)[arg]...);
113 }
114
115 public:
128 template <typename... Args>
129 auto operator()(Args const &...args) const {
130 return _call(std::make_index_sequence<sizeof...(As)>{}, args...);
131 }
132
145 template <typename Arg>
146 auto operator[](Arg const &arg) const {
147 return _call_bra(std::make_index_sequence<sizeof...(As)>{}, arg);
148 }
149
150 // FIXME copy needed for the && case only. Overload ?
155 [[nodiscard]] auto shape() const { return std::get<0>(a).shape(); }
156
161 [[nodiscard]] long size() const { return std::get<0>(a).size(); }
162 };
163
168 template <class F>
169 struct mapped {
171 F f;
172
182 template <Array A0, Array... As>
183 expr_call<F, A0, As...> operator()(A0 &&a0, As &&...as) const {
184 EXPECTS(((as.shape() == a0.shape()) && ...)); // same shape
185 return {f, {std::forward<A0>(a0), std::forward<As>(as)...}};
186 }
187 };
188
198 template <class F>
200 return {std::move(f)};
201 }
202
205} // namespace nda
Check if a given type satisfies the array concept.
Definition concepts.hpp:230
Provides concepts for the nda library.
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:199
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:126
constexpr bool is_range_or_ellipsis
Constexpr variable that is true if the type T is either an nda::range, an nda::range::all_t or an nda...
Definition range.hpp:76
Macros used in the nda library.
Includes the itertools header and provides some additional utilities.
A lazy function call expression on arrays/views.
Definition map.hpp:90
long size() const
Get the total size of the nda::Array objects.
Definition map.hpp:161
auto shape() const
Get the shape of the nda::Array objects.
Definition map.hpp:155
auto operator()(Args const &...args) const
Function call operator.
Definition map.hpp:129
std::tuple< const As... > a
Tuple containing the nda::Array arguments.
Definition map.hpp:95
F f
Callable object of the expression.
Definition map.hpp:92
auto operator[](Arg const &arg) const
Subscript operator.
Definition map.hpp:146
Functor that is returned by the nda::map function.
Definition map.hpp:169
expr_call< F, A0, As... > operator()(A0 &&a0, As &&...as) const
Function call operator that returns a lazy function call expression.
Definition map.hpp:183
F f
Callable object.
Definition map.hpp:171
Provides type traits for the nda library.