Numworks Epsilon  1.4.1
Graphing Calculator Operating System
float_pair_store.cpp
Go to the documentation of this file.
1 #include "float_pair_store.h"
2 #include <cmath>
3 #include <assert.h>
4 #include <stddef.h>
5 #include <ion.h>
6 
7 namespace Shared {
8 
10  m_numberOfPairs(0),
11  m_data{}
12 {
13 }
14 
15 double FloatPairStore::get(int i, int j) {
17  return m_data[i][j];
18 }
19 
20 void FloatPairStore::set(double f, int i, int j) {
21  if (j >= k_maxNumberOfPairs) {
22  return;
23  }
24  m_data[i][j] = f;
25  if (j >= m_numberOfPairs) {
26  int otherI = i == 0 ? 1 : 0;
27  m_data[otherI][j] = defaultValue(otherI, j);
29  }
30 }
31 
33  return m_numberOfPairs;
34 }
35 
38  for (int k = i; k < m_numberOfPairs; k++) {
39  m_data[0][k] = m_data[0][k+1];
40  m_data[1][k] = m_data[1][k+1];
41  }
42  /* We reset the values of the empty row to ensure the correctness of the
43  * checksum. */
44  m_data[0][m_numberOfPairs] = 0;
45  m_data[1][m_numberOfPairs] = 0;
46 }
47 
49  /* We reset all values to 0 to ensure the correctness of the checksum.*/
50  for (int k = 0; k < m_numberOfPairs; k++) {
51  m_data[0][k] = 0;
52  m_data[1][k] = 0;
53  }
54  m_numberOfPairs = 0;
55 }
56 
58  for (int k = 0; k < m_numberOfPairs; k++) {
59  m_data[i][k] = defaultValue(i, k);
60  }
61 }
62 
64  double result = 0;
65  for (int k = 0; k < m_numberOfPairs; k++) {
66  result += m_data[i][k];
67  }
68  return result;
69 }
70 
72  /* Ideally, we would only compute the checksum of the first m_numberOfPairs
73  * pairs. However, the two values of a pair are not stored consecutively. We
74  * thus compute the checksum on all pairs and ensure to set the pair at 0
75  * when removing them. */
76  size_t dataLengthInBytes = k_maxNumberOfPairs*2*sizeof(double);
77  assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4
78  return Ion::crc32((uint32_t *)m_data, dataLengthInBytes/sizeof(uint32_t));
79 }
80 
81 double FloatPairStore::defaultValue(int i, int j) {
82  if(i == 0 && j > 1) {
83  return 2*m_data[i][j-1]-m_data[i][j-2];
84  } else {
85  return 0.0;
86  }
87 }
88 
89 }
#define assert(e)
Definition: assert.h:9
void set(double f, int i, int j)
static constexpr int k_maxNumberOfPairs
unsigned int uint32_t
Definition: stdint.h:6
virtual double defaultValue(int i, int j)
double get(int i, int j)
double m_data[2][k_maxNumberOfPairs]
uint32_t crc32(const uint32_t *data, size_t length)
Definition: device.cpp:37