Eigen  5.0.1-dev+master
 
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 if (this == &other) return *this;
139 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
140 m_rows = other.m_rows;
141 return *this;
142 }
143 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void swap(DenseStorage_impl& other) {
144 swap_plain_array(m_data, other.m_data, size(), other.size());
145 numext::swap(m_rows, other.m_rows);
146 }
147 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index rows, Index /*cols*/) { m_rows = rows; }
148 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index rows, Index /*cols*/) { m_rows = rows; }
149 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
150 EIGEN_DEVICE_FUNC constexpr Index cols() const { return Cols; }
151 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * Cols; }
152 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data.array; }
153 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data.array; }
154};
155template <typename T, int Size, int Rows, int Options>
156class DenseStorage_impl<T, Size, Rows, Dynamic, Options> {
157 plain_array<T, Size, Options> m_data;
158 Index m_cols = 0;
159
160 public:
161 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
162 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
163 : m_cols(other.m_cols) {
164 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
165 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
166 }
167 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index /*rows*/, Index cols)
168 : m_cols(cols) {
169 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
170 EIGEN_UNUSED_VARIABLE(size);
171 }
172 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
173 if (this == &other) return *this;
174 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
175 m_cols = other.m_cols;
176 return *this;
177 }
178 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void swap(DenseStorage_impl& other) {
179 swap_plain_array(m_data, other.m_data, size(), other.size());
180 numext::swap(m_cols, other.m_cols);
181 }
182 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index /*rows*/, Index cols) { m_cols = cols; }
183 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index /*rows*/, Index cols) { m_cols = cols; }
184 EIGEN_DEVICE_FUNC constexpr Index rows() const { return Rows; }
185 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
186 EIGEN_DEVICE_FUNC constexpr Index size() const { return Rows * m_cols; }
187 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data.array; }
188 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data.array; }
189};
190template <typename T, int Size, int Options>
191class DenseStorage_impl<T, Size, Dynamic, Dynamic, Options> {
192 plain_array<T, Size, Options> m_data;
193 Index m_rows = 0;
194 Index m_cols = 0;
195
196 public:
197 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
198 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
199 : m_rows(other.m_rows), m_cols(other.m_cols) {
200 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
201 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
202 }
203 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index rows, Index cols)
204 : m_rows(rows), m_cols(cols) {
205 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
206 EIGEN_UNUSED_VARIABLE(size);
207 }
208 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
209 if (this == &other) return *this;
210 smart_copy(other.m_data.array, other.m_data.array + other.size(), m_data.array);
211 m_rows = other.m_rows;
212 m_cols = other.m_cols;
213 return *this;
214 }
215 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void swap(DenseStorage_impl& other) {
216 swap_plain_array(m_data, other.m_data, size(), other.size());
217 numext::swap(m_rows, other.m_rows);
218 numext::swap(m_cols, other.m_cols);
219 }
220 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index rows, Index cols) {
221 m_rows = rows;
222 m_cols = cols;
223 }
224 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index rows, Index cols) {
225 m_rows = rows;
226 m_cols = cols;
227 }
228 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
229 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
230 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * m_cols; }
231 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data.array; }
232 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data.array; }
233};
234// null matrix variants
235template <typename T, int Rows, int Cols, int Options>
236class DenseStorage_impl<T, 0, Rows, Cols, Options> {
237 public:
238 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
239 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(const DenseStorage_impl&) = default;
240 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(Index /*size*/, Index /*rows*/, Index /*cols*/) {}
241 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(const DenseStorage_impl&) = default;
242 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl&) {}
243 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index /*rows*/, Index /*cols*/) {}
244 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index /*rows*/, Index /*cols*/) {}
245 EIGEN_DEVICE_FUNC constexpr Index rows() const { return Rows; }
246 EIGEN_DEVICE_FUNC constexpr Index cols() const { return Cols; }
247 EIGEN_DEVICE_FUNC constexpr Index size() const { return Rows * Cols; }
248 EIGEN_DEVICE_FUNC constexpr T* data() { return nullptr; }
249 EIGEN_DEVICE_FUNC constexpr const T* data() const { return nullptr; }
250};
251template <typename T, int Cols, int Options>
252class DenseStorage_impl<T, 0, Dynamic, Cols, Options> {
253 Index m_rows = 0;
254
255 public:
256 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
257 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(const DenseStorage_impl&) = default;
258 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(Index /*size*/, Index rows, Index /*cols*/) : m_rows(rows) {}
259 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(const DenseStorage_impl&) = default;
260 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept { numext::swap(m_rows, other.m_rows); }
261 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index rows, Index /*cols*/) { m_rows = rows; }
262 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index rows, Index /*cols*/) { m_rows = rows; }
263 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
264 EIGEN_DEVICE_FUNC constexpr Index cols() const { return Cols; }
265 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * Cols; }
266 EIGEN_DEVICE_FUNC constexpr T* data() { return nullptr; }
267 EIGEN_DEVICE_FUNC constexpr const T* data() const { return nullptr; }
268};
269template <typename T, int Rows, int Options>
270class DenseStorage_impl<T, 0, Rows, Dynamic, Options> {
271 Index m_cols = 0;
272
273 public:
274 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
275 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(const DenseStorage_impl&) = default;
276 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(Index /*size*/, Index /*rows*/, Index cols) : m_cols(cols) {}
277 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(const DenseStorage_impl&) = default;
278 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept { numext::swap(m_cols, other.m_cols); }
279 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index /*rows*/, Index cols) { m_cols = cols; }
280 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index /*rows*/, Index cols) { m_cols = cols; }
281 EIGEN_DEVICE_FUNC constexpr Index rows() const { return Rows; }
282 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
283 EIGEN_DEVICE_FUNC constexpr Index size() const { return Rows * m_cols; }
284 EIGEN_DEVICE_FUNC constexpr T* data() { return nullptr; }
285 EIGEN_DEVICE_FUNC constexpr const T* data() const { return nullptr; }
286};
287template <typename T, int Options>
288class DenseStorage_impl<T, 0, Dynamic, Dynamic, Options> {
289 Index m_rows = 0;
290 Index m_cols = 0;
291
292 public:
293 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
294 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(const DenseStorage_impl&) = default;
295 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(Index /*size*/, Index rows, Index cols) : m_rows(rows), m_cols(cols) {}
296 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(const DenseStorage_impl&) = default;
297 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept {
298 numext::swap(m_rows, other.m_rows);
299 numext::swap(m_cols, other.m_cols);
300 }
301 EIGEN_DEVICE_FUNC constexpr void conservativeResize(Index /*size*/, Index rows, Index cols) {
302 m_rows = rows;
303 m_cols = cols;
304 }
305 EIGEN_DEVICE_FUNC constexpr void resize(Index /*size*/, Index rows, Index cols) {
306 m_rows = rows;
307 m_cols = cols;
308 }
309 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
310 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
311 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * m_cols; }
312 EIGEN_DEVICE_FUNC constexpr T* data() { return nullptr; }
313 EIGEN_DEVICE_FUNC constexpr const T* data() const { return nullptr; }
314};
315// fixed-size matrix with dynamic memory allocation not currently supported
316template <typename T, int Rows, int Cols, int Options>
317class DenseStorage_impl<T, Dynamic, Rows, Cols, Options> {};
318// dynamic-sized variants
319template <typename T, int Cols, int Options>
320class DenseStorage_impl<T, Dynamic, Dynamic, Cols, Options> {
321 static constexpr bool Align = (Options & DontAlign) == 0;
322 T* m_data = nullptr;
323 Index m_rows = 0;
324
325 public:
326 static constexpr int Size = Dynamic;
327 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
328 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
329 : m_data(conditional_aligned_new_auto<T, Align>(other.size())), m_rows(other.m_rows) {
330 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
331 smart_copy(other.m_data, other.m_data + other.size(), m_data);
332 }
333 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index rows, Index /*cols*/)
334 : m_data(conditional_aligned_new_auto<T, Align>(size)), m_rows(rows) {
335 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
336 }
337 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(DenseStorage_impl&& other) noexcept
338 : m_data(other.m_data), m_rows(other.m_rows) {
339 other.m_data = nullptr;
340 other.m_rows = 0;
341 }
342 EIGEN_DEVICE_FUNC ~DenseStorage_impl() { conditional_aligned_delete_auto<T, Align>(m_data, size()); }
343 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
344 if (this == &other) return *this;
345 resize(other.size(), other.rows(), other.cols());
346 smart_copy(other.m_data, other.m_data + other.size(), m_data);
347 return *this;
348 }
349 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(DenseStorage_impl&& other) noexcept {
350 this->swap(other);
351 return *this;
352 }
353 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept {
354 numext::swap(m_data, other.m_data);
355 numext::swap(m_rows, other.m_rows);
356 }
357 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void conservativeResize(Index size, Index rows, Index /*cols*/) {
358 m_data = conditional_aligned_realloc_new_auto<T, Align>(m_data, size, this->size());
359 m_rows = rows;
360 }
361 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void resize(Index size, Index rows, Index /*cols*/) {
362 Index oldSize = this->size();
363 if (oldSize != size) {
364 conditional_aligned_delete_auto<T, Align>(m_data, oldSize);
365 // Leave a valid empty object if the allocation below throws.
366 m_data = nullptr;
367 m_rows = 0;
368 m_data = conditional_aligned_new_auto<T, Align>(size);
369 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
370 }
371 m_rows = rows;
372 }
373 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
374 EIGEN_DEVICE_FUNC constexpr Index cols() const { return Cols; }
375 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * Cols; }
376 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data; }
377 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data; }
378};
379template <typename T, int Rows, int Options>
380class DenseStorage_impl<T, Dynamic, Rows, Dynamic, Options> {
381 static constexpr bool Align = (Options & DontAlign) == 0;
382 T* m_data = nullptr;
383 Index m_cols = 0;
384
385 public:
386 static constexpr int Size = Dynamic;
387 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
388 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
389 : m_data(conditional_aligned_new_auto<T, Align>(other.size())), m_cols(other.m_cols) {
390 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
391 smart_copy(other.m_data, other.m_data + other.size(), m_data);
392 }
393 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index /*rows*/, Index cols)
394 : m_data(conditional_aligned_new_auto<T, Align>(size)), m_cols(cols) {
395 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
396 }
397 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(DenseStorage_impl&& other) noexcept
398 : m_data(other.m_data), m_cols(other.m_cols) {
399 other.m_data = nullptr;
400 other.m_cols = 0;
401 }
402 EIGEN_DEVICE_FUNC ~DenseStorage_impl() { conditional_aligned_delete_auto<T, Align>(m_data, size()); }
403 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
404 if (this == &other) return *this;
405 resize(other.size(), other.rows(), other.cols());
406 smart_copy(other.m_data, other.m_data + other.size(), m_data);
407 return *this;
408 }
409 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(DenseStorage_impl&& other) noexcept {
410 this->swap(other);
411 return *this;
412 }
413 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept {
414 numext::swap(m_data, other.m_data);
415 numext::swap(m_cols, other.m_cols);
416 }
417 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void conservativeResize(Index size, Index /*rows*/, Index cols) {
418 m_data = conditional_aligned_realloc_new_auto<T, Align>(m_data, size, this->size());
419 m_cols = cols;
420 }
421 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void resize(Index size, Index /*rows*/, Index cols) {
422 Index oldSize = this->size();
423 if (oldSize != size) {
424 conditional_aligned_delete_auto<T, Align>(m_data, oldSize);
425 // Leave a valid empty object if the allocation below throws.
426 m_data = nullptr;
427 m_cols = 0;
428 m_data = conditional_aligned_new_auto<T, Align>(size);
429 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
430 }
431 m_cols = cols;
432 }
433 EIGEN_DEVICE_FUNC constexpr Index rows() const { return Rows; }
434 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
435 EIGEN_DEVICE_FUNC constexpr Index size() const { return Rows * m_cols; }
436 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data; }
437 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data; }
438};
439template <typename T, int Options>
440class DenseStorage_impl<T, Dynamic, Dynamic, Dynamic, Options> {
441 static constexpr bool Align = (Options & DontAlign) == 0;
442 T* m_data = nullptr;
443 Index m_rows = 0;
444 Index m_cols = 0;
445
446 public:
447 static constexpr int Size = Dynamic;
448 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl() = default;
449 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(const DenseStorage_impl& other)
450 : m_data(conditional_aligned_new_auto<T, Align>(other.size())), m_rows(other.m_rows), m_cols(other.m_cols) {
451 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN(Index size = other.size())
452 smart_copy(other.m_data, other.m_data + other.size(), m_data);
453 }
454 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl(Index size, Index rows, Index cols)
455 : m_data(conditional_aligned_new_auto<T, Align>(size)), m_rows(rows), m_cols(cols) {
456 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
457 }
458 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl(DenseStorage_impl&& other) noexcept
459 : m_data(other.m_data), m_rows(other.m_rows), m_cols(other.m_cols) {
460 other.m_data = nullptr;
461 other.m_rows = 0;
462 other.m_cols = 0;
463 }
464 EIGEN_DEVICE_FUNC ~DenseStorage_impl() { conditional_aligned_delete_auto<T, Align>(m_data, size()); }
465 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr DenseStorage_impl& operator=(const DenseStorage_impl& other) {
466 if (this == &other) return *this;
467 resize(other.size(), other.rows(), other.cols());
468 smart_copy(other.m_data, other.m_data + other.size(), m_data);
469 return *this;
470 }
471 EIGEN_DEVICE_FUNC constexpr DenseStorage_impl& operator=(DenseStorage_impl&& other) noexcept {
472 this->swap(other);
473 return *this;
474 }
475 EIGEN_DEVICE_FUNC constexpr void swap(DenseStorage_impl& other) noexcept {
476 numext::swap(m_data, other.m_data);
477 numext::swap(m_rows, other.m_rows);
478 numext::swap(m_cols, other.m_cols);
479 }
480 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void conservativeResize(Index size, Index rows, Index cols) {
481 m_data = conditional_aligned_realloc_new_auto<T, Align>(m_data, size, this->size());
482 m_rows = rows;
483 m_cols = cols;
484 }
485 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE constexpr void resize(Index size, Index rows, Index cols) {
486 Index oldSize = this->size();
487 if (oldSize != size) {
488 conditional_aligned_delete_auto<T, Align>(m_data, oldSize);
489 // Leave a valid empty object if the allocation below throws.
490 m_data = nullptr;
491 m_rows = 0;
492 m_cols = 0;
493 m_data = conditional_aligned_new_auto<T, Align>(size);
494 EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
495 }
496 m_rows = rows;
497 m_cols = cols;
498 }
499 EIGEN_DEVICE_FUNC constexpr Index rows() const { return m_rows; }
500 EIGEN_DEVICE_FUNC constexpr Index cols() const { return m_cols; }
501 EIGEN_DEVICE_FUNC constexpr Index size() const { return m_rows * m_cols; }
502 EIGEN_DEVICE_FUNC constexpr T* data() { return m_data; }
503 EIGEN_DEVICE_FUNC constexpr const T* data() const { return m_data; }
504};
505template <typename T, int Size, int Rows, int Cols>
506struct use_default_move {
507 static constexpr bool DynamicObject = Size == Dynamic;
508 static constexpr bool TrivialObject =
509 (!NumTraits<T>::RequireInitialization) && (Rows >= 0) && (Cols >= 0) && (Size == Rows * Cols);
510 static constexpr bool value = DynamicObject || TrivialObject;
511};
512} // end namespace internal
513
526template <typename T, int Size, int Rows, int Cols, int Options,
527 bool Trivial = internal::use_default_move<T, Size, Rows, Cols>::value>
528class DenseStorage : public internal::DenseStorage_impl<T, Size, Rows, Cols, Options> {
529 using Base = internal::DenseStorage_impl<T, Size, Rows, Cols, Options>;
530
531 public:
532 EIGEN_DEVICE_FUNC constexpr DenseStorage() = default;
533 EIGEN_DEVICE_FUNC constexpr DenseStorage(const DenseStorage&) = default;
534 EIGEN_DEVICE_FUNC constexpr DenseStorage(Index size, Index rows, Index cols) : Base(size, rows, cols) {}
535 EIGEN_DEVICE_FUNC constexpr DenseStorage& operator=(const DenseStorage&) = default;
536 // if DenseStorage meets the requirements of use_default_move, then use the move construction and move assignment
537 // operation defined in DenseStorage_impl, or the compiler-generated version if none is defined
538 EIGEN_DEVICE_FUNC constexpr DenseStorage(DenseStorage&&) = default;
539 EIGEN_DEVICE_FUNC constexpr DenseStorage& operator=(DenseStorage&&) = default;
540};
541template <typename T, int Size, int Rows, int Cols, int Options>
542class DenseStorage<T, Size, Rows, Cols, Options, false>
543 : public internal::DenseStorage_impl<T, Size, Rows, Cols, Options> {
544 using Base = internal::DenseStorage_impl<T, Size, Rows, Cols, Options>;
545
546 public:
547 EIGEN_DEVICE_FUNC constexpr DenseStorage() = default;
548 EIGEN_DEVICE_FUNC constexpr DenseStorage(const DenseStorage&) = default;
549 EIGEN_DEVICE_FUNC constexpr DenseStorage(Index size, Index rows, Index cols) : Base(size, rows, cols) {}
550 EIGEN_DEVICE_FUNC constexpr DenseStorage& operator=(const DenseStorage&) = default;
551 // if DenseStorage does not meet the requirements of use_default_move, then defer to the copy construction and copy
552 // assignment behavior
553 EIGEN_DEVICE_FUNC constexpr DenseStorage(DenseStorage&& other)
554 : DenseStorage(static_cast<const DenseStorage&>(other)) {}
555 EIGEN_DEVICE_FUNC constexpr DenseStorage& operator=(DenseStorage&& other) {
556 *this = other;
557 return *this;
558 }
559};
560
561} // end namespace Eigen
562
563#endif // EIGEN_MATRIXSTORAGE_H
@ DontAlign
Definition Constants.h:325