BackProp Fixes

This commit is contained in:
IlyaShurupov 2023-10-25 11:03:15 +03:00
parent efbb09f8ba
commit 0542b7ba2e
11 changed files with 189 additions and 106 deletions

View file

@ -182,10 +182,26 @@ namespace tp {
mBuff = (tType*) mAllocator.allocate(sizeof(tType) * tMinSize);
}
explicit Buffer(ualni size) :
mSize(size),
mLoad(0) {
Buffer(const InitialierList<tType>& input) {
mSize = 0;
for (const auto& val : input) {
mSize++;
}
mBuff = (tType*) mAllocator.allocate(sizeof(tType) * mSize);
mLoad = 0;
for (const auto& val : input) {
mBuff[mLoad] = val;
mLoad++;
}
}
explicit Buffer(ualni size) {
mBuff = (tType*) mAllocator.allocate(sizeof(tType) * size);
mSize = size;
mLoad = size;
for (ualni i = 0; i < mLoad; i++) {
new (mBuff + i) tType();
}
}
Buffer(const Buffer& in) :
@ -274,11 +290,25 @@ namespace tp {
}
public:
Buffer& operator=(const tp::InitialierList<tType>& init) {
Buffer& operator=(const tp::InitialierList<tType>& input) {
// TODO : optimize
for (auto arg : init) {
append(arg);
for (ualni i = 0; i < mLoad; i++) {
mBuff[i].~tType();
}
mAllocator.deallocate(mBuff);
mSize = 0;
for (const auto& val : input) {
mSize++;
}
mBuff = (tType*) mAllocator.allocate(sizeof(tType) * mSize);
mLoad = 0;
for (const auto& val : input) {
mBuff[mLoad] = val;
mLoad++;
}
return *this;
}
@ -320,10 +350,10 @@ namespace tp {
}
void reserve(ualni aSize) {
if (aSize == mSize) return;
for (ualni i = 0; i < mLoad; i++) {
mBuff[i].~tType();
}
mAllocator.deallocate(mBuff);
mBuff = (tType*) mAllocator.allocate(sizeof(tType) * aSize);
mSize = aSize;
mLoad = aSize;

View file

@ -22,10 +22,10 @@ TEST_DEF_STATIC(Simple1) {
TEST_DEF_STATIC(Simple2) {
Buffer<TestClass, HeapAlloc> buff(size);
TEST(buff.size() == 0);
TEST(buff.size() == size);
for (auto i : Range(size * 10))
buff.append(TestClass(i));
TEST(buff.size() == size * 10);
TEST(buff.size() == size + size * 10);
while (buff.size())
buff.pop();
TEST(buff.size() == 0);

View file

@ -8,6 +8,8 @@ class TestClass {
tp::ualni val1;
public:
TestClass() { val1 = 0; }
explicit TestClass(tp::ualni val) :
val1(val) {}