-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange.h
More file actions
167 lines (137 loc) · 3.39 KB
/
Range.h
File metadata and controls
167 lines (137 loc) · 3.39 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
159
160
161
162
163
164
165
166
167
#ifndef RANGE_H
#define RANGE_H
#include <EasySerDes.h>
#include <algorithm>
#include <optional>
namespace util {
template <typename T>
class Range {
public:
Range(const T& first, const T& last);
const T& Min() const;
const T& Max() const;
T ValueRange() const;
T ValueDifference() const;
const T& First() const;
const T& Last() const;
bool Contains(const T& value) const;
double Similarity(const Range<T>& other) const;
void SetFirst(const T& newFirst);
void SetLast(const T& newLast);
void SetRange(const T& first, const T& last);
bool operator>(const Range<T>& other) const;
bool operator<(const Range<T>& other) const;
bool operator==(const Range<T>& other) const;
bool operator>=(const Range<T>& other) const;
bool operator<=(const Range<T>& other) const;
private:
T first_;
T last_;
};
template<typename T>
Range<T>::Range(const T& first, const T& last)
: first_(first)
, last_(last)
{
}
template<typename T>
const T& Range<T>::Min() const
{
return std::min(first_, last_);
}
template<typename T>
const T& Range<T>::Max() const
{
return std::max(first_, last_);
}
template<typename T>
T Range<T>::ValueRange() const
{
return Max() - Min();
}
template<typename T>
T Range<T>::ValueDifference() const
{
return first_ - last_;
}
template<typename T>
const T& Range<T>::First() const
{
return first_;
}
template<typename T>
const T& Range<T>::Last() const
{
return last_;
}
template<typename T>
bool Range<T>::Contains(const T& value) const
{
return value >= Min() && value <= Max();
}
template<typename T>
double Range<T>::Similarity(const Range<T>& other) const
{
double overlap = static_cast<double>(std::min(Max(), other.Max()) - std::max(Min(), other.Min()));
if (overlap > 0.0) {
double combinedRange = static_cast<double>(std::max(Max(), other.Max()) - std::min(Min(), other.Min()));
return overlap / combinedRange;
} else {
return 1.0;
}
}
template<typename T>
void Range<T>::SetFirst(const T& newFirst)
{
first_ = newFirst;
}
template<typename T>
void Range<T>::SetLast(const T& newLast)
{
last_ = newLast;
}
template<typename T>
void Range<T>::SetRange(const T& first, const T& last)
{
first_ = first;
last_ = last;
}
template<typename T>
bool Range<T>::operator>(const Range<T>& other) const
{
return Min() > other.Min() || (Min() == other.Min() && Max() > other.Max());
}
template<typename T>
bool Range<T>::operator<(const Range<T>& other) const
{
return Min() < other.Min() || (Min() == other.Min() && Max() < other.Max());
}
template<typename T>
bool Range<T>::operator==(const Range<T>& other) const
{
return first_ == other.first_ && last_ == other.last_;
}
template<typename T>
bool Range<T>::operator>=(const Range<T>& other) const
{
return *this > other || *this == other;
}
template<typename T>
bool Range<T>::operator<=(const Range<T>& other) const
{
return *this < other || *this == other;
}
} // end namespace util
template<typename T>
class esd::Serialiser<util::Range<T>> : public esd::ClassHelper<util::Range<T>, T, T> {
public:
static void Configure()
{
using This = ClassHelper<util::Range<T>, T, T>;
This::SetConstruction(
This::CreateParameter(&util::Range<T>::First, "First"),
This::CreateParameter(&util::Range<T>::Last, "Last")
);
}
};
#endif // RANGE_H