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--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 "./stdutil/array.hpp"
15
16#include <array>
17#include <type_traits>
18
19namespace nda {
20
25
33 template <int R, typename F>
35 static_assert(CallableWithLongs<F, R>, "Error in nda::array_adapter: Lambda should be callable with R integers");
36
37 // Shape of the array.
38 std::array<long, R> myshape;
39
40 // Callable object.
41 F f;
42
43 public:
51 template <typename Int>
52 array_adapter(std::array<Int, R> const &shape, F f) : myshape(stdutil::make_std_array<long>(shape)), f(f) {}
53
58 [[nodiscard]] auto const &shape() const { return myshape; }
59
64 [[nodiscard]] long size() const { return stdutil::product(myshape); }
65
73 template <typename... Ints>
74 auto operator()(long i0, Ints... is) const {
75 static_assert((std::is_convertible_v<Ints, long> and ...), "Error in nda::array_adapter: Arguments must be convertible to long");
76 return f(i0, is...);
77 }
78 };
79
80 // Class template argument deduction guides.
81 template <auto R, typename Int, typename F>
82 array_adapter(std::array<Int, R>, F) -> array_adapter<R, F>;
83
85
90 template <int R, typename F>
91 inline constexpr char get_algebra<array_adapter<R, F>> = 'A';
92
93} // namespace nda
Provides utility functions for std::array.
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:42
Provides concepts for the nda library.
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:115
constexpr auto product(std::array< T, R > const &a)
Calculate the product of all elements in a std::array.
Definition array.hpp:335