Archiver Initial

This commit is contained in:
IlyaShurupov 2023-07-25 18:47:00 +03:00 committed by Ilya Shurupov
parent 4a5877784b
commit 31e420b311
25 changed files with 652 additions and 124 deletions

View file

@ -20,7 +20,7 @@ namespace tp {
template<typename tType, ualni tSize>
class ConstSizeBuffer {
typedef SelCopyArg<tType> Arg;
typedef SelectValueOrReference<tType> Arg;
private:
tType mBuff[tSize];
@ -29,6 +29,12 @@ namespace tp {
public:
ConstSizeBuffer() = default;
~ConstSizeBuffer() {
for (auto i = 0; i < mLoad; i++) {
mBuff[i].~tType();
}
}
public:
[[nodiscard]] ualni size() const { return mLoad; }
[[nodiscard]] ualni getBuffSize() const { return tSize; }
@ -75,13 +81,18 @@ namespace tp {
mLoad--;
}
ConstSizeBuffer& operator=(const tp::init_list<tType>& init) {
ConstSizeBuffer& operator=(const tp::InitialierList<tType>& init) {
for (auto arg : init) {
append(arg);
}
return *this;
}
void clear() {
this->~ConstSizeBuffer();
new (this) ConstSizeBuffer();
}
public:
class IteratorPointer {
@ -125,6 +136,24 @@ namespace tp {
[[nodiscard]] Iterator end() const {
return Iterator(mBuff + mLoad);
}
public:
template<class tArchiver>
void archiveWrite(tArchiver& ar) const {
ar << mLoad;
for (auto item : *this) {
ar << item.data();
}
}
template<class tArchiver>
void archiveRead(tArchiver& ar) {
clear();
ar >> mLoad;
for (auto i = 0; i < mLoad; i++) {
ar >> mBuff[i];
}
}
};
template<
@ -135,7 +164,7 @@ namespace tp {
ualni tMinSize = 4
>
class Buffer {
typedef SelCopyArg<tType> Arg;
typedef SelectValueOrReference<tType> Arg;
private:
tType* mBuff;
@ -157,10 +186,14 @@ namespace tp {
for (ualni i = 0; i < mLoad; i++) new (&mBuff[i]) tType(in.mBuff[i]);
}
void clear() {
this->~Buffer();
new (this) Buffer();
}
Buffer& operator=(const Buffer& in) {
if (this == &in) return *this;
this->~Buffer();
new (this) Buffer(in);
clear();
return *this;
}
@ -219,7 +252,7 @@ namespace tp {
}
public:
Buffer& operator=(const tp::init_list<tType>& init) {
Buffer& operator=(const tp::InitialierList<tType>& init) {
// TODO : optimize
for (auto arg : init) {
append(arg);
@ -307,6 +340,28 @@ namespace tp {
return Iterator(mBuff + mLoad);
}
public:
template<class tArchiver>
void archiveWrite(tArchiver& ar) const {
ar << mLoad;
for (auto item : *this) {
ar << item.data();
}
}
template<class tArchiver>
void archiveRead(tArchiver& ar) {
clear();
decltype(mLoad) len;
ar >> len;
for (auto i = len; i; i--) {
// TODO : optimize
if (mLoad == mSize) resizeBuffer(tResizePolicy(mSize));
ar >> mBuff[mLoad];
mLoad++;
}
}
private:
void resizeBuffer(ualni newSize) {
DEBUG_ASSERT(newSize >= mLoad)

View file

@ -15,7 +15,7 @@ namespace tp {
public:
typedef ualni Index;
typedef Pair<Index, Index> Index2D;
typedef SelCopyArg<tType> tTypeArg;
typedef SelectValueOrReference<tType> tTypeArg;
private:
tAllocator mAlloc;
@ -40,6 +40,7 @@ namespace tp {
~Buffer2D() {
deleteBuffer();
mSize = { 0, 0 };
}
explicit Buffer2D(Index2D aSize) {
@ -55,17 +56,17 @@ namespace tp {
}
inline tType& get(const Index2D& at) {
DEBUG_ASSERT(at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0)
DEBUG_ASSERT(mBuff && at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0)
return *(mBuff + mSize.x * at.y + at.x);
}
inline const tType& get(const Index2D& at) const {
DEBUG_ASSERT(at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0)
DEBUG_ASSERT(mBuff && at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0)
return *(mBuff + mSize.x * at.y + at.x);
}
inline void set(const Index2D& at, tTypeArg value) {
DEBUG_ASSERT(at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0)
DEBUG_ASSERT(mBuff && at.x < mSize.x && at.y < mSize.y && at.x >= 0 && at.y >= 0)
*(mBuff + mSize.x * at.y + at.x) = value;
}
@ -77,10 +78,35 @@ namespace tp {
}
void assign(tType value) {
DEBUG_ASSERT(mBuff);
Index len = mSize.x * mSize.y;
for (Index i = 0; i < len; i++) {
mBuff[i] = value;
}
}
public:
template<class tArchiver>
void archiveWrite(tArchiver& ar) const {
ar << mSize;
for (auto i = 0; i < mSize.x; i++) {
for (auto j = 0; j < mSize.y; j++) {
ar << get(i, j);
}
}
}
template<class tArchiver>
void archiveRead(tArchiver& ar) {
decltype(mSize) size;
deleteBuffer();
ar >> size;
reserve(size);
for (auto i = 0; i < mSize.x; i++) {
for (auto j = 0; j < mSize.y; j++) {
ar >> get(i, j);
}
}
}
};
}

View file

@ -10,7 +10,7 @@ namespace tp {
template <typename Type, class Allocator = DefaultAllocator>
class List {
typedef SelCopyArg<Type> TypeArg;
typedef SelectValueOrReference<Type> TypeArg;
typedef ualni Index;
public:
@ -73,7 +73,7 @@ namespace tp {
List() = default;
List(const init_list<Type>& list) { operator=(list); }
List(const InitialierList<Type>& list) { operator=(list); }
[[nodiscard]] inline Node* first() const { return mFirst; }
[[nodiscard]] inline Node* last() const { return mLast; }
@ -221,7 +221,7 @@ namespace tp {
return *this;
}
List& operator+=(const init_list<Type>& list) {
List& operator+=(const InitialierList<Type>& list) {
for (auto item : list) {
pushBack(item);
}
@ -235,7 +235,7 @@ namespace tp {
return *this;
}
List& operator=(const init_list<Type>& list) {
List& operator=(const InitialierList<Type>& list) {
removeAll();
*this += list;
return *this;
@ -295,22 +295,23 @@ namespace tp {
}
}
template<class Saver>
void write(Saver& file) const {
file.write(mLength);
public:
template<class tArchiver>
void archiveWrite(tArchiver& ar) const {
ar << mLength;
for (auto item : *this) {
file.write(item.data());
ar << item.data();
}
}
template<class Loader>
void read(Loader& file) {
template<class tArchiver>
void archiveRead(tArchiver& ar) {
removeAll();
ualni len;
file.read(len);
decltype(mLength) len;
ar >> len;
for (auto i = len; i; i--) {
auto node = newNodeNotConstructed();
file.read(node->data);
ar >> node->data;
pushBack(node);
}
}

View file

@ -7,11 +7,11 @@
namespace tp {
template<typename Key>
ualni DefaultHashFunc(SelCopyArg<Key> key) {
ualni DefaultHashFunc(SelectValueOrReference<Key> key) {
return hash(key);
}
template<typename tKey, typename tVal, class tAllocator = DefaultAllocator, ualni(*tHashFunc)(SelCopyArg<tKey>) = DefaultHashFunc<tKey>, int tTableInitialSize = 4>
template<typename tKey, typename tVal, class tAllocator = DefaultAllocator, ualni(*tHashFunc)(SelectValueOrReference<tKey>) = DefaultHashFunc<tKey>, int tTableInitialSize = 4>
class Map {
enum {
@ -20,8 +20,8 @@ namespace tp {
MAP_MAX_LOAD_PERCENTAGE = 66,
};
typedef SelCopyArg<tKey> KeyArg;
typedef SelCopyArg<tVal> ValArg;
typedef SelectValueOrReference<tKey> KeyArg;
typedef SelectValueOrReference<tVal> ValArg;
public:
@ -35,7 +35,8 @@ namespace tp {
struct Idx {
alni idx = -1;
operator bool() { return idx != -1; }
[[nodiscard]] bool isValid() const { return bool(*this); }
explicit operator bool() const { return idx != -1; }
};
private:
@ -365,24 +366,24 @@ namespace tp {
return mNSlots;
}
template<class Saver>
void write(Saver& file) {
file.write(mNEntries);
template<class Archiver>
void archiveWrite(Archiver& ar) const {
ar << mNEntries;
for (auto item : *this) {
file.write(item->val);
file.write(item->key);
ar << item->val;
ar << item->key;
}
}
template<class Loader>
void read(Loader& file) {
template<class Archiver>
void archiveRead(Archiver& ar) {
removeAll();
ualni len;
file.read(len);
decltype(mNSlots) len;
ar >> len;
for (auto i = len; i; i--) {
auto node = newNodeNotConstructed();
file.read(node->val);
file.read(node->key);
ar >> node->val;
ar >> node->key;
put(node);
}
}

View file

@ -25,8 +25,8 @@ namespace tp {
template <typename Key, typename Data, class Allocator = DefaultAllocator>
class AvlTree {
typedef SelCopyArg<Key> KeyArg;
typedef SelCopyArg<Data> DataArg;
typedef SelectValueOrReference<Key> KeyArg;
typedef SelectValueOrReference<Data> DataArg;
public:
class Node {
@ -369,5 +369,32 @@ namespace tp {
}
bool isValid() { return findInvalidNode(head()) == nullptr; }
template<typename tFunctor>
void traverse(Node* node, bool after, tFunctor functor) {
if (!after) functor(node);
if (node->mLeft) traverse(node->mLeft, after, functor);
if (node->mRight) traverse(node->mRight, after, functor);
if (after) functor(node);
}
void removeAll() {
traverse(mRoot, true, [this](Node* node) {
deleteNode(node);
});
mRoot = nullptr;
mSize = 0;
}
public:
template<class tArchiver>
void archiveWrite(tArchiver& file) const {
FAIL("not implemented")
}
template<class tArchiver>
void archiveRead(tArchiver&) {
FAIL("not implemented")
}
};
}

View file

@ -2,6 +2,7 @@
#include "Tests.hpp"
#include "Testing.hpp"
#include "Archiver.hpp"
using namespace tp;
@ -53,18 +54,19 @@ TEST_DEF_STATIC(Copy) {
list2.removeAll();
}
TEST_DEF_STATIC(SaveLoad) {
TEST_DEF_STATIC(Serialization) {
tp::List<TestClass, HeapAlloc> list = { TestClass(1), TestClass(2), TestClass(3), TestClass(4) };
TestFile file;
ArchiverExample<1024, false> write;
ArchiverExample<1024, true> read;
list.write(file);
write % list;
list.removeAll();
file.setAddress(0);
memCopy(read.mBuff, write.mBuff, sizeof(write.mBuff));
list.read(file);
read % list;
ualni i = 0;
for (auto iter : list) {
@ -79,5 +81,5 @@ TEST_DEF_STATIC(SaveLoad) {
TEST_DEF(List) {
testSimplePointer();
testSimpleReference();
testSaveLoad();
testSerialization();
}

View file

@ -3,6 +3,7 @@
#include "Tests.hpp"
#include "Testing.hpp"
#include "Map.hpp"
#include "Archiver.hpp"
using namespace tp;
@ -100,17 +101,18 @@ TEST_DEF_STATIC(SaveLoad) {
map.put(i, TestClass(i));
}
TestFile file;
ArchiverExample<1024, false> write;
ArchiverExample<1024, true> read;
map.write(file);
write % map;
map.removeAll();
TEST(map.size() == 0);
file.setAddress(0);
memCopy(read.mBuff, write.mBuff, sizeof(write.mBuff));
map.read(file);
read % map;
TEST(map.size() == 10);

View file

@ -28,37 +28,6 @@ public:
void setVal(tp::ualni val) { val1 = val; }
};
class TestFile {
tp::ualni mem[1024] = { 0 };
tp::ualni address = 0;
public:
TestFile() = default;
template<typename Type>
void write(const Type& val) {
val.write(*this);
}
template<>
void write<tp::ualni>(const tp::ualni& val) {
mem[address] = val;
address++;
}
void setAddress(tp::ualni addr) { address = addr; }
template<typename Type>
void read(Type& val) {
val.read(*this);
}
template<>
void read<tp::ualni>(tp::ualni& val) {
val = mem[address];
address++;
}
};
void testList();
void testMap();