2007-10-14 14:45:31 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-22 20:25:33 +02:00
|
|
|
// for linear algebra.
|
2007-10-14 14:45:31 +00:00
|
|
|
//
|
2010-02-18 20:42:38 -05:00
|
|
|
// Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
|
2010-06-24 23:21:58 +02:00
|
|
|
// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2007-10-14 14:45:31 +00:00
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2007-10-14 14:45:31 +00:00
|
|
|
|
2007-12-19 09:30:53 +00:00
|
|
|
#ifndef EIGEN_MAP_H
|
|
|
|
|
#define EIGEN_MAP_H
|
2007-10-14 14:45:31 +00:00
|
|
|
|
2023-08-21 16:25:22 +00:00
|
|
|
// IWYU pragma: private
|
2021-09-10 19:12:26 +00:00
|
|
|
#include "./InternalHeaderCheck.h"
|
|
|
|
|
|
2021-03-08 12:39:11 -05:00
|
|
|
namespace Eigen {
|
2012-04-15 11:06:28 +01:00
|
|
|
|
2016-01-01 21:45:06 +01:00
|
|
|
namespace internal {
|
|
|
|
|
template <typename PlainObjectType, int MapOptions, typename StrideType>
|
|
|
|
|
struct traits<Map<PlainObjectType, MapOptions, StrideType> > : public traits<PlainObjectType> {
|
|
|
|
|
typedef traits<PlainObjectType> TraitsBase;
|
|
|
|
|
enum {
|
2017-08-22 13:27:37 +02:00
|
|
|
PlainObjectTypeInnerSize = ((traits<PlainObjectType>::Flags & RowMajorBit) == RowMajorBit)
|
|
|
|
|
? PlainObjectType::ColsAtCompileTime
|
|
|
|
|
: PlainObjectType::RowsAtCompileTime,
|
|
|
|
|
|
2016-01-01 21:45:06 +01:00
|
|
|
InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0
|
|
|
|
|
? int(PlainObjectType::InnerStrideAtCompileTime)
|
|
|
|
|
: int(StrideType::InnerStrideAtCompileTime),
|
|
|
|
|
OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
|
2017-08-22 13:27:37 +02:00
|
|
|
? (InnerStrideAtCompileTime == Dynamic || PlainObjectTypeInnerSize == Dynamic
|
|
|
|
|
? Dynamic
|
|
|
|
|
: int(InnerStrideAtCompileTime) * int(PlainObjectTypeInnerSize))
|
2016-01-01 21:45:06 +01:00
|
|
|
: int(StrideType::OuterStrideAtCompileTime),
|
|
|
|
|
Alignment = int(MapOptions) & int(AlignedMask),
|
|
|
|
|
Flags0 = TraitsBase::Flags & (~NestByRefBit),
|
|
|
|
|
Flags = is_lvalue<PlainObjectType>::value ? int(Flags0) : (int(Flags0) & ~LvalueBit)
|
|
|
|
|
};
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2016-01-01 21:45:06 +01:00
|
|
|
private:
|
|
|
|
|
enum { Options }; // Expressions don't have Options
|
|
|
|
|
};
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
2008-01-03 19:36:32 +00:00
|
|
|
/** \class Map
|
2010-07-06 13:10:08 +01:00
|
|
|
* \ingroup Core_Module
|
2008-01-03 19:36:32 +00:00
|
|
|
*
|
|
|
|
|
* \brief A matrix or vector expression mapping an existing array of data.
|
|
|
|
|
*
|
2011-05-03 17:08:14 +01:00
|
|
|
* \tparam PlainObjectType the equivalent matrix type of the mapped data
|
2021-08-16 12:02:33 +00:00
|
|
|
* \tparam MapOptions specifies the pointer alignment in bytes. It can be: \c #Aligned128, \c #Aligned64, \c #Aligned32,
|
2011-09-03 15:18:21 +01:00
|
|
|
* \c #Aligned16, \c #Aligned8 or \c #Unaligned. The default is \c #Unaligned. \tparam StrideType optionally specifies
|
2010-02-26 21:29:04 -05:00
|
|
|
* 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.
|
2008-06-27 01:22:35 +00:00
|
|
|
*
|
2008-01-03 19:36:32 +00:00
|
|
|
* 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,
|
2010-02-26 21:29:04 -05:00
|
|
|
* 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.
|
|
|
|
|
*
|
2011-02-12 17:43:29 +00:00
|
|
|
* Here's an example of simply mapping a contiguous array as a \ref TopicStorageOrders "column-major" matrix:
|
2010-02-26 21:29:04 -05:00
|
|
|
* \include Map_simple.cpp
|
|
|
|
|
* Output: \verbinclude Map_simple.out
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
* \include Map_inner_stride.cpp
|
|
|
|
|
* Output: \verbinclude Map_inner_stride.out
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
2010-06-25 14:48:16 +02:00
|
|
|
* Here, we're specifying the outer stride as a runtime parameter. Note that here \c OuterStride<> is
|
|
|
|
|
* a short version of \c OuterStride<Dynamic> because the default template parameter of OuterStride
|
|
|
|
|
* is \c Dynamic
|
2010-02-26 21:29:04 -05:00
|
|
|
* \include Map_outer_stride.cpp
|
|
|
|
|
* Output: \verbinclude Map_outer_stride.out
|
|
|
|
|
*
|
|
|
|
|
* For more details and for an example of specifying both an inner and an outer stride, see class Stride.
|
2009-03-09 19:23:31 +00:00
|
|
|
*
|
2009-10-23 14:26:14 +02:00
|
|
|
* \b Tip: to change the array of data mapped by a Map object, you can use the C++
|
2009-02-09 09:54:48 +00:00
|
|
|
* placement new syntax:
|
|
|
|
|
*
|
|
|
|
|
* Example: \include Map_placement_new.cpp
|
|
|
|
|
* Output: \verbinclude Map_placement_new.out
|
2008-01-03 19:36:32 +00:00
|
|
|
*
|
2011-09-03 15:18:21 +01:00
|
|
|
* This class is the return type of PlainObjectBase::Map() but can also be used directly.
|
2008-01-03 19:36:32 +00:00
|
|
|
*
|
2011-09-03 15:18:21 +01:00
|
|
|
* \sa PlainObjectBase::Map(), \ref TopicStorageOrders
|
2008-01-03 19:36:32 +00:00
|
|
|
*/
|
2010-04-26 16:59:04 +02:00
|
|
|
template <typename PlainObjectType, int MapOptions, typename StrideType>
|
|
|
|
|
class Map : public MapBase<Map<PlainObjectType, MapOptions, StrideType> > {
|
2007-10-14 14:45:31 +00:00
|
|
|
public:
|
2010-04-16 10:13:32 -04:00
|
|
|
typedef MapBase<Map> Base;
|
2010-01-22 10:15:41 +01:00
|
|
|
EIGEN_DENSE_PUBLIC_INTERFACE(Map)
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-12-10 02:09:58 -05:00
|
|
|
typedef typename Base::PointerType PointerType;
|
2011-01-21 10:42:19 -05:00
|
|
|
typedef PointerType PointerArgType;
|
2026-02-26 11:23:53 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr inline PointerType cast_to_pointer_type(PointerArgType ptr) const { return ptr; }
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2024-11-15 21:11:01 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr Index innerStride() const {
|
2010-02-18 20:42:38 -05:00
|
|
|
return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
|
|
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2024-11-15 21:11:01 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr Index outerStride() const {
|
2010-02-18 20:42:38 -05:00
|
|
|
return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
|
2017-09-26 10:53:33 +02:00
|
|
|
: internal::traits<Map>::OuterStrideAtCompileTime != Dynamic
|
|
|
|
|
? Index(internal::traits<Map>::OuterStrideAtCompileTime)
|
2017-08-22 13:27:37 +02:00
|
|
|
: IsVectorAtCompileTime ? (this->size() * innerStride())
|
|
|
|
|
: int(Flags) & RowMajorBit ? (this->cols() * innerStride())
|
|
|
|
|
: (this->rows() * innerStride());
|
2010-02-18 20:42:38 -05:00
|
|
|
}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-02-26 21:29:04 -05:00
|
|
|
/** Constructor in the fixed-size case.
|
|
|
|
|
*
|
2012-12-24 13:33:22 +01:00
|
|
|
* \param dataPtr pointer to the array to map
|
2015-06-09 13:32:12 +02:00
|
|
|
* \param stride optional Stride object, passing the strides.
|
2010-02-26 21:29:04 -05:00
|
|
|
*/
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr explicit inline Map(PointerArgType dataPtr, const StrideType& stride = StrideType())
|
2015-06-09 13:32:12 +02:00
|
|
|
: Base(cast_to_pointer_type(dataPtr)), m_stride(stride) {}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-02-26 21:29:04 -05:00
|
|
|
/** Constructor in the dynamic-size vector case.
|
|
|
|
|
*
|
2012-12-24 13:33:22 +01:00
|
|
|
* \param dataPtr pointer to the array to map
|
2015-06-09 13:32:12 +02:00
|
|
|
* \param size the size of the vector expression
|
|
|
|
|
* \param stride optional Stride object, passing the strides.
|
2010-02-26 21:29:04 -05:00
|
|
|
*/
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr inline Map(PointerArgType dataPtr, Index size, const StrideType& stride = StrideType())
|
2015-06-09 13:32:12 +02:00
|
|
|
: Base(cast_to_pointer_type(dataPtr), size), m_stride(stride) {}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-02-26 21:29:04 -05:00
|
|
|
/** Constructor in the dynamic-size matrix case.
|
|
|
|
|
*
|
2012-12-24 13:33:22 +01:00
|
|
|
* \param dataPtr pointer to the array to map
|
2015-06-09 13:32:12 +02:00
|
|
|
* \param rows the number of rows of the matrix expression
|
|
|
|
|
* \param cols the number of columns of the matrix expression
|
|
|
|
|
* \param stride optional Stride object, passing the strides.
|
2010-02-26 21:29:04 -05:00
|
|
|
*/
|
2026-02-24 19:59:10 -08:00
|
|
|
EIGEN_DEVICE_FUNC constexpr inline Map(PointerArgType dataPtr, Index rows, Index cols,
|
|
|
|
|
const StrideType& stride = StrideType())
|
2015-06-09 13:32:12 +02:00
|
|
|
: Base(cast_to_pointer_type(dataPtr), rows, cols), m_stride(stride) {}
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2008-06-27 01:22:35 +00:00
|
|
|
EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
|
2023-11-29 11:12:48 +00:00
|
|
|
|
2010-02-18 20:42:38 -05:00
|
|
|
protected:
|
|
|
|
|
StrideType m_stride;
|
2008-06-27 01:22:35 +00:00
|
|
|
};
|
2007-12-19 08:14:00 +00:00
|
|
|
|
2012-04-15 11:06:28 +01:00
|
|
|
} // end namespace Eigen
|
2007-10-14 14:45:31 +00:00
|
|
|
|
2007-12-19 09:30:53 +00:00
|
|
|
#endif // EIGEN_MAP_H
|