Skip to content

Commit dd61735

Browse files
committed
Some adjustments(still not evaluating left formula with right formula correctly)
-Now erases all left/right values before doing operations except for during opIndex==1 operations -Recycling of indexes disabled for formulas by default now(re-enable with preprocessor variable BlazesEnable_VariableFormula_IndexRecycling)
1 parent 85e0d03 commit dd61735

2 files changed

Lines changed: 77 additions & 90 deletions

File tree

GlobalCode/Databases/MediumDecFormula.hpp

Lines changed: 67 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -46,82 +46,31 @@ namespace BlazesRusCode
4646
/// <param name="LeftIterator">The left iterator.</param>
4747
/// <param name="RightIterator">The right iterator.</param>
4848
/// <param name="Value">The value.</param>
49-
void SwitchOpToBoolVal(FormData& FormCopy, FormElement& OpVal, FormData::iterator& LeftIterator, FormData::iterator& RightIterator, bool Value)
49+
void SwitchOpToBoolVal(FormData& FormCopy, FormElement& OpVal, bool Value)
5050
{
5151
if (Value) { OpVal.ElementCat = FormulaElementType::trueVal; }
5252
else { OpVal.ElementCat = FormulaElementType::falseVal; }
53-
54-
if (LeftIterator->second.ElementCat == FormulaElementType::Num) { FormCopy.NumMap.erase(LeftIterator->first); }
55-
if (RightIterator->second.ElementCat == FormulaElementType::Num) { FormCopy.NumMap.erase(RightIterator->first); }
56-
57-
int LeftKey = LeftIterator->first; int RightKey = RightIterator->first;
58-
FormCopy.erase(LeftIterator); FormCopy.erase(RightKey);
59-
FormCopy.RemovedIndexes.push_back(LeftKey); FormCopy.RemovedIndexes.push_back(RightKey);
60-
}
61-
62-
/// <summary>
63-
/// Switches the operator into boolean value and erases old right side value.
64-
/// </summary>
65-
/// <param name="FormCopy">The formula data copy.</param>
66-
/// <param name="OpVal">The op value.</param>
67-
/// <param name="RightIterator">The right side value iterator.</param>
68-
/// <param name="Value">The value.</param>
69-
void SwitchOpToRBoolVal(FormData& FormCopy, FormElement& OpVal, FormData::iterator& RightIterator, bool Value)
70-
{
71-
if (Value) { OpVal.ElementCat = FormulaElementType::trueVal; }
72-
else { OpVal.ElementCat = FormulaElementType::falseVal; }
73-
74-
if (RightIterator->second.ElementCat == FormulaElementType::Num) { FormCopy.NumMap.erase(RightIterator->first); }
75-
76-
FormCopy.erase(RightIterator);
77-
FormCopy.RemovedIndexes.push_back(RightIterator->first);
7853
}
7954

8055
/// <summary>
81-
/// Switches the operator into Value and erases old right side Value
82-
/// </summary>
83-
/// <param name="FormCopy">The formula data copy.</param>
84-
/// <param name="OpVal">The operator element value.</param>
85-
/// <param name="OpKey">The operator key.</param>
86-
/// <param name="RightIterator">The right side value iterator.</param>
87-
/// <param name="Value">The value to turn operator into.</param>
88-
void SwitchOpToRVal(FormData& FormCopy, FormElement& OpVal, int OpKey, FormData::iterator& RightIterator, MediumDec Value)
89-
{
90-
FormCopy.NumMap.insert_or_assign(OpKey, Value);
91-
OpVal.ElementCat = FormulaElementType::Num;
92-
93-
if (RightIterator->second.ElementCat == FormulaElementType::Num) { FormCopy.NumMap.erase(RightIterator->first); }
94-
95-
int RightKey = RightIterator->first;
96-
FormCopy.erase(RightKey);
97-
FormCopy.RemovedIndexes.push_back(RightKey);
98-
}
99-
100-
/// <summary>
101-
/// Switches the operator into Value and erases old left+right side Value
56+
/// Switches the operator into Value
10257
/// </summary>
10358
/// <param name="FormCopy">The formula data copy.</param>
10459
/// <param name="OpVal">The operator element value.</param>
10560
/// <param name="OpKey">The operator key.</param>
10661
/// <param name="RightIterator">The right value iterator.</param>
10762
/// <param name="RightIterator">The right value iterator.</param>
10863
/// <param name="Value">The value to turn operator into.</param>
109-
void SwitchOpToVal(FormData& FormCopy, FormElement& OpVal, int OpKey, FormData::iterator& LeftIterator, FormData::iterator& RightIterator, MediumDec Value)
64+
void SwitchOpToVal(FormData& FormCopy, FormElement& OpVal, int OpKey, MediumDec Value)
11065
{
11166
FormCopy.NumMap.insert_or_assign(OpKey, Value);
11267
OpVal.ElementCat = FormulaElementType::Num;
113-
114-
if (LeftIterator->second.ElementCat == FormulaElementType::Num) { FormCopy.NumMap.erase(LeftIterator->first); }
115-
if (RightIterator->second.ElementCat == FormulaElementType::Num) { FormCopy.NumMap.erase(RightIterator->first); }
116-
117-
int LeftKey = LeftIterator->first; int RightKey = RightIterator->first;
118-
FormCopy.erase(LeftIterator); FormCopy.erase(RightKey);
119-
FormCopy.RemovedIndexes.push_back(LeftKey); FormCopy.RemovedIndexes.push_back(RightKey);
12068
}
12169

12270
void EvaluateOperations(size_t FormIndex = 0)
12371
{
12472
FormData& FormDRef = Data.at(FormIndex);
73+
std::cout << "Performing Evaluation on FormSegment#" << FormIndex << " with formula content:" << FormDRef.ToString() << std::endl;
12574
MediumDec valResult;
12675

12776
bool TempBool;
@@ -136,6 +85,7 @@ namespace BlazesRusCode
13685
MediumDec leftResult;
13786
MediumDec rightResult;
13887
int OpTargetKey;
88+
int leftKey;
13989

14090
//Applying operations via C++ variation of order of operations
14191
//https://en.cppreference.com/w/cpp/language/operator_precedence
@@ -147,7 +97,7 @@ namespace BlazesRusCode
14797
OpTargetKey = *CurrentVal;
14898
OpIterator = FormDRef.find(OpTargetKey);
14999
OpVal = FormDRef[*CurrentVal];
150-
if (OpVal.ElementCat != FormulaElementType::Not && OpVal.ElementCat != FormulaElementType::Sqrt)
100+
if (opIndex != 1 && (opIndex != 0 || (OpVal.ElementCat != FormulaElementType::Sqrt && OpVal.ElementCat != FormulaElementType::LN && OpVal.ElementCat != FormulaElementType::LOGTEN)))
151101
{
152102
LeftVal = OpIterator - 1;
153103
if (LeftVal->second.ElementCat == FormulaElementType::Formula)//FormulaDetected
@@ -176,12 +126,16 @@ namespace BlazesRusCode
176126
else
177127
continue;//Ignore if not condensed down to single value
178128
}
129+
std::cout << "Left Formula condensed into " << leftValue.ToString() << std::endl;
179130
}
180131
else if (LeftVal->second.ElementCat == FormulaElementType::Variable)//Ignore non-set variables for this version
181132
continue;
182133
else
183134
leftValue = LeftVal->second.ElementCat == FormulaElementType::trueVal ? 1 : (LeftVal->second.ElementCat == FormulaElementType::falseVal ? 0 : FormDRef.NumMap[LeftVal->first]);
135+
leftKey = LeftVal->first;
184136
}
137+
else
138+
leftKey = -1;
185139
RightVal = OpIterator + 1;
186140
if (RightVal->second.ElementCat == FormulaElementType::Formula)//FormulaDetected
187141
{
@@ -190,7 +144,7 @@ namespace BlazesRusCode
190144
{
191145
FormData::iterator targetElem = FormDRef.begin();
192146
if (targetElem->second.ElementCat != FormulaElementType::Variable)
193-
leftValue = targetElem->second.ElementCat == FormulaElementType::trueVal ? MediumDec::One : (targetElem->second.ElementCat == FormulaElementType::falseVal ? MediumDec::Zero : targetSegmentRef.NumMap[targetElem->first]);
147+
rightValue = targetElem->second.ElementCat == FormulaElementType::trueVal ? MediumDec::One : (targetElem->second.ElementCat == FormulaElementType::falseVal ? MediumDec::Zero : targetSegmentRef.NumMap[targetElem->first]);
194148
else
195149
continue;//Ignore operation with unknown variable value
196150
}
@@ -209,11 +163,28 @@ namespace BlazesRusCode
209163
else
210164
continue;//Ignore if not condensed down to single value
211165
}
166+
std::cout << "Right Formula condensed into " << rightValue.ToString() << std::endl;
212167
}
213168
else if (RightVal->second.ElementCat == FormulaElementType::Variable)
214169
continue;
215170
else
216171
rightValue = RightVal->second.ElementCat == FormulaElementType::trueVal ? 1 : (RightVal->second.ElementCat == FormulaElementType::falseVal ? 0 : FormDRef.NumMap[RightVal->first]);
172+
if(opIndex!=1)//Erase left and right values ahead of time(in most most cases)
173+
{
174+
int RightKey = RightVal->first;
175+
if(leftKey!=-1)
176+
{
177+
if (LeftVal->second.ElementCat == FormulaElementType::Num) { FormDRef.NumMap.erase(leftKey); }
178+
FormDRef.erase(leftKey);
179+
}
180+
if (RightVal->second.ElementCat == FormulaElementType::Num) { FormDRef.NumMap.erase(RightKey); }
181+
FormDRef.erase(RightKey);
182+
183+
//if(OpIterator->first!= OpTargetKey)//Fix operator target if needed
184+
// OpVal = FormDRef[OpTargetKey];
185+
}
186+
187+
217188

218189
switch (opIndex)
219190
{
@@ -224,33 +195,33 @@ namespace BlazesRusCode
224195
{
225196
case FormulaElementType::Pow:
226197
leftValue = MediumDec::PowOp(leftValue, rightValue);
227-
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, rightValue);
198+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, rightValue);
228199
break;
229200
case FormulaElementType::Sqrt:
230201
rightValue = MediumDec::Sqrt(rightValue);
231-
SwitchOpToRVal(FormDRef, OpVal, OpTargetKey, RightVal, rightValue);
202+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, rightValue);
232203
break;
233204
case FormulaElementType::NthRoot:
234205
rightValue = MediumDec::NthRootV2(rightValue, (int)leftValue);
235-
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, rightValue);
206+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, rightValue);
236207
break;
237208
case FormulaElementType::LN:
238209
rightValue = MediumDec::Ln(rightValue);
239-
SwitchOpToRVal(FormDRef, OpVal, OpTargetKey, RightVal, rightValue);
210+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, rightValue);
240211
break;
241212
case FormulaElementType::LOGTEN:
242213
rightValue = MediumDec::Log10(rightValue);
243-
SwitchOpToRVal(FormDRef, OpVal, OpTargetKey, RightVal, rightValue);
214+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, rightValue);
244215
break;
245216
case FormulaElementType::BaseNLog:
246217
rightValue = MediumDec::Log(rightValue, leftValue);
247-
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, rightValue);
218+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, rightValue);
248219
break;
249220
default://placeholder code
250221
break;
251222
}
252223
break;
253-
case 1:
224+
case 1://Remove operator in this case and change right side value
254225
switch (OpVal.ElementCat)
255226
{
256227
case FormulaElementType::Not:
@@ -262,37 +233,50 @@ namespace BlazesRusCode
262233
{
263234
FormDRef.at(RightVal->first).ElementCat = FormulaElementType::trueVal;
264235
}
265-
else
236+
else if (RightVal->second.ElementCat == FormulaElementType::Formula)
237+
{
238+
FormDRef.at(RightVal->first).ElementCat = rightValue == MediumDec::Zero ? FormulaElementType::trueVal : FormulaElementType::falseVal;
239+
}
240+
else//Assuming is number
266241
{
267242
if (rightValue == MediumDec::Zero)//Zero is false otherwise count as if it was true
268-
SwitchOpToRBoolVal(FormDRef, OpVal, RightVal, true);
243+
FormDRef.NumMap[RightVal->first] = MediumDec::One;//SwitchOpToBoolVal(FormDRef, OpVal, true);
269244
else
270-
SwitchOpToRBoolVal(FormDRef, OpVal, RightVal, false);
245+
FormDRef.NumMap[RightVal->first] = MediumDec::Zero;//SwitchOpToBoolVal(FormDRef, OpVal, false);
271246
}
272-
FormDRef.erase(OpIterator);
273247
break;
274-
case FormulaElementType::Negative://Only applies to numbers
275-
rightValue.SwapNegativeStatus();//rightValue = -rightValue;
276-
FormDRef.NumMap[RightVal->first] = rightValue;
248+
case FormulaElementType::Negative://Only applies to numbers or Formulas(for now)
249+
if (RightVal->second.ElementCat == FormulaElementType::Formula)
250+
{
251+
rightValue.SwapNegativeStatus();
252+
FormDRef.at(RightVal->first).ElementCat = FormulaElementType::Num;
253+
FormDRef.NumMap.insert_or_assign(RightVal->first, rightValue);
254+
}
255+
else
256+
{
257+
rightValue.SwapNegativeStatus();//rightValue = -rightValue;
258+
FormDRef.NumMap[RightVal->first] = rightValue;
259+
}
277260
break;
278261
default://placeholder code
279262
break;
280263
}
264+
FormDRef.erase(OpIterator);
281265
break;
282266
case 2:// Multiplication, division, and remainder
283267
switch (OpVal.ElementCat)
284268
{
285269
case FormulaElementType::Mult:
286270
leftValue *= rightValue;
287-
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, leftValue);
271+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, leftValue);
288272
break;
289273
case FormulaElementType::Div:
290274
leftValue /= rightValue;
291-
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, leftValue);
275+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, leftValue);
292276
break;
293277
case FormulaElementType::Rem:
294278
leftValue %= rightValue;
295-
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, leftValue);
279+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, leftValue);
296280
break;
297281
default://placeholder code
298282
break;
@@ -304,21 +288,14 @@ namespace BlazesRusCode
304288
case FormulaElementType::Add:
305289
{
306290
leftValue += rightValue;
307-
//SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, leftValue);
291+
//SwitchOpToVal(FormDRef, OpVal, OpTargetKey, leftValue);
308292
FormDRef.NumMap.insert_or_assign(OpTargetKey, leftValue);
309293
FormDRef[OpTargetKey].ElementCat = FormulaElementType::Num;
310-
311-
//if (LeftVal->second.ElementCat == FormulaElementType::Num) { FormDRef.NumMap.erase(LeftVal->first); }
312-
//if (RightVal->second.ElementCat == FormulaElementType::Num) { FormDRef.NumMap.erase(RightVal->first); }
313-
314-
int LeftKey = LeftVal->first; int RightKey = RightVal->first;
315-
FormDRef.erase(LeftVal); FormDRef.erase(RightKey);
316-
//FormDRef.RemovedIndexes.push_back(LeftKey); FormDRef.RemovedIndexes.push_back(RightKey);
317294
break;
318295
}
319296
case FormulaElementType::Sub:
320297
leftValue -= rightValue;
321-
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, leftValue);
298+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, leftValue);
322299
break;
323300
}
324301
break;
@@ -338,7 +315,7 @@ namespace BlazesRusCode
338315
TempBool = leftValue >= rightValue;
339316
break;
340317
}
341-
SwitchOpToBoolVal(FormDRef, OpVal, LeftVal, RightVal, TempBool);
318+
SwitchOpToBoolVal(FormDRef, OpVal, TempBool);
342319
break;
343320
case 5://==, !=
344321
switch (OpVal.ElementCat)
@@ -350,27 +327,27 @@ namespace BlazesRusCode
350327
TempBool = leftValue != rightValue;
351328
break;
352329
}
353-
SwitchOpToBoolVal(FormDRef, OpVal, LeftVal, RightVal, TempBool);
330+
SwitchOpToBoolVal(FormDRef, OpVal, TempBool);
354331
break;
355332
case 6://&
356333
leftValue = leftValue & rightValue;
357-
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, leftValue);
334+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, leftValue);
358335
break;
359336
case 7://XOR
360337
leftValue = leftValue ^ rightValue;
361-
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, leftValue);
338+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, leftValue);
362339
break;
363340
case 8:// | Bitwise OR (inclusive or)
364341
leftValue = leftValue ^ rightValue;
365-
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, LeftVal, RightVal, leftValue);
342+
SwitchOpToVal(FormDRef, OpVal, OpTargetKey, leftValue);
366343
break;
367344
case 9://&&
368345
TempBool = leftValue && rightValue;
369-
SwitchOpToBoolVal(FormDRef, OpVal, LeftVal, RightVal, TempBool);
346+
SwitchOpToBoolVal(FormDRef, OpVal, TempBool);
370347
break;
371348
case 10:// || (Logical OR)
372349
TempBool = leftValue || rightValue;
373-
SwitchOpToBoolVal(FormDRef, OpVal, LeftVal, RightVal, TempBool);
350+
SwitchOpToBoolVal(FormDRef, OpVal, TempBool);
374351
break;
375352
//case 11://Ternary conditional, =, +=, -=, *=, /=, %=,<<=, >>=, &= , ^=, |= (Not supported yet)
376353
// break;

0 commit comments

Comments
 (0)