TRIQS/nda 1.3.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
19namespace nda {
20
25
39 template <template <typename...> class TMPLT, typename T>
40 struct is_instantiation_of : std::false_type {};
41
42 // Specialization of nda::is_instantiation_of for when it evaluates to true.
43 template <template <typename...> class TMPLT, typename... U>
44 struct is_instantiation_of<TMPLT, TMPLT<U...>> : std::true_type {};
45
47 template <template <typename...> class TMPLT, typename T>
49
51 template <typename T, typename... Ts>
52 static constexpr bool is_any_of = (std::is_same_v<Ts, T> or ... or false);
53
55 template <typename... Ts>
56 static constexpr bool always_true = true;
57
59 template <typename... Ts>
60 static constexpr bool always_false = false;
61
63 template <typename T>
65
67 template <typename S>
68 inline constexpr bool is_scalar_v = std::is_arithmetic_v<std::remove_cvref_t<S>> or is_complex_v<S>;
69
74 template <typename S>
75 inline constexpr bool is_scalar_or_convertible_v = is_scalar_v<S> or std::is_constructible_v<std::complex<double>, S>;
76
81 template <typename S, typename A>
82 inline constexpr bool is_scalar_for_v =
83 (is_scalar_v<typename A::value_type> ? is_scalar_or_convertible_v<S> : std::is_same_v<S, typename A::value_type>);
84
86 template <typename T>
87 inline constexpr bool is_double_or_complex_v = is_complex_v<T> or std::is_same_v<double, std::remove_cvref_t<T>>;
88
90 template <typename T>
92
94
99
114 template <typename A>
115 inline constexpr char get_algebra = 'N';
116
117 // Specialization of nda::get_algebra for cvref types.
118 template <typename A>
119 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
120 inline constexpr char get_algebra<A> = get_algebra<std::remove_cvref_t<A>>;
121
123 template <typename A>
124 requires(std::ranges::contiguous_range<A> or requires { std::declval<A const>().shape(); })
125 constexpr int get_rank = []() {
126 if constexpr (std::ranges::contiguous_range<A>)
127 return 1;
128 else
129 return std::tuple_size_v<std::remove_cvref_t<decltype(std::declval<A const>().shape())>>;
130 }();
131
133 template <typename A>
134 inline constexpr bool is_regular_v = false;
135
136 // Specialization of nda::is_regular_v for cvref types.
137 template <typename A>
138 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
139 inline constexpr bool is_regular_v<A> = is_regular_v<std::remove_cvref_t<A>>;
140
142 template <typename A>
143 inline constexpr bool is_view_v = false;
144
145 // Specialization of nda::is_view_v for cvref types.
146 template <typename A>
147 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
148 inline constexpr bool is_view_v<A> = is_view_v<std::remove_cvref_t<A>>;
149
151 template <typename A>
153
155 template <typename A>
156 inline constexpr bool is_matrix_or_view_v = is_regular_or_view_v<A> and (get_algebra<A> == 'M') and (get_rank<A> == 2);
157
165 template <typename A>
166 decltype(auto) get_first_element(A const &a) {
167 if constexpr (is_scalar_v<A>) {
168 return a;
169 } else {
170 return [&a]<auto... Is>(std::index_sequence<Is...>) -> decltype(auto) {
171 return a((0 * Is)...); // repeat 0 sizeof...(Is) times
172 }(std::make_index_sequence<get_rank<A>>{});
173 }
174 }
175
180 template <typename A>
181 using get_value_t = std::decay_t<decltype(get_first_element(std::declval<A const>()))>;
182
184 template <typename A0, typename... As>
185 inline constexpr bool have_same_value_type_v = (std::is_same_v<get_value_t<A0>, get_value_t<As>> and ... and true);
186
188 template <typename A0, typename... As>
189 inline constexpr bool have_same_rank_v = ((get_rank<A0> == get_rank<As>)and... and true);
190
192
197
211 enum class layout_prop_e : uint64_t {
212 none = 0x0,
213 strided_1d = 0x1,
214 smallest_stride_is_one = 0x2,
215 contiguous = strided_1d | smallest_stride_is_one
216 };
217
227 if (from == layout_prop_e::contiguous) return true;
228 return ((to == layout_prop_e::none) or (to == from));
229 }
230
238 constexpr layout_prop_e operator|(layout_prop_e lhs, layout_prop_e rhs) { return layout_prop_e(uint64_t(lhs) | uint64_t(rhs)); }
239
247 constexpr layout_prop_e operator&(layout_prop_e lhs, layout_prop_e rhs) { return layout_prop_e{uint64_t(lhs) & uint64_t(rhs)}; }
248
255 inline constexpr bool has_strided_1d(layout_prop_e lp) { return uint64_t(lp) & uint64_t(layout_prop_e::strided_1d); }
256
263 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); }
264
271 inline constexpr bool has_contiguous(layout_prop_e lp) { return has_strided_1d(lp) and has_smallest_stride_is_one(lp); }
272
273 // FIXME : I need a NONE for stride_order. For the scalars ...
286 uint64_t stride_order = 0;
287
289 layout_prop_e prop = layout_prop_e::none;
290 };
291
302 if (lhs.stride_order == rhs.stride_order)
303 return layout_info_t{lhs.stride_order, layout_prop_e(uint64_t(lhs.prop) & uint64_t(rhs.prop))};
304 else
305 return layout_info_t{uint64_t(-1), layout_prop_e::none};
306 }
307
309 template <typename A>
311
312 // Specialization of nda::get_layout_info for cvref types.
313 template <typename A>
314 requires(!std::is_same_v<A, std::remove_cvref_t<A>>)
315 inline constexpr layout_info_t get_layout_info<A> = get_layout_info<std::remove_cvref_t<A>>;
316
318 template <typename A>
320
322 template <typename A>
324
326 template <typename A>
328
334 long value;
335 };
336
338
339} // 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:134
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:115
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:156
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:185
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:125
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:181
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:189
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:143
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:152
decltype(auto) get_first_element(A const &a)
Get the first element of an array/view or simply return the scalar if a scalar is given.
Definition traits.hpp:166
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:226
constexpr bool has_contiguous(layout_prop_e lp)
Checks if a layout property has the contiguous property.
Definition traits.hpp:271
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:327
constexpr bool has_strided_1d(layout_prop_e lp)
Checks if a layout property has the strided_1d property.
Definition traits.hpp:255
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:323
constexpr layout_prop_e operator&(layout_prop_e lhs, layout_prop_e rhs)
Bitwise AND operator for two layout properties.
Definition traits.hpp:247
constexpr layout_prop_e operator|(layout_prop_e lhs, layout_prop_e rhs)
Bitwise OR operator for two layout properties.
Definition traits.hpp:238
constexpr layout_info_t get_layout_info
Constexpr variable that specifies the nda::layout_info_t of type A.
Definition traits.hpp:310
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:263
constexpr bool has_contiguous_layout
Constexpr variable that is true if type A has the contiguous nda::layout_prop_e guarantee.
Definition traits.hpp:319
layout_prop_e
Compile-time guarantees of the memory layout of an array/view.
Definition traits.hpp:211
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:48
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:64
constexpr bool is_blas_lapack_v
Alias for nda::is_double_or_complex_v.
Definition traits.hpp:91
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:60
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:82
static constexpr bool is_any_of
Constexpr variable that is true if type T is contained in the parameter pack Ts.
Definition traits.hpp:52
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:87
static constexpr bool always_true
Constexpr variable that is always true regardless of the types in Ts.
Definition traits.hpp:56
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:68
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:75
A small wrapper around a single long integer to be used as a linear index.
Definition traits.hpp:332
long value
Linear index.
Definition traits.hpp:334
Check if type T is of type TMPLT<...>.
Definition traits.hpp:40
Stores information about the memory layout and the stride order of an array/view.
Definition traits.hpp:284
uint64_t stride_order
Stride order of the array/view.
Definition traits.hpp:286
layout_prop_e prop
Memory layout properties of the array/view.
Definition traits.hpp:289