TRIQS/nda 2.0.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
56 template <typename F, Array... As>
57 inline constexpr bool is_expression<expr_call<F, As...>> = true;
58
63
82 template <typename F, Array... As>
83 struct expr_call {
85 F f;
86
94 std::tuple<As...> a;
95
96 private:
97 // The const member functions below read expr_call::a directly: expr_call::operand would const-qualify the
98 // operands, so that e.g. slicing would yield an expression over const views.
99
100 // Implementation of the function call operator.
101 template <size_t... Is, typename... Args>
102 [[gnu::always_inline]] [[nodiscard]] auto _call(std::index_sequence<Is...>, Args const &...args) const {
103 // if args contains a range, we need to return an expr_call on the resulting slice
104 if constexpr ((is_range_or_ellipsis<Args> or ... or false)) {
105 return mapped<F>{f}(std::get<Is>(a)(args...)...);
106 } else {
107 return f(std::get<Is>(a)(args...)...);
108 }
109 }
110
111 // Implementation of the subscript operator.
112 template <size_t... Is, typename Arg>
113 [[gnu::always_inline]] auto _call_bra(std::index_sequence<Is...>, Arg const &arg) const {
114 return f(std::get<Is>(a)[arg]...);
115 }
116
117 public:
130 template <typename... Args>
131 auto operator()(Args const &...args) const {
132 return _call(std::make_index_sequence<sizeof...(As)>{}, args...);
133 }
134
147 template <typename Arg>
148 auto operator[](Arg const &arg) const {
149 return _call_bra(std::make_index_sequence<sizeof...(As)>{}, arg);
150 }
151
152 // FIXME copy needed for the && case only. Overload ?
157 [[nodiscard]] auto shape() const { return operand().shape(); }
158
163 [[nodiscard]] long size() const { return operand().size(); }
164
174 template <size_t I = 0>
175 [[nodiscard]] auto &operand() {
176 return std::get<I>(a);
177 }
178
180 template <size_t I = 0>
181 [[nodiscard]] auto const &operand() const {
182 return std::get<I>(a);
183 }
184 };
185
190 template <class F>
191 struct mapped {
193 F f;
194
204 template <Array A0, Array... As>
205 expr_call<F, A0, As...> operator()(A0 &&a0, As &&...as) const {
206 EXPECTS(((as.shape() == a0.shape()) && ...)); // same shape
207 return {f, {std::forward<A0>(a0), std::forward<As>(as)...}};
208 }
209
219 template <Scalar T0, Scalar... Ts>
220 auto operator()(T0 t0, Ts... ts) const {
221 return f(t0, ts...);
222 }
223 };
224
234 template <class F>
236 return {std::move(f)};
237 }
238
240
241} // namespace nda
Check if a given type satisfies the array concept.
Definition concepts.hpp:212
Check if a given type is either an arithmetic or complex type.
Definition concepts.hpp:83
Provides concepts for the nda library.
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:235
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:149
constexpr bool is_expression
Constexpr variable that is true if type A is a lazy expression type.
Definition traits.hpp:168
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:83
long size() const
Get the total size of the nda::Array objects.
Definition map.hpp:163
auto shape() const
Get the shape of the nda::Array objects.
Definition map.hpp:157
std::tuple< As... > a
Tuple containing the nda::Array arguments.
Definition map.hpp:94
auto operator()(Args const &...args) const
Function call operator.
Definition map.hpp:131
auto & operand()
Get one of the nda::Array arguments of the expression.
Definition map.hpp:175
F f
Callable object of the expression.
Definition map.hpp:85
auto operator[](Arg const &arg) const
Subscript operator.
Definition map.hpp:148
auto const & operand() const
Const overload of expr_call::operand.
Definition map.hpp:181
Functor that is returned by the nda::map function.
Definition map.hpp:191
expr_call< F, A0, As... > operator()(A0 &&a0, As &&...as) const
Function call operator that returns a lazy function call expression.
Definition map.hpp:205
F f
Callable object.
Definition map.hpp:193
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:220
Provides type traits for the nda library.