SHARE
TWEET

Untitled

a guest Aug 25th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <assert.h>
  2.  
  3. template <typename T>
  4. class Arr2D
  5. {
  6. public:
  7.     template <typename... Args>
  8.     Arr2D(uint16_t size_x, uint16_t size_y, Args&&... args);
  9.     ~Arr2D();
  10.  
  11.     inline uint16_t getSizeX() const { return msizex; };
  12.     inline uint16_t getSizeY() const { return msizey; };
  13.  
  14.     inline T* getData() const { return values; };
  15.     inline uint32_t getElementCount() const { return msizex * msizey; }
  16.     inline size_t getDataSize() const { return getElementCount() * sizeof(T); }
  17.  
  18.     void set(uint16_t x, uint16_t y, T value);
  19.     T get(uint16_t x, uint16_t y) const;
  20.     T& at(uint16_t x, uint16_t y);
  21.     const T& constAt(uint16_t x, uint16_t y) const;
  22.  
  23. private:
  24.     const uint16_t msizex, msizey;
  25.     T* values;
  26. };
  27.  
  28. template <typename T>
  29. template <typename... Args>
  30. Arr2D<T>::Arr2D(uint16_t size_x, uint16_t size_y, Args&&... args) :
  31.     msizex(size_x),
  32.     msizey(size_y)
  33. {
  34.     values = static_cast<T*>(operator new(getDataSize()));
  35.     for (uint32_t i = 0; i < getElementCount(); i++)
  36.         new (values + i) T(args...);
  37. }
  38.  
  39. template <typename T>
  40. Arr2D<T>::~Arr2D()
  41. {
  42.     for (uint32_t i = 0; i < getElementCount(); i++)
  43.         (values + i)->~T();
  44.     operator delete(values, getDataSize());
  45. }
  46.  
  47. template <typename T>
  48. void Arr2D<T>::set(uint16_t x, uint16_t y, T value)
  49. {
  50.     assert(x < msizex);
  51.     assert(y < msizey);
  52.     values[x + y * msizex] = value;
  53. }
  54.  
  55. template <typename T>
  56. T Arr2D<T>::get(uint16_t x, uint16_t y) const
  57. {
  58.     assert(x < msizex);
  59.     assert(y < msizey);
  60.     return values[x + y * msizex];
  61. }
  62.  
  63. template <typename T>
  64. T& Arr2D<T>::at(uint16_t x, uint16_t y)
  65. {
  66.     assert(x < msizex);
  67.     assert(y < msizey);
  68.     return values[x + y * msizex];
  69. }
  70.  
  71. template <typename T>
  72. const T& Arr2D<T>::constAt(uint16_t x, uint16_t y) const
  73. {
  74.     assert(x < msizex);
  75.     assert(y < msizey);
  76.     return values[x + y * msizex];
  77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top