1- #pragma once
2-
3- #include < interval-tree/interval_types.hpp>
4- #include < algorithm>
5- #include < vector>
6-
7- template <typename numerical_type, typename interval_kind_ = lib_interval_tree::closed>
8- struct multi_join_interval
9- {
10- public:
11- using value_type = numerical_type;
12- using interval_kind = interval_kind_;
13-
14- #ifndef INTERVAL_TREE_SAFE_INTERVALS
15- #if __cplusplus >= 201703L
16- constexpr
17- #endif
18- multi_join_interval (value_type low, value_type high)
19- : low_{low}
20- , high_{high}
21- {
22- if (low > high)
23- throw std::invalid_argument (" Low border is not lower or equal to high border." );
24- }
25- #else
26- #if __cplusplus >= 201703L
27- constexpr
28- #endif
29- multi_join_interval (value_type low, value_type high)
30- : low_{std::min (low, high)}
31- , high_{std::max (low, high)}
32- {
33- }
34- #endif
35- virtual ~multi_join_interval () = default ;
36- friend bool operator ==(multi_join_interval const & lhs, multi_join_interval const & other)
37- {
38- return lhs.low_ == other.low_ && lhs.high_ == other.high_ ;
39- }
40- friend bool operator !=(multi_join_interval const & lhs, multi_join_interval const & other)
41- {
42- return lhs.low_ != other.low_ || lhs.high_ != other.high_ ;
43- }
44- value_type low () const
45- {
46- return low_;
47- }
48- value_type high () const
49- {
50- return high_;
51- }
52- bool overlaps (value_type l, value_type h) const
53- {
54- return low_ <= h && l <= high_;
55- }
56- bool overlaps_exclusive (value_type l, value_type h) const
57- {
58- return low_ < h && l < high_;
59- }
60- bool overlaps (multi_join_interval const & other) const
61- {
62- return overlaps (other.low_ , other.high_ );
63- }
64- bool overlaps_exclusive (multi_join_interval const & other) const
65- {
66- return overlaps_exclusive (other.low_ , other.high_ );
67- }
68- bool within (value_type value) const
69- {
70- return interval_kind::within (low_, high_, value);
71- }
72- bool within (multi_join_interval const & other) const
73- {
74- return low_ <= other.low_ && high_ >= other.high_ ;
75- }
76- value_type operator -(multi_join_interval const & other) const
77- {
78- if (overlaps (other))
79- return 0 ;
80- if (high_ < other.low_ )
81- return other.low_ - high_;
82- else
83- return low_ - other.high_ ;
84- }
85- value_type size () const
86- {
87- return high_ - low_;
88- }
89- std::vector<multi_join_interval> join (multi_join_interval const & other) const
90- {
91- const auto min = std::min (low_, other.low_ );
92- const auto max = std::max (high_, other.high_ );
93- const auto avg = (min + max) / 2 ;
94- return {
95- {min, avg},
96- {avg, max},
97- };
98- }
99-
100- protected:
101- value_type low_;
102- value_type high_;
1+ #pragma once
2+
3+ #include < interval-tree/interval_types.hpp>
4+ #include < algorithm>
5+ #include < vector>
6+
7+ template <typename numerical_type, typename interval_kind_ = lib_interval_tree::closed>
8+ struct multi_join_interval
9+ {
10+ public:
11+ using value_type = numerical_type;
12+ using interval_kind = interval_kind_;
13+
14+ #ifndef INTERVAL_TREE_SAFE_INTERVALS
15+ #if __cplusplus >= 201703L
16+ constexpr
17+ #endif
18+ multi_join_interval (value_type low, value_type high)
19+ : low_{low}
20+ , high_{high}
21+ {
22+ if (low > high)
23+ throw std::invalid_argument (" Low border is not lower or equal to high border." );
24+ }
25+ #else
26+ #if __cplusplus >= 201703L
27+ constexpr
28+ #endif
29+ multi_join_interval (value_type low, value_type high)
30+ : low_{std::min (low, high)}
31+ , high_{std::max (low, high)}
32+ {
33+ }
34+ #endif
35+ virtual ~multi_join_interval () = default ;
36+ friend bool operator ==(multi_join_interval const & lhs, multi_join_interval const & other)
37+ {
38+ return lhs.low_ == other.low_ && lhs.high_ == other.high_ ;
39+ }
40+ friend bool operator !=(multi_join_interval const & lhs, multi_join_interval const & other)
41+ {
42+ return lhs.low_ != other.low_ || lhs.high_ != other.high_ ;
43+ }
44+ value_type low () const
45+ {
46+ return low_;
47+ }
48+ value_type high () const
49+ {
50+ return high_;
51+ }
52+ bool overlaps (value_type l, value_type h) const
53+ {
54+ return low_ <= h && l <= high_;
55+ }
56+ bool overlaps_exclusive (value_type l, value_type h) const
57+ {
58+ return low_ < h && l < high_;
59+ }
60+ bool overlaps (multi_join_interval const & other) const
61+ {
62+ return overlaps (other.low_ , other.high_ );
63+ }
64+ bool overlaps_exclusive (multi_join_interval const & other) const
65+ {
66+ return overlaps_exclusive (other.low_ , other.high_ );
67+ }
68+ bool within (value_type value) const
69+ {
70+ return interval_kind::within (low_, high_, value);
71+ }
72+ bool within (multi_join_interval const & other) const
73+ {
74+ return low_ <= other.low_ && high_ >= other.high_ ;
75+ }
76+ value_type operator -(multi_join_interval const & other) const
77+ {
78+ if (overlaps (other))
79+ return 0 ;
80+ if (high_ < other.low_ )
81+ return other.low_ - high_;
82+ else
83+ return low_ - other.high_ ;
84+ }
85+ value_type size () const
86+ {
87+ return high_ - low_;
88+ }
89+ std::vector<multi_join_interval> join (multi_join_interval const & other) const
90+ {
91+ const auto min = std::min (low_, other.low_ );
92+ const auto max = std::max (high_, other.high_ );
93+ const auto avg = (min + max) / 2 ;
94+ return {
95+ {min, avg},
96+ {avg, max},
97+ };
98+ }
99+
100+ protected:
101+ value_type low_;
102+ value_type high_;
103103};
0 commit comments