From facc17ec1be9ed558b16e205c529eacfb8913eb3 Mon Sep 17 00:00:00 2001 From: IlushaShurupov Date: Sat, 15 Jul 2023 13:25:10 +0300 Subject: [PATCH] Buffer 2d (No tests) --- Containers/public/Buffer2D.hpp | 67 +++++++++++++++++++++++++++++++ Containers/tests/Buffer2DTest.cpp | 27 +++++++++++++ Containers/tests/Tests.cpp | 1 + Containers/tests/Tests.hpp | 3 +- 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 Containers/public/Buffer2D.hpp create mode 100644 Containers/tests/Buffer2DTest.cpp diff --git a/Containers/public/Buffer2D.hpp b/Containers/public/Buffer2D.hpp new file mode 100644 index 0000000..064be52 --- /dev/null +++ b/Containers/public/Buffer2D.hpp @@ -0,0 +1,67 @@ + +#include "Buffer.hpp" + +namespace tp { + + template< typename tType, ualni tSizeX, ualni tSizeY > + using ConstSizeBuffer2D = ConstSizeBuffer, tSizeY>; + + template + class Buffer2D { + typedef SelCopyArg tTypeArg; + typedef Pair Size; + + Size mSize; + tType* mBuff = nullptr; + + public: + + Buffer2D() = default; + + ~Buffer2D() { + delete mBuff; + } + + explicit Buffer2D(Pair aSize) { + reserve(aSize); + } + + [[nodiscard]] Pair size() const { + return { mSize.x, mSize.y }; + } + + tType* getBuff() const { + return mBuff; + } + + inline tType& get(uhalni x, uhalni y) { + DEBUG_ASSERT(x < mSize.x && y < mSize.y && x >= 0 && y >= 0) + return *(mBuff + mSize.x * y + x); + } + + inline const tType& get(uhalni x, uhalni y) const { + DEBUG_ASSERT(x < mSize.x && y < mSize.y && x >= 0 && y >= 0) + return *(mBuff + mSize.x * y + x); + } + + inline void set(uhalni x, uhalni y, tTypeArg value) { + DEBUG_ASSERT(x < mSize.x && y < mSize.y && x >= 0 && y >= 0) + *(mBuff + mSize.x * y + x) = value; + } + + void reserve(Size newSize) { + if (mSize.x != newSize.x || mSize.y != newSize.y) { + delete mBuff; + mBuff = new tType[newSize.x * newSize.y]; + mSize = newSize; + } + } + + void assign(tType value) { + uhalni len = mSize.x * mSize.y; + for (uhalni i = 0; i < len; i++) { + mBuff[i] = value; + } + } + }; +} \ No newline at end of file diff --git a/Containers/tests/Buffer2DTest.cpp b/Containers/tests/Buffer2DTest.cpp new file mode 100644 index 0000000..9a667e2 --- /dev/null +++ b/Containers/tests/Buffer2DTest.cpp @@ -0,0 +1,27 @@ + +#include "Tests.hpp" +#include "Testing.hpp" + +#include "Buffer2D.hpp" + +#include + +using namespace tp; + +const ualni size = 1000; + +TEST_DEF_STATIC(Simple1) { + Buffer2D buff; + buff.reserve({ 4, 4 }); + buff.set( 2, 2, 5); + TEST(buff.get(2, 2) == 5); +} + +TEST_DEF_STATIC(Simple2) { + // TEST(false); +} + +TEST_DEF(Buffer2d) { + testSimple1(); + testSimple2(); +} \ No newline at end of file diff --git a/Containers/tests/Tests.cpp b/Containers/tests/Tests.cpp index 25a659d..a5de12e 100644 --- a/Containers/tests/Tests.cpp +++ b/Containers/tests/Tests.cpp @@ -37,6 +37,7 @@ int main() { testMap(); testAvl(); testBuffer(); + testBuffer2d(); testModule.deinitialize(); } diff --git a/Containers/tests/Tests.hpp b/Containers/tests/Tests.hpp index 98d2c71..084d6bc 100644 --- a/Containers/tests/Tests.hpp +++ b/Containers/tests/Tests.hpp @@ -71,4 +71,5 @@ public: void testList(); void testMap(); void testAvl(); -void testBuffer(); \ No newline at end of file +void testBuffer(); +void testBuffer2d(); \ No newline at end of file