TRIQS/nda
2.0.0
Multi-dimensional array library for C++
Toggle main menu visibility
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
19
namespace
nda {
20
25
33
template
<
int
R,
typename
F>
34
class
array_adapter
{
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
array.hpp
Provides utility functions for std::array.
nda::array_adapter::array_adapter
array_adapter(std::array< Int, R > const &shape, F f)
Construct a new array adapter object.
Definition
array_adapter.hpp:52
nda::array_adapter::operator()
auto operator()(long i0, Ints... is) const
Function call operator simply forwards the arguments to the callable object.
Definition
array_adapter.hpp:74
nda::array_adapter::size
long size() const
Get the total size of the adapter.
Definition
array_adapter.hpp:64
nda::array_adapter::shape
auto const & shape() const
Get shape of the adapter.
Definition
array_adapter.hpp:58
nda::CallableWithLongs
Check if a given type can be called with a certain number of long arguments.
Definition
concepts.hpp:46
concepts.hpp
Provides concepts for the nda library.
nda::get_algebra
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition
traits.hpp:116
nda::stdutil::product
constexpr auto product(std::array< T, R > const &a)
Calculate the product of all elements in a std::array.
Definition
array.hpp:335
nda
array_adapter.hpp
Generated by
1.17.0