change to namespaces

This commit is contained in:
Dennis Eichhorn 2022-09-19 18:57:59 +02:00
parent 04ab177b23
commit 9c0859daff
6 changed files with 116 additions and 96 deletions

View File

@ -14,50 +14,47 @@
#include <stdlib.h> #include <stdlib.h>
namespace Utils { namespace Utils {
class ArraySort { namespace ArraySort {
private: inline
void reverse_int(int *arr, int size)
public: {
static inline for (int low = 0, high = size - 1; low < high; ++low, --high) {
void reverse_int(int *arr, int size) int tmp = arr[low];
{ arr[low] = arr[high];
for (int low = 0, high = size - 1; low < high; ++low, --high) { arr[high] = tmp;
int tmp = arr[low];
arr[low] = arr[high];
arr[high] = tmp;
}
} }
}
static inline inline
void reverse_float(float *arr, int size) void reverse_float(float *arr, int size)
{ {
for (int low = 0, high = size - 1; low < high; ++low, --high) { for (int low = 0, high = size - 1; low < high; ++low, --high) {
float tmp = arr[low]; float tmp = arr[low];
arr[low] = arr[high]; arr[low] = arr[high];
arr[high] = tmp; arr[high] = tmp;
}
} }
}
static inline inline
void reverse_double(double *arr, int size) void reverse_double(double *arr, int size)
{ {
for (int low = 0, high = size - 1; low < high; ++low, --high) { for (int low = 0, high = size - 1; low < high; ++low, --high) {
double tmp = arr[low]; double tmp = arr[low];
arr[low] = arr[high]; arr[low] = arr[high];
arr[high] = tmp; arr[high] = tmp;
}
} }
}
static inline inline
void reverse_char(char *arr, int size) void reverse_char(char *arr, int size)
{ {
for (int low = 0, high = size - 1; low < high; ++low, --high) { for (int low = 0, high = size - 1; low < high; ++low, --high) {
char tmp = arr[low]; char tmp = arr[low];
arr[low] = arr[high]; arr[low] = arr[high];
arr[high] = tmp; arr[high] = tmp;
}
} }
}; }
}
} }
#endif #endif

View File

@ -45,6 +45,7 @@ namespace Utils {
return false; return false;
} }
}
} }
#endif #endif

View File

@ -113,6 +113,7 @@ namespace Utils {
return file; return file;
} }
}
} }
#endif #endif

View File

@ -136,6 +136,7 @@ namespace Utils {
return text_diff { diffValues, diffMasks, diffIndex }; return text_diff { diffValues, diffMasks, diffIndex };
} }
}
} }
#endif #endif

View File

@ -13,30 +13,52 @@
#include <stdio.h> #include <stdio.h>
#include "MathUtils.h" #include "MathUtils.h"
#define ASSERT_EQUALS(a, b, t1, t2) \ #ifdef _MSC_VER
({ __typeof__ (a) _a = (a); \ #define ASSERT_EQUALS(a, b, t1, t2) (\
__typeof__ (b) _b = (b); \ if (a == b) { \
if (_a == _b) { \ printf("."); \
printf("."); \ } else { \
} else { \ printf("[F]"); \
printf("[F]"); \ printf("\n\n%s - %i: ", __FILE__, __LINE__); \
printf("\n\n%s - %i: ", __FILE__, __LINE__); \ printf(t1, a); printf(" != "); printf(t2, b); printf("\n"); \
printf(t1, a); printf(" != "); printf(t2, b); printf("\n"); \ return 0; } \
return 0; } \ )
})
#define ASSERT_EQUALS_WITH_DELTA(a, b, delta, t1, t2) \ #define ASSERT_EQUALS_WITH_DELTA(a, b, delta, t1, t2) (\
({ __typeof__ (a) _a = (a); \ if (oms_abs(a - b) <= _delta) { \
__typeof__ (b) _b = (b); \ printf("."); \
__typeof__ (delta) _delta = (delta); \ } else { \
__typeof__ (delta) _d = (_a - _b); \ printf("[F]"); \
if (oms_abs(_d) <= _delta) { \ printf("\n\n%s - %i: ", __FILE__, __LINE__); \
printf("."); \ printf(t1, a); printf(" != "); printf(t2, b); printf("\n"); \
} else { \ return 0; } \
printf("[F]"); \ )
printf("\n\n%s - %i: ", __FILE__, __LINE__); \ #else
printf(t1, a); printf(" != "); printf(t2, b); printf("\n"); \ #define ASSERT_EQUALS(a, b, t1, t2) \
return 0; } \ ({ __typeof__ (a) _a = (a); \
}) __typeof__ (b) _b = (b); \
if (_a == _b) { \
printf("."); \
} else { \
printf("[F]"); \
printf("\n\n%s - %i: ", __FILE__, __LINE__); \
printf(t1, _a); printf(" != "); printf(t2, _b); printf("\n"); \
return 0; } \
})
#define ASSERT_EQUALS_WITH_DELTA(a, b, delta, t1, t2) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
__typeof__ (delta) _delta = (delta); \
__typeof__ (delta) _d = (_a - _b); \
if (oms_abs(_d) <= _delta) { \
printf("."); \
} else { \
printf("[F]"); \
printf("\n\n%s - %i: ", __FILE__, __LINE__); \
printf(t1, _a); printf(" != "); printf(t2, _b); printf("\n"); \
return 0; } \
})
#endif
#endif #endif

View File

@ -18,46 +18,44 @@
#include "FileUtils.h" #include "FileUtils.h"
namespace Utils { namespace Utils {
class WebUtils { namespace WebUtils {
private: static
static int write_download_data (void *ptr, size_t size, size_t nmeb, void *stream)
int write_download_data (void *ptr, size_t size, size_t nmeb, void *stream) {
{ Utils::FileUtils::file_body *out = (Utils::FileUtils::file_body *) stream;
Utils::FileUtils::file_body *out = (Utils::FileUtils::file_body *) stream; int outSize = size * nmeb;
int outSize = size * nmeb;
out->content = (char *) malloc(outSize + 1); out->content = (char *) malloc(outSize + 1);
if (!out->content) { if (!out->content) {
fprintf(stderr, "CRITICAL: malloc failed"); fprintf(stderr, "CRITICAL: malloc failed");
}
if (out->content) {
memcpy(out->content, ptr, outSize);
out->size = outSize;
out->content[out->size] = 0;
}
return out->size;
} }
public: if (out->content) {
static memcpy(out->content, ptr, outSize);
Utils::FileUtils::file_body download (char *url)
{
file_body page = {0};
CURL *h = curl_easy_init(); out->size = outSize;
curl_easy_setopt(h, CURLOPT_URL, url); out->content[out->size] = 0;
curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, write_download_data);
curl_easy_setopt(h, CURLOPT_WRITEDATA, &page);
curl_easy_perform(h);
curl_easy_cleanup(h);
return page;
} }
};
return out->size;
}
static
Utils::FileUtils::file_body download (char *url)
{
file_body page = {0};
CURL *h = curl_easy_init();
curl_easy_setopt(h, CURLOPT_URL, url);
curl_easy_setopt(h, CURLOPT_WRITEFUNCTION, write_download_data);
curl_easy_setopt(h, CURLOPT_WRITEDATA, &page);
curl_easy_perform(h);
curl_easy_cleanup(h);
return page;
}
}
} }
#endif #endif