forked from uyras/partsEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dos2.h
74 lines (61 loc) · 1.97 KB
/
dos2.h
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
#ifndef DOS2_H
#define DOS2_H
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cmath>
#include <unordered_map>
using namespace std;
template <typename T>
class Dos2
{
public:
Dos2():min(0),max(0),intervals(0){}
Dos2(double min, double max, unsigned intervals)
{
this->resize(min,max,intervals);
}
inline void resize(double min, double max, unsigned intervals){
this->min = min;
this->max= max;
this->intervals = intervals;
data.resize(intervals);
}
//virtual void save(string file);
//virtual void load(string file);
//string toString();
inline T& at(unsigned i){return data[i];}
inline const T& at(unsigned i) const {return data[i];}
inline T& operator[](double a){return data[num(a)];}
inline const T& operator[](double a) const {return data[num(a)];}
inline unsigned Intervals() const {return intervals;}
inline double Min() const {return this->min;}
inline double Max() const {return this->max;}
/**
* @brief val Возращает значение, с которого начинается i интервал
* @param i Интервал, для которого надо получить соответствующее double значение
* @return
*/
inline double val(unsigned i) const{ return min + (max-min)*((double)i/(double)(intervals)); }
/**
* @brief num Возращает номер интервала для соответствующего значения
* @param a
*/
inline unsigned num(double a) const{
if (a<min)
return 0;
if(a>=max)
return intervals-1;
unsigned r= (unsigned)floor((a-min)/(max-min)*double(intervals));
if (r>intervals-1)
return unsigned(intervals-1);
else
return r;
}
protected:
double min, max, delta;
unsigned intervals;
vector< T > data;
};
#endif // DOS2_H