MOAB  4.9.3pre
MapBase.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) 2007-2010 Benoit Jacob <[email protected]>
00005 // Copyright (C) 2008 Gael Guennebaud <[email protected]>
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_MAPBASE_H
00012 #define EIGEN_MAPBASE_H
00013 
00014 #define EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived) \
00015       EIGEN_STATIC_ASSERT((int(internal::evaluator<Derived>::Flags) & LinearAccessBit) || Derived::IsVectorAtCompileTime, \
00016                           YOU_ARE_TRYING_TO_USE_AN_INDEX_BASED_ACCESSOR_ON_AN_EXPRESSION_THAT_DOES_NOT_SUPPORT_THAT)
00017 
00018 namespace Eigen { 
00019 
00027 template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
00028   : public internal::dense_xpr_base<Derived>::type
00029 {
00030   public:
00031 
00032     typedef typename internal::dense_xpr_base<Derived>::type Base;
00033     enum {
00034       RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
00035       ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
00036       SizeAtCompileTime = Base::SizeAtCompileTime
00037     };
00038 
00039     typedef typename internal::traits<Derived>::StorageKind StorageKind;
00040     typedef typename internal::traits<Derived>::Scalar Scalar;
00041     typedef typename internal::packet_traits<Scalar>::type PacketScalar;
00042     typedef typename NumTraits<Scalar>::Real RealScalar;
00043     typedef typename internal::conditional<
00044                          bool(internal::is_lvalue<Derived>::value),
00045                          Scalar *,
00046                          const Scalar *>::type
00047                      PointerType;
00048 
00049     using Base::derived;
00050 //    using Base::RowsAtCompileTime;
00051 //    using Base::ColsAtCompileTime;
00052 //    using Base::SizeAtCompileTime;
00053     using Base::MaxRowsAtCompileTime;
00054     using Base::MaxColsAtCompileTime;
00055     using Base::MaxSizeAtCompileTime;
00056     using Base::IsVectorAtCompileTime;
00057     using Base::Flags;
00058     using Base::IsRowMajor;
00059 
00060     using Base::rows;
00061     using Base::cols;
00062     using Base::size;
00063     using Base::coeff;
00064     using Base::coeffRef;
00065     using Base::lazyAssign;
00066     using Base::eval;
00067 
00068     using Base::innerStride;
00069     using Base::outerStride;
00070     using Base::rowStride;
00071     using Base::colStride;
00072 
00073     // bug 217 - compile error on ICC 11.1
00074     using Base::operator=;
00075 
00076     typedef typename Base::CoeffReturnType CoeffReturnType;
00077 
00078     EIGEN_DEVICE_FUNC inline Index rows() const { return m_rows.value(); }
00079     EIGEN_DEVICE_FUNC inline Index cols() const { return m_cols.value(); }
00080 
00087     EIGEN_DEVICE_FUNC inline const Scalar* data() const { return m_data; }
00088 
00089     EIGEN_DEVICE_FUNC
00090     inline const Scalar& coeff(Index rowId, Index colId) const
00091     {
00092       return m_data[colId * colStride() + rowId * rowStride()];
00093     }
00094 
00095     EIGEN_DEVICE_FUNC
00096     inline const Scalar& coeff(Index index) const
00097     {
00098       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
00099       return m_data[index * innerStride()];
00100     }
00101 
00102     EIGEN_DEVICE_FUNC
00103     inline const Scalar& coeffRef(Index rowId, Index colId) const
00104     {
00105       return this->m_data[colId * colStride() + rowId * rowStride()];
00106     }
00107 
00108     EIGEN_DEVICE_FUNC
00109     inline const Scalar& coeffRef(Index index) const
00110     {
00111       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
00112       return this->m_data[index * innerStride()];
00113     }
00114 
00115     template<int LoadMode>
00116     inline PacketScalar packet(Index rowId, Index colId) const
00117     {
00118       return internal::ploadt<PacketScalar, LoadMode>
00119                (m_data + (colId * colStride() + rowId * rowStride()));
00120     }
00121 
00122     template<int LoadMode>
00123     inline PacketScalar packet(Index index) const
00124     {
00125       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
00126       return internal::ploadt<PacketScalar, LoadMode>(m_data + index * innerStride());
00127     }
00128 
00129     EIGEN_DEVICE_FUNC
00130     explicit inline MapBase(PointerType dataPtr) : m_data(dataPtr), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime)
00131     {
00132       EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00133       checkSanity<Derived>();
00134     }
00135 
00136     EIGEN_DEVICE_FUNC
00137     inline MapBase(PointerType dataPtr, Index vecSize)
00138             : m_data(dataPtr),
00139               m_rows(RowsAtCompileTime == Dynamic ? vecSize : Index(RowsAtCompileTime)),
00140               m_cols(ColsAtCompileTime == Dynamic ? vecSize : Index(ColsAtCompileTime))
00141     {
00142       EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00143       eigen_assert(vecSize >= 0);
00144       eigen_assert(dataPtr == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == vecSize);
00145       checkSanity<Derived>();
00146     }
00147 
00148     EIGEN_DEVICE_FUNC
00149     inline MapBase(PointerType dataPtr, Index rows, Index cols)
00150             : m_data(dataPtr), m_rows(rows), m_cols(cols)
00151     {
00152       eigen_assert( (dataPtr == 0)
00153               || (   rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
00154                   && cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
00155       checkSanity<Derived>();
00156     }
00157 
00158     #ifdef EIGEN_MAPBASE_PLUGIN
00159     #include EIGEN_MAPBASE_PLUGIN
00160     #endif
00161 
00162   protected:
00163 
00164     template<typename T>
00165     EIGEN_DEVICE_FUNC
00166     void checkSanity(typename internal::enable_if<(internal::traits<T>::Alignment>0),void*>::type = 0) const
00167     {
00168 #if EIGEN_MAX_ALIGN_BYTES>0
00169       eigen_assert((   ((size_t(m_data) % internal::traits<Derived>::Alignment) == 0)
00170                     || (cols() * rows() * innerStride() * sizeof(Scalar)) < internal::traits<Derived>::Alignment ) && "data is not aligned");
00171 #endif
00172     }
00173 
00174     template<typename T>
00175     EIGEN_DEVICE_FUNC
00176     void checkSanity(typename internal::enable_if<internal::traits<T>::Alignment==0,void*>::type = 0) const
00177     {}
00178 
00179     PointerType m_data;
00180     const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
00181     const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
00182 };
00183 
00184 template<typename Derived> class MapBase<Derived, WriteAccessors>
00185   : public MapBase<Derived, ReadOnlyAccessors>
00186 {
00187     typedef MapBase<Derived, ReadOnlyAccessors> ReadOnlyMapBase;
00188   public:
00189 
00190     typedef MapBase<Derived, ReadOnlyAccessors> Base;
00191 
00192     typedef typename Base::Scalar Scalar;
00193     typedef typename Base::PacketScalar PacketScalar;
00194     typedef typename Base::StorageIndex StorageIndex;
00195     typedef typename Base::PointerType PointerType;
00196 
00197     using Base::derived;
00198     using Base::rows;
00199     using Base::cols;
00200     using Base::size;
00201     using Base::coeff;
00202     using Base::coeffRef;
00203 
00204     using Base::innerStride;
00205     using Base::outerStride;
00206     using Base::rowStride;
00207     using Base::colStride;
00208 
00209     typedef typename internal::conditional<
00210                     internal::is_lvalue<Derived>::value,
00211                     Scalar,
00212                     const Scalar
00213                   >::type ScalarWithConstIfNotLvalue;
00214 
00215     EIGEN_DEVICE_FUNC
00216     inline const Scalar* data() const { return this->m_data; }
00217     EIGEN_DEVICE_FUNC
00218     inline ScalarWithConstIfNotLvalue* data() { return this->m_data; } // no const-cast here so non-const-correct code will give a compile error
00219 
00220     EIGEN_DEVICE_FUNC
00221     inline ScalarWithConstIfNotLvalue& coeffRef(Index row, Index col)
00222     {
00223       return this->m_data[col * colStride() + row * rowStride()];
00224     }
00225 
00226     EIGEN_DEVICE_FUNC
00227     inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
00228     {
00229       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
00230       return this->m_data[index * innerStride()];
00231     }
00232 
00233     template<int StoreMode>
00234     inline void writePacket(Index row, Index col, const PacketScalar& val)
00235     {
00236       internal::pstoret<Scalar, PacketScalar, StoreMode>
00237                (this->m_data + (col * colStride() + row * rowStride()), val);
00238     }
00239 
00240     template<int StoreMode>
00241     inline void writePacket(Index index, const PacketScalar& val)
00242     {
00243       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
00244       internal::pstoret<Scalar, PacketScalar, StoreMode>
00245                 (this->m_data + index * innerStride(), val);
00246     }
00247 
00248     EIGEN_DEVICE_FUNC explicit inline MapBase(PointerType dataPtr) : Base(dataPtr) {}
00249     EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index vecSize) : Base(dataPtr, vecSize) {}
00250     EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index p_rows, Index p_cols) : Base(dataPtr, p_rows, p_cols) {}
00251 
00252     EIGEN_DEVICE_FUNC
00253     Derived& operator=(const MapBase& other)
00254     {
00255       ReadOnlyMapBase::Base::operator=(other);
00256       return derived();
00257     }
00258 
00259     // In theory we could simply refer to Base:Base::operator=, but MSVC does not like Base::Base,
00260     // see bugs 821 and 920.
00261     using ReadOnlyMapBase::Base::operator=;
00262 };
00263 
00264 #undef EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS
00265 
00266 } // end namespace Eigen
00267 
00268 #endif // EIGEN_MAPBASE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines