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--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#include "./concepts.hpp"
14#include "./layout/range.hpp"
15#include "./macros.hpp"
16#include "./traits.hpp"
17
18#include <cstddef>
19#include <utility>
20#include <tuple>
21
22namespace nda {
23
25 // Forward declarations.
26 template <typename F, Array... A>
27 struct expr_call;
28
29 template <class F>
30 struct mapped;
32
33 namespace detail {
34
35 // Implementation of the nda::get_algebra trait for function call expressions.
36 template <typename... Char>
37 constexpr char _impl_find_common_algebra(char x0, Char... x) {
38 return (((x == x0) && ...) ? x0 : 'N');
39 }
40
41 } // namespace detail
42
52 template <typename F, Array... As>
53 constexpr char get_algebra<expr_call<F, As...>> = detail::_impl_find_common_algebra(get_algebra<As>...);
54
59
78 template <typename F, Array... As>
79 struct expr_call {
81 F f;
82
84 std::tuple<const As...> a;
85
86 private:
87 // Implementation of the function call operator.
88 template <size_t... Is, typename... Args>
89 [[gnu::always_inline]] [[nodiscard]] auto _call(std::index_sequence<Is...>, Args const &...args) const {
90 // if args contains a range, we need to return an expr_call on the resulting slice
91 if constexpr ((is_range_or_ellipsis<Args> or ... or false)) {
92 return mapped<F>{f}(std::get<Is>(a)(args...)...);
93 } else {
94 return f(std::get<Is>(a)(args...)...);
95 }
96 }
97
98 // Implementation of the subscript operator.
99 template <size_t... Is, typename Arg>
100 [[gnu::always_inline]] auto _call_bra(std::index_sequence<Is...>, Arg const &arg) const {
101 return f(std::get<Is>(a)[arg]...);
102 }
103
104 public:
117 template <typename... Args>
118 auto operator()(Args const &...args) const {
119 return _call(std::make_index_sequence<sizeof...(As)>{}, args...);
120 }
121
134 template <typename Arg>
135 auto operator[](Arg const &arg) const {
136 return _call_bra(std::make_index_sequence<sizeof...(As)>{}, arg);
137 }
138
139 // FIXME copy needed for the && case only. Overload ?
144 [[nodiscard]] auto shape() const { return std::get<0>(a).shape(); }
145
150 [[nodiscard]] long size() const { return std::get<0>(a).size(); }
151 };
152
157 template <class F>
158 struct mapped {
160 F f;
161
171 template <Array A0, Array... As>
172 expr_call<F, A0, As...> operator()(A0 &&a0, As &&...as) const {
173 EXPECTS(((as.shape() == a0.shape()) && ...)); // same shape
174 return {f, {std::forward<A0>(a0), std::forward<As>(as)...}};
175 }
176
186 template <Scalar T0, Scalar... Ts>
187 auto operator()(T0 t0, Ts... ts) const {
188 return f(t0, ts...);
189 }
190 };
191
201 template <class F>
203 return {std::move(f)};
204 }
205
207
208} // namespace nda
Check if a given type satisfies the array concept.
Definition concepts.hpp:230
Check if a given type is either an arithmetic or complex type.
Definition concepts.hpp:108
Provides concepts for the nda library.
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:202
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:115
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:63
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:79
long size() const
Get the total size of the nda::Array objects.
Definition map.hpp:150
auto shape() const
Get the shape of the nda::Array objects.
Definition map.hpp:144
auto operator()(Args const &...args) const
Function call operator.
Definition map.hpp:118
std::tuple< const As... > a
Tuple containing the nda::Array arguments.
Definition map.hpp:84
F f
Callable object of the expression.
Definition map.hpp:81
auto operator[](Arg const &arg) const
Subscript operator.
Definition map.hpp:135
Functor that is returned by the nda::map function.
Definition map.hpp:158
expr_call< F, A0, As... > operator()(A0 &&a0, As &&...as) const
Function call operator that returns a lazy function call expression.
Definition map.hpp:172
F f
Callable object.
Definition map.hpp:160
auto operator()(T0 t0, Ts... ts) const
Function call operator that returns the result of the callable object applied to the scalar arguments...
Definition map.hpp:187
Provides type traits for the nda library.