Buffer 2d (No tests)
This commit is contained in:
parent
ffa90323a4
commit
0ec44ee2d1
4 changed files with 97 additions and 1 deletions
67
Containers/public/Buffer2D.hpp
Normal file
67
Containers/public/Buffer2D.hpp
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
|
||||
#include "Buffer.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
template< typename tType, ualni tSizeX, ualni tSizeY >
|
||||
using ConstSizeBuffer2D = ConstSizeBuffer<ConstSizeBuffer<tType, tSizeX>, tSizeY>;
|
||||
|
||||
template <typename tType>
|
||||
class Buffer2D {
|
||||
typedef SelCopyArg<tType> tTypeArg;
|
||||
typedef Pair<uhalni, uhalni> Size;
|
||||
|
||||
Size mSize;
|
||||
tType* mBuff = nullptr;
|
||||
|
||||
public:
|
||||
|
||||
Buffer2D() = default;
|
||||
|
||||
~Buffer2D() {
|
||||
delete mBuff;
|
||||
}
|
||||
|
||||
explicit Buffer2D(Pair<ualni, ualni> aSize) {
|
||||
reserve(aSize);
|
||||
}
|
||||
|
||||
[[nodiscard]] Pair<ualni, ualni> 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;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
27
Containers/tests/Buffer2DTest.cpp
Normal file
27
Containers/tests/Buffer2DTest.cpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
|
||||
#include "Tests.hpp"
|
||||
#include "Testing.hpp"
|
||||
|
||||
#include "Buffer2D.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace tp;
|
||||
|
||||
const ualni size = 1000;
|
||||
|
||||
TEST_DEF_STATIC(Simple1) {
|
||||
Buffer2D<int> 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();
|
||||
}
|
||||
|
|
@ -37,6 +37,7 @@ int main() {
|
|||
testMap();
|
||||
testAvl();
|
||||
testBuffer();
|
||||
testBuffer2d();
|
||||
|
||||
testModule.deinitialize();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,4 +71,5 @@ public:
|
|||
void testList();
|
||||
void testMap();
|
||||
void testAvl();
|
||||
void testBuffer();
|
||||
void testBuffer();
|
||||
void testBuffer2d();
|
||||
Loading…
Add table
Add a link
Reference in a new issue