diff --git a/main.cpp b/main.cpp index 5ea87b8..e65fd86 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,7 @@ #include #include -#include #include -#include +#include #define pt cout << #define pter cerr << @@ -17,9 +16,9 @@ unsigned long allocatedMem = 0; void *alloc(size_t size) { void *ptr = malloc(size); - if (ptr == nullptr) { + if (!ptr) { pter "Memory allocation failed" << nl; - return nullptr; + return NULL; } allocatedMem += 1; return ptr; @@ -36,6 +35,52 @@ bool assert_alloc() { // -------- END RAII HELPERS-------- + +// -------- START SIMPLE VEC IMPLEMENTATION -------- +template +class myVec { + T *data; + size_t capacity; + size_t length; + + void resize() { + if (length == capacity || !length) { + capacity = capacity ? capacity << 1 : 1; + T *newData = static_cast(alloc(capacity * sizeof(T))); + for (size_t i = 0; i < length; i++) { + newData[i] = data[i]; + } + dealloc(data); + data = newData; + } + } + +public: + myVec() { + capacity = 0; + length = 0; + } + + void push_back(const T &val) { + resize(); + data[length++] = val; + } + + void pop_back() { + if (length) { + length--; + } + } + + T &operator[](size_t index) { + return data[index]; + } + int size() { + return length; + } +}; + +// -------- END SIMPLE VEC IMPLEMENTATION -------- struct Move { char player; int moveX; @@ -45,7 +90,7 @@ struct Move { struct Moves { int x; int y; - vector moves; + myVec moves; }; bool isValidPlayer(char c) { @@ -301,7 +346,7 @@ void solve(Moves moves, char **board) { int init(char *fileName) { // --- IO START --- FILE *inp = fopen(fileName, "r"); - if (inp == nullptr) { + if (!inp) { pter "File not found" << nl; return 1; }