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-2024 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: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
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
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:
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
69 [[nodiscard]] auto const &shape() const { return myshape; }
70
75 [[nodiscard]] long size() const { return stdutil::product(myshape); }
76
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
101 template <int R, typename F>
102 inline constexpr char get_algebra<array_adapter<R, F>> = 'A';
103
104} // namespace nda
Provides utility functions for std::array.
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.
Check if a given type can be called with a certain number of long arguments.
Definition concepts.hpp:53
Provides concepts for the nda library.
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:126
constexpr auto product(std::array< T, R > const &a)
Calculate the product of all elements in a std::array.
Definition array.hpp:348