psana_test/include/flattenNdarray.h

Go to the documentation of this file.
00001 #ifndef PSANA_TEST_FLATTENNDARRAY_H
00002 #define PSANA_TEST_FLATTENNDARRAY_H
00003 
00004 #include "ndarray/ndarray.h"
00005 #include <vector>
00006 #include <stdexcept>
00007 
00008 namespace psana_test {
00009 
00010 template <class T, unsigned R>
00011 void flatten(const ndarray<const T,R> &a, std::vector<T> &v) {
00012   throw std::runtime_error("flatten( ndarray<const T,R>, vector<T> &) called for unsupported R value");
00013 }
00014 
00015 template <class T>
00016 void flatten(const ndarray<const T,1> &a, std::vector<T> &v) {
00017   unsigned n = a.shape()[0];
00018   v.resize(n);
00019   for (unsigned idx = 0; idx < n; idx ++ )  {
00020     v[idx] = a[idx];
00021   }
00022 }
00023 
00024 template <class T>
00025 void flatten(const ndarray<const T,2> &a, std::vector<T> &v) {
00026   unsigned n = a.shape()[0] * a.shape()[1];
00027   v.resize(n);
00028   unsigned idx = 0;
00029   for (unsigned i = 0; i < a.shape()[0]; ++i )  {
00030     for (unsigned j = 0; j < a.shape()[1]; ++j)  {
00031       v[idx++] = a[i][j];
00032     }
00033   }
00034 }
00035 
00036 template <class T>
00037 void flatten(const ndarray<const T,3> &a, std::vector<T> &v) {
00038   unsigned n = a.shape()[0] * a.shape()[1] * a.shape()[2];
00039   v.resize(n);
00040   unsigned idx = 0;
00041   for (unsigned i = 0; i < a.shape()[0]; ++i )  {
00042     for (unsigned j = 0; j < a.shape()[1]; ++j)  {
00043       for (unsigned k = 0; k < a.shape()[2]; ++k)  {
00044         v[idx++] = a[i][j][k];
00045       }
00046     }
00047   }
00048 }
00049 
00050 template <class T>
00051 void flatten(const ndarray<const T,4> &a, std::vector<T> &v) {
00052   unsigned n = a.shape()[0] * a.shape()[1] * a.shape()[2] * a.shape()[3];
00053   v.resize(n);
00054   unsigned idx = 0;
00055   for (unsigned i = 0; i < a.shape()[0]; ++i )  {
00056     for (unsigned j = 0; j < a.shape()[1]; ++j)  {
00057       for (unsigned k = 0; k < a.shape()[2]; ++k)  {
00058         for (unsigned l = 0; l < a.shape()[3]; ++l)  {
00059           v[idx++] = a[i][j][k][l];
00060         }
00061       }
00062     }
00063   }
00064 }
00065 
00066 template <class T>
00067 void flatten(const ndarray<const T,5> &a, std::vector<T> &v) {
00068   unsigned n = a.shape()[0] * a.shape()[1] * a.shape()[2] * a.shape()[3] * a.shape()[4];
00069   v.resize(n);
00070   unsigned idx = 0;
00071   for (unsigned i = 0; i < a.shape()[0]; ++i )  {
00072     for (unsigned j = 0; j < a.shape()[1]; ++j)  {
00073       for (unsigned k = 0; k < a.shape()[2]; ++k)  {
00074         for (unsigned l = 0; l < a.shape()[3]; ++l)  {
00075           for (unsigned m = 0; m < a.shape()[4]; ++m)  {
00076             v[idx++] = a[i][j][k][l][m];
00077           }
00078         }
00079       }
00080     }
00081   }
00082 }
00083 
00084 template <class T>
00085 void flatten(const ndarray<const T,6> &a, std::vector<T> &v) {
00086   unsigned n = a.shape()[0] * a.shape()[1] * a.shape()[2] * a.shape()[3] * a.shape()[4] * a.shape()[5];
00087   v.resize(n);
00088   unsigned idx = 0;
00089   for (unsigned i = 0; i < a.shape()[0]; ++i )  {
00090     for (unsigned j = 0; j < a.shape()[1]; ++j)  {
00091       for (unsigned k = 0; k < a.shape()[2]; ++k)  {
00092         for (unsigned l = 0; l < a.shape()[3]; ++l)  {
00093           for (unsigned m = 0; m < a.shape()[4]; ++m)  {
00094             for (unsigned n = 0; n < a.shape()[5]; ++n)  {
00095               v[idx++] = a[i][j][k][l][m][n];
00096             }
00097           }
00098         }
00099       }
00100     }
00101   }
00102 }
00103 
00104 }; // namespace psana_test
00105 
00106 #endif

Generated on 19 Dec 2016 for PSDMSoftware by  doxygen 1.4.7