MUQ  0.4.3
VectorSlice.h
Go to the documentation of this file.
1 #ifndef VECTORSLICE_H
2 #define VECTORSLICE_H
3 
4 #include <vector>
5 #include <Eigen/Core>
6 
7 namespace muq {
8 namespace Utilities {
9 
23  template<typename VecType, typename ScalarType>
25  {
26  public:
27 
29 
31  data = vec.data;
32  startInd = vec.startInd;
33  endInd = vec.endInd;
34  skip = vec.skip;
35  return *this;
36  }
37 
53  VectorSlice(VecType& dataIn,
54  int startIndIn,
55  int endIndIn,
56  int skipIn=1) : data(dataIn),
57  startInd(startIndIn),
58  endInd(endIndIn),
59  skip(skipIn)
60  {
61  assert(skip!=0);
62  assert(startInd>=0);
63  assert(endInd>=-1);
64  if(skip>0){
65  assert(endInd>startInd);
66  }else{
67  assert(endInd<startInd);
68  }
69  }
70 
71  ScalarType& operator()(int i) {
72  CheckBounds(i);
73  return data[startInd + i*skip];
74  }
75 
76  ScalarType operator()(int i) const {
77  CheckBounds(i);
78  return data[startInd + i*skip];
79  }
80 
81  ScalarType& operator[](int i) {
82  return data[startInd + i*skip];
83  }
84 
85  ScalarType operator[](int i) const {
86  return data[startInd + i*skip];
87  }
88 
89  unsigned int size() const{
90  return std::ceil( double(std::abs(endInd-startInd)) / double(std::abs(skip)) );
91  };
92 
93 
94  VecType& data;
96 
97  void CheckBounds(int i) const {
98  if(skip>0){
99  assert(startInd + i*skip < endInd);
100  }else{
101  assert(startInd + i*skip > endInd);
102  }
103  }
104 
105  };
106 
107 
108  template<typename ScalarType>
109  VectorSlice<std::vector<ScalarType>, ScalarType> GetSlice(std::vector<ScalarType>& dataIn,
110  int startIndIn,
111  int endIndIn,
112  int skipIn=1)
113  {
114  return VectorSlice<std::vector<ScalarType>,ScalarType>(dataIn, startIndIn, endIndIn, skipIn);
115  }
116 
117  template<typename ScalarType>
118  VectorSlice<Eigen::Matrix<ScalarType,Eigen::Dynamic,1>, ScalarType> GetSlice(Eigen::Matrix<ScalarType,Eigen::Dynamic,1>& dataIn,
119  int startIndIn,
120  int endIndIn,
121  int skipIn=1)
122  {
123  return VectorSlice<Eigen::Matrix<ScalarType,Eigen::Dynamic,1>,ScalarType>(dataIn, startIndIn, endIndIn, skipIn);
124  }
125 
126  template<typename VectorType, typename ScalarType>
128  int startIndIn,
129  int endIndIn,
130  int skipIn=1)
131  {
133  dataIn.startInd + dataIn.skip*startIndIn,
134  std::max(dataIn.startInd + dataIn.skip*endIndIn,-1),
135  dataIn.skip * skipIn);
136 
137  }
138 
139 }
140 }
141 
142 
143 #endif // #ifndef VECTORSLICE_H
Enables a subset of a vector to be easily accessed or reversed without copying memory.
Definition: VectorSlice.h:25
ScalarType operator()(int i) const
Definition: VectorSlice.h:76
void CheckBounds(int i) const
Definition: VectorSlice.h:97
VectorSlice< VecType, ScalarType > & operator=(VectorSlice< VecType, ScalarType > const &vec)
Definition: VectorSlice.h:30
unsigned int size() const
Definition: VectorSlice.h:89
VectorSlice(VectorSlice< VecType, ScalarType > const &vec)
Definition: VectorSlice.h:28
VectorSlice(VecType &dataIn, int startIndIn, int endIndIn, int skipIn=1)
Definition: VectorSlice.h:53
ScalarType & operator[](int i)
Definition: VectorSlice.h:81
ScalarType & operator()(int i)
Definition: VectorSlice.h:71
ScalarType operator[](int i) const
Definition: VectorSlice.h:85
VectorSlice< std::vector< ScalarType >, ScalarType > GetSlice(std::vector< ScalarType > &dataIn, int startIndIn, int endIndIn, int skipIn=1)
Definition: VectorSlice.h:109