Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorInitializer.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H
0011 #define EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H
0012 
0013 #if EIGEN_HAS_VARIADIC_TEMPLATES
0014 
0015 #include <initializer_list>
0016 
0017 namespace Eigen {
0018 
0019 /** \class TensorInitializer
0020   * \ingroup CXX11_Tensor_Module
0021   *
0022   * \brief Helper template to initialize Tensors from std::initializer_lists.
0023   */
0024 namespace internal {
0025 
0026 template <typename Derived, int N>
0027 struct Initializer {
0028   typedef std::initializer_list<
0029     typename Initializer<Derived, N - 1>::InitList> InitList;
0030 
0031   static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
0032                   Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>* indices,
0033                   const InitList& vals) {
0034     int i = 0;
0035     for (const auto& v : vals) {
0036       (*indices)[traits<Derived>::NumDimensions - N] = i++;
0037       Initializer<Derived, N - 1>::run(tensor, indices, v);
0038     }
0039   }
0040 };
0041 
0042 template <typename Derived>
0043 struct Initializer<Derived, 1> {
0044   typedef std::initializer_list<typename traits<Derived>::Scalar> InitList;
0045 
0046   static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
0047                   Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>* indices,
0048                   const InitList& vals) {
0049     int i = 0;
0050     // There is likely a faster way to do that than iterating.
0051     for (const auto& v : vals) {
0052       (*indices)[traits<Derived>::NumDimensions - 1] = i++;
0053       tensor.coeffRef(*indices) = v;
0054     }
0055   }
0056 };
0057 
0058 template <typename Derived>
0059 struct Initializer<Derived, 0> {
0060   typedef typename traits<Derived>::Scalar InitList;
0061 
0062   static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
0063                   Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>*,
0064                   const InitList& v) {
0065     tensor.coeffRef(0) = v;
0066   }
0067 };
0068 
0069 
0070 template <typename Derived, int N>
0071 void initialize_tensor(TensorEvaluator<Derived, DefaultDevice>& tensor,
0072                        const typename Initializer<Derived, traits<Derived>::NumDimensions>::InitList& vals) {
0073   Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions> indices;
0074   Initializer<Derived, traits<Derived>::NumDimensions>::run(tensor, &indices, vals);
0075 }
0076 
0077 }  // namespace internal
0078 }  // namespace Eigen
0079 
0080 #endif  // EIGEN_HAS_VARIADIC_TEMPLATES
0081 
0082 #endif  // EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H