Numworks Epsilon  1.4.1
Graphing Calculator Operating System
calculation_store.cpp
Go to the documentation of this file.
1 #include "calculation_store.h"
2 #include <assert.h>
3 using namespace Poincare;
4 
5 namespace Calculation {
6 
7 CalculationStore::CalculationStore() :
8  m_startIndex(0)
9 {
10 }
11 
12 Calculation * CalculationStore::push(const char * text, Context * context) {
13  Calculation * result = &m_calculations[m_startIndex];
14  result->setContent(text, context, ansExpression(context));
15  m_startIndex++;
16  if (m_startIndex >= k_maxNumberOfCalculations) {
17  m_startIndex = 0;
18  }
19  return result;
20 }
21 
23  int j = 0;
24  Calculation * currentCalc = &m_calculations[m_startIndex];
25  Calculation * previousCalc = nullptr;
26  while (j <= i) {
27  if (!currentCalc++->isEmpty()) {
28  previousCalc = currentCalc - 1;
29  j++;
30  }
31  if (currentCalc >= m_calculations + k_maxNumberOfCalculations) {
32  currentCalc = m_calculations;
33  }
34  }
35  return previousCalc;
36 }
37 
39  Calculation * currentCalc= m_calculations;
40  int numberOfCalculations = 0;
41  while (currentCalc < m_calculations + k_maxNumberOfCalculations) {
42  if (!currentCalc++->isEmpty()) {
44  }
45  }
46  return numberOfCalculations;
47 }
48 
50  int numberOfCalc = numberOfCalculations();
51  assert(i >= 0 && i < numberOfCalc);
52  int indexFirstCalc = m_startIndex;
53  while (m_calculations[indexFirstCalc].isEmpty()) {
54  indexFirstCalc++;
55  if (indexFirstCalc == k_maxNumberOfCalculations) {
56  indexFirstCalc = 0;
57  }
58  assert(indexFirstCalc != m_startIndex);
59  }
60  int absoluteIndexCalculationI = indexFirstCalc+i;
61  absoluteIndexCalculationI = absoluteIndexCalculationI >= k_maxNumberOfCalculations ? absoluteIndexCalculationI - k_maxNumberOfCalculations : absoluteIndexCalculationI;
62 
63  int index = absoluteIndexCalculationI;
64  for (int k = i; k < numberOfCalc-1; k++) {
65  int nextIndex = index+1 >= k_maxNumberOfCalculations ? 0 : index+1;
66  m_calculations[index] = m_calculations[nextIndex];
67  index++;
68  if (index == k_maxNumberOfCalculations) {
69  index = 0;
70  }
71  }
72  m_calculations[index].reset();
73  m_startIndex--;
74  if (m_startIndex == -1) {
75  m_startIndex = k_maxNumberOfCalculations-1;
76  }
77 }
78 
80  m_startIndex = 0;
81  for (int i = 0; i < k_maxNumberOfCalculations; i++) {
82  m_calculations[i].reset();
83  }
84 }
85 
87  for (int i = 0; i < k_maxNumberOfCalculations; i++) {
88  m_calculations[i].tidy();
89  }
90 }
91 
93  if (numberOfCalculations() == 0) {
94  static Rational defaultExpression(0);
95  return &defaultExpression;
96  }
97  Calculation * lastCalculation = calculationAtIndex(numberOfCalculations()-1);
98  /* Special case: the exact output is a Store expression.
99  * Remark: Store expressions are always reduced but if the simplification
100  * process was interrupted, the exact output is identical to the input.
101  * To avoid turning 'ans->A' in '2->A->A' (which cannot be parsed), ans
102  * is replaced by the approximation output in that special case.*/
103  bool exactOuptutInvolvesStore = lastCalculation->exactOutput(context)->recursivelyMatches([](const Expression * e, Context & context) {
104  return e->type() == Expression::Type::Store;
105  }, *context);
106  if (lastCalculation->input()->isApproximate(*context) || exactOuptutInvolvesStore) {
107  return lastCalculation->approximateOutput(context);
108  }
109  return lastCalculation->exactOutput(context);
110 }
111 
112 }
Calculation * push(const char *text, Poincare::Context *context)
Calculation * calculationAtIndex(int i)
#define assert(e)
Definition: assert.h:9
static constexpr int k_maxNumberOfCalculations
Poincare::Expression * ansExpression(Poincare::Context *context)
virtual Type type() const =0