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 namespace Eigen {
21 
34 template<typename T, typename Dimensions, int Options_> class TensorStorage;
35 
36 
37 // Pure fixed-size storage
38 template<typename T, int Options_, typename FixedDimensions>
39 class TensorStorage<T, FixedDimensions, Options_>
40 {
41  private:
42  static const std::size_t Size = FixedDimensions::total_size;
43 
44  EIGEN_ALIGN_MAX T m_data[Size];
45  FixedDimensions m_dimensions;
46 
47  public:
48  EIGEN_DEVICE_FUNC
49  EIGEN_STRONG_INLINE TensorStorage() {
50  }
51 
52  EIGEN_DEVICE_FUNC
53  EIGEN_STRONG_INLINE T *data() { return m_data; }
54  EIGEN_DEVICE_FUNC
55  EIGEN_STRONG_INLINE const T *data() const { return m_data; }
56 
57  EIGEN_DEVICE_FUNC
58  EIGEN_STRONG_INLINE const FixedDimensions& dimensions() const { return m_dimensions; }
59 
60  EIGEN_DEVICE_FUNC
61  EIGEN_STRONG_INLINE DenseIndex size() const { return m_dimensions.TotalSize(); }
62 };
63 
64 
65 // pure dynamic
66 template<typename T, int Options_, typename IndexType, int NumIndices_>
67 class TensorStorage<T, DSizes<IndexType, NumIndices_>, Options_>
68 {
69  public:
70  typedef IndexType Index;
71  typedef DSizes<IndexType, NumIndices_> Dimensions;
72  typedef TensorStorage<T, DSizes<IndexType, NumIndices_>, Options_> Self;
73 
74  EIGEN_DEVICE_FUNC TensorStorage() : m_data(0), m_dimensions() {
75  if (NumIndices_ == 0) {
76  m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(1);
77  }
78  }
79  EIGEN_DEVICE_FUNC TensorStorage(internal::constructor_without_unaligned_array_assert)
80  : m_data(0), m_dimensions(internal::template repeat<NumIndices_, Index>(0)) {}
81  EIGEN_DEVICE_FUNC TensorStorage(Index size, const array<Index, NumIndices_>& dimensions)
82  : m_data(internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(size)), m_dimensions(dimensions)
83  { EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN }
84 
85  EIGEN_DEVICE_FUNC TensorStorage(const Self& other)
86  : m_data(internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(internal::array_prod(other.m_dimensions)))
87  , m_dimensions(other.m_dimensions)
88  {
89  internal::smart_copy(other.m_data, other.m_data+internal::array_prod(other.m_dimensions), m_data);
90  }
91  EIGEN_DEVICE_FUNC Self& operator=(const Self& other)
92  {
93  if (this != &other) {
94  Self tmp(other);
95  this->swap(tmp);
96  }
97  return *this;
98  }
99 
100  EIGEN_DEVICE_FUNC ~TensorStorage() { internal::conditional_aligned_delete_auto<T,(Options_&DontAlign)==0>(m_data, internal::array_prod(m_dimensions)); }
101  EIGEN_DEVICE_FUNC void swap(Self& other)
102  { numext::swap(m_data,other.m_data); numext::swap(m_dimensions,other.m_dimensions); }
103 
104  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const {return m_dimensions;}
105 
106  EIGEN_DEVICE_FUNC void resize(Index size, const array<Index, NumIndices_>& nbDimensions)
107  {
108  eigen_assert(size >= 1);
109  const Index currentSz = internal::array_prod(m_dimensions);
110  if(size != currentSz)
111  {
112  internal::conditional_aligned_delete_auto<T,(Options_&DontAlign)==0>(m_data, currentSz);
113  if (size)
114  m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(size);
115  else if (NumIndices_ == 0) {
116  m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(1);
117  }
118  else
119  m_data = 0;
120  EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN
121  }
122  m_dimensions = nbDimensions;
123  }
124 
125  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T *data() { return m_data; }
126  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T *data() const { return m_data; }
127 
128  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
129 
130  private:
131  T *m_data;
132  Dimensions m_dimensions;
133 };
134 
135 } // end namespace Eigen
136 
137 #endif // EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
Namespace containing all symbols from the Eigen library.
Definition: CXX11Meta.h:13