// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2014 Benoit Steiner // // 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/. #ifndef EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H #define EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H #if EIGEN_HAS_VARIADIC_TEMPLATES #include namespace Eigen { /** \class TensorInitializer * \ingroup CXX11_Tensor_Module * * \brief Helper template to initialize Tensors from std::initializer_lists. */ namespace internal { template struct Initializer { typedef std::initializer_list< typename Initializer::InitList> InitList; static void run(TensorEvaluator& tensor, Eigen::array::Index, traits::NumDimensions>* indices, const InitList& vals) { int i = 0; for (auto v : vals) { (*indices)[traits::NumDimensions - N] = i++; Initializer::run(tensor, indices, v); } } }; template struct Initializer { typedef std::initializer_list::Scalar> InitList; static void run(TensorEvaluator& tensor, Eigen::array::Index, traits::NumDimensions>* indices, const InitList& vals) { int i = 0; // There is likely a faster way to do that than iterating. for (auto v : vals) { (*indices)[traits::NumDimensions - 1] = i++; tensor.coeffRef(*indices) = v; } } }; template struct Initializer { typedef typename traits::Scalar InitList; static void run(TensorEvaluator& tensor, Eigen::array::Index, traits::NumDimensions>*, const InitList& v) { tensor.coeffRef(0) = v; } }; template void initialize_tensor(TensorEvaluator& tensor, const typename Initializer::NumDimensions>::InitList& vals) { Eigen::array::Index, traits::NumDimensions> indices; Initializer::NumDimensions>::run(tensor, &indices, vals); } } // namespace internal } // namespace Eigen #endif // EIGEN_HAS_VARIADIC_TEMPLATES #endif // EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H