00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef EIGEN_MAPBASE_H
00027 #define EIGEN_MAPBASE_H
00028
00043 template<typename Derived> class MapBase
00044 : public MatrixBase<Derived>
00045 {
00046 public:
00047
00048 typedef MatrixBase<Derived> Base;
00049 enum {
00050 IsRowMajor = (int(ei_traits<Derived>::Flags) & RowMajorBit) ? 1 : 0,
00051 PacketAccess = ei_traits<Derived>::PacketAccess,
00052 RowsAtCompileTime = ei_traits<Derived>::RowsAtCompileTime,
00053 ColsAtCompileTime = ei_traits<Derived>::ColsAtCompileTime,
00054 SizeAtCompileTime = Base::SizeAtCompileTime
00055 };
00056
00057 typedef typename ei_traits<Derived>::AlignedDerivedType AlignedDerivedType;
00058 typedef typename ei_traits<Derived>::Scalar Scalar;
00059 typedef typename Base::PacketScalar PacketScalar;
00060 using Base::derived;
00061
00062 inline int rows() const { return m_rows.value(); }
00063 inline int cols() const { return m_cols.value(); }
00064
00065 inline int stride() const { return derived().stride(); }
00066 inline const Scalar* data() const { return m_data; }
00067
00068 template<bool IsForceAligned,typename Dummy> struct force_aligned_impl {
00069 static AlignedDerivedType run(MapBase& a) { return a.derived(); }
00070 };
00071
00072 template<typename Dummy> struct force_aligned_impl<false,Dummy> {
00073 static AlignedDerivedType run(MapBase& a) { return a.derived()._convertToForceAligned(); }
00074 };
00075
00078 AlignedDerivedType forceAligned()
00079 {
00080 return force_aligned_impl<int(PacketAccess)==int(ForceAligned),Derived>::run(*this);
00081 }
00082
00083 inline const Scalar& coeff(int row, int col) const
00084 {
00085 if(IsRowMajor)
00086 return m_data[col + row * stride()];
00087 else
00088 return m_data[row + col * stride()];
00089 }
00090
00091 inline Scalar& coeffRef(int row, int col)
00092 {
00093 if(IsRowMajor)
00094 return const_cast<Scalar*>(m_data)[col + row * stride()];
00095 else
00096 return const_cast<Scalar*>(m_data)[row + col * stride()];
00097 }
00098
00099 inline const Scalar coeff(int index) const
00100 {
00101 ei_assert(Derived::IsVectorAtCompileTime || (ei_traits<Derived>::Flags & LinearAccessBit));
00102 if ( ((RowsAtCompileTime == 1) == IsRowMajor) || !int(Derived::IsVectorAtCompileTime) )
00103 return m_data[index];
00104 else
00105 return m_data[index*stride()];
00106 }
00107
00108 inline Scalar& coeffRef(int index)
00109 {
00110 ei_assert(Derived::IsVectorAtCompileTime || (ei_traits<Derived>::Flags & LinearAccessBit));
00111 if ( ((RowsAtCompileTime == 1) == IsRowMajor) || !int(Derived::IsVectorAtCompileTime) )
00112 return const_cast<Scalar*>(m_data)[index];
00113 else
00114 return const_cast<Scalar*>(m_data)[index*stride()];
00115 }
00116
00117 template<int LoadMode>
00118 inline PacketScalar packet(int row, int col) const
00119 {
00120 return ei_ploadt<Scalar, int(PacketAccess) == ForceAligned ? Aligned : LoadMode>
00121 (m_data + (IsRowMajor ? col + row * stride()
00122 : row + col * stride()));
00123 }
00124
00125 template<int LoadMode>
00126 inline PacketScalar packet(int index) const
00127 {
00128 return ei_ploadt<Scalar, int(PacketAccess) == ForceAligned ? Aligned : LoadMode>(m_data + index);
00129 }
00130
00131 template<int StoreMode>
00132 inline void writePacket(int row, int col, const PacketScalar& x)
00133 {
00134 ei_pstoret<Scalar, PacketScalar, int(PacketAccess) == ForceAligned ? Aligned : StoreMode>
00135 (const_cast<Scalar*>(m_data) + (IsRowMajor ? col + row * stride()
00136 : row + col * stride()), x);
00137 }
00138
00139 template<int StoreMode>
00140 inline void writePacket(int index, const PacketScalar& x)
00141 {
00142 ei_pstoret<Scalar, PacketScalar, int(PacketAccess) == ForceAligned ? Aligned : StoreMode>
00143 (const_cast<Scalar*>(m_data) + index, x);
00144 }
00145
00146 inline MapBase(const Scalar* data) : m_data(data), m_rows(RowsAtCompileTime), m_cols(ColsAtCompileTime)
00147 {
00148 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
00149 }
00150
00151 inline MapBase(const Scalar* data, int size)
00152 : m_data(data),
00153 m_rows(RowsAtCompileTime == Dynamic ? size : RowsAtCompileTime),
00154 m_cols(ColsAtCompileTime == Dynamic ? size : ColsAtCompileTime)
00155 {
00156 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00157 ei_assert(size > 0 || data == 0);
00158 ei_assert(SizeAtCompileTime == Dynamic || SizeAtCompileTime == size);
00159 }
00160
00161 inline MapBase(const Scalar* data, int rows, int cols)
00162 : m_data(data), m_rows(rows), m_cols(cols)
00163 {
00164 ei_assert( (data == 0)
00165 || ( rows > 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
00166 && cols > 0 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols)));
00167 }
00168
00169 Derived& operator=(const MapBase& other)
00170 {
00171 return Base::operator=(other);
00172 }
00173
00174 template<typename OtherDerived>
00175 Derived& operator=(const MatrixBase<OtherDerived>& other)
00176 {
00177 return Base::operator=(other);
00178 }
00179
00180 using Base::operator*=;
00181
00182 template<typename OtherDerived>
00183 Derived& operator+=(const MatrixBase<OtherDerived>& other)
00184 { return derived() = forceAligned() + other; }
00185
00186 template<typename OtherDerived>
00187 Derived& operator-=(const MatrixBase<OtherDerived>& other)
00188 { return derived() = forceAligned() - other; }
00189
00190 Derived& operator*=(const Scalar& other)
00191 { return derived() = forceAligned() * other; }
00192
00193 Derived& operator/=(const Scalar& other)
00194 { return derived() = forceAligned() / other; }
00195
00196 protected:
00197 const Scalar* EIGEN_RESTRICT m_data;
00198 const ei_int_if_dynamic<RowsAtCompileTime> m_rows;
00199 const ei_int_if_dynamic<ColsAtCompileTime> m_cols;
00200 };
00201
00202 #endif // EIGEN_MAPBASE_H