IODA Bundle
RowSlice.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020 NOAA/NWS/NCEP/EMC
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #pragma once
9 
10 #include <Eigen/Dense>
11 #include <string>
12 #include <vector>
13 
14 
15 /// Collection of template methods that are used to slice array and vector data.
16 
17 /// \brief Slice eigen array using indicies of eigen arrays.
18 template<class EigenType, class EigenIdxType>
19 EigenType rowSlice(const EigenType& arr, const EigenIdxType& idxVec)
20 {
21  EigenType result(idxVec.rows(), arr.cols());
22 
23  for (size_t rowIdx = 0; rowIdx < idxVec.rows(); rowIdx++)
24  {
25  result.row(rowIdx) = arr.row(idxVec.at(rowIdx)(0));
26  }
27 
28  return result;
29 }
30 
31 /// \brief Slice eigen array using std::vector of indicies.
32 template<class EigenType, typename IdxType>
33 EigenType rowSlice(const EigenType& arr, const std::vector<IdxType>& idxVec)
34 {
35  EigenType result(idxVec.size(), arr.cols());
36 
37  for (size_t rowIdx = 0; rowIdx < idxVec.size(); rowIdx++)
38  {
39  result.row(rowIdx) = arr.row(static_cast<Eigen::Index>(idxVec[rowIdx]));
40  }
41 
42  return result;
43 }
44 
45 /// \brief Slice string vector using indicies of eigen arrays.
46 template<class EigenIdxType>
47 std::vector<std::string> rowSlice(const std::vector<std::string>& arr,
48  const EigenIdxType& idxVec)
49 {
50  std::vector<std::string> result;
51  result.resize(idxVec.rows());
52 
53  for (Eigen::Index rowIdx = 0; rowIdx < idxVec.rows(); rowIdx++)
54  {
55  result[rowIdx] = arr[idxVec.at(rowIdx)(0)];
56  }
57 
58  return result;
59 }
60 
61 /// \brief Slice string vector using std::vector of indicies.
62 template<typename IdxType>
63 std::vector<std::string> rowSlice(const std::vector<std::string>& arr,
64  const std::vector<IdxType>& idxVec)
65 {
66  std::vector<std::string> result;
67  result.resize(idxVec.size());
68 
69  for (Eigen::Index rowIdx = 0; rowIdx < idxVec.size(); rowIdx++)
70  {
71  result[rowIdx] = arr[static_cast<Eigen::Index>(idxVec[rowIdx])];
72  }
73 
74  return result;
75 }
76 
EigenType rowSlice(const EigenType &arr, const EigenIdxType &idxVec)
Collection of template methods that are used to slice array and vector data.
Definition: RowSlice.h:19