diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/containers/2d_array | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/containers/2d_array')
-rw-r--r-- | library/cpp/containers/2d_array/2d_array.cpp | 1 | ||||
-rw-r--r-- | library/cpp/containers/2d_array/2d_array.h | 125 | ||||
-rw-r--r-- | library/cpp/containers/2d_array/ya.make | 9 |
3 files changed, 135 insertions, 0 deletions
diff --git a/library/cpp/containers/2d_array/2d_array.cpp b/library/cpp/containers/2d_array/2d_array.cpp new file mode 100644 index 0000000000..03115c7e2f --- /dev/null +++ b/library/cpp/containers/2d_array/2d_array.cpp @@ -0,0 +1 @@ +#include "2d_array.h" diff --git a/library/cpp/containers/2d_array/2d_array.h b/library/cpp/containers/2d_array/2d_array.h new file mode 100644 index 0000000000..9e24650637 --- /dev/null +++ b/library/cpp/containers/2d_array/2d_array.h @@ -0,0 +1,125 @@ +#pragma once + +#include <util/system/yassert.h> +#include <util/generic/algorithm.h> + +#ifdef _DEBUG +template <class T> +struct TBoundCheck { + T* Data; + size_t Size; + TBoundCheck(T* d, size_t s) { + Data = d; + Size = s; + } + T& operator[](size_t i) const { + Y_ASSERT(i >= 0 && i < Size); + return Data[i]; + } +}; +#endif + +template <class T> +class TArray2D { +private: + typedef T* PT; + T* Data; + T** PData; + size_t XSize; + size_t YSize; + +private: + void Copy(const TArray2D& a) { + XSize = a.XSize; + YSize = a.YSize; + Create(); + for (size_t i = 0; i < XSize * YSize; i++) + Data[i] = a.Data[i]; + } + void Destroy() { + delete[] Data; + delete[] PData; + } + void Create() { + Data = new T[XSize * YSize]; + PData = new PT[YSize]; + for (size_t i = 0; i < YSize; i++) + PData[i] = Data + i * XSize; + } + +public: + TArray2D(size_t xsize = 1, size_t ysize = 1) { + XSize = xsize; + YSize = ysize; + Create(); + } + TArray2D(const TArray2D& a) { + Copy(a); + } + TArray2D& operator=(const TArray2D& a) { + Destroy(); + Copy(a); + return *this; + } + ~TArray2D() { + Destroy(); + } + void SetSizes(size_t xsize, size_t ysize) { + if (XSize == xsize && YSize == ysize) + return; + Destroy(); + XSize = xsize; + YSize = ysize; + Create(); + } + void Clear() { + SetSizes(1, 1); + } +#ifdef _DEBUG + TBoundCheck<T> operator[](size_t i) const { + Y_ASSERT(i < YSize); + return TBoundCheck<T>(PData[i], XSize); + } +#else + T* operator[](size_t i) const { + Y_ASSERT(i < YSize); + return PData[i]; + } +#endif + size_t GetXSize() const { + return XSize; + } + size_t GetYSize() const { + return YSize; + } + void FillZero() { + memset(Data, 0, sizeof(T) * XSize * YSize); + } + void FillEvery(const T& a) { + for (size_t i = 0; i < XSize * YSize; i++) + Data[i] = a; + } + void Swap(TArray2D& a) { + std::swap(Data, a.Data); + std::swap(PData, a.PData); + std::swap(XSize, a.XSize); + std::swap(YSize, a.YSize); + } +}; + +template <class T> +inline bool operator==(const TArray2D<T>& a, const TArray2D<T>& b) { + if (a.GetXSize() != b.GetXSize() || a.GetYSize() != b.GetYSize()) + return false; + for (size_t y = 0; y < a.GetYSize(); ++y) { + for (size_t x = 0; x < a.GetXSize(); ++x) + if (a[y][x] != b[y][x]) + return false; + } + return true; +} + +template <class T> +inline bool operator!=(const TArray2D<T>& a, const TArray2D<T>& b) { + return !(a == b); +} diff --git a/library/cpp/containers/2d_array/ya.make b/library/cpp/containers/2d_array/ya.make new file mode 100644 index 0000000000..71d56b902f --- /dev/null +++ b/library/cpp/containers/2d_array/ya.make @@ -0,0 +1,9 @@ +LIBRARY() + +OWNER(kirillovs) + +SRCS( + 2d_array.cpp +) + +END() |