reduce use of std

This commit is contained in:
Sandipsinh Rathod 2024-10-11 13:55:22 -04:00
parent 3d9ddad0b6
commit 2ea9252c53
No known key found for this signature in database

109
main.cpp

@ -3,16 +3,66 @@
#include <sstream>
#include <cstdio>
#define pt cout <<
#define pter cerr <<
#define pt std::cout <<
#define pter std::cerr <<
#define nl "\n"
using namespace std;
// TODO: drop pair
// remove global def of std
// TODO: drop myPair
// spam comments
// -------- START PAIR IMPL --------
template<typename T1, typename T2>
struct myPair {
T1 first;
T2 second;
myPair() : first(), second() {}
myPair(const T1 &first, const T2 &second) : first(first), second(second) {}
};
// -------- END PAIR IMPL --------
// -------- START HELPER FUNCTIONS --------
void swapChar(char &a, char &b) {
if (a == b) {
return;
}
a ^= b;
b ^= a;
a ^= b;
}
// Recursively reverse a string
void reverseString(string &str, int start, int end) {
if (start >= end) {
return;
}
swapChar(str[start], str[end]);
reverseString(str, start + 1, end - 1);
}
string toString(int x) {
if (!x) return "0";
string result;
while (x) {
result.push_back(x % 10 + '0');
x /= 10;
}
reverseString(result, 0, result.size() - 1);
return result;
}
int max(int a, int b) {
return a > b ? a : b;
}
// -------- END HELPER FUNCTIONS --------
// -------- START RAII HELPERS--------
unsigned long allocatedMem = 0;
@ -107,39 +157,6 @@ bool isValidPlayer(char c) {
return c >= 'A' && c <= 'Z' && c != 'X';
}
void swapChar(char &a, char &b) {
if (a == b) {
return;
}
a ^= b;
b ^= a;
a ^= b;
}
// Recursively reverse a string
void reverseString(string &str, int start, int end) {
if (start >= end) {
return;
}
swapChar(str[start], str[end]);
reverseString(str, start + 1, end - 1);
}
string toString(int x) {
if (!x) return "0";
string result;
while (x) {
result.push_back(x % 10 + '0');
x /= 10;
}
reverseString(result, 0, result.size() - 1);
return result;
}
Moves serialize(const string &s) {
Moves result;
istringstream iss(s);
@ -279,7 +296,7 @@ void printBoard(char **board, int x, int y) {
}
pt nl;
}
pt endl;
pt std::endl;
}
char toLowerCase(char c) {
@ -290,13 +307,13 @@ int normalize(char c) {
return toLowerCase(c) - 'a';
}
void swap(pair<char, pair<int, bool> > *a, int i, int j) {
pair<char, pair<int, bool> > temp = a[i];
void swap(myPair<char, myPair<int, bool> > *a, int i, int j) {
myPair<char, myPair<int, bool> > temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void bubbleSort(pair<char, pair<int, bool> > *a) {
void bubbleSort(myPair<char, myPair<int, bool> > *a) {
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25 - i; j++) {
if (a[j].second.first > a[j + 1].second.first)
@ -307,12 +324,12 @@ void bubbleSort(pair<char, pair<int, bool> > *a) {
void printScores(int *points, char blacklist) {
pair<char, pair<int, bool> > *arr = static_cast<pair<char, pair<int, bool> > *>(alloc(
26 * sizeof(pair<char, pair<int, bool> >)));
myPair<char, myPair<int, bool> > *arr = static_cast<myPair<char, myPair<int, bool> > *>(alloc(
26 * sizeof(myPair<char, myPair<int, bool> >)));
for (int i = 0; i < 26; i++) {
pair<char, pair<int, bool> > p;
pair<int, bool> p1;
myPair<char, myPair<int, bool> > p;
myPair<int, bool> p1;
p1.first = points[i];
p1.second = false;
p.first = static_cast<char>(i + 'A');
@ -349,7 +366,7 @@ void printScores(int *points, char blacklist) {
}
dealloc(arr);
pt endl;
pt std::endl;
}
bool isValidMove(int x, int y, char **board) {