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
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
88 std::tuple<const As...> a;
89
90 private:
91 // Implementation of the function call operator.
92 template <size_t... Is, typename... Args>
93 [[gnu::always_inline]] [[nodiscard]] auto _call(std::index_sequence<Is...>, Args const &...args) const {
94 // if args contains a range, we need to return an expr_call on the resulting slice
95 if constexpr ((is_range_or_ellipsis<Args> or ... or false)) {
96 return mapped<F>{f}(std::get<Is>(a)(args...)...);
97 } else {
98 return f(std::get<Is>(a)(args...)...);
99 }
100 }
101
102 // Implementation of the subscript operator.
103 template <size_t... Is, typename Arg>
104 [[gnu::always_inline]] auto _call_bra(std::index_sequence<Is...>, Arg const &arg) const {
105 return f(std::get<Is>(a)[arg]...);
106 }
107
108 public:
121 template <typename... Args>
122 auto operator()(Args const &...args) const {
123 return _call(std::make_index_sequence<sizeof...(As)>{}, args...);
124 }
125
138 template <typename Arg>
139 auto operator[](Arg const &arg) const {
140 return _call_bra(std::make_index_sequence<sizeof...(As)>{}, arg);
141 }
142
143 // FIXME copy needed for the && case only. Overload ?
148 [[nodiscard]] auto shape() const { return std::get<0>(a).shape(); }
149
154 [[nodiscard]] long size() const { return std::get<0>(a).size(); }
155 };
156
161 template <class F>
162 struct mapped {
164 F f;
165
175 template <Array A0, Array... As>
176 expr_call<F, A0, As...> operator()(A0 &&a0, As &&...as) const {
177 EXPECTS(((as.shape() == a0.shape()) && ...)); // same shape
178 return {f, {std::forward<A0>(a0), std::forward<As>(as)...}};
179 }
180
190 template <Scalar T0, Scalar... Ts>
191 auto operator()(T0 t0, Ts... ts) const {
192 return f(t0, ts...);
193 }
194 };
195
205 template <class F>
207 return {std::move(f)};
208 }
209
211
212} // namespace nda
Check if a given type satisfies the array concept.
Definition concepts.hpp:205
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:206
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:116
constexpr bool is_expression
Constexpr variable that is true if type A is a lazy expression type.
Definition traits.hpp:135
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:154
auto shape() const
Get the shape of the nda::Array objects.
Definition map.hpp:148
auto operator()(Args const &...args) const
Function call operator.
Definition map.hpp:122
std::tuple< const As... > a
Tuple containing the nda::Array arguments.
Definition map.hpp:88
F f
Callable object of the expression.
Definition map.hpp:85
auto operator[](Arg const &arg) const
Subscript operator.
Definition map.hpp:139
Functor that is returned by the nda::map function.
Definition map.hpp:162
expr_call< F, A0, As... > operator()(A0 &&a0, As &&...as) const
Function call operator that returns a lazy function call expression.
Definition map.hpp:176
F f
Callable object.
Definition map.hpp:164
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:191
Provides type traits for the nda library.