Archiver Initial
This commit is contained in:
parent
01ba8160ef
commit
3575cbc543
25 changed files with 652 additions and 124 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue