From 2ea9252c531a253176cfc18e44476d927fae22ea Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Fri, 11 Oct 2024 13:55:22 -0400 Subject: [PATCH] reduce use of `std` --- main.cpp | 109 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 46 deletions(-) diff --git a/main.cpp b/main.cpp index 11fdd93..ef43d79 100644 --- a/main.cpp +++ b/main.cpp @@ -3,16 +3,66 @@ #include #include -#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 +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 > *a, int i, int j) { - pair > temp = a[i]; +void swap(myPair > *a, int i, int j) { + myPair > temp = a[i]; a[i] = a[j]; a[j] = temp; } -void bubbleSort(pair > *a) { +void bubbleSort(myPair > *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 > *a) { void printScores(int *points, char blacklist) { - pair > *arr = static_cast > *>(alloc( - 26 * sizeof(pair >))); + myPair > *arr = static_cast > *>(alloc( + 26 * sizeof(myPair >))); for (int i = 0; i < 26; i++) { - pair > p; - pair p1; + myPair > p; + myPair p1; p1.first = points[i]; p1.second = false; p.first = static_cast(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) {