aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Core/PlainObjectBase.h
diff options
context:
space:
mode:
authorGravatar David Tellenbach <david.tellenbach@tellnotes.org>2019-01-21 16:25:57 +0100
committerGravatar David Tellenbach <david.tellenbach@tellnotes.org>2019-01-21 16:25:57 +0100
commitdb152b9ee6effd3799f70a621f495c427cb3c33f (patch)
tree25a9c96f0cdfbed28072ea7e2f8602412ce6de7f /Eigen/src/Core/PlainObjectBase.h
parent543529da6a1eabf415f4f8b56495fad76b57ba22 (diff)
PR 572: Add initializer list constructors to Matrix and Array (include unit tests and doc)
- {1,2,3,4,5,...} for fixed-size vectors only - {{1,2,3},{4,5,6}} for the general cases - {{1,2,3,4,5,....}} is allowed for both row and column-vector
Diffstat (limited to 'Eigen/src/Core/PlainObjectBase.h')
-rw-r--r--Eigen/src/Core/PlainObjectBase.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/Eigen/src/Core/PlainObjectBase.h b/Eigen/src/Core/PlainObjectBase.h
index f551dabb0..1a996b0aa 100644
--- a/Eigen/src/Core/PlainObjectBase.h
+++ b/Eigen/src/Core/PlainObjectBase.h
@@ -526,6 +526,77 @@ class PlainObjectBase : public internal::dense_xpr_base<Derived>::type
// EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
}
+ #ifdef EIGEN_PARSED_BY_DOXYGEN
+ /**
+ * \brief Construct a vector with fixed number of rows or a rowvector with fixed number of
+ * columns by passing an initializer list \cpp11
+ *
+ * \only_for_vectors
+ *
+ * \warning To construct a vector or rowvector of fixed size, the number of values passed through
+ * the initializer list must match the the fixed number of rows in the vector case or
+ * the fixed number of columns in the rowvector case. */
+ EIGEN_DEVICE_FUNC
+ explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list<Scalar>& list);
+
+ /**
+ * \brief Constructs a Matrix or Array and initializes it by elements given by an initializer list of initializer
+ * lists \cpp11 */
+ EIGEN_DEVICE_FUNC
+ explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list<std::initializer_list<Scalar>>& list);
+ #else // EIGEN_PARSED_BY_DOXYGEN
+ #if EIGEN_HAS_CXX11
+ template<typename T>
+ EIGEN_DEVICE_FUNC
+ explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list<T>& list,
+ typename internal::enable_if<internal::is_same<T, Scalar>::value, T>::type* = 0,
+ typename internal::enable_if<RowsAtCompileTime != Dynamic
+ && ColsAtCompileTime != Dynamic
+ && IsVectorAtCompileTime == 1, T>::type* = 0)
+ : m_storage()
+ {
+ _check_template_params();
+ EIGEN_STATIC_ASSERT_FIXED_SIZE(PlainObjectBase);
+ resize(list.size());
+ std::copy(list.begin(), list.end(), m_storage.data());
+ }
+
+ EIGEN_DEVICE_FUNC
+ explicit EIGEN_STRONG_INLINE PlainObjectBase(const std::initializer_list<std::initializer_list<Scalar>>& list)
+ : m_storage()
+ {
+ _check_template_params();
+
+ size_t list_size = 0;
+ if (list.begin() != list.end()) {
+ list_size = list.begin()->size();
+ }
+
+ // This is to allow syntax like VectorXi {{1, 2, 3, 4}}
+ if (ColsAtCompileTime == 1 && list.size() == 1) {
+ eigen_assert(list_size == static_cast<size_t>(RowsAtCompileTime) || RowsAtCompileTime == Dynamic);
+ resize(list_size, ColsAtCompileTime);
+ std::copy(list.begin()->begin(), list.begin()->end(), m_storage.data());
+ } else {
+ eigen_assert(list.size() == static_cast<size_t>(RowsAtCompileTime) || RowsAtCompileTime == Dynamic);
+ eigen_assert(list_size == static_cast<size_t>(ColsAtCompileTime) || ColsAtCompileTime == Dynamic);
+ resize(list.size(), list_size);
+
+ Index row_index = 0;
+ for (const std::initializer_list<Scalar>& row : list) {
+ eigen_assert(list_size == row.size());
+ Index col_index = 0;
+ for (const Scalar& e : row) {
+ coeffRef(row_index, col_index) = e;
+ ++col_index;
+ }
+ ++row_index;
+ }
+ }
+ }
+ #endif // end EIGEN_HAS_CXX11
+ #endif // end EIGEN_PARSED_BY_DOXYGEN
+
/** \sa PlainObjectBase::operator=(const EigenBase<OtherDerived>&) */
template<typename OtherDerived>
EIGEN_DEVICE_FUNC