-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp_mystack.hpp
187 lines (155 loc) · 5 KB
/
mp_mystack.hpp
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
//
// Stack of lexemes used by MathParser
//
#pragma once
#include <math.h>
class MathLexeme {
friend class MyStack;
friend class MathParser;
enum MathLexType {
Begin = 0,
End = 1,
Number = 2,
Function = 3,
Unary = 4,
Binary = 5,
LeftPar = 6,
RightPar = 7
};
enum MathLexNumItem { Constant, Variable };
enum MathLexBiItem { Plus = 0, Minus = 1, Multiply = 2, Divide = 3, Power = 4 };
MathLexeme() = default;
explicit MathLexeme(MathLexType iType);
MathLexeme(MathLexType iType, int iItem);
MathLexeme(MathLexType iType, int iItem, double dValue);
MathLexType iType{}; // type of lexeme
int iItem{ 0 }; // sub-type
double dValue{ 0.0 }; // value of a variable (for Number/Constant)
double (*pFunction)(double) { nullptr }; // pointer to a function (for Function)
size_t iPosition{ 0 }; // position in the string (for error reporting)
size_t iRunTimeIndex{ 0 }; // index of value stored in RunTimeMemory
static constexpr size_t MathLexNumberOfFunctions{ 45 };
static constexpr size_t MathLexNumberOfConstants{ 2 };
static const bool IsPriorityHigher[5][5];
static double (*const BinaryOpAddress[5])(double, double);
static const wchar_t *const FunctionID[MathLexNumberOfFunctions];
static const bool IsFunctionAllowed[MathLexNumberOfFunctions];
static double (*const FunctionAddress[MathLexNumberOfFunctions])(double);
static const wchar_t *const ConstantID[MathLexNumberOfConstants];
static const bool IsConstantAllowed[MathLexNumberOfConstants];
static const double ConstantValue[MathLexNumberOfConstants];
static double mysqrt(double);
static double myexp(double);
static double myln(double);
static double mylg(double);
static double mylog(double);
static double mysin(double);
static double mycos(double);
static double mysec(double);
static double mycsc(double);
static double mytg(double);
static double myctg(double);
static double mytan(double);
static double mycot(double);
static double myarcsin(double);
static double myarccos(double);
static double myarcsec(double);
static double myarccsc(double);
static double myarctg(double);
static double myarcctg(double);
static double myarctan(double);
static double myarccot(double);
static double mysech(double);
static double mycsch(double);
static double mysh(double);
static double mych(double);
static double myth(double);
static double mycth(double);
static double mysinh(double);
static double mycosh(double);
static double mytanh(double);
static double mycoth(double);
static double myarsh(double);
static double myarch(double);
static double myarth(double);
static double myarcth(double);
static double myarsech(double);
static double myarcsch(double);
static double myarcsinh(double);
static double myarccosh(double);
static double myarctanh(double);
static double myarccoth(double);
static double myarcsech(double);
static double myarccsch(double);
static double myabs(double);
static double myint(double);
};
inline MathLexeme::MathLexeme(MathLexType iType) : iType(iType)
{
}
inline MathLexeme::MathLexeme(MathLexType iType, int iItem)
: iType(iType), iItem(iItem)
{
}
inline MathLexeme::MathLexeme(MathLexType iType, int iItem, double dValue)
: iType(iType), iItem(iItem), dValue(dValue)
{
}
class MyStack { // unsafe but fast
friend class MathParser;
MyStack() noexcept;
~MyStack();
void Resize(size_t iNewSize);
operator MathLexeme* () const;
void Push(const MathLexeme& Lexeme);
void Pop();
void Pop(MathLexeme& Lexeme);
MathLexeme Top();
MathLexeme SecondFromTop();
void Reset();
MathLexeme* pMemory;
size_t iTopOfStack;
size_t iCurrentStackSize;
};
inline MyStack::MyStack() noexcept
: pMemory{ nullptr }, iTopOfStack{ 0 }, iCurrentStackSize{ 0 }
{
}
inline MyStack::~MyStack()
{
delete *this;
}
inline void MyStack::Resize(size_t iNewSize)
{
delete *this;
pMemory = new MathLexeme[iNewSize * sizeof(MathLexeme)]{};
iCurrentStackSize = iNewSize;
}
inline MyStack::operator MathLexeme* () const
{
return pMemory;
}
inline void MyStack::Push(const MathLexeme& Lexeme)
{
pMemory[iTopOfStack++] = Lexeme;
}
inline void MyStack::Pop()
{
--iTopOfStack;
}
inline void MyStack::Pop(MathLexeme& Lexeme)
{
Lexeme = pMemory[--iTopOfStack];
}
inline MathLexeme MyStack::Top()
{
return pMemory[iTopOfStack - 1];
}
inline MathLexeme MyStack::SecondFromTop()
{
return pMemory[iTopOfStack - 2];
}
inline void MyStack::Reset()
{
iTopOfStack = 0;
}