QUGaR 0.0.4
Loading...
Searching...
No Matches
tensor_index_tp.hpp
Go to the documentation of this file.
1// --------------------------------------------------------------------------
2//
3// Copyright (C) 2025-present by Pablo Antolin
4//
5// This file is part of the QUGaR library.
6//
7// SPDX-License-Identifier: MIT
8//
9// --------------------------------------------------------------------------
10
11#ifndef QUGAR_LIBRARY_TENSOR_INDEX_TP_HPP
12#define QUGAR_LIBRARY_TENSOR_INDEX_TP_HPP
13
20
21#include <qugar/vector.hpp>
22
23
24#include <array>
25#include <cassert>
26#include <cstddef>
27#include <functional>
28#include <type_traits>
29
30namespace qugar {
31
32template<int dim> class TensorIndexTP;
33
37template<int dim> class TensorSizeTP : public Vector<int, dim>
38{
39public:
41
43
46
49 explicit TensorSizeTP(const int size);
50
53 explicit TensorSizeTP(const Vector<int, dim> &sizes);
54
57 explicit TensorSizeTP(const TensorIndexTP<dim> &sizes);
58
60
66
72
75 [[nodiscard]] int size() const;
76
80 [[nodiscard]] bool operator==(const TensorSizeTP<dim> &rhs) const;
81
88 [[nodiscard]] TensorSizeTP<dim> operator+(const TensorSizeTP<dim> &rhs) const;
89};
90
94template<int dim> class TensorIndexTP : public Vector<int, dim>
95{
96public:
98
99
101 template<int aux_dim = dim>
102 requires(aux_dim == dim && dim > 0)
105
106 template<int aux_dim = dim>
107 requires(aux_dim == dim && dim == 0)
110
114 template<int aux_dim = dim>
115 requires(aux_dim == dim && dim > 0)
116 explicit TensorIndexTP(int ind) : Vector<int, dim>(ind)
117 {}
118
121 explicit TensorIndexTP(const Vector<int, dim> &indices);
122
125 explicit TensorIndexTP(const TensorSizeTP<dim> &indices);
126
134 template<int aux_dim = dim>
135 requires(aux_dim == dim && dim > 0)
136 TensorIndexTP(int flat_index, const TensorIndexTP<dim> &size)
137 {
138 int total_size = prod(size.as_Vector());
139
140 // NOLINTNEXTLINE (readability-simplify-boolean-expr)
141 assert(0 <= flat_index && flat_index < total_size);
142
143 for (int dir = dim - 1; dir >= 0; --dir) {
144 total_size /= size(dir);
145 const auto ind = flat_index / total_size;
146 this->operator()(dir) = ind;
147 flat_index -= ind * total_size;
148 }
149 }
150
158 template<int aux_dim = dim>
159 requires(aux_dim == dim && dim > 0)
160 TensorIndexTP(int flat_index, const TensorSizeTP<dim> &size) : TensorIndexTP(flat_index, TensorIndexTP(size))
161 {}
162
165 template<typename... T>
166 requires(dim > 1 && sizeof...(T) == dim && std::conjunction_v<std::is_same<int, T>...>)
167 explicit TensorIndexTP(T... indices) : TensorIndexTP(Vector<int, dim>{ indices... })
168 {}
169
171
177 {
178 // For an unknown reason, the linker (llvm 17) doesn't find the symbol if the implementation is moved to the .cpp
179 // file
180 return dynamic_cast<const Vector<int, dim> &>(*this);
181 }
182
188
194 template<typename S> [[nodiscard]] int flat(const S &size) const;
195
199 [[nodiscard]] bool operator==(const TensorIndexTP<dim> &rhs) const;
200
204 [[nodiscard]] bool operator!=(const TensorIndexTP<dim> &rhs) const;
205
210 [[nodiscard]] bool operator<(const TensorIndexTP<dim> &rhs) const;
211
215 template<int aux_dim = dim>
216 requires(aux_dim == dim && dim > 1)
218
225 [[nodiscard]] std::size_t hash() const;
226};
227
228}// namespace qugar
229
230namespace std {
231template<int dim> struct hash<qugar::TensorIndexTP<dim>>
232{
233 size_t operator()(const qugar::TensorIndexTP<dim> &tid) const noexcept { return tid.hash(); }
234};
235}// namespace std
236
237namespace qugar {
238
242template<int dim> class TensorIndexRangeTP
243{
244public:
248 TensorIndexRangeTP(const TensorIndexTP<dim> &lower_bound, const TensorIndexTP<dim> &upper_bound);
249
253 explicit TensorIndexRangeTP(const TensorIndexTP<dim> &upper_bound);
254
258 explicit TensorIndexRangeTP(const TensorSizeTP<dim> &upper_bound);
259
263 explicit TensorIndexRangeTP(int upper_bound);
264
267 [[nodiscard]] TensorSizeTP<dim> get_sizes() const;
268
271 [[nodiscard]] int size() const;
272
277 [[nodiscard]] std::array<TensorIndexRangeTP<dim>, 2> split() const;
278
279
282 {
283 public:
289 const TensorIndexTP<dim> &lower_bound,
290 const TensorIndexTP<dim> &upper_bound);
291
294 [[nodiscard]] const TensorIndexTP<dim> &operator*() const;
295
298 [[nodiscard]] const TensorIndexTP<dim> *operator->() const;
299
303 [[nodiscard]] int flat() const;
304
308
311 // NOLINTNEXTLINE (cert-dcl21-cpp) // Check deprecated.
313
318 [[nodiscard]] bool operator==(const Iterator &rhs) const;
319
324 [[nodiscard]] bool operator!=(const Iterator &rhs) const;
325
326 private:
333 };
334
339
343
347 Iterator cend() const;
348
351 Iterator end() const;
352
355 [[nodiscard]] const TensorIndexTP<dim> &get_lower_bound() const;
356
359 [[nodiscard]] const TensorIndexTP<dim> &get_upper_bound() const;
360
364 [[nodiscard]] bool is_in_range(const TensorIndexTP<dim> &index) const;
365
369 [[nodiscard]] bool is_in_range(int index) const;
370
371private:
376};
377
378
379}// namespace qugar
380
381#endif// QUGAR_LIBRARY_TENSOR_INDEX_TP_HPP
Iterator class for tensor index ranges.
Definition tensor_index_tp.hpp:282
TensorIndexTP< dim > index_
Iterator index.
Definition tensor_index_tp.hpp:328
bool operator!=(const Iterator &rhs) const
Compares if two iterators are different. They are considered to be different if at least one of the c...
const TensorIndexTP< dim > & operator*() const
Dereference operator.
Iterator & operator++()
Pre increments the iterator.
bool operator==(const Iterator &rhs) const
Compares if two iterators are equal. They are considered to be equal if their indices and bounds are ...
const TensorIndexTP< dim > * operator->() const
Indirection operator.
int flat() const
Transforms the current tensor index into a flat one by considering the upper bound as associated size...
TensorIndexTP< dim > lower_bound_
Lower bound.
Definition tensor_index_tp.hpp:330
Iterator operator++(int)
Post increments the iterator.
Iterator(const TensorIndexTP< dim > &index, const TensorIndexTP< dim > &lower_bound, const TensorIndexTP< dim > &upper_bound)
Constructs a new iterator object given and index and its lower and upper bounds.
TensorIndexTP< dim > upper_bound_
Upper bound.
Definition tensor_index_tp.hpp:332
Class representing a dim-dimensional range defined by lower and upper tensor bounds.
Definition tensor_index_tp.hpp:243
bool is_in_range(int index) const
Checks that the given index is contained in the range.
std::array< TensorIndexRangeTP< dim >, 2 > split() const
Splits the current range along the direction with a largest number of indices.
TensorIndexRangeTP(int upper_bound)
Constructs a new TensorIndexRangeTP object from its upper bound. The lower bound is assumed to be zer...
TensorIndexRangeTP(const TensorIndexTP< dim > &upper_bound)
Constructs a new TensorIndexRangeTP object from its upper bound. The lower bound is assumed to be zer...
Iterator cbegin() const
Creates a begin iterator.
TensorSizeTP< dim > get_sizes() const
Gets the sizes along all the directions.
const TensorIndexTP< dim > & get_upper_bound() const
Gets the upper bound.
TensorIndexRangeTP(const TensorIndexTP< dim > &lower_bound, const TensorIndexTP< dim > &upper_bound)
Constructs a new TensorIndexRangeTP object from its lower and upper bounds.
TensorIndexTP< dim > upper_bound_
Upper bounds.
Definition tensor_index_tp.hpp:375
int size() const
returns the number of entries in the range.
bool is_in_range(const TensorIndexTP< dim > &index) const
Checks that the given index is contained in the range.
Iterator end() const
Creates an end iterator.
Iterator begin() const
Creates a begin iterator.
const TensorIndexTP< dim > & get_lower_bound() const
Gets the lower bound.
TensorIndexTP< dim > lower_bound_
Lower bounds.
Definition tensor_index_tp.hpp:373
TensorIndexRangeTP(const TensorSizeTP< dim > &upper_bound)
Constructs a new TensorIndexRangeTP object from its upper bound. The lower bound is assumed to be zer...
Iterator cend() const
Creates an end iterator.
Class representing a dim-dimensional tensor-product indices.
Definition tensor_index_tp.hpp:95
TensorIndexTP(int ind)
Constructs a new TensorIndexTP object from an index.
Definition tensor_index_tp.hpp:116
bool operator<(const TensorIndexTP< dim > &rhs) const
Checks if the current tensor index is smaller than rhs. To determine if one is smaller than the other...
TensorIndexTP()
Definition tensor_index_tp.hpp:108
const Vector< int, dim > & as_Vector() const
Returns the tensor index casted as a Vector.
Definition tensor_index_tp.hpp:176
TensorIndexTP()
Default constructor. Initializes indices to zero.
Definition tensor_index_tp.hpp:103
Vector< int, dim > & as_Vector()
Returns the tensor index casted as a Vector.
TensorIndexTP< dim - 1 > remove_component(int comp) const
Creates a new index by removing one of its component.
TensorIndexTP(T... indices)
Constructs the tensor index from an argument list.
Definition tensor_index_tp.hpp:167
int flat(const S &size) const
Gets the flat index associated to the current tensor index for a given tensor size.
TensorIndexTP(const TensorSizeTP< dim > &indices)
Constructs a new TensorIndexTP object from its indices.
TensorIndexTP(int flat_index, const TensorIndexTP< dim > &size)
Constructs a new TensorIndexTP object from a flat index and an associated tensor size following the l...
Definition tensor_index_tp.hpp:136
TensorIndexTP(int flat_index, const TensorSizeTP< dim > &size)
Constructs a new TensorIndexTP object from a flat index and an associated tensor size following the l...
Definition tensor_index_tp.hpp:160
bool operator!=(const TensorIndexTP< dim > &rhs) const
Checks if two tensor indices are different by comparing all its components. They are different if at ...
std::size_t hash() const
Computes a hash value for the tensor index.
TensorIndexTP(const Vector< int, dim > &indices)
Constructs a new TensorIndexTP object from its indices.
bool operator==(const TensorIndexTP< dim > &rhs) const
Checks if two tensor indices are equal by comparing all its components. They are equal if all the com...
Class representing a dim-dimensional tensor-product sizes container.
Definition tensor_index_tp.hpp:38
TensorSizeTP< dim > operator+(const TensorSizeTP< dim > &rhs) const
Adds two TensorSizeTP objects.
int size() const
Gets the total number of entries (product of sizes along all directions).
TensorSizeTP(const TensorIndexTP< dim > &sizes)
Construct a new TensorSizeTP object from sizes along directions.
const Vector< int, dim > & as_Vector() const
Returns the tensor size casted as a Vector.
Vector< int, dim > & as_Vector()
Returns the tensor size casted as a Vector.
TensorSizeTP()
Default constructor. Initializes indices to zero.
bool operator==(const TensorSizeTP< dim > &rhs) const
Checks if two tensor sizes are equal by comparing all its components. They are equal if all the compo...
TensorSizeTP(const Vector< int, dim > &sizes)
Construct a new TensorSizeTP object from sizes along directions.
TensorSizeTP(const int size)
Construct a new TensorSizeTP object from size along directions.
QUGaR's main namespace.
Definition affine_transf.hpp:28
::algoim::uvector< T, dim > Vector
Class representing a vector.
Definition vector.hpp:31
std::ptrdiff_t index
Definition types.hpp:19
Definition tensor_index_tp.hpp:230
size_t operator()(const qugar::TensorIndexTP< dim > &tid) const noexcept
Definition tensor_index_tp.hpp:233