$darkmode
Eigen-unsupported  5.0.1-dev
TensorStorage.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2013 Christian Seiler <christian@iwakd.de>
5 // Copyright (C) 2014-2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
12 #define EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
13 
14 #ifdef EIGEN_TENSOR_STORAGE_CTOR_PLUGIN
15 #define EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN EIGEN_TENSOR_STORAGE_CTOR_PLUGIN;
16 #else
17 #define EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN
18 #endif
19 
20 // IWYU pragma: private
21 #include "./InternalHeaderCheck.h"
22 
23 namespace Eigen {
24 
36 template <typename T, typename Dimensions, int Options>
37 class TensorStorage;
38 
39 // Pure fixed-size storage
40 template <typename T, typename FixedDimensions, int Options_>
41 class TensorStorage {
42  private:
43  static constexpr std::size_t Size = FixedDimensions::total_size;
44 
45  // Allocate an array of size at least one to prevent compiler warnings.
46  static constexpr std::size_t MinSize = max_n_1<Size>::size;
47  EIGEN_ALIGN_MAX T m_data[MinSize];
48 
49  public:
50  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorStorage() {}
51 
52  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T* data() { return m_data; }
53  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T* data() const { return m_data; }
54 
55  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const FixedDimensions dimensions() const { return FixedDimensions(); }
56 
57  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE DenseIndex size() const { return Size; }
58 };
59 
60 // pure dynamic
61 template <typename T, typename IndexType, int NumIndices_, int Options_>
62 class TensorStorage<T, DSizes<IndexType, NumIndices_>, Options_> {
63  public:
64  typedef IndexType Index;
65  typedef DSizes<IndexType, NumIndices_> Dimensions;
66  typedef TensorStorage<T, DSizes<IndexType, NumIndices_>, Options_> Self;
67 
68  EIGEN_DEVICE_FUNC TensorStorage() : m_data(0), m_dimensions() {
69  if (NumIndices_ == 0) {
70  m_data = internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(1);
71  }
72  }
73  EIGEN_DEVICE_FUNC TensorStorage(Index size, const array<Index, NumIndices_>& dimensions)
74  : m_data(internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(size)), m_dimensions(dimensions) {
75  EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN
76  }
77 
78  template <typename... DenseIndex>
79  EIGEN_DEVICE_FUNC TensorStorage(DenseIndex... indices) : m_dimensions(indices...) {
80  m_data = internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(internal::array_prod(m_dimensions));
81  }
82 
83  EIGEN_DEVICE_FUNC TensorStorage(const Self& other)
84  : m_data(internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(
85  internal::array_prod(other.m_dimensions))),
86  m_dimensions(other.m_dimensions) {
87  internal::smart_copy(other.m_data, other.m_data + internal::array_prod(other.m_dimensions), m_data);
88  }
89  EIGEN_DEVICE_FUNC Self& operator=(const Self& other) {
90  if (this != &other) {
91  Self tmp(other);
92  this->swap(tmp);
93  }
94  return *this;
95  }
96 
97  EIGEN_DEVICE_FUNC TensorStorage(Self&& other) : TensorStorage() { *this = std::move(other); }
98 
99  EIGEN_DEVICE_FUNC Self& operator=(Self&& other) {
100  numext::swap(m_data, other.m_data);
101  numext::swap(m_dimensions, other.m_dimensions);
102  return *this;
103  }
104 
105  EIGEN_DEVICE_FUNC ~TensorStorage() {
106  internal::conditional_aligned_delete_auto<T, (Options_ & DontAlign) == 0>(m_data,
107  internal::array_prod(m_dimensions));
108  }
109  EIGEN_DEVICE_FUNC void swap(Self& other) {
110  numext::swap(m_data, other.m_data);
111  numext::swap(m_dimensions, other.m_dimensions);
112  }
113 
114  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
115 
116  EIGEN_DEVICE_FUNC void resize(Index size, const array<Index, NumIndices_>& nbDimensions) {
117  const Index currentSz = internal::array_prod(m_dimensions);
118  if (size != currentSz) {
119  internal::conditional_aligned_delete_auto<T, (Options_ & DontAlign) == 0>(m_data, currentSz);
120  if (size)
121  m_data = internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(size);
122  else if (NumIndices_ == 0) {
123  m_data = internal::conditional_aligned_new_auto<T, (Options_ & DontAlign) == 0>(1);
124  } else
125  m_data = 0;
126  EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
127  }
128  m_dimensions = nbDimensions;
129  }
130 
131  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T* data() { return m_data; }
132  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T* data() const { return m_data; }
133 
134  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
135 
136  private:
137  T* m_data;
138  Dimensions m_dimensions;
139 };
140 
141 } // end namespace Eigen
142 
143 #endif // EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
std::enable_if_t< std::is_base_of< DenseBase< std::decay_t< DerivedA > >, std::decay_t< DerivedA > >::value &&std::is_base_of< DenseBase< std::decay_t< DerivedB > >, std::decay_t< DerivedB > >::value, void > swap(DerivedA &&a, DerivedB &&b)