-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfillNaN.cpp
99 lines (86 loc) · 1.77 KB
/
fillNaN.cpp
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
#include <cmath>
// #include <cstdlib>
#include <limits>
#include <random>
namespace {
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_real_distribution<> dist(0, 1);
const double NaN = std::numeric_limits<double>::quiet_NaN();
}
double generateValue()
{
return dist(e2);
}
double* createHistory(int numValues)
{
auto values = reinterpret_cast<double *>(
std::malloc(sizeof(double) * numValues));
for (int i = 0; i < numValues; ++i)
{
if (i % 7 > 5
|| generateValue() <= (10.0 / 365.0))
{
values[i] = NaN;
}
else
{
values[i] = generateValue();
}
}
return values;
}
double* cloneHistory(const double* values, int numValues)
{
auto newValues = reinterpret_cast<double *>(
std::malloc(sizeof(double) * numValues));
for (int i = 0; i < numValues; ++i)
{
newValues[i] = values[i];
}
return newValues;
}
void destroyHistory(double* values)
{
std::free(values);
}
void fillNaN(double* values, int numValues)
{
if (numValues == 0)
{
return;
}
auto lastValid = values[0];
for (int i = 1; i < numValues; ++i)
{
double& value = values[i];
if (std::isnan(value))
{
value = lastValid;
}
else
{
lastValid = value;
}
}
}
void fillNaN_reversed(double* values, int numValues)
{
if (numValues == 0)
{
return;
}
auto lastValid = values[numValues - 1];
for (int i = numValues - 1; i >= 0; --i)
{
double& value = values[i];
if (std::isnan(value))
{
value = lastValid;
}
else
{
lastValid = value;
}
}
}