-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.h
143 lines (130 loc) · 3.92 KB
/
Utilities.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#pragma once
#include <iostream>
#include <limits>
#include <cmath>
#include <numbers>
#include <string>
struct fieldExtrema
{
double max = std::numeric_limits<double>::min(); // default access modifier is public for structs btw
int iMax = -1;
int jMax = -1;
int nMax = -1;
double min = std::numeric_limits<double>::max();
int iMin = -1;
int jMin = -1;
int nMin = -1;
void print()
{
std::cout << "Field Extrema: max " << (*this).max << " @ " << (*this).iMax << ", " << (*this).jMax << ", " << (*this).nMax <<
" | min " << (*this).min << " @ " << (*this).iMin << ", " << (*this).jMin << ", " << (*this).nMin << "\n";
}
void setExtrema(double fieldVal, int i, int j, int n)
{
if (fieldVal > this->max)
{
this->max = fieldVal;
this->iMax = i;
this->jMax = j;
this->nMax = n;
}
if (fieldVal < this->min)
{
this->min = fieldVal;
this->iMin = i;
this->jMin = j;
this->nMin = n;
}
}
};
// // misc utilities
/* I DIDNT USE THESE FUNCTIONS BECAUSE I THOUGHT THEY DIDNT WORK; MAYBE THEY DO, CAN TRY USING THEM AGAIN
template<typename T>
size_t getSizeOfArrEle(T* arr) {
using ElementType = typename std::remove_pointer<T>::type;
return sizeof(ElementType);
}
template<typename T>
size_t getSizeOfArrEle(T** arr) {
using ElementType = typename std::remove_pointer<typename std::remove_pointer<T>::type>::type;
return sizeof(ElementType);
}
template<typename T>
size_t getSizeOfArrEle(T*** arr) {
using ElementType = typename std::remove_pointer<typename std::remove_pointer<typename std::remove_pointer<T>::type>::type>::type;
return sizeof(ElementType);
}*/
template <typename T>
void allocate3DArray(T ****arr, int xCount, int yCount, int zCount) {
*arr = (T***) malloc(xCount * sizeof(T**));
if (*arr == NULL) {
perror("Failed to allocate memory for arr");
exit(EXIT_FAILURE);
}
for (int i = 0; i < xCount; i++) {
(*arr)[i] = (T**) malloc(yCount * sizeof(T*));
if ((*arr)[i] == NULL) {
perror("Failed to allocate memory for arr[i]");
exit(EXIT_FAILURE);
}
for (int j = 0; j < yCount; j++) {
(*arr)[i][j] = (T*) malloc(zCount * sizeof(T));
if ((*arr)[i][j] == NULL) {
perror("Failed to allocate memory for arr[i][j]");
exit(EXIT_FAILURE);
}
}
}
}
template <typename T>
void free3DArray(T ***arr, int xCount, int yCount) {
for (int i = 0; i < xCount; i++) {
for (int j = 0; j < yCount; j++) {
free(arr[i][j]); // Free each array of elements
}
free(arr[i]); // Free each array of pointers
}
free(arr); // Free the array of pointers to pointers
}
template <typename T>
void allocate2DArray(T ***arr, int xCount, int yCount) {
*arr = (T**) malloc(xCount * sizeof(T*));
if (*arr == NULL) {
perror("Failed to allocate memory for arr");
exit(EXIT_FAILURE);
}
for (int i = 0; i < xCount; i++) {
(*arr)[i] = (T*) malloc(yCount * sizeof(T*));
if ((*arr)[i] == NULL) {
perror("Failed to allocate memory for arr[i]");
exit(EXIT_FAILURE);
}
}
}
template <typename T>
void free2DArray(T **arr, int xCount) {
for (int i = 0; i < xCount; i++) {
free(arr[i]); // Free each array of pointers
}
free(arr); // Free the array of pointers to pointers
}
// // math utilities
inline static double function_sigmoid(double x)
{
return 1 / (1 + exp(-x));
}
inline double function_step(double x)
{
if (x < 0)
{
return 0;
}
else
{
return 1;
}
}
double xYToAngle(double x, double y);
// // string utilities
int* checkForIn(std::string checkFor, std::string checkIn);
std::string deleteInterval(std::string text, int from, int to);