RideDistributor  0.0.1
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
Tensor.hpp
Go to the documentation of this file.
1 #ifndef TENSOR_H
2 #define TENSOR_H
3 
4 #include <string>
5 #include <memory>
6 #include <vector>
7 #include <sstream>
8 #include <algorithm>
9 
10 #ifndef NDEBUG
11 #include <iostream>
12 
13 #define DBG_PRINT_2D_TENSOR(s, m) std::cout << s << std::endl; \
14  for(unsigned r = 0; r < m.getDims()[0]; ++r){ \
15  for(unsigned c = 0; c < m.getDims()[1]; ++c){ \
16  std::cout << m(r, c); \
17  if(c < m.getDims()[1] - 1) std::cout << ", "; \
18  } \
19  std::cout << std::endl; \
20  }
21 
22 #define DBG_PRINT_1D_TENSOR(s, v) std::cout << s << std::endl; \
23  for(unsigned i = 0; i < v.getDims()[0]; ++i){ \
24  std::cout << v(i); \
25  if(i < v.getDims()[0] - 1) std::cout << ", "; \
26  } \
27  std::cout << std::endl
28 #else
29 #define DBG_PRINT_2D_TENSOR(_, _)
30 #define DBG_PRINT_1D_TENSOR(_, _)
31 #endif
32 
33 #define MAX_SHOWN_ENTRIES (unsigned)20
34 
35 
36 template<typename T>
37 class Tensor {
38 public:
39 
41  Tensor(): size(0) {};
42 
48  Tensor(const std::vector<unsigned> dims);
49 
56  Tensor(const std::vector<unsigned> dims, const std::vector<T> data);
57 
64  Tensor(const std::vector<unsigned> dims, const T& defaultValue);
65 
71  Tensor(const Tensor<T>& other);
72 
79  inline T& operator()(const int i) const;
80 
88  inline T& operator()(const int row, const int col) const;
89 
98  inline T& operator()(const int row, const int col, const int offset) const;
99 
109  Tensor<T>& operator=(const Tensor<T>& rhs);
110 
116  const unsigned getSize() const;
117 
123  const std::vector<unsigned> getDims() const;
124 
125 private:
126 
128  unsigned size;
129 
131  std::vector<unsigned> dims;
132 
134  std::shared_ptr<T[]> data;
135 
136 };
137 
138 #include "Tensor.tpp"
139 
140 #endif // TENSOR_H
Tensor()
Definition: Tensor.hpp:41
const unsigned getSize() const
Definition: Tensor.tpp:62
Tensor< T > & operator=(const Tensor< T > &rhs)
Definition: Tensor.tpp:54
T & operator()(const int i) const
Definition: Tensor.tpp:42
Definition: Tensor.hpp:37
const std::vector< unsigned > getDims() const
Definition: Tensor.tpp:66