-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionLib.h
193 lines (175 loc) · 4 KB
/
FunctionLib.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef FUNCTIONS_LIB
#define FUNCTIONS_LIB 0
#include <vector>
#include <string>
#include <cmath>
typedef double(*BFPTR)(double,double);
typedef double(*UFPTR)(double);
//IMPLEMENTING FUNCTIONS***************
//Unary
double NEGATE(double r)
{
return -1*r;
}
//binary
double ADD(double l, double r)
{
return l + r;
}
double SUBTRACT(double l, double r)
{
return l + r;
}
double MULTIPLY(double r, double l)
{
return r*l;
}
double DIVIDE(double r, double l)
{
return r/l;
}
double MOD(double r, double l)
{
return (int(r)%int(l));
}
//*************************************
//DEFINING FUNCTIONS LIBRARY***********
union FunctionPtr
{
double (*uptr) (double r); //pointer to unary funtion
double (*bptr) (double l,double r); //pointer to binary function
};
struct FunctionDef
{
std::string name;
bool isBinary;
FunctionPtr ptr;
};
class FunctionLib
{
private:
std::vector<FunctionDef> list;
public:
FunctionLib();
int GetListSize();
std::string GetFunctionName(int index);
double GetFunction(std::string name, double r);
double GetFunction(std::string name, double l, double r);
double GetFunction(int index, double r);
double GetFunction(int index, double l, double r);
UFPTR GetFunctionPtr(std::string name);
BFPTR GetFunctionPtr(std::string name, bool isBinary);
UFPTR GetFunctionPtr(int index);
BFPTR GetFunctionPtr(int index, bool isBinary);
int GetFunctionIndex(std::string name, bool isBinary);
};
double FunctionLib::GetFunction(std::string name, double r)
{
for(int i=list.size()-1; i>=0; i--)
{
if(list[i].name==name && list[i].isBinary==false)
{
return list[i].ptr.uptr(r);
}
}
}
double FunctionLib::GetFunction(std::string name, double l, double r)
{
for(int i=list.size()-1; i>=0; i--)
{
if(list[i].name==name && list[i].isBinary==true)
{
return list[i].ptr.bptr(l,r);
}
}
}
double FunctionLib::GetFunction(int index, double r)
{
return list[index].ptr.uptr(r);
}
double FunctionLib::GetFunction(int index, double l, double r)
{
return list[index].ptr.bptr(l,r);
}
UFPTR FunctionLib::GetFunctionPtr(std::string name)
{
for(int i=list.size()-1; i>=0; i--)
{
if(list[i].name==name && list[i].isBinary==false)
{
return list[i].ptr.uptr;
}
}
}
BFPTR FunctionLib::GetFunctionPtr(std::string name, bool isBinary)
{
for(int i=list.size()-1; i>=0; i--)
{
if(list[i].name==name && list[i].isBinary==true)
{
return list[i].ptr.bptr;
}
}
}
UFPTR FunctionLib::GetFunctionPtr(int index)
{
return list[index].ptr.uptr;
}
BFPTR FunctionLib::GetFunctionPtr(int index, bool isBinary)
{
return list[index].ptr.bptr;
}
int FunctionLib::GetListSize()
{
return list.size();
}
std::string FunctionLib::GetFunctionName(int index)
{
return list[index].name;
}
int FunctionLib::GetFunctionIndex(std::string name, bool isBinary)
{
for(int i=list.size()-1; i>=0; i--)
{
if(list[i].name==name && list[i].isBinary==isBinary)
{
return i;
}
}
return -1;//not found
}
//**********************************
// ADDING FUNCTIONS TO LIBRARY
//**********************************
FunctionLib::FunctionLib()
{
FunctionDef tmp;
//unary
tmp.name = "-";
tmp.isBinary = false; //string representation
tmp.ptr.uptr=NEGATE; //assign function to apropriate pointer (uptr - unary, bptr - binary)
list.push_back(tmp); //add to list
//binary
tmp.name = "+";
tmp.isBinary = true; //binary or unary
tmp.ptr.bptr=ADD; //assign function to apropriate pointer (uptr - unary, bptr - binary)
list.push_back(tmp); //add to list
tmp.name = "-";
tmp.isBinary = true;
tmp.ptr.bptr=SUBTRACT;
list.push_back(tmp);
tmp.name = "*";
tmp.isBinary = true;
tmp.ptr.bptr=MULTIPLY;
list.push_back(tmp);
tmp.name = "/";
tmp.isBinary = true;
tmp.ptr.bptr=DIVIDE;
list.push_back(tmp);
tmp.name = "%";
tmp.isBinary = true;
tmp.ptr.bptr=MOD;
list.push_back(tmp);
}
//********************************************
#endif