Eigen  5.0.1
 
Loading...
Searching...
No Matches
DenseStorage.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2006-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6// Copyright (C) 2010-2013 Hauke Heibel <hauke.heibel@gmail.com>
7//
8// This Source Code Form is subject to the terms of the Mozilla
9// Public License v. 2.0. If a copy of the MPL was not distributed
10// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11// SPDX-License-Identifier: MPL-2.0
12
13#ifndef EIGEN_MATRIXSTORAGE_H
14#define EIGEN_MATRIXSTORAGE_H
15
16#ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
17#define EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(X) \
18 X; \
19 EIGEN_DENSE_STORAGE_CTOR_PLUGIN;
20#else
21#define EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(X)
22#endif
23
24// IWYU pragma: private
25#include "./InternalHeaderCheck.h"
26
27namespace Eigen {
28
29namespace internal {
30
31#if defined(EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT)
32#define EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT(Alignment)
33#else
34#define EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT(Alignment) \
35 eigen_assert((is_constant_evaluated() || (std::uintptr_t(array) % Alignment == 0)) && \
36 "this assertion is explained here: " \
37 "http://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html" \
38 " **** READ THIS WEB PAGE !!! ****");
39#endif
40
41#if EIGEN_STACK_ALLOCATION_LIMIT
42#define EIGEN_MAKE_STACK_ALLOCATION_ASSERT(X) \
43 EIGEN_STATIC_ASSERT(X <= EIGEN_STACK_ALLOCATION_LIMIT, OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG)
44#else
45#define EIGEN_MAKE_STACK_ALLOCATION_ASSERT(X)
46#endif
47
52
53template <typename T, int Size, int MatrixOrArrayOptions,
54 int Alignment = (MatrixOrArrayOptions & DontAlign) ? 0 : compute_default_alignment<T, Size>::value>
55struct plain_array {
56 EIGEN_ALIGN_TO_BOUNDARY(Alignment) T array[Size];
57#if defined(EIGEN_NO_DEBUG) || defined(EIGEN_TESTING_PLAINOBJECT_CTOR)
58 EIGEN_DEVICE_FUNC constexpr plain_array() = default;
59#else
60 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr plain_array() {
61 EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT(Alignment)
62 EIGEN_MAKE_STACK_ALLOCATION_ASSERT(Size * sizeof(T))
63 }
64#endif
65};
66
67template <typename T, int Size, int MatrixOrArrayOptions>
68struct plain_array<T, Size, MatrixOrArrayOptions, 0> {
69 // on some 32-bit platforms, stack-allocated arrays are aligned to 4 bytes, not the preferred alignment of T
70 EIGEN_ALIGN_TO_BOUNDARY(alignof(T)) T array[Size];
71#if defined(EIGEN_NO_DEBUG) || defined(EIGEN_TESTING_PLAINOBJECT_CTOR)
72 EIGEN_DEVICE_FUNC constexpr plain_array() = default;
73#else
74 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr plain_array() { EIGEN_MAKE_STACK_ALLOCATION_ASSERT(Size * sizeof(T)) }
75#endif
76};
77
78template <typename T, int Size, int Options, int Alignment>
79EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void swap_plain_array(plain_array<T, Size, Options, Alignment>& a,
80 plain_array<T, Size, Options, Alignment>& b,
81 Index a_size, Index b_size) {
82 Index common_size = numext::mini(a_size, b_size);
83 std::swap_ranges(a.array, a.array + common_size, b.array);
84 if (a_size > b_size)
85 smart_copy(a.array + common_size, a.array + a_size, b.array + common_size);
86 else if (b_size > a_size)
87 smart_copy(b.array + common_size, b.array + b_size, a.array + common_size);
88}
89
90template <typename T, int Size, int Rows, int Cols, int Options>
91class DenseStorage_impl {
92 plain_array<T, Size, Options> m_data;
93
94 public:
95#ifndef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
96 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
97 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(const DenseStorage_impl&) = default;
98#else
99 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl() {
100 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = Size)
101 }
102 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other) {
103 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = Size)
104 smart_copy(other.m_data.array, other.m_data.array + Size, m_data.array);
105 }
106#endif
107 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(Index /*size*/, Index /*rows*/, Index /*cols*/) {}
108 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(const DenseStorage_impl&) = default;
109 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void swap(DenseStorage_impl& other) {
110 numext::swap(m_data, other.m_data);
111 }
112 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index /*rows*/, Index /*cols*/) {}
113 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index /*rows*/, Index /*cols*/) {}
114 EIGEN_DEVICE_FUNC constexpr Index rows() const { return Rows; }
115 EIGEN_DEVICE_FUNC constexpr Index cols() const { return Cols; }
116 EIGEN_DEVICE_FUNC constexpr Index size() const { return Rows * Cols; }
117 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data.array; }
118 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data.array; }
119};
120template <typename T, int Size, int Cols, int Options>
121class DenseStorage_impl<T, Size, Dynamic, Cols, Options> {
122 plain_array<T, Size, Options> m_data;
123 Index m_rows = 0;
124
125 public:
126 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
127 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
128 : m_rows(other.m_rows) {
129 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
130 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
131 }
132 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index rows, Index /*cols*/)
133 : m_rows(rows) {
134 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
135 EIGEN_UNUSED_VARIABLE(size);
136 }
137 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
138 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
139 m_rows = other.m_rows;
140 return *this;
141 }
142 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void swap(DenseStorage_impl& other) {
143 swap_plain_array(m_data, other.m_data, size(), other.size());
144 numext::swap(m_rows, other.m_rows);
145 }
146 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index rows, Index /*cols*/) { m_rows = rows; }
147 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index rows, Index /*cols*/) { m_rows = rows; }
148 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
149 EIGEN_DEVICE_FUNC constexpr Index cols() const { return Cols; }
150 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * Cols; }
151 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data.array; }
152 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data.array; }
153};
154template <typename T, int Size, int Rows, int Options>
155class DenseStorage_impl<T, Size, Rows, Dynamic, Options> {
156 plain_array<T, Size, Options> m_data;
157 Index m_cols = 0;
158
159 public:
160 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
161 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
162 : m_cols(other.m_cols) {
163 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
164 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
165 }
166 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index /*rows*/, Index cols)
167 : m_cols(cols) {
168 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
169 EIGEN_UNUSED_VARIABLE(size);
170 }
171 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
172 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
173 m_cols = other.m_cols;
174 return *this;
175 }
176 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void swap(DenseStorage_impl& other) {
177 swap_plain_array(m_data, other.m_data, size(), other.size());
178 numext::swap(m_cols, other.m_cols);
179 }
180 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index /*rows*/, Index cols) { m_cols = cols; }
181 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index /*rows*/, Index cols) { m_cols = cols; }
182 EIGEN_DEVICE_FUNC constexpr Index rows() const { return Rows; }
183 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
184 EIGEN_DEVICE_FUNC constexpr Index size() const { return Rows * m_cols; }
185 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data.array; }
186 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data.array; }
187};
188template <typename T, int Size, int Options>
189class DenseStorage_impl<T, Size, Dynamic, Dynamic, Options> {
190 plain_array<T, Size, Options> m_data;
191 Index m_rows = 0;
192 Index m_cols = 0;
193
194 public:
195 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
196 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
197 : m_rows(other.m_rows), m_cols(other.m_cols) {
198 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
199 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
200 }
201 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index rows, Index cols)
202 : m_rows(rows), m_cols(cols) {
203 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
204 EIGEN_UNUSED_VARIABLE(size);
205 }
206 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
207 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
208 m_rows = other.m_rows;
209 m_cols = other.m_cols;
210 return *this;
211 }
212 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void swap(DenseStorage_impl& other) {
213 swap_plain_array(m_data, other.m_data, size(), other.size());
214 numext::swap(m_rows, other.m_rows);
215 numext::swap(m_cols, other.m_cols);
216 }
217 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index rows, Index cols) {
218 m_rows = rows;
219 m_cols = cols;
220 }
221 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index rows, Index cols) {
222 m_rows = rows;
223 m_cols = cols;
224 }
225 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
226 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
227 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * m_cols; }
228 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data.array; }
229 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data.array; }
230};
231// null matrix variants
232template <typename T, int Rows, int Cols, int Options>
233class DenseStorage_impl<T, 0, Rows, Cols, Options> {
234 public:
235 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
236 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(const DenseStorage_impl&) = default;
237 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(Index /*size*/, Index /*rows*/, Index /*cols*/) {}
238 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(const DenseStorage_impl&) = default;
239 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl&) {}
240 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index /*rows*/, Index /*cols*/) {}
241 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index /*rows*/, Index /*cols*/) {}
242 EIGEN_DEVICE_FUNC constexpr Index rows() const { return Rows; }
243 EIGEN_DEVICE_FUNC constexpr Index cols() const { return Cols; }
244 EIGEN_DEVICE_FUNC constexpr Index size() const { return Rows * Cols; }
245 EIGEN_DEVICE_FUNC constexpr T* data() { return nullptr; }
246 EIGEN_DEVICE_FUNC constexpr const T* data() const { return nullptr; }
247};
248template <typename T, int Cols, int Options>
249class DenseStorage_impl<T, 0, Dynamic, Cols, Options> {
250 Index m_rows = 0;
251
252 public:
253 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
254 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(const DenseStorage_impl&) = default;
255 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(Index /*size*/, Index rows, Index /*cols*/) : m_rows(rows) {}
256 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(const DenseStorage_impl&) = default;
257 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept { numext::swap(m_rows, other.m_rows); }
258 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index rows, Index /*cols*/) { m_rows = rows; }
259 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index rows, Index /*cols*/) { m_rows = rows; }
260 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
261 EIGEN_DEVICE_FUNC constexpr Index cols() const { return Cols; }
262 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * Cols; }
263 EIGEN_DEVICE_FUNC constexpr T* data() { return nullptr; }
264 EIGEN_DEVICE_FUNC constexpr const T* data() const { return nullptr; }
265};
266template <typename T, int Rows, int Options>
267class DenseStorage_impl<T, 0, Rows, Dynamic, Options> {
268 Index m_cols = 0;
269
270 public:
271 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
272 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(const DenseStorage_impl&) = default;
273 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(Index /*size*/, Index /*rows*/, Index cols) : m_cols(cols) {}
274 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(const DenseStorage_impl&) = default;
275 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept { numext::swap(m_cols, other.m_cols); }
276 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index /*rows*/, Index cols) { m_cols = cols; }
277 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index /*rows*/, Index cols) { m_cols = cols; }
278 EIGEN_DEVICE_FUNC constexpr Index rows() const { return Rows; }
279 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
280 EIGEN_DEVICE_FUNC constexpr Index size() const { return Rows * m_cols; }
281 EIGEN_DEVICE_FUNC constexpr T* data() { return nullptr; }
282 EIGEN_DEVICE_FUNC constexpr const T* data() const { return nullptr; }
283};
284template <typename T, int Options>
285class DenseStorage_impl<T, 0, Dynamic, Dynamic, Options> {
286 Index m_rows = 0;
287 Index m_cols = 0;
288
289 public:
290 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
291 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(const DenseStorage_impl&) = default;
292 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(Index /*size*/, Index rows, Index cols) : m_rows(rows), m_cols(cols) {}
293 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(const DenseStorage_impl&) = default;
294 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept {
295 numext::swap(m_rows, other.m_rows);
296 numext::swap(m_cols, other.m_cols);
297 }
298 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index rows, Index cols) {
299 m_rows = rows;
300 m_cols = cols;
301 }
302 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index rows, Index cols) {
303 m_rows = rows;
304 m_cols = cols;
305 }
306 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
307 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
308 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * m_cols; }
309 EIGEN_DEVICE_FUNC constexpr T* data() { return nullptr; }
310 EIGEN_DEVICE_FUNC constexpr const T* data() const { return nullptr; }
311};
312// fixed-size matrix with dynamic memory allocation not currently supported
313template <typename T, int Rows, int Cols, int Options>
314class DenseStorage_impl<T, Dynamic, Rows, Cols, Options> {};
315// dynamic-sized variants
316template <typename T, int Cols, int Options>
317class DenseStorage_impl<T, Dynamic, Dynamic, Cols, Options> {
318 static constexpr bool Align = (Options & DontAlign) == 0;
319 T* m_data = nullptr;
320 Index m_rows = 0;
321
322 public:
323 static constexpr int Size = Dynamic;
324 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
325 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
326 : m_data(conditional_aligned_new_auto<T, Align>(other.size())), m_rows(other.m_rows) {
327 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
328 smart_copy(other.m_data, other.m_data + other.size(), m_data);
329 }
330 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index rows, Index /*cols*/)
331 : m_data(conditional_aligned_new_auto<T, Align>(size)), m_rows(rows) {
332 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
333 }
334 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(DenseStorage_impl&& other) noexcept
335 : m_data(other.m_data), m_rows(other.m_rows) {
336 other.m_data = nullptr;
337 other.m_rows = 0;
338 }
339 EIGEN_DEVICE_FUNC ~DenseStorage_impl() { conditional_aligned_delete_auto<T, Align>(m_data, size()); }
340 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
341 resize(other.size(), other.rows(), other.cols());
342 smart_copy(other.m_data, other.m_data + other.size(), m_data);
343 return *this;
344 }
345 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(DenseStorage_impl&& other) noexcept {
346 this->swap(other);
347 return *this;
348 }
349 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept {
350 numext::swap(m_data, other.m_data);
351 numext::swap(m_rows, other.m_rows);
352 }
353 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void conservativeResize(Index size, Index rows, Index /*cols*/) {
354 m_data = conditional_aligned_realloc_new_auto<T, Align>(m_data, size, this->size());
355 m_rows = rows;
356 }
357 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void resize(Index size, Index rows, Index /*cols*/) {
358 Index oldSize = this->size();
359 if (oldSize != size) {
360 conditional_aligned_delete_auto<T, Align>(m_data, oldSize);
361 m_data = conditional_aligned_new_auto<T, Align>(size);
362 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
363 }
364 m_rows = rows;
365 }
366 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
367 EIGEN_DEVICE_FUNC constexpr Index cols() const { return Cols; }
368 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * Cols; }
369 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data; }
370 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data; }
371};
372template <typename T, int Rows, int Options>
373class DenseStorage_impl<T, Dynamic, Rows, Dynamic, Options> {
374 static constexpr bool Align = (Options & DontAlign) == 0;
375 T* m_data = nullptr;
376 Index m_cols = 0;
377
378 public:
379 static constexpr int Size = Dynamic;
380 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
381 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
382 : m_data(conditional_aligned_new_auto<T, Align>(other.size())), m_cols(other.m_cols) {
383 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
384 smart_copy(other.m_data, other.m_data + other.size(), m_data);
385 }
386 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index /*rows*/, Index cols)
387 : m_data(conditional_aligned_new_auto<T, Align>(size)), m_cols(cols) {
388 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
389 }
390 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(DenseStorage_impl&& other) noexcept
391 : m_data(other.m_data), m_cols(other.m_cols) {
392 other.m_data = nullptr;
393 other.m_cols = 0;
394 }
395 EIGEN_DEVICE_FUNC ~DenseStorage_impl() { conditional_aligned_delete_auto<T, Align>(m_data, size()); }
396 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
397 resize(other.size(), other.rows(), other.cols());
398 smart_copy(other.m_data, other.m_data + other.size(), m_data);
399 return *this;
400 }
401 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(DenseStorage_impl&& other) noexcept {
402 this->swap(other);
403 return *this;
404 }
405 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept {
406 numext::swap(m_data, other.m_data);
407 numext::swap(m_cols, other.m_cols);
408 }
409 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void conservativeResize(Index size, Index /*rows*/, Index cols) {
410 m_data = conditional_aligned_realloc_new_auto<T, Align>(m_data, size, this->size());
411 m_cols = cols;
412 }
413 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void resize(Index size, Index /*rows*/, Index cols) {
414 Index oldSize = this->size();
415 if (oldSize != size) {
416 conditional_aligned_delete_auto<T, Align>(m_data, oldSize);
417 m_data = conditional_aligned_new_auto<T, Align>(size);
418 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
419 }
420 m_cols = cols;
421 }
422 EIGEN_DEVICE_FUNC constexpr Index rows() const { return Rows; }
423 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
424 EIGEN_DEVICE_FUNC constexpr Index size() const { return Rows * m_cols; }
425 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data; }
426 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data; }
427};
428template <typename T, int Options>
429class DenseStorage_impl<T, Dynamic, Dynamic, Dynamic, Options> {
430 static constexpr bool Align = (Options & DontAlign) == 0;
431 T* m_data = nullptr;
432 Index m_rows = 0;
433 Index m_cols = 0;
434
435 public:
436 static constexpr int Size = Dynamic;
437 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
438 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
439 : m_data(conditional_aligned_new_auto<T, Align>(other.size())), m_rows(other.m_rows), m_cols(other.m_cols) {
440 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
441 smart_copy(other.m_data, other.m_data + other.size(), m_data);
442 }
443 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index rows, Index cols)
444 : m_data(conditional_aligned_new_auto<T, Align>(size)), m_rows(rows), m_cols(cols) {
445 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
446 }
447 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(DenseStorage_impl&& other) noexcept
448 : m_data(other.m_data), m_rows(other.m_rows), m_cols(other.m_cols) {
449 other.m_data = nullptr;
450 other.m_rows = 0;
451 other.m_cols = 0;
452 }
453 EIGEN_DEVICE_FUNC ~DenseStorage_impl() { conditional_aligned_delete_auto<T, Align>(m_data, size()); }
454 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
455 resize(other.size(), other.rows(), other.cols());
456 smart_copy(other.m_data, other.m_data + other.size(), m_data);
457 return *this;
458 }
459 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(DenseStorage_impl&& other) noexcept {
460 this->swap(other);
461 return *this;
462 }
463 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept {
464 numext::swap(m_data, other.m_data);
465 numext::swap(m_rows, other.m_rows);
466 numext::swap(m_cols, other.m_cols);
467 }
468 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void conservativeResize(Index size, Index rows, Index cols) {
469 m_data = conditional_aligned_realloc_new_auto<T, Align>(m_data, size, this->size());
470 m_rows = rows;
471 m_cols = cols;
472 }
473 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void resize(Index size, Index rows, Index cols) {
474 Index oldSize = this->size();
475 if (oldSize != size) {
476 conditional_aligned_delete_auto<T, Align>(m_data, oldSize);
477 m_data = conditional_aligned_new_auto<T, Align>(size);
478 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
479 }
480 m_rows = rows;
481 m_cols = cols;
482 }
483 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
484 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
485 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * m_cols; }
486 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data; }
487 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data; }
488};
489template <typename T, int Size, int Rows, int Cols>
490struct use_default_move {
491 static constexpr bool DynamicObject = Size == Dynamic;
492 static constexpr bool TrivialObject =
493 (!NumTraits<T>::RequireInitialization) && (Rows >= 0) && (Cols >= 0) && (Size == Rows * Cols);
494 static constexpr bool value = DynamicObject || TrivialObject;
495};
496} // end namespace internal
497
510template <typename T, int Size, int Rows, int Cols, int Options,
511 bool Trivial = internal::use_default_move<T, Size, Rows, Cols>::value>
512class DenseStorage : public internal::DenseStorage_impl<T, Size, Rows, Cols, Options> {
513 using Base = internal::DenseStorage_impl<T, Size, Rows, Cols, Options>;
514
515 public:
516 EIGEN_DEVICE_FUNC constexpr DenseStorage() = default;
517 EIGEN_DEVICE_FUNC constexpr DenseStorage(const DenseStorage&) = default;
518 EIGEN_DEVICE_FUNC constexpr DenseStorage(Index size, Index rows, Index cols) : Base(size, rows, cols) {}
519 EIGEN_DEVICE_FUNC constexpr DenseStorage& operator=(const DenseStorage&) = default;
520 // if DenseStorage meets the requirements of use_default_move, then use the move construction and move assignment
521 // operation defined in DenseStorage_impl, or the compiler-generated version if none is defined
522 EIGEN_DEVICE_FUNC constexpr DenseStorage(DenseStorage&&) = default;
523 EIGEN_DEVICE_FUNC constexpr DenseStorage& operator=(DenseStorage&&) = default;
524};
525template <typename T, int Size, int Rows, int Cols, int Options>
526class DenseStorage<T, Size, Rows, Cols, Options, false>
527 : public internal::DenseStorage_impl<T, Size, Rows, Cols, Options> {
528 using Base = internal::DenseStorage_impl<T, Size, Rows, Cols, Options>;
529
530 public:
531 EIGEN_DEVICE_FUNC constexpr DenseStorage() = default;
532 EIGEN_DEVICE_FUNC constexpr DenseStorage(const DenseStorage&) = default;
533 EIGEN_DEVICE_FUNC constexpr DenseStorage(Index size, Index rows, Index cols) : Base(size, rows, cols) {}
534 EIGEN_DEVICE_FUNC constexpr DenseStorage& operator=(const DenseStorage&) = default;
535 // if DenseStorage does not meet the requirements of use_default_move, then defer to the copy construction and copy
536 // assignment behavior
537 EIGEN_DEVICE_FUNC constexpr DenseStorage(DenseStorage&& other)
538 : DenseStorage(static_cast<const DenseStorage&>(other)) {}
539 EIGEN_DEVICE_FUNC constexpr DenseStorage& operator=(DenseStorage&& other) {
540 *this = other;
541 return *this;
542 }
543};
544
545} // end namespace Eigen
546
547#endif // EIGEN_MATRIXSTORAGE_H
@ DontAlign
Definition Constants.h:325