OSFI-C++  3.9.2
OpenSF Integration Library
DynamicArray.h
1 /*
2  * openSF Integration Libraries (OSFI)
3  * Deimos Space, S.L.U.
4  *
5  * This file is part of OSFI. OSFI is free software; you can redistribute it
6  * and/or modify it under the terms of the 'ESA Software Community Licence Permissive' as
7  * published by the European Space Agency; either version 2.4 of the License,
8  * or (at your option) any later version. You should have received a
9  * copy of the 'ESA Software Community Licence Permissive - v2.4' along with this program
10  * or one can be found at <http://eop-cfi.esa.int/index.php/docs-and-mission-data/licensing-documents>.
11  *
12  */
13 #ifndef _DYNAMIC_ARRAY_
14 #define _DYNAMIC_ARRAY_
15 
16 #include <vector>
17 #include <exception>
18 #include <stdexcept>
19 
20 #include "ConFM/base.h"
21 
26 template<typename T> class DynamicArray
27 {
28 public:
29 
34 
41  DynamicArray(int rows, int columns){
42  for (int i = 0; i < rows; ++i)
43  {
44  data.push_back(std::vector<T> (columns));
45  }
46  }
47 
53  OSFI_DEPRECATED("Use operator(i,j) instead to access elements")
54  const std::vector<T> & operator [](int i) const {
55  return data[i];
56  }
57 
65  typename std::vector<T>::reference operator ()(int i, int j) {
66  return data[i][j];
67  }
68 
76  typename std::vector<T>::const_reference operator() (int i, int j) const {
77  return data[i][j];
78  }
79 
87  typename std::vector<T>::reference at(int i, int j){
88  return checkIndices(this->data, i, j);
89  }
90 
98  typename std::vector<T>::const_reference at(int i, int j) const {
99  return checkIndices(this->data, i, j);
100  }
101 
107  void resize(int rows, int columns){
108  data.resize(rows);
109  for (int i = 0; i < rows; ++i)
110  data[i].resize(columns);
111  }
112 
117  int getRows() const{
118  return data.size();
119  }
120 
125  int getColumns() const{
126  if (data.size() > 0)
127  return data[0].size();
128  else
129  return 0;
130  }
131 
132 private:
133 
137  std::vector<std::vector<T>> data;
138 
140  template<typename C>
141  static auto checkIndices(C& vec, int i, int j) -> decltype(vec[i][j]) {
142  try{
143  auto& row = vec.at(i);
144  try{
145  return row.at(j);
146  } catch(...){
147  std::throw_with_nested(std::out_of_range("While accessing column" + std::to_string(j)));
148  }
149  } catch(const std::nested_exception&) {
150  throw; // Forward the inner exception
151  } catch(...){
152  std::throw_with_nested(std::out_of_range("While accessing row" + std::to_string(i)));
153  }
154  }
155 
156 };
157 
166 template<typename T>
167 bool operator == (const DynamicArray<T> &y, const DynamicArray<T> &x){
168  if (x.getRows() != y.getRows() || x.getColumns() != y.getColumns())
169  return false; // dimensions don't match
170  for (int i = 0; i < x.getRows(); i++){
171  for(int j = 0; j < x.getColumns(); j++){
172  if (x(i,j) != y(i,j)) // Mismatch at (i,j), no need to check any further
173  return false;
174  }
175  }
176  return true;
177 }
178 
179 template<typename T>
180 bool operator != (const DynamicArray<T> &y, const DynamicArray<T> &x){
181  return !(x==y);
182 }
183 
184 #endif // _DYNAMIC_ARRAY_
DynamicArray::resize
void resize(int rows, int columns)
Definition: DynamicArray.h:107
DynamicArray::getColumns
int getColumns() const
Definition: DynamicArray.h:125
DynamicArray::DynamicArray
DynamicArray(int rows, int columns)
Definition: DynamicArray.h:41
DynamicArray::getRows
int getRows() const
Definition: DynamicArray.h:117
DynamicArray::at
std::vector< T >::reference at(int i, int j)
Definition: DynamicArray.h:87
DynamicArray
Definition: DynamicArray.h:26
DynamicArray::at
std::vector< T >::const_reference at(int i, int j) const
Definition: DynamicArray.h:98
DynamicArray::operator()
std::vector< T >::reference operator()(int i, int j)
Definition: DynamicArray.h:65
DynamicArray::DynamicArray
DynamicArray()
Definition: DynamicArray.h:33
OSFI_DEPRECATED
#define OSFI_DEPRECATED(reason)
Definition: CLP.h:26