Eigen::Map< PlainObjectType, MapOptions, StrideType > Class Template Reference

A matrix or vector expression mapping an existing array of data. More...

+ Inheritance diagram for Eigen::Map< PlainObjectType, MapOptions, StrideType >:

Public Types

typedef MapBase< MapBase
 
typedef PointerType PointerArgType
 
typedef Base::PointerType PointerType
 

Public Member Functions

PointerType cast_to_pointer_type (PointerArgType ptr)
 
EIGEN_CONSTEXPR Index innerStride () const
 
 Map (PointerArgType dataPtr, const StrideType &stride=StrideType())
 
 Map (PointerArgType dataPtr, Index rows, Index cols, const StrideType &stride=StrideType())
 
 Map (PointerArgType dataPtr, Index size, const StrideType &stride=StrideType())
 
EIGEN_CONSTEXPR Index outerStride () const
 

Protected Attributes

StrideType m_stride
 

Detailed Description

template<typename PlainObjectType, int MapOptions, typename StrideType>
class Eigen::Map< PlainObjectType, MapOptions, StrideType >

A matrix or vector expression mapping an existing array of data.

Template Parameters
PlainObjectTypethe equivalent matrix type of the mapped data
MapOptionsspecifies the pointer alignment in bytes. It can be: Aligned128, Aligned64, Aligned32, Aligned16, Aligned8 or Unaligned. The default is Unaligned.
StrideTypeoptionally specifies strides. By default, Map assumes the memory layout of an ordinary, contiguous array. This can be overridden by specifying strides. The type passed here must be a specialization of the Stride template, see examples below.

This class represents a matrix or vector expression mapping an existing array of data. It can be used to let Eigen interface without any overhead with non-Eigen data structures, such as plain C arrays or structures from other libraries. By default, it assumes that the data is laid out contiguously in memory. You can however override this by explicitly specifying inner and outer strides.

Here's an example of simply mapping a contiguous array as a column-major matrix:

int array[9];
for(int i = 0; i < 9; ++i) array[i] = i;
cout<< Map< Matrix3i >(array)<< endl
std::array< T, N > array
Definition: EmulateArray.h:256

Output:

0 3 6
1 4 7
2 5 8

If you need to map non-contiguous arrays, you can do so by specifying strides:

Here's an example of mapping an array as a vector, specifying an inner stride, that is, the pointer increment between two consecutive coefficients. Here, we're specifying the inner stride as a compile-time fixed value.

int array[12];
for(int i = 0; i < 12; ++i) array[i] = i;
cout << Map<VectorXi, 0, InnerStride<2> >
(array, 6) // the inner stride has already been passed as template parameter
<< endl;

Output:

 0
 2
 4
 6
 8
10

Here's an example of mapping an array while specifying an outer stride. Here, since we're mapping as a column-major matrix, 'outer stride' means the pointer increment between two consecutive columns. Here, we're specifying the outer stride as a runtime parameter. Note that here OuterStride<> is a short version of OuterStride<Dynamic> because the default template parameter of OuterStride is Dynamic

int array[12];
for(int i = 0; i < 12; ++i) array[i] = i;
cout << Map<MatrixXi, 0, OuterStride<> >(array, 3, 3, OuterStride<>(4)) << endl;

Output:

 0  4  8
 1  5  9
 2  6 10

For more details and for an example of specifying both an inner and an outer stride, see class Stride.

Tip: to change the array of data mapped by a Map object, you can use the C++ placement new syntax:

Example:

int data[] = {1,2,3,4,5,6,7,8,9};
Map<RowVectorXi> v(data,4);
cout << "The mapped vector v is: " << v << "\n";
new (&v) Map<RowVectorXi>(data+4,5);
cout << "Now v is: " << v << "\n";
Array< int, Dynamic, 1 > v
int data[]

Output:

The mapped vector v is: 1 2 3 4
Now v is: 5 6 7 8 9

This class is the return type of PlainObjectBase::Map() but can also be used directly.

See also
PlainObjectBase::Map(), Storage orders

Definition at line 96 of file Map.h.

Member Typedef Documentation

◆ Base

template<typename PlainObjectType , int MapOptions, typename StrideType >
typedef MapBase<Map> Eigen::Map< PlainObjectType, MapOptions, StrideType >::Base

Definition at line 101 of file Map.h.

◆ PointerArgType

template<typename PlainObjectType , int MapOptions, typename StrideType >
typedef PointerType Eigen::Map< PlainObjectType, MapOptions, StrideType >::PointerArgType

Definition at line 105 of file Map.h.

◆ PointerType

template<typename PlainObjectType , int MapOptions, typename StrideType >
typedef Base::PointerType Eigen::Map< PlainObjectType, MapOptions, StrideType >::PointerType

Definition at line 104 of file Map.h.

Constructor & Destructor Documentation

◆ Map() [1/3]

template<typename PlainObjectType , int MapOptions, typename StrideType >
Eigen::Map< PlainObjectType, MapOptions, StrideType >::Map ( PointerArgType  dataPtr,
const StrideType &  stride = StrideType() 
)
inlineexplicit

Constructor in the fixed-size case.

Parameters
dataPtrpointer to the array to map
strideoptional Stride object, passing the strides.

Definition at line 131 of file Map.h.

132  : Base(cast_to_pointer_type(dataPtr)), m_stride(stride)
133  {
134  }
StrideType m_stride
Definition: Map.h:164
MapBase< Map > Base
Definition: Map.h:101
PointerType cast_to_pointer_type(PointerArgType ptr)
Definition: Map.h:107

◆ Map() [2/3]

template<typename PlainObjectType , int MapOptions, typename StrideType >
Eigen::Map< PlainObjectType, MapOptions, StrideType >::Map ( PointerArgType  dataPtr,
Index  size,
const StrideType &  stride = StrideType() 
)
inline

Constructor in the dynamic-size vector case.

Parameters
dataPtrpointer to the array to map
sizethe size of the vector expression
strideoptional Stride object, passing the strides.

Definition at line 143 of file Map.h.

144  : Base(cast_to_pointer_type(dataPtr), size), m_stride(stride)
145  {
146  }

◆ Map() [3/3]

template<typename PlainObjectType , int MapOptions, typename StrideType >
Eigen::Map< PlainObjectType, MapOptions, StrideType >::Map ( PointerArgType  dataPtr,
Index  rows,
Index  cols,
const StrideType &  stride = StrideType() 
)
inline

Constructor in the dynamic-size matrix case.

Parameters
dataPtrpointer to the array to map
rowsthe number of rows of the matrix expression
colsthe number of columns of the matrix expression
strideoptional Stride object, passing the strides.

Definition at line 156 of file Map.h.

157  : Base(cast_to_pointer_type(dataPtr), rows, cols), m_stride(stride)
158  {
159  }

Member Function Documentation

◆ cast_to_pointer_type()

template<typename PlainObjectType , int MapOptions, typename StrideType >
PointerType Eigen::Map< PlainObjectType, MapOptions, StrideType >::cast_to_pointer_type ( PointerArgType  ptr)
inline

Definition at line 107 of file Map.h.

107 { return ptr; }

◆ innerStride()

template<typename PlainObjectType , int MapOptions, typename StrideType >
EIGEN_CONSTEXPR Index Eigen::Map< PlainObjectType, MapOptions, StrideType >::innerStride ( ) const
inline

Definition at line 110 of file Map.h.

111  {
112  return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
113  }

◆ outerStride()

template<typename PlainObjectType , int MapOptions, typename StrideType >
EIGEN_CONSTEXPR Index Eigen::Map< PlainObjectType, MapOptions, StrideType >::outerStride ( ) const
inline

Definition at line 116 of file Map.h.

117  {
118  return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
119  : internal::traits<Map>::OuterStrideAtCompileTime != Dynamic ? Index(internal::traits<Map>::OuterStrideAtCompileTime)
120  : IsVectorAtCompileTime ? (this->size() * innerStride())
121  : int(Flags)&RowMajorBit ? (this->cols() * innerStride())
122  : (this->rows() * innerStride());
123  }
EIGEN_CONSTEXPR Index innerStride() const
Definition: Map.h:110
const unsigned int RowMajorBit
Definition: Constants.h:68
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24

Member Data Documentation

◆ m_stride

template<typename PlainObjectType , int MapOptions, typename StrideType >
StrideType Eigen::Map< PlainObjectType, MapOptions, StrideType >::m_stride
protected

Definition at line 164 of file Map.h.


The documentation for this class was generated from the following file: