2d_array.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include <util/system/yassert.h>
  3. #include <util/generic/algorithm.h>
  4. #ifdef _DEBUG
  5. template <class T>
  6. struct TBoundCheck {
  7. T* Data;
  8. size_t Size;
  9. TBoundCheck(T* d, size_t s) {
  10. Data = d;
  11. Size = s;
  12. }
  13. T& operator[](size_t i) const {
  14. Y_ASSERT(i >= 0 && i < Size);
  15. return Data[i];
  16. }
  17. };
  18. #endif
  19. template <class T>
  20. class TArray2D {
  21. private:
  22. typedef T* PT;
  23. T* Data;
  24. T** PData;
  25. size_t XSize;
  26. size_t YSize;
  27. private:
  28. void Copy(const TArray2D& a) {
  29. XSize = a.XSize;
  30. YSize = a.YSize;
  31. Create();
  32. for (size_t i = 0; i < XSize * YSize; i++)
  33. Data[i] = a.Data[i];
  34. }
  35. void Destroy() {
  36. delete[] Data;
  37. delete[] PData;
  38. }
  39. void Create() {
  40. Data = new T[XSize * YSize];
  41. PData = new PT[YSize];
  42. for (size_t i = 0; i < YSize; i++)
  43. PData[i] = Data + i * XSize;
  44. }
  45. public:
  46. TArray2D(size_t xsize = 1, size_t ysize = 1) {
  47. XSize = xsize;
  48. YSize = ysize;
  49. Create();
  50. }
  51. TArray2D(const TArray2D& a) {
  52. Copy(a);
  53. }
  54. TArray2D& operator=(const TArray2D& a) {
  55. Destroy();
  56. Copy(a);
  57. return *this;
  58. }
  59. ~TArray2D() {
  60. Destroy();
  61. }
  62. void SetSizes(size_t xsize, size_t ysize) {
  63. if (XSize == xsize && YSize == ysize)
  64. return;
  65. Destroy();
  66. XSize = xsize;
  67. YSize = ysize;
  68. Create();
  69. }
  70. void Clear() {
  71. SetSizes(1, 1);
  72. }
  73. #ifdef _DEBUG
  74. TBoundCheck<T> operator[](size_t i) const {
  75. Y_ASSERT(i < YSize);
  76. return TBoundCheck<T>(PData[i], XSize);
  77. }
  78. #else
  79. T* operator[](size_t i) const {
  80. Y_ASSERT(i < YSize);
  81. return PData[i];
  82. }
  83. #endif
  84. size_t GetXSize() const {
  85. return XSize;
  86. }
  87. size_t GetYSize() const {
  88. return YSize;
  89. }
  90. void FillZero() {
  91. memset(Data, 0, sizeof(T) * XSize * YSize);
  92. }
  93. void FillEvery(const T& a) {
  94. for (size_t i = 0; i < XSize * YSize; i++)
  95. Data[i] = a;
  96. }
  97. void Swap(TArray2D& a) {
  98. std::swap(Data, a.Data);
  99. std::swap(PData, a.PData);
  100. std::swap(XSize, a.XSize);
  101. std::swap(YSize, a.YSize);
  102. }
  103. };
  104. template <class T>
  105. inline bool operator==(const TArray2D<T>& a, const TArray2D<T>& b) {
  106. if (a.GetXSize() != b.GetXSize() || a.GetYSize() != b.GetYSize())
  107. return false;
  108. for (size_t y = 0; y < a.GetYSize(); ++y) {
  109. for (size_t x = 0; x < a.GetXSize(); ++x)
  110. if (a[y][x] != b[y][x])
  111. return false;
  112. }
  113. return true;
  114. }
  115. template <class T>
  116. inline bool operator!=(const TArray2D<T>& a, const TArray2D<T>& b) {
  117. return !(a == b);
  118. }