TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
array_adapter.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2023 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
17/**
18 * @file
19 * @brief Provides an array adapter class.
20 */
21
22#pragma once
23
24#include "./concepts.hpp"
25#include "./stdutil/array.hpp"
26
27#include <array>
28#include <type_traits>
29
30namespace nda {
31
32 /**
33 * @addtogroup av_utils
34 * @{
35 */
36
37 /**
38 * @brief Adapter that consists of a shape and a callable object, which takes `R` integers as arguments (just like an
39 * nda::basic_array or nda::basic_array_view).
40 *
41 * @tparam R Rank of the adapter.
42 * @tparam F Callable type.
43 */
44 template <int R, typename F>
46 static_assert(CallableWithLongs<F, R>, "Error in nda::array_adapter: Lambda should be callable with R integers");
47
48 // Shape of the array.
49 std::array<long, R> myshape;
50
51 // Callable object.
52 F f;
53
54 public:
55 /**
56 * @brief Construct a new array adapter object.
57 *
58 * @tparam Int Integer type.
59 * @param shape Shape of the adapater.
60 * @param f Callable object.
61 */
62 template <typename Int>
63 array_adapter(std::array<Int, R> const &shape, F f) : myshape(stdutil::make_std_array<long>(shape)), f(f) {}
64
65 /**
66 * @brief Get shape of the adapter.
67 * @return `std::array<long, R>` object specifying the shape of the adapter.
68 */
69 [[nodiscard]] auto const &shape() const { return myshape; }
70
71 /**
72 * @brief Get the total size of the adapter.
73 * @return Number of elements in the adapter.
74 */
75 [[nodiscard]] long size() const { return stdutil::product(myshape); }
76
77 /**
78 * @brief Function call operator simply forwards the arguments to the callable object.
79 *
80 * @tparam Ints Integer types (convertible to long).
81 * @param i0 First argument.
82 * @param is Rest of the arguments.
83 */
84 template <typename... Ints>
85 auto operator()(long i0, Ints... is) const {
86 static_assert((std::is_convertible_v<Ints, long> and ...), "Error in nda::array_adapter: Arguments must be convertible to long");
87 return f(i0, is...);
88 }
89 };
90
91 // Class template argument deduction guides.
92 template <auto R, typename Int, typename F>
93 array_adapter(std::array<Int, R>, F) -> array_adapter<R, F>;
94
95 /** @} */
96
97 /**
98 * @ingroup utils_type_traits
99 * @brief Specialization of nda::get_algebra for nda::array_adapter.
100 */
101 template <int R, typename F>
102 inline constexpr char get_algebra<array_adapter<R, F>> = 'A';
103
104} // namespace nda
Adapter that consists of a shape and a callable object, which takes R integers as arguments (just lik...
array_adapter(std::array< Int, R > const &shape, F f)
Construct a new array adapter object.
auto operator()(long i0, Ints... is) const
Function call operator simply forwards the arguments to the callable object.
long size() const
Get the total size of the adapter.
auto const & shape() const
Get shape of the adapter.
constexpr char get_algebra< array_adapter< R, F > >
Specialization of nda::get_algebra for nda::array_adapter.