TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
traits.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 <complex>
14#include <cstdint>
15#include <ranges>
16#include <type_traits>
17#include <utility>
18#include <tuple>
19
20namespace nda {
21
26
40 template <template <typename...> class TMPLT, typename T>
41 struct is_instantiation_of : std::false_type {};
42
43 // Specialization of nda::is_instantiation_of for when it evaluates to true.
44 template <template <typename...> class TMPLT, typename... U>
45 struct is_instantiation_of<TMPLT, TMPLT<U...>> : std::true_type {};
46
48 template <template <typename...> class TMPLT, typename T>
50
52 template <typename T, typename... Ts>
53 static constexpr bool is_any_of = (std::is_same_v<Ts, T> or ... or false);
54
56 template <typename... Ts>
57 static constexpr bool always_true = true;
58
60 template <typename... Ts>
61 static constexpr bool always_false = false;
62
64 template <typename T>
66
68 template <typename S>
69 inline constexpr bool is_scalar_v = std::is_arithmetic_v<std::remove_cvref_t<S>> or is_complex_v<S>;
70
75 template <typename S>
76 inline constexpr bool is_scalar_or_convertible_v = is_scalar_v<S> or std::is_constructible_v<std::complex<double>, S>;
77
82 template <typename S, typename A>
83 inline constexpr bool is_scalar_for_v =
84 (is_scalar_v<typename A::value_type> ? is_scalar_or_convertible_v<S> : std::is_same_v<S, typename A::value_type>);
85
87 template <typename T>
88 inline constexpr bool is_double_or_complex_v = is_complex_v<T> or std::is_same_v<double, std::remove_cvref_t<T>>;
89
94 template <typename T>
95 inline constexpr bool is_blas_lapack_v =
96 std::is_same_v<double, std::remove_cvref_t<T>> or std::is_same_v<std::complex<double>, std::remove_cvref_t<T>>
97 or std::is_same_v<float, std::remove_cvref_t<T>> or std::is_same_v<std::complex<float>, std::remove_cvref_t<T>>;
98
104 template <typename T>
106 using type = T;
107 };
108
109 // Specialization of nda::remove_complex for std::complex types.
110 template <typename T>
111 struct remove_complex<std::complex<T>> {
112 using type = T;
113 };
114
115 // Specialization of nda::remove_complex for cvref types.
116 template <typename T>
117 requires(!std::is_same_v<T, std::remove_cvref_t<T>>)
118 struct remove_complex<T> : remove_complex<std::remove_cvref_t<T>> {};
119
124 template <typename T>
125 using remove_complex_t = typename remove_complex<T>::type;
126
128
133
148 template <typename A>
149 inline constexpr char get_algebra = 'N';
150
151 // Specialization of nda::get_algebra for cvref types.
152 template <typename A>
153 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
154 inline constexpr char get_algebra<A> = get_algebra<std::remove_cvref_t<A>>;
155
157 template <typename A>
158 requires(std::ranges::contiguous_range<A> or requires { std::declval<A const>().shape(); })
159 constexpr int get_rank = []() {
160 if constexpr (std::ranges::contiguous_range<A>)
161 return 1;
162 else
163 return std::tuple_size_v<std::remove_cvref_t<decltype(std::declval<A const>().shape())>>;
164 }();
165
167 template <typename A>
168 inline constexpr bool is_expression = false;
169
170 // Specialization of nda::is_expression for cvref types.
171 template <typename A>
172 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
173 inline constexpr bool is_expression<A> = is_expression<std::remove_cvref_t<A>>;
174
176 template <typename A>
177 inline constexpr bool is_regular_v = false;
178
179 // Specialization of nda::is_regular_v for cvref types.
180 template <typename A>
181 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
182 inline constexpr bool is_regular_v<A> = is_regular_v<std::remove_cvref_t<A>>;
183
185 template <typename A>
186 inline constexpr bool is_view_v = false;
187
188 // Specialization of nda::is_view_v for cvref types.
189 template <typename A>
190 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
191 inline constexpr bool is_view_v<A> = is_view_v<std::remove_cvref_t<A>>;
192
194 template <typename A>
196
198 template <typename A>
199 inline constexpr bool is_matrix_or_view_v = is_regular_or_view_v<A> and (get_algebra<A> == 'M') and (get_rank<A> == 2);
200
208 template <typename A>
209 requires(is_scalar_v<A> or requires { std::declval<A const>().shape(); })
210 decltype(auto) get_first_element(A &&a) {
211 if constexpr (is_scalar_v<A>) {
212 return std::forward<A>(a);
213 } else {
214 return [&a]<auto... Is>(std::index_sequence<Is...>) -> decltype(auto) {
215 return std::forward<A>(a)((0 * Is)...); // repeat 0 sizeof...(Is) times
216 }(std::make_index_sequence<get_rank<A>>{});
217 }
218 }
219
224 template <typename A>
225 using get_value_t = std::decay_t<decltype(get_first_element(std::declval<A const>()))>;
226
232 template <typename A>
233 requires(is_complex_v<get_value_t<A>> or std::is_floating_point_v<get_value_t<A>>)
234 using get_fp_t = typename remove_complex<get_value_t<A>>::type;
235
237 template <typename A0, typename... As>
238 inline constexpr bool have_same_value_type_v = (std::is_same_v<get_value_t<A0>, get_value_t<As>> and ... and true);
239
241 template <typename A0, typename... As>
242 inline constexpr bool have_same_rank_v = ((get_rank<A0> == get_rank<As>) and ... and true);
243
245
250
264 enum class layout_prop_e : uint64_t {
265 none = 0x0,
266 strided_1d = 0x1,
267 smallest_stride_is_one = 0x2,
268 contiguous = strided_1d | smallest_stride_is_one
269 };
270
280 if (from == layout_prop_e::contiguous) return true;
281 return ((to == layout_prop_e::none) or (to == from));
282 }
283
291 constexpr layout_prop_e operator|(layout_prop_e lhs, layout_prop_e rhs) { return layout_prop_e(uint64_t(lhs) | uint64_t(rhs)); }
292
300 constexpr layout_prop_e operator&(layout_prop_e lhs, layout_prop_e rhs) { return layout_prop_e{uint64_t(lhs) & uint64_t(rhs)}; }
301
308 inline constexpr bool has_strided_1d(layout_prop_e lp) { return uint64_t(lp) & uint64_t(layout_prop_e::strided_1d); }
309
316 inline constexpr bool has_smallest_stride_is_one(layout_prop_e lp) { return uint64_t(lp) & uint64_t(layout_prop_e::smallest_stride_is_one); }
317
324 inline constexpr bool has_contiguous(layout_prop_e lp) { return has_strided_1d(lp) and has_smallest_stride_is_one(lp); }
325
326 // FIXME : I need a NONE for stride_order. For the scalars ...
339 uint64_t stride_order = 0;
340
342 layout_prop_e prop = layout_prop_e::none;
343 };
344
355 if (lhs.stride_order == rhs.stride_order)
356 return layout_info_t{lhs.stride_order, layout_prop_e(uint64_t(lhs.prop) & uint64_t(rhs.prop))};
357 else
358 return layout_info_t{uint64_t(-1), layout_prop_e::none};
359 }
360
362 template <typename A>
364
365 // Specialization of nda::get_layout_info for cvref types.
366 template <typename A>
367 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
368 inline constexpr layout_info_t get_layout_info<A> = get_layout_info<std::remove_cvref_t<A>>;
369
371 template <typename A>
373
375 template <typename A>
377
379 template <typename A>
381
387 long value;
388 };
389
391
392} // namespace nda
constexpr bool is_regular_v
Constexpr variable that is true if type A is a regular array, i.e. an nda::basic_array.
Definition traits.hpp:177
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:149
constexpr bool is_expression
Constexpr variable that is true if type A is a lazy expression type.
Definition traits.hpp:168
constexpr bool is_matrix_or_view_v
Constexpr variable that is true if type A is a regular matrix or a view of a matrix.
Definition traits.hpp:199
decltype(auto) get_first_element(A &&a)
Get the first element of an array/view or simply return the scalar if a scalar is given.
Definition traits.hpp:210
constexpr bool have_same_value_type_v
Constexpr variable that is true if all types in As have the same value type as A0.
Definition traits.hpp:238
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:159
std::decay_t< decltype(get_first_element(std::declval< A const >()))> get_value_t
Get the value type of an array/view or a scalar type.
Definition traits.hpp:225
constexpr bool have_same_rank_v
Constexpr variable that is true if all types in As have the same rank as A0.
Definition traits.hpp:242
constexpr bool is_view_v
Constexpr variable that is true if type A is a view, i.e. an nda::basic_array_view.
Definition traits.hpp:186
constexpr bool is_regular_or_view_v
Constexpr variable that is true if type A is either a regular array or a view.
Definition traits.hpp:195
typename remove_complex< get_value_t< A > >::type get_fp_t
Get the floating-point type associated with the value type of an array/view/scalar type.
Definition traits.hpp:234
constexpr bool layout_property_compatible(layout_prop_e from, layout_prop_e to)
Checks if two layout properties are compatible with each other.
Definition traits.hpp:279
constexpr bool has_contiguous(layout_prop_e lp)
Checks if a layout property has the contiguous property.
Definition traits.hpp:324
constexpr bool has_layout_smallest_stride_is_one
Constexpr variable that is true if type A has the smallest_stride_is_one nda::layout_prop_e guarantee...
Definition traits.hpp:380
constexpr bool has_strided_1d(layout_prop_e lp)
Checks if a layout property has the strided_1d property.
Definition traits.hpp:308
constexpr bool has_layout_strided_1d
Constexpr variable that is true if type A has the strided_1d nda::layout_prop_e guarantee.
Definition traits.hpp:376
constexpr layout_prop_e operator&(layout_prop_e lhs, layout_prop_e rhs)
Bitwise AND operator for two layout properties.
Definition traits.hpp:300
constexpr layout_prop_e operator|(layout_prop_e lhs, layout_prop_e rhs)
Bitwise OR operator for two layout properties.
Definition traits.hpp:291
constexpr layout_info_t get_layout_info
Constexpr variable that specifies the nda::layout_info_t of type A.
Definition traits.hpp:363
constexpr bool has_smallest_stride_is_one(layout_prop_e lp)
Checks if a layout property has the smallest_stride_is_one property.
Definition traits.hpp:316
constexpr bool has_contiguous_layout
Constexpr variable that is true if type A has the contiguous nda::layout_prop_e guarantee.
Definition traits.hpp:372
layout_prop_e
Compile-time guarantees of the memory layout of an array/view.
Definition traits.hpp:264
constexpr bool is_instantiation_of_v
Constexpr variable that is true if type T is an instantiation of TMPLT (see nda::is_instantiation_of)...
Definition traits.hpp:49
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:65
constexpr bool is_blas_lapack_v
Constexpr variable that is true if type T is either of type 'float', double, std::complex<float>' or ...
Definition traits.hpp:95
static constexpr bool always_false
Constexpr variable that is always false regardless of the types in Ts (used to trigger static_assert)...
Definition traits.hpp:61
constexpr bool is_scalar_for_v
Constexpr variable used to check requirements when initializing an nda::basic_array or nda::basic_arr...
Definition traits.hpp:83
static constexpr bool is_any_of
Constexpr variable that is true if type T is contained in the parameter pack Ts.
Definition traits.hpp:53
constexpr bool is_double_or_complex_v
Constexpr variable that is true if type T is a std::complex type or a double type.
Definition traits.hpp:88
static constexpr bool always_true
Constexpr variable that is always true regardless of the types in Ts.
Definition traits.hpp:57
typename remove_complex< T >::type remove_complex_t
Alias template for the nested type in nda::remove_complex.
Definition traits.hpp:125
constexpr bool is_scalar_v
Constexpr variable that is true if type S is a scalar type, i.e. arithmetic or complex.
Definition traits.hpp:69
constexpr bool is_scalar_or_convertible_v
Constexpr variable that is true if type S is a scalar type (see nda::is_scalar_v) or if a std::comple...
Definition traits.hpp:76
A small wrapper around a single long integer to be used as a linear index.
Definition traits.hpp:385
long value
Linear index.
Definition traits.hpp:387
Check if type T is of type TMPLT<...>.
Definition traits.hpp:41
Stores information about the memory layout and the stride order of an array/view.
Definition traits.hpp:337
uint64_t stride_order
Stride order of the array/view.
Definition traits.hpp:339
layout_prop_e prop
Memory layout properties of the array/view.
Definition traits.hpp:342
Trait that removes std::complex from a type and exposes its underlying value type.
Definition traits.hpp:105