-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.cpp
154 lines (133 loc) · 4.46 KB
/
utilities.cpp
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
144
145
146
147
148
149
150
151
152
153
154
/**
* @file utilities.cpp
* @author Al Timofeyev
* @date April 15, 2019
* @brief This utilities file is used as a helper file for ProcessFunctions.h
* and SearchAlgorithms.h, and to create matricies using the Mersenne Twister.
*/
#include "utilities.h"
// Utility Functions.
// --------------------------------------------------------------------------------------------------------------------
/**
* @brief Parses a string of numbers into a vector of doubles.
*
* Constructs and returns a vector of doubles, given a string list of
* numbers and a delimiter.
*
* @note The input string str MUST be a list of doubles!
*
* @param str A string list of numbers.
* @param delimiter A string of character(s) used to separate the numbers in the string list.
*
* @return Returns a vector filled with doubles that were extracted from the string list.
*/
vector<double> parseStringDbl(string str, string delimiter)
{
// Copy the string str over to a char[] array.
char list[str.size()+1];
strcpy(list, str.c_str());
// Copy the delimiter into a char[] array.
char delims[delimiter.size()+1];
strcpy(delims, delimiter.c_str());
// Set up a vector where to store the numbers.
vector<double> numList;
// Set up a tokenizing variable.
char * token;
token = strtok (list,delims);
// While there are still tokens
while(token != 0)
{
if(strcmp(token, "pi") == 0)
numList.push_back(M_PI);
else
numList.push_back(stod(token));
token = strtok(NULL, delims);
}
// Return the vector of doubles.
return numList;
}
/**
* @brief Parses a string of numbers into a vector of integers.
*
* Constructs and returns a vector of integers, given a string list of
* numbers and a delimiter.
*
* @note The input string list MUST be a list of integers!
*
* @param str A string list of numbers.
* @param delimiter A string of character(s) used to separate the numbers in the string list.
*
* @return Returns a vector filled with integers that were extracted from the string list.
*/
vector<int> parseStringInt(string str, string delimiter)
{
// Copy the string str over to a char[] array.
char list[str.size()+1];
strcpy(list, str.c_str());
// Copy the delimiter into a char[] array.
char delims[delimiter.size()+1];
strcpy(delims, delimiter.c_str());
// Set up a vector where to store the numbers.
vector<int> numList;
// Set up a tokenizing variable.
char * token;
token = strtok (list,delims);
// While there are still tokens
while(token != 0)
{
if(strcmp(token, "pi") == 0)
numList.push_back((int)M_PI);
else
numList.push_back(stoi(token));
token = strtok(NULL, delims);
}
// Return the vector of integers.
return numList;
}
/**
* @brief Parses a string of elements into a vector of strings.
*
* Constructs and returns a vector of strings, given a string list of
* elements and a delimiter.
*
* @param str A string list of characters.
* @param delimiter A string of character(s) used to separate the numbers in the string list.
*
* @return Returns a vector filled with integers that were extracted from the string list.
*/
vector<string> parseStringStr(string str, string delimiter)
{
// Copy the string str over to a char[] array.
char list[str.size()+1];
strcpy(list, str.c_str());
// Copy the delimiter into a char[] array.
char delims[delimiter.size()+1];
strcpy(delims, delimiter.c_str());
// Set up a vector where to store the string elements.
vector<string> strList;
// Set up a tokenizing variable.
char * token;
token = strtok (list,delims);
// While there are still tokens
while(token != 0)
{
strList.push_back(token);
token = strtok(NULL, delims);
}
// Return the vector of strings.
return strList;
}
/**
* @brief Resizes the vector to size 3.
*
* Resizes the given vector to size three in order to prep it for the
* matrix of a function. Because to generate a matrix, you only need
* 3 values: function ID, minimum bound, maximum bound.
*
* @param setup The vector that's going to be resized for the matrix setup.
*/
void prepForFunctionMatrix(vector<double> &setup)
{
// Resize the vector to size 3.
setup.resize(3);
}