DenseStorage.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2006-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
00006 // Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
00007 //
00008 // This Source Code Form is subject to the terms of the Mozilla
00009 // Public License v. 2.0. If a copy of the MPL was not distributed
00010 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00011 
00012 #ifndef EIGEN_MATRIXSTORAGE_H
00013 #define EIGEN_MATRIXSTORAGE_H
00014 
00015 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
00016   #define EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN EIGEN_DENSE_STORAGE_CTOR_PLUGIN;
00017 #else
00018   #define EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN
00019 #endif
00020 
00021 namespace Eigen {
00022 
00023 namespace internal {
00024 
00025 struct constructor_without_unaligned_array_assert {};
00026 
00031 template <typename T, int Size, int MatrixOrArrayOptions,
00032           int Alignment = (MatrixOrArrayOptions&DontAlign) ? 0
00033                         : (((Size*sizeof(T))%16)==0) ? 16
00034                         : 0 >
00035 struct plain_array
00036 {
00037   T array[Size];
00038 
00039   plain_array() 
00040   { 
00041     EIGEN_STATIC_ASSERT(Size * sizeof(T) <= 128 * 128 * 8, OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG);
00042   }
00043 
00044   plain_array(constructor_without_unaligned_array_assert) 
00045   { 
00046     EIGEN_STATIC_ASSERT(Size * sizeof(T) <= 128 * 128 * 8, OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG);
00047   }
00048 };
00049 
00050 #if defined(EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT)
00051   #define EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT(sizemask)
00052 #elif EIGEN_GNUC_AT_LEAST(4,7) 
00053   // GCC 4.7 is too aggressive in its optimizations and remove the alignement test based on the fact the array is declared to be aligned.
00054   // See this bug report: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53900
00055   // Hiding the origin of the array pointer behind a function argument seems to do the trick even if the function is inlined:
00056   template<typename PtrType>
00057   EIGEN_ALWAYS_INLINE PtrType eigen_unaligned_array_assert_workaround_gcc47(PtrType array) { return array; }
00058   #define EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT(sizemask) \
00059     eigen_assert((reinterpret_cast<size_t>(eigen_unaligned_array_assert_workaround_gcc47(array)) & sizemask) == 0 \
00060               && "this assertion is explained here: " \
00061               "http://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html" \
00062               " **** READ THIS WEB PAGE !!! ****");
00063 #else
00064   #define EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT(sizemask) \
00065     eigen_assert((reinterpret_cast<size_t>(array) & sizemask) == 0 \
00066               && "this assertion is explained here: " \
00067               "http://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html" \
00068               " **** READ THIS WEB PAGE !!! ****");
00069 #endif
00070 
00071 template <typename T, int Size, int MatrixOrArrayOptions>
00072 struct plain_array<T, Size, MatrixOrArrayOptions, 16>
00073 {
00074   EIGEN_USER_ALIGN16 T array[Size];
00075 
00076   plain_array() 
00077   { 
00078     EIGEN_MAKE_UNALIGNED_ARRAY_ASSERT(0xf);
00079     EIGEN_STATIC_ASSERT(Size * sizeof(T) <= 128 * 128 * 8, OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG);
00080   }
00081 
00082   plain_array(constructor_without_unaligned_array_assert) 
00083   { 
00084     EIGEN_STATIC_ASSERT(Size * sizeof(T) <= 128 * 128 * 8, OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG);
00085   }
00086 };
00087 
00088 template <typename T, int MatrixOrArrayOptions, int Alignment>
00089 struct plain_array<T, 0, MatrixOrArrayOptions, Alignment>
00090 {
00091   EIGEN_USER_ALIGN16 T array[1];
00092   plain_array() {}
00093   plain_array(constructor_without_unaligned_array_assert) {}
00094 };
00095 
00096 } // end namespace internal
00097 
00110 template<typename T, int Size, int _Rows, int _Cols, int _Options> class DenseStorage;
00111 
00112 // purely fixed-size matrix
00113 template<typename T, int Size, int _Rows, int _Cols, int _Options> class DenseStorage
00114 {
00115     internal::plain_array<T,Size,_Options> m_data;
00116   public:
00117     inline DenseStorage() {}
00118     inline DenseStorage(internal::constructor_without_unaligned_array_assert)
00119       : m_data(internal::constructor_without_unaligned_array_assert()) {}
00120     inline DenseStorage(DenseIndex,DenseIndex,DenseIndex) {}
00121     inline void swap(DenseStorage& other) { std::swap(m_data,other.m_data); }
00122     static inline DenseIndex rows(void) {return _Rows;}
00123     static inline DenseIndex cols(void) {return _Cols;}
00124     inline void conservativeResize(DenseIndex,DenseIndex,DenseIndex) {}
00125     inline void resize(DenseIndex,DenseIndex,DenseIndex) {}
00126     inline const T *data() const { return m_data.array; }
00127     inline T *data() { return m_data.array; }
00128 };
00129 
00130 // null matrix
00131 template<typename T, int _Rows, int _Cols, int _Options> class DenseStorage<T, 0, _Rows, _Cols, _Options>
00132 {
00133   public:
00134     inline DenseStorage() {}
00135     inline DenseStorage(internal::constructor_without_unaligned_array_assert) {}
00136     inline DenseStorage(DenseIndex,DenseIndex,DenseIndex) {}
00137     inline void swap(DenseStorage& ) {}
00138     static inline DenseIndex rows(void) {return _Rows;}
00139     static inline DenseIndex cols(void) {return _Cols;}
00140     inline void conservativeResize(DenseIndex,DenseIndex,DenseIndex) {}
00141     inline void resize(DenseIndex,DenseIndex,DenseIndex) {}
00142     inline const T *data() const { return 0; }
00143     inline T *data() { return 0; }
00144 };
00145 
00146 // more specializations for null matrices; these are necessary to resolve ambiguities
00147 template<typename T, int _Options> class DenseStorage<T, 0, Dynamic, Dynamic, _Options>
00148 : public DenseStorage<T, 0, 0, 0, _Options> { };
00149 
00150 template<typename T, int _Rows, int _Options> class DenseStorage<T, 0, _Rows, Dynamic, _Options>
00151 : public DenseStorage<T, 0, 0, 0, _Options> { };
00152 
00153 template<typename T, int _Cols, int _Options> class DenseStorage<T, 0, Dynamic, _Cols, _Options>
00154 : public DenseStorage<T, 0, 0, 0, _Options> { };
00155 
00156 // dynamic-size matrix with fixed-size storage
00157 template<typename T, int Size, int _Options> class DenseStorage<T, Size, Dynamic, Dynamic, _Options>
00158 {
00159     internal::plain_array<T,Size,_Options> m_data;
00160     DenseIndex m_rows;
00161     DenseIndex m_cols;
00162   public:
00163     inline DenseStorage() : m_rows(0), m_cols(0) {}
00164     inline DenseStorage(internal::constructor_without_unaligned_array_assert)
00165       : m_data(internal::constructor_without_unaligned_array_assert()), m_rows(0), m_cols(0) {}
00166     inline DenseStorage(DenseIndex, DenseIndex nbRows, DenseIndex nbCols) : m_rows(nbRows), m_cols(nbCols) {}
00167     inline void swap(DenseStorage& other)
00168     { std::swap(m_data,other.m_data); std::swap(m_rows,other.m_rows); std::swap(m_cols,other.m_cols); }
00169     inline DenseIndex rows() const {return m_rows;}
00170     inline DenseIndex cols() const {return m_cols;}
00171     inline void conservativeResize(DenseIndex, DenseIndex nbRows, DenseIndex nbCols) { m_rows = nbRows; m_cols = nbCols; }
00172     inline void resize(DenseIndex, DenseIndex nbRows, DenseIndex nbCols) { m_rows = nbRows; m_cols = nbCols; }
00173     inline const T *data() const { return m_data.array; }
00174     inline T *data() { return m_data.array; }
00175 };
00176 
00177 // dynamic-size matrix with fixed-size storage and fixed width
00178 template<typename T, int Size, int _Cols, int _Options> class DenseStorage<T, Size, Dynamic, _Cols, _Options>
00179 {
00180     internal::plain_array<T,Size,_Options> m_data;
00181     DenseIndex m_rows;
00182   public:
00183     inline DenseStorage() : m_rows(0) {}
00184     inline DenseStorage(internal::constructor_without_unaligned_array_assert)
00185       : m_data(internal::constructor_without_unaligned_array_assert()), m_rows(0) {}
00186     inline DenseStorage(DenseIndex, DenseIndex nbRows, DenseIndex) : m_rows(nbRows) {}
00187     inline void swap(DenseStorage& other) { std::swap(m_data,other.m_data); std::swap(m_rows,other.m_rows); }
00188     inline DenseIndex rows(void) const {return m_rows;}
00189     inline DenseIndex cols(void) const {return _Cols;}
00190     inline void conservativeResize(DenseIndex, DenseIndex nbRows, DenseIndex) { m_rows = nbRows; }
00191     inline void resize(DenseIndex, DenseIndex nbRows, DenseIndex) { m_rows = nbRows; }
00192     inline const T *data() const { return m_data.array; }
00193     inline T *data() { return m_data.array; }
00194 };
00195 
00196 // dynamic-size matrix with fixed-size storage and fixed height
00197 template<typename T, int Size, int _Rows, int _Options> class DenseStorage<T, Size, _Rows, Dynamic, _Options>
00198 {
00199     internal::plain_array<T,Size,_Options> m_data;
00200     DenseIndex m_cols;
00201   public:
00202     inline DenseStorage() : m_cols(0) {}
00203     inline DenseStorage(internal::constructor_without_unaligned_array_assert)
00204       : m_data(internal::constructor_without_unaligned_array_assert()), m_cols(0) {}
00205     inline DenseStorage(DenseIndex, DenseIndex, DenseIndex nbCols) : m_cols(nbCols) {}
00206     inline void swap(DenseStorage& other) { std::swap(m_data,other.m_data); std::swap(m_cols,other.m_cols); }
00207     inline DenseIndex rows(void) const {return _Rows;}
00208     inline DenseIndex cols(void) const {return m_cols;}
00209     inline void conservativeResize(DenseIndex, DenseIndex, DenseIndex nbCols) { m_cols = nbCols; }
00210     inline void resize(DenseIndex, DenseIndex, DenseIndex nbCols) { m_cols = nbCols; }
00211     inline const T *data() const { return m_data.array; }
00212     inline T *data() { return m_data.array; }
00213 };
00214 
00215 // purely dynamic matrix.
00216 template<typename T, int _Options> class DenseStorage<T, Dynamic, Dynamic, Dynamic, _Options>
00217 {
00218     T *m_data;
00219     DenseIndex m_rows;
00220     DenseIndex m_cols;
00221   public:
00222     inline DenseStorage() : m_data(0), m_rows(0), m_cols(0) {}
00223     inline DenseStorage(internal::constructor_without_unaligned_array_assert)
00224        : m_data(0), m_rows(0), m_cols(0) {}
00225     inline DenseStorage(DenseIndex size, DenseIndex nbRows, DenseIndex nbCols)
00226       : m_data(internal::conditional_aligned_new_auto<T,(_Options&DontAlign)==0>(size)), m_rows(nbRows), m_cols(nbCols)
00227     { EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN }
00228     inline ~DenseStorage() { internal::conditional_aligned_delete_auto<T,(_Options&DontAlign)==0>(m_data, m_rows*m_cols); }
00229     inline void swap(DenseStorage& other)
00230     { std::swap(m_data,other.m_data); std::swap(m_rows,other.m_rows); std::swap(m_cols,other.m_cols); }
00231     inline DenseIndex rows(void) const {return m_rows;}
00232     inline DenseIndex cols(void) const {return m_cols;}
00233     inline void conservativeResize(DenseIndex size, DenseIndex nbRows, DenseIndex nbCols)
00234     {
00235       m_data = internal::conditional_aligned_realloc_new_auto<T,(_Options&DontAlign)==0>(m_data, size, m_rows*m_cols);
00236       m_rows = nbRows;
00237       m_cols = nbCols;
00238     }
00239     void resize(DenseIndex size, DenseIndex nbRows, DenseIndex nbCols)
00240     {
00241       if(size != m_rows*m_cols)
00242       {
00243         internal::conditional_aligned_delete_auto<T,(_Options&DontAlign)==0>(m_data, m_rows*m_cols);
00244         if (size)
00245           m_data = internal::conditional_aligned_new_auto<T,(_Options&DontAlign)==0>(size);
00246         else
00247           m_data = 0;
00248         EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN
00249       }
00250       m_rows = nbRows;
00251       m_cols = nbCols;
00252     }
00253     inline const T *data() const { return m_data; }
00254     inline T *data() { return m_data; }
00255 };
00256 
00257 // matrix with dynamic width and fixed height (so that matrix has dynamic size).
00258 template<typename T, int _Rows, int _Options> class DenseStorage<T, Dynamic, _Rows, Dynamic, _Options>
00259 {
00260     T *m_data;
00261     DenseIndex m_cols;
00262   public:
00263     inline DenseStorage() : m_data(0), m_cols(0) {}
00264     inline DenseStorage(internal::constructor_without_unaligned_array_assert) : m_data(0), m_cols(0) {}
00265     inline DenseStorage(DenseIndex size, DenseIndex, DenseIndex nbCols) : m_data(internal::conditional_aligned_new_auto<T,(_Options&DontAlign)==0>(size)), m_cols(nbCols)
00266     { EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN }
00267     inline ~DenseStorage() { internal::conditional_aligned_delete_auto<T,(_Options&DontAlign)==0>(m_data, _Rows*m_cols); }
00268     inline void swap(DenseStorage& other) { std::swap(m_data,other.m_data); std::swap(m_cols,other.m_cols); }
00269     static inline DenseIndex rows(void) {return _Rows;}
00270     inline DenseIndex cols(void) const {return m_cols;}
00271     inline void conservativeResize(DenseIndex size, DenseIndex, DenseIndex nbCols)
00272     {
00273       m_data = internal::conditional_aligned_realloc_new_auto<T,(_Options&DontAlign)==0>(m_data, size, _Rows*m_cols);
00274       m_cols = nbCols;
00275     }
00276     EIGEN_STRONG_INLINE void resize(DenseIndex size, DenseIndex, DenseIndex nbCols)
00277     {
00278       if(size != _Rows*m_cols)
00279       {
00280         internal::conditional_aligned_delete_auto<T,(_Options&DontAlign)==0>(m_data, _Rows*m_cols);
00281         if (size)
00282           m_data = internal::conditional_aligned_new_auto<T,(_Options&DontAlign)==0>(size);
00283         else
00284           m_data = 0;
00285         EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN
00286       }
00287       m_cols = nbCols;
00288     }
00289     inline const T *data() const { return m_data; }
00290     inline T *data() { return m_data; }
00291 };
00292 
00293 // matrix with dynamic height and fixed width (so that matrix has dynamic size).
00294 template<typename T, int _Cols, int _Options> class DenseStorage<T, Dynamic, Dynamic, _Cols, _Options>
00295 {
00296     T *m_data;
00297     DenseIndex m_rows;
00298   public:
00299     inline DenseStorage() : m_data(0), m_rows(0) {}
00300     inline DenseStorage(internal::constructor_without_unaligned_array_assert) : m_data(0), m_rows(0) {}
00301     inline DenseStorage(DenseIndex size, DenseIndex nbRows, DenseIndex) : m_data(internal::conditional_aligned_new_auto<T,(_Options&DontAlign)==0>(size)), m_rows(nbRows)
00302     { EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN }
00303     inline ~DenseStorage() { internal::conditional_aligned_delete_auto<T,(_Options&DontAlign)==0>(m_data, _Cols*m_rows); }
00304     inline void swap(DenseStorage& other) { std::swap(m_data,other.m_data); std::swap(m_rows,other.m_rows); }
00305     inline DenseIndex rows(void) const {return m_rows;}
00306     static inline DenseIndex cols(void) {return _Cols;}
00307     inline void conservativeResize(DenseIndex size, DenseIndex nbRows, DenseIndex)
00308     {
00309       m_data = internal::conditional_aligned_realloc_new_auto<T,(_Options&DontAlign)==0>(m_data, size, m_rows*_Cols);
00310       m_rows = nbRows;
00311     }
00312     EIGEN_STRONG_INLINE void resize(DenseIndex size, DenseIndex nbRows, DenseIndex)
00313     {
00314       if(size != m_rows*_Cols)
00315       {
00316         internal::conditional_aligned_delete_auto<T,(_Options&DontAlign)==0>(m_data, _Cols*m_rows);
00317         if (size)
00318           m_data = internal::conditional_aligned_new_auto<T,(_Options&DontAlign)==0>(size);
00319         else
00320           m_data = 0;
00321         EIGEN_INTERNAL_DENSE_STORAGE_CTOR_PLUGIN
00322       }
00323       m_rows = nbRows;
00324     }
00325     inline const T *data() const { return m_data; }
00326     inline T *data() { return m_data; }
00327 };
00328 
00329 } // end namespace Eigen
00330 
00331 #endif // EIGEN_MATRIX_H


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Sat Jun 8 2019 19:36:57