TensorInitializer.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H
00011 #define EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H
00012 
00013 #if EIGEN_HAS_VARIADIC_TEMPLATES
00014 
00015 #include <initializer_list>
00016 
00017 namespace Eigen {
00018 
00024 namespace internal {
00025 
00026 template <typename Derived, int N>
00027 struct Initializer {
00028   typedef std::initializer_list<
00029     typename Initializer<Derived, N - 1>::InitList> InitList;
00030 
00031   static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
00032                   Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>* indices,
00033                   const InitList& vals) {
00034     int i = 0;
00035     for (auto v : vals) {
00036       (*indices)[traits<Derived>::NumDimensions - N] = i++;
00037       Initializer<Derived, N - 1>::run(tensor, indices, v);
00038     }
00039   }
00040 };
00041 
00042 template <typename Derived>
00043 struct Initializer<Derived, 1> {
00044   typedef std::initializer_list<typename traits<Derived>::Scalar> InitList;
00045 
00046   static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
00047                   Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>* indices,
00048                   const InitList& vals) {
00049     int i = 0;
00050     // There is likely a faster way to do that than iterating.
00051     for (auto v : vals) {
00052       (*indices)[traits<Derived>::NumDimensions - 1] = i++;
00053       tensor.coeffRef(*indices) = v;
00054     }
00055   }
00056 };
00057 
00058 template <typename Derived>
00059 struct Initializer<Derived, 0> {
00060   typedef typename traits<Derived>::Scalar InitList;
00061 
00062   static void run(TensorEvaluator<Derived, DefaultDevice>& tensor,
00063                   Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions>*,
00064                   const InitList& v) {
00065     tensor.coeffRef(0) = v;
00066   }
00067 };
00068 
00069 
00070 template <typename Derived, int N>
00071 void initialize_tensor(TensorEvaluator<Derived, DefaultDevice>& tensor,
00072                        const typename Initializer<Derived, traits<Derived>::NumDimensions>::InitList& vals) {
00073   Eigen::array<typename traits<Derived>::Index, traits<Derived>::NumDimensions> indices;
00074   Initializer<Derived, traits<Derived>::NumDimensions>::run(tensor, &indices, vals);
00075 }
00076 
00077 }  // namespace internal
00078 }  // namespace Eigen
00079 
00080 #endif  // EIGEN_HAS_VARIADIC_TEMPLATES
00081 
00082 #endif  // EIGEN_CXX11_TENSOR_TENSOR_INITIALIZER_H
 All Classes Functions Variables Typedefs Enumerator