Map.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_MAP_H
27 #define EIGEN_MAP_H
28 
29 namespace Eigen {
30 
82 namespace internal {
83 template<typename PlainObjectType, int MapOptions, typename StrideType>
84 struct traits<Map<PlainObjectType, MapOptions, StrideType> >
85  : public traits<PlainObjectType>
86 {
87  typedef traits<PlainObjectType> TraitsBase;
88  typedef typename PlainObjectType::Index Index;
89  typedef typename PlainObjectType::Scalar Scalar;
90  enum {
91  InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0
92  ? int(PlainObjectType::InnerStrideAtCompileTime)
93  : int(StrideType::InnerStrideAtCompileTime),
94  OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
95  ? int(PlainObjectType::OuterStrideAtCompileTime)
96  : int(StrideType::OuterStrideAtCompileTime),
97  HasNoInnerStride = InnerStrideAtCompileTime == 1,
98  HasNoOuterStride = StrideType::OuterStrideAtCompileTime == 0,
99  HasNoStride = HasNoInnerStride && HasNoOuterStride,
100  IsAligned = bool(EIGEN_ALIGN) && ((int(MapOptions)&Aligned)==Aligned),
101  IsDynamicSize = PlainObjectType::SizeAtCompileTime==Dynamic,
102  KeepsPacketAccess = bool(HasNoInnerStride)
103  && ( bool(IsDynamicSize)
104  || HasNoOuterStride
105  || ( OuterStrideAtCompileTime!=Dynamic
106  && ((static_cast<int>(sizeof(Scalar))*OuterStrideAtCompileTime)%16)==0 ) ),
107  Flags0 = TraitsBase::Flags & (~NestByRefBit),
108  Flags1 = IsAligned ? (int(Flags0) | AlignedBit) : (int(Flags0) & ~AlignedBit),
109  Flags2 = (bool(HasNoStride) || bool(PlainObjectType::IsVectorAtCompileTime))
110  ? int(Flags1) : int(Flags1 & ~LinearAccessBit),
111  Flags3 = is_lvalue<PlainObjectType>::value ? int(Flags2) : (int(Flags2) & ~LvalueBit),
112  Flags = KeepsPacketAccess ? int(Flags3) : (int(Flags3) & ~PacketAccessBit)
113  };
114 private:
115  enum { Options }; // Expressions don't have Options
116 };
117 }
118 
119 template<typename PlainObjectType, int MapOptions, typename StrideType> class Map
120  : public MapBase<Map<PlainObjectType, MapOptions, StrideType> >
121 {
122  public:
123 
126 
127  typedef typename Base::PointerType PointerType;
128 #if EIGEN2_SUPPORT_STAGE <= STAGE30_FULL_EIGEN3_API
129  typedef const Scalar* PointerArgType;
130  inline PointerType cast_to_pointer_type(PointerArgType ptr) { return const_cast<PointerType>(ptr); }
131 #else
132  typedef PointerType PointerArgType;
133  inline PointerType cast_to_pointer_type(PointerArgType ptr) { return ptr; }
134 #endif
135 
136  inline Index innerStride() const
137  {
138  return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
139  }
140 
141  inline Index outerStride() const
142  {
143  return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
144  : IsVectorAtCompileTime ? this->size()
145  : int(Flags)&RowMajorBit ? this->cols()
146  : this->rows();
147  }
148 
154  inline Map(PointerArgType data, const StrideType& stride = StrideType())
155  : Base(cast_to_pointer_type(data)), m_stride(stride)
156  {
157  PlainObjectType::Base::_check_template_params();
158  }
159 
166  inline Map(PointerArgType data, Index size, const StrideType& stride = StrideType())
167  : Base(cast_to_pointer_type(data), size), m_stride(stride)
168  {
169  PlainObjectType::Base::_check_template_params();
170  }
171 
179  inline Map(PointerArgType data, Index rows, Index cols, const StrideType& stride = StrideType())
180  : Base(cast_to_pointer_type(data), rows, cols), m_stride(stride)
181  {
182  PlainObjectType::Base::_check_template_params();
183  }
184 
186 
187  protected:
188  StrideType m_stride;
189 };
190 
191 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
192 inline Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
193  ::Array(const Scalar *data)
194 {
195  this->_set_noalias(Eigen::Map<const Array>(data));
196 }
197 
198 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
200  ::Matrix(const Scalar *data)
201 {
202  this->_set_noalias(Eigen::Map<const Matrix>(data));
203 }
204 
205 } // end namespace Eigen
206 
207 #endif // EIGEN_MAP_H