This commit is contained in:
Ilusha 2023-05-28 01:16:30 +03:00 committed by IlushaShurupov
parent d4c558a59a
commit db05d963be
74 changed files with 4473 additions and 3231 deletions

122
Utils/public/Debugging.hpp Normal file
View file

@ -0,0 +1,122 @@
#pragma once
#include "Environment.hpp"
#include "Map.hpp"
#define MAX_CALL_DEPTH_CAPTURE 16
#define MAX_CALL_CAPTURES_MEM_SIZE_MB 16
#define MAX_DEBUG_INFO_LEN 63
#define FRAMES_TO_SKIP_START 2
#define FRAMES_TO_SKIP_END 3
namespace tp {
class CallStackCapture {
public:
typedef tp::alni FramePointer;
class CallStack {
friend CallStackCapture;
FramePointer frames[MAX_CALL_DEPTH_CAPTURE];
public:
[[nodiscard]] ualni getDepth() const;
class Iterator {
const FramePointer* mFrame;
public:
explicit Iterator(const FramePointer* frame) : mFrame(frame) {};
FramePointer getFrame() { return *mFrame; }
bool operator==(const Iterator& in) const { return in.mFrame == mFrame; }
void operator++() { mFrame++; }
const Iterator& operator*() const { return *this; }
};
[[nodiscard]] Iterator begin() const { return Iterator(frames + FRAMES_TO_SKIP_START); }
[[nodiscard]] Iterator end() const { return Iterator(frames + FRAMES_TO_SKIP_START + getDepth()); }
};
class DebugSymbols {
friend CallStackCapture;
char function[MAX_DEBUG_INFO_LEN + 1] = { 0 };
char file[MAX_DEBUG_INFO_LEN + 1] = { 0 };
ualni line = 0;
public:
[[nodiscard]] const char* getFunc() const { return function; }
[[nodiscard]] const char* getFile() const { return file; }
[[nodiscard]] ualni getLine() const { return line; }
};
public:
CallStackCapture();
~CallStackCapture();
[[nodiscard]] const CallStack* getSnapshot();
const DebugSymbols* getSymbols(FramePointer fp);
public:
template<class Saver>
void write(Saver& file) {
file.write(mBuffLoad);
for (auto cs : *this) {
file.write(cs.getCallStack()->getDepth());
for (auto frame : *cs.getCallStack()) {
file.write((ualni) frame.getFrame());
}
}
file.write(mSymbols);
}
// independent of the configuration
template<class Loader>
void read(Loader& file) {
clear();
ualni loadLen;
file.read(loadLen);
for (auto cs = loadLen; cs; cs--) {
ualni callStackLen;
file.read(callStackLen);
for (auto fp = callStackLen; fp; fp--) {
// --
}
}
}
public:
class Iterator {
const CallStack* mSnapshot;
public:
explicit Iterator(const CallStack* start) : mSnapshot(start) {};
const CallStack* getCallStack() { return mSnapshot; }
bool operator==(const Iterator& in) const { return in.mSnapshot == mSnapshot; }
void operator++() { mSnapshot++; }
const Iterator& operator*() const { return *this; }
};
[[nodiscard]] Iterator begin() const { return Iterator(mBuff); }
[[nodiscard]] Iterator end() const { return Iterator(mBuff + mBuffLoad); }
private:
struct CallStackKey {
CallStack* cs;
bool operator==(const CallStackKey& in) const;
};
static void platformWriteStackTrace(CallStack* stack);
static void platformWriteDebugSymbols(FramePointer frame, DebugSymbols* out);
[[nodiscard]] static ualni hashCallStack(CallStackKey key);
void clear();
private:
ualni mBuffLen;
ualni mBuffLoad;
CallStack* mBuff;
Map<CallStackKey, CallStack*, DefaultAllocator, hashCallStack> mSnapshots;
Map<FramePointer, DebugSymbols> mSymbols;
};
extern CallStackCapture* gCSCapture;
}

97
Utils/public/Sorting.hpp Normal file
View file

@ -0,0 +1,97 @@
#pragma once
#include "Utils.hpp"
namespace tp {
template <typename Type>
inline bool compare(const Type& val1, const Type& val2) {
return val1 > val2;
}
struct SortMerge {
template <typename Type>
static void sort(Type* buff, int length, bool (*grater)(const Type& obj1, const Type& obj2) = &compare) {
mergeSort(buff, 0, length - 1, grater);
}
private:
template <typename Type>
static void merge(Type* buff, int left, int middle, int right, bool (*grater)(const Type& obj1, const Type& obj2)) {
int n1 = middle - left + 1;
int n2 = right - middle;
Type* Left = new Type[n1];
Type* Right = new Type[n2];
for (int i = 0; i < n1; i++) {
Left[i] = buff[left + i];
}
for (int j = 0; j < n2; j++) {
Right[j] = buff[middle + 1 + j];
}
int i = 0;
int j = 0;
int k = left;
while (i < n1 && j < n2) {
if (!(grater(Left[i], Right[j]))) {
buff[k] = Left[i];
i++;
} else {
buff[k] = Right[j];
j++;
}
k++;
}
while (i < n1) {
buff[k] = Left[i];
i++;
k++;
}
while (j < n2) {
buff[k] = Right[j];
j++;
k++;
}
delete[] Left;
delete[] Right;
}
template <typename Type>
static void mergeSort(Type* buff, int left, int right, bool (*grater)(const Type& obj1, const Type& obj2)) {
if (left >= right) {
return;
}
int middle = left + (right - left) / 2;
mergeSort(buff, left, middle, grater);
mergeSort(buff, middle + 1, right, grater);
merge(buff, left, middle, right, grater);
}
};
struct SortInsert {
template <typename Type>
static void sort(Type* buff, int length, bool (*grater)(const Type& obj1, const Type& obj2) = &compare) {
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (grater(*buff[i], *buff[j])) {
swap(buff[i], buff[j]);
}
}
}
}
};
}

63
Utils/public/Testing.hpp Normal file
View file

@ -0,0 +1,63 @@
#pragma once
#include "List.hpp"
namespace tp {
class Testing {
public:
struct FailedCheck {
const char* expression = nullptr;
const char* file = nullptr;
ualni line = 0;
};
Testing() = default;
void startTest(const char* name);
void endTest();
void addFailedCheck(const FailedCheck& info);
void reportState();
void setRootName(const char* name);
[[nodiscard]] bool hasFailed();
private:
struct TestingNode {
List<FailedCheck> mFailedChecks;
List<TestingNode*> mSubTests;
const char* mName = "Unnamed";
TestingNode* mParent = nullptr;
bool mHasFailed = false;
void report(const char* path = nullptr) const;
void updateState();
~TestingNode();
};
TestingNode mRootTest;
TestingNode* mCurrent = &mRootTest;
};
extern Testing gTesting;
}
#define TEST_DEF(Name)\
static void Name##FunctorBody();\
void test##Name() { \
tp::gTesting.startTest(#Name);\
Name##FunctorBody();\
tp::gTesting.endTest();\
} \
void Name##FunctorBody()
#define TEST_DEF_STATIC(Name)\
static void Name##FunctorBody();\
static void test##Name() { \
tp::gTesting.startTest(#Name);\
Name##FunctorBody();\
tp::gTesting.endTest();\
} \
void Name##FunctorBody()
#define TEST(expr) if (!(expr)) tp::gTesting.addFailedCheck({ #expr, __FILE__, __LINE__ })
#define TEST_EQUAL(l, r) if (!((l) == (r))) tp::gTesting.addFailedCheck({ #l" == "#r, __FILE__, __LINE__ })

58
Utils/public/Timing.hpp Normal file
View file

@ -0,0 +1,58 @@
#pragma once
#include "Environment.hpp"
namespace tp {
typedef alni time_ms;
typedef alni time_ns;
extern time_ms gCurrentTime;
class Timer {
time_ms mStart;
time_ms mDuration;
public:
Timer();
explicit Timer(time_ms time);
time_ms start();
time_ms duration();
void setDuration(time_ms dur);
bool isTimeout();
void reset();
time_ms timePassed();
time_ms remainder();
void wait();
float easeIn(time_ms duration = 0);
float easeOut(time_ms duration = 0);
};
void sleep(time_ms duration);
time_ms get_time();
struct FpsCounter {
halni frames = 0;
Timer time;
halni fps = 0;
FpsCounter() : time(1000) {}
void update(bool log = true) {
frames++;
if (time.isTimeout()) {
fps = frames;
if (log) {
// printf("fps %i \n", fps);
}
frames = 0;
time.reset();
}
}
};
}

79
Utils/public/Utils.hpp Normal file
View file

@ -0,0 +1,79 @@
#pragma once
#include "BaseModule.hpp"
#define PTR_OFFSET(first, offset) (*((&first) + offset))
#define MEMBER_OFFSET(s, m) (alni(&(((s*)0)->m)))
namespace tp {
extern ModuleManifest gModuleUtils;
void memsetv(void* p, uhalni bytesize, uint1 val);
void memcp(void* left, const void* right, uhalni len);
int1 memequal(const void* left, const void* right, uhalni len);
}
namespace tp {
[[nodiscard]] alnf randf();
}
namespace tp {
template <typename T1, typename T2>
class Pair {
public:
Pair() {}
Pair(T1 t1, T2 t2) : head(t1), tail(t2) {}
union { T1 t1; T1 head; T1 x; };
union { T2 t2; T2 tail; T2 y; };
};
template <typename Type = alni>
class Bits {
Type mFlags = 0;
public:
Bits() = default;
explicit Bits(Type val) { mFlags = val; }
explicit Bits(bool val) { for (int bit = 0; bit < sizeof(Type); bit++) { set(bit, val); } }
bool get(int1 idx) { return mFlags & (1l << idx); }
void set(int1 idx, bool val) {
if (val) {
mFlags |= (1l << idx);
} else {
mFlags &= ~(1l << idx);
}
}
};
template<typename tType = ualni>
class Range {
public:
class Iterator {
public:
tType mIndex;
explicit Iterator(tType pStartIndex) : mIndex(pStartIndex) {}
tType index() const { return mIndex; }
inline void operator++() { mIndex++; }
inline operator tType() const { return mIndex; }
inline bool operator==(Iterator pIndex) { return mIndex == pIndex.mIndex; }
inline bool operator!=(Iterator pIndex) { return mIndex != pIndex.mIndex; }
inline const Iterator& operator*() { return *this; }
};
tType mBegin = 0;
tType mEnd = 0;
Range() = default;
explicit Range(tType pEndIndex) : mBegin(0), mEnd(pEndIndex) {}
Range(tType pStartIndex, tType pEndIndex) : mBegin(pStartIndex), mEnd(pEndIndex) {}
bool valid() { return mBegin < mEnd; }
tType idxBegin() const { return mBegin; }
tType idxEnd() const { return mEnd; }
Iterator begin() { return Iterator(mBegin); }
Iterator end() { return Iterator(mEnd); }
};
}