OpenPose  1.7.0
The first real-time multi-person system to jointly detect human body, hand, facial, and foot keypoints
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
array.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_CORE_ARRAY_HPP
2 #define OPENPOSE_CORE_ARRAY_HPP
3 
4 #include <memory> // std::shared_ptr
5 #include <vector>
9 
10 namespace op
11 {
20  template<typename T>
21  class Array
22  {
23  public:
24  // ------------------------------ Constructors and Data Allocator Functions ------------------------------ //
31  explicit Array(const int size);
32 
39  explicit Array(const std::vector<int>& sizes = {});
40 
48  Array(const int size, const T value);
49 
57  Array(const std::vector<int>& sizes, const T value);
58 
66  Array(const int size, T* const dataPtr);
67 
75  Array(const std::vector<int>& sizes, T* const dataPtr);
76 
89  Array(const Array<T>& array, const int index, const bool noCopy = false);
90 
95  template<typename T2>
96  Array(const Array<T2>& array) :
97  Array{array.getSize()}
98  {
99  try
100  {
101  // Copy
102  for (auto i = 0u ; i < array.getVolume() ; i++)
103  pData[i] = T(array[i]);
104  }
105  catch (const std::exception& e)
106  {
107  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
108  }
109  }
110 
119  Array<T>(const Array<T>& array);
120 
127  Array<T>& operator=(const Array<T>& array);
128 
134  Array<T>(Array<T>&& array);
135 
143 
151  Array<T> clone() const;
152 
159  void reset(const int size);
160 
168  void reset(const std::vector<int>& sizes = {});
169 
177  void reset(const int size, const T value);
178 
187  void reset(const std::vector<int>& sizes, const T value);
188 
196  void reset(const int size, T* const dataPtr);
197 
205  void reset(const std::vector<int>& sizes, T* const dataPtr);
206 
212  void setFrom(const Matrix& cvMat);
213 
219  void setTo(const T value);
220 
221 
222 
223  // ------------------------------ Data Information Functions ------------------------------ //
228  inline bool empty() const
229  {
230  return (mVolume == 0);
231  }
232 
238  inline std::vector<int> getSize() const
239  {
240  return mSize;
241  }
242 
249  int getSize(const int index) const;
250 
256  std::string printSize() const;
257 
262  inline size_t getNumberDimensions() const
263  {
264  return mSize.size();
265  }
266 
272  inline size_t getVolume() const
273  {
274  return mVolume;
275  }
276 
286  size_t getVolume(const int indexA, const int indexB = -1) const;
287 
293  std::vector<int> getStride() const;
294 
299  int getStride(const int index) const;
300 
301 
302 
303  // ------------------------------ Data Access Functions And Operators ------------------------------ //
310  inline T* getPtr()
311  {
312  return pData; // spData.get()
313  }
314 
319  inline const T* getConstPtr() const
320  {
321  return pData; // spData.get()
322  }
323 
329  inline T* getPseudoConstPtr() const
330  {
331  return pData; // spData.get()
332  }
333 
346  const Matrix& getConstCvMat() const;
347 
356 
365  inline T& operator[](const int index)
366  {
367  #ifdef NDEBUG
368  return pData[index]; // spData.get()[index]
369  #else
370  return at(index);
371  #endif
372  }
373 
381  inline const T& operator[](const int index) const
382  {
383  #ifdef NDEBUG
384  return pData[index]; // spData.get()[index]
385  #else
386  return at(index);
387  #endif
388  }
389 
398  inline T& operator[](const std::vector<int>& indexes)
399  {
400  return operator[](getIndex(indexes));
401  }
402 
410  inline const T& operator[](const std::vector<int>& indexes) const
411  {
412  return operator[](getIndex(indexes));
413  }
414 
422  inline T& at(const int index)
423  {
424  return commonAt(index);
425  }
426 
434  inline const T& at(const int index) const
435  {
436  return commonAt(index);
437  }
438 
446  inline T& at(const std::vector<int>& indexes)
447  {
448  return at(getIndexAndCheck(indexes));
449  }
450 
458  inline const T& at(const std::vector<int>& indexes) const
459  {
460  return at(getIndexAndCheck(indexes));
461  }
462 
475  const std::string toString() const;
476 
477  private:
478  std::vector<int> mSize;
479  size_t mVolume;
480  std::shared_ptr<T> spData;
481  T* pData; // pData is a wrapper of spData. Used for Pybind11 binding.
482  std::pair<bool, Matrix> mCvMatData;
483 
491  int getIndex(const std::vector<int>& indexes) const;
492 
500  int getIndexAndCheck(const std::vector<int>& indexes) const;
501 
507  T& commonAt(const int index) const;
508 
509  void resetAuxiliary(const std::vector<int>& sizes, T* const dataPtr = nullptr);
510  };
511 
512  // Static methods
514 }
515 
516 #endif // OPENPOSE_CORE_ARRAY_HPP
void reset(const std::vector< int > &sizes={})
const T & operator[](const int index) const
Definition: array.hpp:381
void reset(const int size)
void setTo(const T value)
void reset(const std::vector< int > &sizes, T *const dataPtr)
std::vector< int > getStride() const
std::string printSize() const
Array(const Array< T > &array, const int index, const bool noCopy=false)
std::vector< int > getSize() const
Definition: array.hpp:238
Array(const std::vector< int > &sizes={})
const T & at(const int index) const
Definition: array.hpp:434
Matrix & getCvMat()
size_t getVolume(const int indexA, const int indexB=-1) const
size_t getNumberDimensions() const
Definition: array.hpp:262
T & at(const int index)
Definition: array.hpp:422
Array(const int size)
T * getPseudoConstPtr() const
Definition: array.hpp:329
T & at(const std::vector< int > &indexes)
Definition: array.hpp:446
Array(const std::vector< int > &sizes, T *const dataPtr)
Array(const std::vector< int > &sizes, const T value)
const Matrix & getConstCvMat() const
Array< T > & operator=(const Array< T > &array)
Array(const int size, T *const dataPtr)
void setFrom(const Matrix &cvMat)
bool empty() const
Definition: array.hpp:228
T & operator[](const int index)
Definition: array.hpp:365
T & operator[](const std::vector< int > &indexes)
Definition: array.hpp:398
int getStride(const int index) const
Array< T > clone() const
int getSize(const int index) const
const T & operator[](const std::vector< int > &indexes) const
Definition: array.hpp:410
const T * getConstPtr() const
Definition: array.hpp:319
void reset(const int size, const T value)
Array(const int size, const T value)
void reset(const std::vector< int > &sizes, const T value)
void reset(const int size, T *const dataPtr)
Array< T > & operator=(Array< T > &&array)
const T & at(const std::vector< int > &indexes) const
Definition: array.hpp:458
size_t getVolume() const
Definition: array.hpp:272
T * getPtr()
Definition: array.hpp:310
const std::string toString() const
Array(const Array< T2 > &array)
Definition: array.hpp:96
#define OVERLOAD_C_OUT(className)
Definition: macros.hpp:77
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")