TensorStorage.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2013 Christian Seiler <christian@iwakd.de>
00005 // Copyright (C) 2014-2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
00006 //
00007 // This Source Code Form is subject to the terms of the Mozilla
00008 // Public License v. 2.0. If a copy of the MPL was not distributed
00009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00010 
00011 #ifndef EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
00012 #define EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
00013 
00014 #ifdef EIGEN_TENSOR_STORAGE_CTOR_PLUGIN
00015   #define EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN EIGEN_TENSOR_STORAGE_CTOR_PLUGIN;
00016 #else
00017   #define EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN
00018 #endif
00019 
00020 namespace Eigen {
00021 
00034 template<typename T, typename Dimensions, int Options_> class TensorStorage;
00035 
00036 
00037 // Pure fixed-size storage
00038 template<typename T, int Options_, typename FixedDimensions>
00039 class TensorStorage<T, FixedDimensions, Options_>
00040 {
00041  private:
00042   static const std::size_t Size = FixedDimensions::total_size;
00043 
00044   // Allocate an array of size at least one to prevent compiler warnings.
00045   static const std::size_t MinSize = max_n_1<Size>::size;
00046   EIGEN_ALIGN_MAX T m_data[MinSize];
00047 
00048   FixedDimensions m_dimensions;
00049 
00050  public:
00051   EIGEN_DEVICE_FUNC
00052   EIGEN_STRONG_INLINE TensorStorage() {
00053   }
00054 
00055   EIGEN_DEVICE_FUNC
00056   EIGEN_STRONG_INLINE T *data() { return m_data; }
00057   EIGEN_DEVICE_FUNC
00058   EIGEN_STRONG_INLINE const T *data() const { return m_data; }
00059 
00060   EIGEN_DEVICE_FUNC
00061   EIGEN_STRONG_INLINE const FixedDimensions& dimensions() const { return m_dimensions; }
00062 
00063   EIGEN_DEVICE_FUNC
00064   EIGEN_STRONG_INLINE DenseIndex size() const { return m_dimensions.TotalSize(); }
00065 };
00066 
00067 
00068 // pure dynamic
00069 template<typename T, int Options_, typename IndexType, int NumIndices_>
00070 class TensorStorage<T, DSizes<IndexType, NumIndices_>, Options_>
00071 {
00072   public:
00073     typedef IndexType Index;
00074     typedef DSizes<IndexType, NumIndices_> Dimensions;
00075     typedef TensorStorage<T, DSizes<IndexType, NumIndices_>, Options_> Self;
00076 
00077     EIGEN_DEVICE_FUNC TensorStorage() : m_data(0), m_dimensions() {
00078       if (NumIndices_ == 0) {
00079         m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(1);
00080       }
00081     }
00082     EIGEN_DEVICE_FUNC TensorStorage(internal::constructor_without_unaligned_array_assert)
00083       : m_data(0), m_dimensions(internal::template repeat<NumIndices_, Index>(0)) {}
00084     EIGEN_DEVICE_FUNC TensorStorage(Index size, const array<Index, NumIndices_>& dimensions)
00085         : m_data(internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(size)), m_dimensions(dimensions)
00086       { EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN }
00087 
00088 #if EIGEN_HAS_VARIADIC_TEMPLATES
00089     template <typename... DenseIndex>
00090     EIGEN_DEVICE_FUNC TensorStorage(DenseIndex... indices) : m_dimensions(indices...) {
00091       m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(internal::array_prod(m_dimensions));
00092     }
00093 #endif
00094 
00095     EIGEN_DEVICE_FUNC TensorStorage(const Self& other)
00096       : m_data(internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(internal::array_prod(other.m_dimensions)))
00097       , m_dimensions(other.m_dimensions)
00098     {
00099       internal::smart_copy(other.m_data, other.m_data+internal::array_prod(other.m_dimensions), m_data);
00100     }
00101     EIGEN_DEVICE_FUNC Self& operator=(const Self& other)
00102     {
00103       if (this != &other) {
00104         Self tmp(other);
00105         this->swap(tmp);
00106       }
00107       return *this;
00108     }
00109 
00110     EIGEN_DEVICE_FUNC  ~TensorStorage() { internal::conditional_aligned_delete_auto<T,(Options_&DontAlign)==0>(m_data, internal::array_prod(m_dimensions)); }
00111     EIGEN_DEVICE_FUNC  void swap(Self& other)
00112     { numext::swap(m_data,other.m_data); numext::swap(m_dimensions,other.m_dimensions); }
00113 
00114     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const {return m_dimensions;}
00115 
00116     EIGEN_DEVICE_FUNC void resize(Index size, const array<Index, NumIndices_>& nbDimensions)
00117     {
00118       const Index currentSz = internal::array_prod(m_dimensions);
00119       if(size != currentSz)
00120       {
00121         internal::conditional_aligned_delete_auto<T,(Options_&DontAlign)==0>(m_data, currentSz);
00122         if (size)
00123           m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(size);
00124         else if (NumIndices_ == 0) {
00125           m_data = internal::conditional_aligned_new_auto<T,(Options_&DontAlign)==0>(1);
00126         }
00127         else 
00128           m_data = 0;
00129         EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN({})
00130       }
00131       m_dimensions = nbDimensions;
00132     }
00133 
00134     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T *data() { return m_data; }
00135     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const T *data() const { return m_data; }
00136 
00137     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
00138 
00139  private:
00140   T *m_data;
00141   Dimensions m_dimensions;
00142 };
00143 
00144 } // end namespace Eigen
00145 
00146 #endif // EIGEN_CXX11_TENSOR_TENSORSTORAGE_H
 All Classes Functions Variables Typedefs Enumerator