-
Notifications
You must be signed in to change notification settings - Fork 2
/
Binner.cpp
107 lines (84 loc) · 2.12 KB
/
Binner.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
100
101
102
103
104
105
106
107
#include <assert.h>
#include <math.h>
#include "Binner.h"
using namespace hiddenMarkovModel;
Binner::Binner(unsigned int size, double *dataToFit, unsigned long dataSize){
assert(size > 0);
assert(dataSize > 0);
this->size = size;
this->min = dataToFit[0];
this->max = dataToFit[0];
for (unsigned long i = 1; i < dataSize; i += 1){
if (dataToFit[i] < min){
this->min = dataToFit[i];
}
else if (dataToFit[i] > max){
this->max = dataToFit[i];
}
}
if (this->max == this->min){
this->max += 1;
}
this->binSize = (this->max - this->min) / this->size;
};
Binner::Binner(unsigned int size, double *dataToFit, unsigned long dataSize, double minValue, double maxValue){
assert(size > 0);
assert(dataSize > 0);
this->size = size;
this->min = minValue;
this->max = maxValue;
for (unsigned long i = 0; i < dataSize; i += 1){
if (dataToFit[i] < min){
this->min = dataToFit[i];
}
else if (dataToFit[i] > max){
this->max = dataToFit[i];
}
}
if (this->max <= this->min){
this->max = this->min + 1;
}
this->binSize = (this->max - this->min) / this->size;
};
Binner::Binner(unsigned int size, double minValue, double maxValue){
assert(size > 0);
this->size = size;
this->min = minValue;
this->max = maxValue;
if (this->max <= this->min){
this->max = this->min + 1;
}
this->binSize = (this->max - this->min) / this->size;
};
unsigned int Binner::getBinIndex(double value) const{
assert(value >= this->min);
assert(value <= this->max);
if (value == this->max){
return this->size - 1;
}
else {
return (int) floor((value - this->min) / this->binSize);
}
};
unsigned int Binner::operator[](double value) const{
return this->getBinIndex(value);
};
double Binner::getBinValue(unsigned int bin) const{
assert(bin < this->size);
return (0.5 + (double) bin) * this->binSize + this->min;
};
double Binner::operator()(unsigned int bin) const{
return this->getBinValue(bin);
};
unsigned int Binner::getSize() const{
return this->size;
};
double Binner::getMin() const{
return this->min;
}
double Binner::getMax() const{
return this->max;
}
double Binner::getBinSize() const{
return this->binSize;
}