double_pair_store.cpp
4.49 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
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
#include "double_pair_store.h"
#include <cmath>
#include <assert.h>
#include <stddef.h>
#include <ion.h>
namespace Shared {
void DoublePairStore::set(double f, int series, int i, int j) {
assert(series >= 0 && series < k_numberOfSeries);
if (j >= k_maxNumberOfPairs) {
return;
}
m_data[series][i][j] = f;
if (j >= m_numberOfPairs[series]) {
int otherI = i == 0 ? 1 : 0;
m_data[series][otherI][j] = defaultValue(series, otherI, j);
m_numberOfPairs[series]++;
}
}
int DoublePairStore::numberOfPairs() const {
int result = 0;
for (int i = 0; i < k_numberOfSeries; i++) {
result += m_numberOfPairs[i];
}
return result;
}
void DoublePairStore::deletePairOfSeriesAtIndex(int series, int j) {
m_numberOfPairs[series]--;
for (int k = j; k < m_numberOfPairs[series]; k++) {
m_data[series][0][k] = m_data[series][0][k+1];
m_data[series][1][k] = m_data[series][1][k+1];
}
/* We reset the values of the empty row to ensure the correctness of the
* checksum. */
m_data[series][0][m_numberOfPairs[series]] = 0;
m_data[series][1][m_numberOfPairs[series]] = 0;
}
void DoublePairStore::deleteAllPairsOfSeries(int series) {
assert(series >= 0 && series < k_numberOfSeries);
/* We reset all values to 0 to ensure the correctness of the checksum.*/
for (int k = 0; k < m_numberOfPairs[series]; k++) {
m_data[series][0][k] = 0;
m_data[series][1][k] = 0;
}
m_numberOfPairs[series] = 0;
}
void DoublePairStore::deleteAllPairs() {
for (int i = 0; i < k_numberOfSeries; i ++) {
deleteAllPairsOfSeries(i);
}
}
void DoublePairStore::resetColumn(int series, int i) {
assert(series >= 0 && series < k_numberOfSeries);
assert(i == 0 || i == 1);
for (int k = 0; k < m_numberOfPairs[series]; k++) {
m_data[series][i][k] = defaultValue(series, i, k);
}
}
bool DoublePairStore::isEmpty() const {
for (int i = 0; i < k_numberOfSeries; i++) {
if (!seriesIsEmpty(i)) {
return false;
}
}
return true;
}
int DoublePairStore::numberOfNonEmptySeries() const {
int nonEmptySeriesCount = 0;
for (int i = 0; i< k_numberOfSeries; i++) {
if (!seriesIsEmpty(i)) {
nonEmptySeriesCount++;
}
}
return nonEmptySeriesCount;
}
int DoublePairStore::indexOfKthNonEmptySeries(int k) const {
assert(k >= 0 && k < numberOfNonEmptySeries());
int nonEmptySeriesCount = 0;
for (int i = 0; i < k_numberOfSeries; i++) {
if (!seriesIsEmpty(i)) {
if (nonEmptySeriesCount == k) {
return i;
}
nonEmptySeriesCount++;
}
}
assert(false);
return 0;
}
double DoublePairStore::sumOfColumn(int series, int i) const {
assert(series >= 0 && series < k_numberOfSeries);
assert(i == 0 || i == 1);
double result = 0;
for (int k = 0; k < m_numberOfPairs[series]; k++) {
result += m_data[series][i][k];
}
return result;
}
bool DoublePairStore::seriesNumberOfAbscissaeGreaterOrEqualTo(int series, int i) const {
assert(series >= 0 && series < k_numberOfSeries);
int count = 0;
for (int j = 0; j < m_numberOfPairs[series]; j++) {
if (count >= i) {
return true;
}
double currentAbsissa = m_data[series][0][j];
bool firstOccurence = true;
for (int k = 0; k < j; k++) {
if (m_data[series][0][k] == currentAbsissa) {
firstOccurence = false;
break;
}
}
if (firstOccurence) {
count++;
}
}
return count >= i;
}
uint32_t DoublePairStore::storeChecksum() const {
/* 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_numberOfSeries*k_maxNumberOfPairs*k_numberOfColumnsPerSeries*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));
}
uint32_t DoublePairStore::storeChecksumForSeries(int series) const {
size_t dataLengthInBytes = k_maxNumberOfPairs*k_numberOfColumnsPerSeries*sizeof(double);
assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4
return Ion::crc32((uint32_t *)m_data[series], dataLengthInBytes/sizeof(uint32_t));
}
double DoublePairStore::defaultValue(int series, int i, int j) const {
assert(series >= 0 && series < k_numberOfSeries);
if(i == 0 && j > 1) {
return 2*m_data[series][i][j-1]-m_data[series][i][j-2];
} else {
return 0.0;
}
}
}