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