Map.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #ifndef EIGEN_MAP_H
  11. #define EIGEN_MAP_H
  12. namespace Eigen {
  13. namespace internal {
  14. template<typename PlainObjectType, int MapOptions, typename StrideType>
  15. struct traits<Map<PlainObjectType, MapOptions, StrideType> >
  16. : public traits<PlainObjectType>
  17. {
  18. typedef traits<PlainObjectType> TraitsBase;
  19. enum {
  20. PlainObjectTypeInnerSize = ((traits<PlainObjectType>::Flags&RowMajorBit)==RowMajorBit)
  21. ? PlainObjectType::ColsAtCompileTime
  22. : PlainObjectType::RowsAtCompileTime,
  23. InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0
  24. ? int(PlainObjectType::InnerStrideAtCompileTime)
  25. : int(StrideType::InnerStrideAtCompileTime),
  26. OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
  27. ? (InnerStrideAtCompileTime==Dynamic || PlainObjectTypeInnerSize==Dynamic
  28. ? Dynamic
  29. : int(InnerStrideAtCompileTime) * int(PlainObjectTypeInnerSize))
  30. : int(StrideType::OuterStrideAtCompileTime),
  31. Alignment = int(MapOptions)&int(AlignedMask),
  32. Flags0 = TraitsBase::Flags & (~NestByRefBit),
  33. Flags = is_lvalue<PlainObjectType>::value ? int(Flags0) : (int(Flags0) & ~LvalueBit)
  34. };
  35. private:
  36. enum { Options }; // Expressions don't have Options
  37. };
  38. }
  39. /** \class Map
  40. * \ingroup Core_Module
  41. *
  42. * \brief A matrix or vector expression mapping an existing array of data.
  43. *
  44. * \tparam PlainObjectType the equivalent matrix type of the mapped data
  45. * \tparam MapOptions specifies the pointer alignment in bytes. It can be: \c #Aligned128, , \c #Aligned64, \c #Aligned32, \c #Aligned16, \c #Aligned8 or \c #Unaligned.
  46. * The default is \c #Unaligned.
  47. * \tparam StrideType optionally specifies strides. By default, Map assumes the memory layout
  48. * of an ordinary, contiguous array. This can be overridden by specifying strides.
  49. * The type passed here must be a specialization of the Stride template, see examples below.
  50. *
  51. * This class represents a matrix or vector expression mapping an existing array of data.
  52. * It can be used to let Eigen interface without any overhead with non-Eigen data structures,
  53. * such as plain C arrays or structures from other libraries. By default, it assumes that the
  54. * data is laid out contiguously in memory. You can however override this by explicitly specifying
  55. * inner and outer strides.
  56. *
  57. * Here's an example of simply mapping a contiguous array as a \ref TopicStorageOrders "column-major" matrix:
  58. * \include Map_simple.cpp
  59. * Output: \verbinclude Map_simple.out
  60. *
  61. * If you need to map non-contiguous arrays, you can do so by specifying strides:
  62. *
  63. * Here's an example of mapping an array as a vector, specifying an inner stride, that is, the pointer
  64. * increment between two consecutive coefficients. Here, we're specifying the inner stride as a compile-time
  65. * fixed value.
  66. * \include Map_inner_stride.cpp
  67. * Output: \verbinclude Map_inner_stride.out
  68. *
  69. * Here's an example of mapping an array while specifying an outer stride. Here, since we're mapping
  70. * as a column-major matrix, 'outer stride' means the pointer increment between two consecutive columns.
  71. * Here, we're specifying the outer stride as a runtime parameter. Note that here \c OuterStride<> is
  72. * a short version of \c OuterStride<Dynamic> because the default template parameter of OuterStride
  73. * is \c Dynamic
  74. * \include Map_outer_stride.cpp
  75. * Output: \verbinclude Map_outer_stride.out
  76. *
  77. * For more details and for an example of specifying both an inner and an outer stride, see class Stride.
  78. *
  79. * \b Tip: to change the array of data mapped by a Map object, you can use the C++
  80. * placement new syntax:
  81. *
  82. * Example: \include Map_placement_new.cpp
  83. * Output: \verbinclude Map_placement_new.out
  84. *
  85. * This class is the return type of PlainObjectBase::Map() but can also be used directly.
  86. *
  87. * \sa PlainObjectBase::Map(), \ref TopicStorageOrders
  88. */
  89. template<typename PlainObjectType, int MapOptions, typename StrideType> class Map
  90. : public MapBase<Map<PlainObjectType, MapOptions, StrideType> >
  91. {
  92. public:
  93. typedef MapBase<Map> Base;
  94. EIGEN_DENSE_PUBLIC_INTERFACE(Map)
  95. typedef typename Base::PointerType PointerType;
  96. typedef PointerType PointerArgType;
  97. EIGEN_DEVICE_FUNC
  98. inline PointerType cast_to_pointer_type(PointerArgType ptr) { return ptr; }
  99. EIGEN_DEVICE_FUNC
  100. inline Index innerStride() const
  101. {
  102. return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
  103. }
  104. EIGEN_DEVICE_FUNC
  105. inline Index outerStride() const
  106. {
  107. return int(StrideType::OuterStrideAtCompileTime) != 0 ? m_stride.outer()
  108. : int(internal::traits<Map>::OuterStrideAtCompileTime) != Dynamic ? Index(internal::traits<Map>::OuterStrideAtCompileTime)
  109. : IsVectorAtCompileTime ? (this->size() * innerStride())
  110. : (int(Flags)&RowMajorBit) ? (this->cols() * innerStride())
  111. : (this->rows() * innerStride());
  112. }
  113. /** Constructor in the fixed-size case.
  114. *
  115. * \param dataPtr pointer to the array to map
  116. * \param stride optional Stride object, passing the strides.
  117. */
  118. EIGEN_DEVICE_FUNC
  119. explicit inline Map(PointerArgType dataPtr, const StrideType& stride = StrideType())
  120. : Base(cast_to_pointer_type(dataPtr)), m_stride(stride)
  121. {
  122. PlainObjectType::Base::_check_template_params();
  123. }
  124. /** Constructor in the dynamic-size vector case.
  125. *
  126. * \param dataPtr pointer to the array to map
  127. * \param size the size of the vector expression
  128. * \param stride optional Stride object, passing the strides.
  129. */
  130. EIGEN_DEVICE_FUNC
  131. inline Map(PointerArgType dataPtr, Index size, const StrideType& stride = StrideType())
  132. : Base(cast_to_pointer_type(dataPtr), size), m_stride(stride)
  133. {
  134. PlainObjectType::Base::_check_template_params();
  135. }
  136. /** Constructor in the dynamic-size matrix case.
  137. *
  138. * \param dataPtr pointer to the array to map
  139. * \param rows the number of rows of the matrix expression
  140. * \param cols the number of columns of the matrix expression
  141. * \param stride optional Stride object, passing the strides.
  142. */
  143. EIGEN_DEVICE_FUNC
  144. inline Map(PointerArgType dataPtr, Index rows, Index cols, const StrideType& stride = StrideType())
  145. : Base(cast_to_pointer_type(dataPtr), rows, cols), m_stride(stride)
  146. {
  147. PlainObjectType::Base::_check_template_params();
  148. }
  149. EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
  150. protected:
  151. StrideType m_stride;
  152. };
  153. } // end namespace Eigen
  154. #endif // EIGEN_MAP_H