File indexing completed on 2024-04-06 11:59:31
0001 #ifndef TRange_H
0002 #define TRange_H
0003
0004
0005
0006 #include <iostream>
0007 #include <utility>
0008 #include <algorithm>
0009
0010 template <class T>
0011 class TRange : public std::pair<T, T> {
0012 public:
0013 TRange() {}
0014
0015 TRange(const T& aMin, const T& aMax) : std::pair<T, T>(aMin, aMax) {}
0016
0017 TRange(const std::pair<T, T>& apair) : std::pair<T, T>(apair) {}
0018
0019
0020 const T& min() const { return this->first; }
0021
0022
0023 const T& max() const { return this->second; }
0024
0025 T mean() const { return (this->first + this->second) / 2.; }
0026
0027
0028 bool empty() const { return (this->second < this->first); }
0029
0030
0031 bool inside(const T& value) const {
0032 if (value < this->first || this->second < value)
0033 return false;
0034 else
0035 return true;
0036 }
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 TRange<T> sum(const TRange<T>& r) const {
0053 if (this->empty())
0054 return r;
0055 else if (r.empty())
0056 return *this;
0057 else
0058 return TRange((min() < r.min()) ? min() : r.min(), (max() < r.max()) ? r.max() : max());
0059 }
0060
0061 void sort() {
0062 if (empty())
0063 std::swap(this->first, this->second);
0064 }
0065 };
0066
0067 template <class T>
0068 std::ostream& operator<<(std::ostream& out, const TRange<T>& r) {
0069 return out << "(" << r.min() << "," << r.max() << ")";
0070 }
0071 #endif