float_pair_store.cpp
2.06 KB
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
#include "float_pair_store.h"
#include <cmath>
#include <assert.h>
#include <stddef.h>
#include <ion.h>
namespace Shared {
FloatPairStore::FloatPairStore() :
m_numberOfPairs(0),
m_data{}
{
}
double FloatPairStore::get(int i, int j) {
assert(j < m_numberOfPairs);
return m_data[i][j];
}
void FloatPairStore::set(double f, int i, int j) {
if (j >= k_maxNumberOfPairs) {
return;
}
m_data[i][j] = f;
if (j >= m_numberOfPairs) {
int otherI = i == 0 ? 1 : 0;
m_data[otherI][j] = defaultValue(otherI);
m_numberOfPairs++;
}
}
int FloatPairStore::numberOfPairs() {
return m_numberOfPairs;
}
void FloatPairStore::deletePairAtIndex(int i) {
m_numberOfPairs--;
for (int k = i; k < m_numberOfPairs; k++) {
m_data[0][k] = m_data[0][k+1];
m_data[1][k] = m_data[1][k+1];
}
/* We reset the values of the empty row to ensure the correctness of the
* checksum. */
m_data[0][m_numberOfPairs] = 0;
m_data[1][m_numberOfPairs] = 0;
}
void FloatPairStore::deleteAllPairs() {
/* We reset all values to 0 to ensure the correctness of the checksum.*/
for (int k = 0; k < m_numberOfPairs; k++) {
m_data[0][k] = 0;
m_data[1][k] = 0;
}
m_numberOfPairs = 0;
}
void FloatPairStore::resetColumn(int i) {
for (int k = 0; k < m_numberOfPairs; k++) {
m_data[i][k] = defaultValue(i);
}
}
double FloatPairStore::sumOfColumn(int i) {
double result = 0;
for (int k = 0; k < m_numberOfPairs; k++) {
result += m_data[i][k];
}
return result;
}
uint32_t FloatPairStore::storeChecksum() {
/* Ideally, we would only compute the checksum of the first m_numberOfPairs
* pairs. However, the two values of a pair are not stored consecutively. We
* thus compute the checksum on all pairs and ensure to set the pair at 0
* when removing them. */
size_t dataLengthInBytes = k_maxNumberOfPairs*2*sizeof(double);
assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4
return Ion::crc32((uint32_t *)m_data, dataLengthInBytes/sizeof(uint32_t));
}
double FloatPairStore::defaultValue(int i) {
return 0.0;
}
}