Map.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 <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_MAP_H
00027 #define EIGEN_MAP_H
00028 
00029 namespace Eigen { 
00030 
00082 namespace internal {
00083 template<typename PlainObjectType, int MapOptions, typename StrideType>
00084 struct traits<Map<PlainObjectType, MapOptions, StrideType> >
00085   : public traits<PlainObjectType>
00086 {
00087   typedef traits<PlainObjectType> TraitsBase;
00088   typedef typename PlainObjectType::Index Index;
00089   typedef typename PlainObjectType::Scalar Scalar;
00090   enum {
00091     InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0
00092                              ? int(PlainObjectType::InnerStrideAtCompileTime)
00093                              : int(StrideType::InnerStrideAtCompileTime),
00094     OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
00095                              ? int(PlainObjectType::OuterStrideAtCompileTime)
00096                              : int(StrideType::OuterStrideAtCompileTime),
00097     HasNoInnerStride = InnerStrideAtCompileTime == 1,
00098     HasNoOuterStride = StrideType::OuterStrideAtCompileTime == 0,
00099     HasNoStride = HasNoInnerStride && HasNoOuterStride,
00100     IsAligned = bool(EIGEN_ALIGN) && ((int(MapOptions)&Aligned)==Aligned),
00101     IsDynamicSize = PlainObjectType::SizeAtCompileTime==Dynamic,
00102     KeepsPacketAccess = bool(HasNoInnerStride)
00103                         && ( bool(IsDynamicSize)
00104                            || HasNoOuterStride
00105                            || ( OuterStrideAtCompileTime!=Dynamic
00106                            && ((static_cast<int>(sizeof(Scalar))*OuterStrideAtCompileTime)%16)==0 ) ),
00107     Flags0 = TraitsBase::Flags & (~NestByRefBit),
00108     Flags1 = IsAligned ? (int(Flags0) | AlignedBit) : (int(Flags0) & ~AlignedBit),
00109     Flags2 = (bool(HasNoStride) || bool(PlainObjectType::IsVectorAtCompileTime))
00110            ? int(Flags1) : int(Flags1 & ~LinearAccessBit),
00111     Flags3 = is_lvalue<PlainObjectType>::value ? int(Flags2) : (int(Flags2) & ~LvalueBit),
00112     Flags = KeepsPacketAccess ? int(Flags3) : (int(Flags3) & ~PacketAccessBit)
00113   };
00114 private:
00115   enum { Options }; // Expressions don't have Options
00116 };
00117 }
00118 
00119 template<typename PlainObjectType, int MapOptions, typename StrideType> class Map
00120   : public MapBase<Map<PlainObjectType, MapOptions, StrideType> >
00121 {
00122   public:
00123 
00124     typedef MapBase<Map> Base;
00125     EIGEN_DENSE_PUBLIC_INTERFACE(Map)
00126 
00127     typedef typename Base::PointerType PointerType;
00128 #if EIGEN2_SUPPORT_STAGE <= STAGE30_FULL_EIGEN3_API
00129     typedef const Scalar* PointerArgType;
00130     inline PointerType cast_to_pointer_type(PointerArgType ptr) { return const_cast<PointerType>(ptr); }
00131 #else
00132     typedef PointerType PointerArgType;
00133     inline PointerType cast_to_pointer_type(PointerArgType ptr) { return ptr; }
00134 #endif
00135 
00136     inline Index innerStride() const
00137     {
00138       return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
00139     }
00140 
00141     inline Index outerStride() const
00142     {
00143       return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
00144            : IsVectorAtCompileTime ? this->size()
00145            : int(Flags)&RowMajorBit ? this->cols()
00146            : this->rows();
00147     }
00148 
00154     inline Map(PointerArgType data, const StrideType& stride = StrideType())
00155       : Base(cast_to_pointer_type(data)), m_stride(stride)
00156     {
00157       PlainObjectType::Base::_check_template_params();
00158     }
00159 
00166     inline Map(PointerArgType data, Index size, const StrideType& stride = StrideType())
00167       : Base(cast_to_pointer_type(data), size), m_stride(stride)
00168     {
00169       PlainObjectType::Base::_check_template_params();
00170     }
00171 
00179     inline Map(PointerArgType data, Index rows, Index cols, const StrideType& stride = StrideType())
00180       : Base(cast_to_pointer_type(data), rows, cols), m_stride(stride)
00181     {
00182       PlainObjectType::Base::_check_template_params();
00183     }
00184 
00185     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
00186 
00187   protected:
00188     StrideType m_stride;
00189 };
00190 
00191 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00192 inline Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
00193   ::Array(const Scalar *data)
00194 {
00195   this->_set_noalias(Eigen::Map<const Array>(data));
00196 }
00197 
00198 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00199 inline Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
00200   ::Matrix(const Scalar *data)
00201 {
00202   this->_set_noalias(Eigen::Map<const Matrix>(data));
00203 }
00204 
00205 } // end namespace Eigen
00206 
00207 #endif // EIGEN_MAP_H