Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:06:09

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
0005 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_MAPBASE_H
0012 #define EIGEN_MAPBASE_H
0013 
0014 #define EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived) \
0015       EIGEN_STATIC_ASSERT((int(internal::evaluator<Derived>::Flags) & LinearAccessBit) || Derived::IsVectorAtCompileTime, \
0016                           YOU_ARE_TRYING_TO_USE_AN_INDEX_BASED_ACCESSOR_ON_AN_EXPRESSION_THAT_DOES_NOT_SUPPORT_THAT)
0017 
0018 namespace RivetEigen {
0019 
0020 /** \ingroup Core_Module
0021   *
0022   * \brief Base class for dense Map and Block expression with direct access
0023   *
0024   * This base class provides the const low-level accessors (e.g. coeff, coeffRef) of dense
0025   * Map and Block objects with direct access.
0026   * Typical users do not have to directly deal with this class.
0027   *
0028   * This class can be extended by through the macro plugin \c EIGEN_MAPBASE_PLUGIN.
0029   * See \link TopicCustomizing_Plugins customizing Eigen \endlink for details.
0030   *
0031   * The \c Derived class has to provide the following two methods describing the memory layout:
0032   *  \code Index innerStride() const; \endcode
0033   *  \code Index outerStride() const; \endcode
0034   *
0035   * \sa class Map, class Block
0036   */
0037 template<typename Derived> class MapBase<Derived, ReadOnlyAccessors>
0038   : public internal::dense_xpr_base<Derived>::type
0039 {
0040   public:
0041 
0042     typedef typename internal::dense_xpr_base<Derived>::type Base;
0043     enum {
0044       RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
0045       ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
0046       InnerStrideAtCompileTime = internal::traits<Derived>::InnerStrideAtCompileTime,
0047       SizeAtCompileTime = Base::SizeAtCompileTime
0048     };
0049 
0050     typedef typename internal::traits<Derived>::StorageKind StorageKind;
0051     typedef typename internal::traits<Derived>::Scalar Scalar;
0052     typedef typename internal::packet_traits<Scalar>::type PacketScalar;
0053     typedef typename NumTraits<Scalar>::Real RealScalar;
0054     typedef typename internal::conditional<
0055                          bool(internal::is_lvalue<Derived>::value),
0056                          Scalar *,
0057                          const Scalar *>::type
0058                      PointerType;
0059 
0060     using Base::derived;
0061 //    using Base::RowsAtCompileTime;
0062 //    using Base::ColsAtCompileTime;
0063 //    using Base::SizeAtCompileTime;
0064     using Base::MaxRowsAtCompileTime;
0065     using Base::MaxColsAtCompileTime;
0066     using Base::MaxSizeAtCompileTime;
0067     using Base::IsVectorAtCompileTime;
0068     using Base::Flags;
0069     using Base::IsRowMajor;
0070 
0071     using Base::rows;
0072     using Base::cols;
0073     using Base::size;
0074     using Base::coeff;
0075     using Base::coeffRef;
0076     using Base::lazyAssign;
0077     using Base::eval;
0078 
0079     using Base::innerStride;
0080     using Base::outerStride;
0081     using Base::rowStride;
0082     using Base::colStride;
0083 
0084     // bug 217 - compile error on ICC 11.1
0085     using Base::operator=;
0086 
0087     typedef typename Base::CoeffReturnType CoeffReturnType;
0088 
0089     /** \copydoc DenseBase::rows() */
0090     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0091     inline Index rows() const EIGEN_NOEXCEPT { return m_rows.value(); }
0092     /** \copydoc DenseBase::cols() */
0093     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0094     inline Index cols() const EIGEN_NOEXCEPT { return m_cols.value(); }
0095 
0096     /** Returns a pointer to the first coefficient of the matrix or vector.
0097       *
0098       * \note When addressing this data, make sure to honor the strides returned by innerStride() and outerStride().
0099       *
0100       * \sa innerStride(), outerStride()
0101       */
0102     EIGEN_DEVICE_FUNC inline const Scalar* data() const { return m_data; }
0103 
0104     /** \copydoc PlainObjectBase::coeff(Index,Index) const */
0105     EIGEN_DEVICE_FUNC
0106     inline const Scalar& coeff(Index rowId, Index colId) const
0107     {
0108       return m_data[colId * colStride() + rowId * rowStride()];
0109     }
0110 
0111     /** \copydoc PlainObjectBase::coeff(Index) const */
0112     EIGEN_DEVICE_FUNC
0113     inline const Scalar& coeff(Index index) const
0114     {
0115       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
0116       return m_data[index * innerStride()];
0117     }
0118 
0119     /** \copydoc PlainObjectBase::coeffRef(Index,Index) const */
0120     EIGEN_DEVICE_FUNC
0121     inline const Scalar& coeffRef(Index rowId, Index colId) const
0122     {
0123       return this->m_data[colId * colStride() + rowId * rowStride()];
0124     }
0125 
0126     /** \copydoc PlainObjectBase::coeffRef(Index) const */
0127     EIGEN_DEVICE_FUNC
0128     inline const Scalar& coeffRef(Index index) const
0129     {
0130       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
0131       return this->m_data[index * innerStride()];
0132     }
0133 
0134     /** \internal */
0135     template<int LoadMode>
0136     inline PacketScalar packet(Index rowId, Index colId) const
0137     {
0138       return internal::ploadt<PacketScalar, LoadMode>
0139                (m_data + (colId * colStride() + rowId * rowStride()));
0140     }
0141 
0142     /** \internal */
0143     template<int LoadMode>
0144     inline PacketScalar packet(Index index) const
0145     {
0146       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
0147       return internal::ploadt<PacketScalar, LoadMode>(m_data + index * innerStride());
0148     }
0149 
0150     /** \internal Constructor for fixed size matrices or vectors */
0151     EIGEN_DEVICE_FUNC
0152     explicit inline MapBase(PointerType dataPtr) : m_data(dataPtr), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime)
0153     {
0154       EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
0155       checkSanity<Derived>();
0156     }
0157 
0158     /** \internal Constructor for dynamically sized vectors */
0159     EIGEN_DEVICE_FUNC
0160     inline MapBase(PointerType dataPtr, Index vecSize)
0161             : m_data(dataPtr),
0162               m_rows(RowsAtCompileTime == Dynamic ? vecSize : Index(RowsAtCompileTime)),
0163               m_cols(ColsAtCompileTime == Dynamic ? vecSize : Index(ColsAtCompileTime))
0164     {
0165       EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
0166       eigen_assert(vecSize >= 0);
0167       eigen_assert(dataPtr == 0 || SizeAtCompileTime == Dynamic || SizeAtCompileTime == vecSize);
0168       checkSanity<Derived>();
0169     }
0170 
0171     /** \internal Constructor for dynamically sized matrices */
0172     EIGEN_DEVICE_FUNC
0173     inline MapBase(PointerType dataPtr, Index rows, Index cols)
0174             : m_data(dataPtr), m_rows(rows), m_cols(cols)
0175     {
0176       eigen_assert( (dataPtr == 0)
0177               || (   rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
0178                   && cols >= 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
0179       checkSanity<Derived>();
0180     }
0181 
0182     #ifdef EIGEN_MAPBASE_PLUGIN
0183     #include EIGEN_MAPBASE_PLUGIN
0184     #endif
0185 
0186   protected:
0187     EIGEN_DEFAULT_COPY_CONSTRUCTOR(MapBase)
0188     EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(MapBase)
0189 
0190     template<typename T>
0191     EIGEN_DEVICE_FUNC
0192     void checkSanity(typename internal::enable_if<(internal::traits<T>::Alignment>0),void*>::type = 0) const
0193     {
0194 #if EIGEN_MAX_ALIGN_BYTES>0
0195       // innerStride() is not set yet when this function is called, so we optimistically assume the lowest plausible value:
0196       const Index minInnerStride = InnerStrideAtCompileTime == Dynamic ? 1 : Index(InnerStrideAtCompileTime);
0197       EIGEN_ONLY_USED_FOR_DEBUG(minInnerStride);
0198       eigen_assert((   ((internal::UIntPtr(m_data) % internal::traits<Derived>::Alignment) == 0)
0199                     || (cols() * rows() * minInnerStride * sizeof(Scalar)) < internal::traits<Derived>::Alignment ) && "data is not aligned");
0200 #endif
0201     }
0202 
0203     template<typename T>
0204     EIGEN_DEVICE_FUNC
0205     void checkSanity(typename internal::enable_if<internal::traits<T>::Alignment==0,void*>::type = 0) const
0206     {}
0207 
0208     PointerType m_data;
0209     const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
0210     const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
0211 };
0212 
0213 /** \ingroup Core_Module
0214   *
0215   * \brief Base class for non-const dense Map and Block expression with direct access
0216   *
0217   * This base class provides the non-const low-level accessors (e.g. coeff and coeffRef) of
0218   * dense Map and Block objects with direct access.
0219   * It inherits MapBase<Derived, ReadOnlyAccessors> which defines the const variant for reading specific entries.
0220   *
0221   * \sa class Map, class Block
0222   */
0223 template<typename Derived> class MapBase<Derived, WriteAccessors>
0224   : public MapBase<Derived, ReadOnlyAccessors>
0225 {
0226     typedef MapBase<Derived, ReadOnlyAccessors> ReadOnlyMapBase;
0227   public:
0228 
0229     typedef MapBase<Derived, ReadOnlyAccessors> Base;
0230 
0231     typedef typename Base::Scalar Scalar;
0232     typedef typename Base::PacketScalar PacketScalar;
0233     typedef typename Base::StorageIndex StorageIndex;
0234     typedef typename Base::PointerType PointerType;
0235 
0236     using Base::derived;
0237     using Base::rows;
0238     using Base::cols;
0239     using Base::size;
0240     using Base::coeff;
0241     using Base::coeffRef;
0242 
0243     using Base::innerStride;
0244     using Base::outerStride;
0245     using Base::rowStride;
0246     using Base::colStride;
0247 
0248     typedef typename internal::conditional<
0249                     internal::is_lvalue<Derived>::value,
0250                     Scalar,
0251                     const Scalar
0252                   >::type ScalarWithConstIfNotLvalue;
0253 
0254     EIGEN_DEVICE_FUNC
0255     inline const Scalar* data() const { return this->m_data; }
0256     EIGEN_DEVICE_FUNC
0257     inline ScalarWithConstIfNotLvalue* data() { return this->m_data; } // no const-cast here so non-const-correct code will give a compile error
0258 
0259     EIGEN_DEVICE_FUNC
0260     inline ScalarWithConstIfNotLvalue& coeffRef(Index row, Index col)
0261     {
0262       return this->m_data[col * colStride() + row * rowStride()];
0263     }
0264 
0265     EIGEN_DEVICE_FUNC
0266     inline ScalarWithConstIfNotLvalue& coeffRef(Index index)
0267     {
0268       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
0269       return this->m_data[index * innerStride()];
0270     }
0271 
0272     template<int StoreMode>
0273     inline void writePacket(Index row, Index col, const PacketScalar& val)
0274     {
0275       internal::pstoret<Scalar, PacketScalar, StoreMode>
0276                (this->m_data + (col * colStride() + row * rowStride()), val);
0277     }
0278 
0279     template<int StoreMode>
0280     inline void writePacket(Index index, const PacketScalar& val)
0281     {
0282       EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS(Derived)
0283       internal::pstoret<Scalar, PacketScalar, StoreMode>
0284                 (this->m_data + index * innerStride(), val);
0285     }
0286 
0287     EIGEN_DEVICE_FUNC explicit inline MapBase(PointerType dataPtr) : Base(dataPtr) {}
0288     EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index vecSize) : Base(dataPtr, vecSize) {}
0289     EIGEN_DEVICE_FUNC inline MapBase(PointerType dataPtr, Index rows, Index cols) : Base(dataPtr, rows, cols) {}
0290 
0291     EIGEN_DEVICE_FUNC
0292     Derived& operator=(const MapBase& other)
0293     {
0294       ReadOnlyMapBase::Base::operator=(other);
0295       return derived();
0296     }
0297 
0298     // In theory we could simply refer to Base:Base::operator=, but MSVC does not like Base::Base,
0299     // see bugs 821 and 920.
0300     using ReadOnlyMapBase::Base::operator=;
0301   protected:
0302     EIGEN_DEFAULT_COPY_CONSTRUCTOR(MapBase)
0303     EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(MapBase)
0304 };
0305 
0306 #undef EIGEN_STATIC_ASSERT_INDEX_BASED_ACCESS
0307 
0308 } // end namespace RivetEigen
0309 
0310 #endif // EIGEN_MAPBASE_H