add custom vec impl
This commit is contained in:
parent
8ff786df97
commit
1843fd06de
57
main.cpp
57
main.cpp
@ -1,8 +1,7 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <cstdio>
|
||||
|
||||
#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<typename T>
|
||||
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<T *>(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<Move> moves;
|
||||
myVec<Move> 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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user