00001 #ifndef theplu_yat_utility_segment
00002 #define theplu_yat_utility_segment
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "yat_assert.h"
00026
00027 #include <algorithm>
00028 #include <functional>
00029
00030 namespace theplu {
00031 namespace yat {
00032 namespace utility {
00033
00046 template<typename T, class Compare = std::less<T> >
00047 class Segment
00048 {
00049 public:
00053 typedef T value_type;
00054
00058 Segment(void) {}
00059
00065 Segment(const T& begin, const T& end)
00066 : begin_(begin), end_(end) {}
00067
00071 T& begin(void) { return begin_; }
00072
00076 const T& begin(void) const { return begin_; }
00077
00081 T& end(void) { return end_; }
00082
00086 const T& end(void) const { return end_; }
00087
00088 private:
00089 T begin_;
00090 T end_;
00091
00092
00093
00094
00095 };
00096
00112 template<typename T, class Compare>
00113 bool compare(const Segment<T, Compare>& lhs, const Segment<T, Compare>& rhs)
00114 {
00115 Compare c;
00116
00117 YAT_ASSERT(!c(lhs.end(), lhs.begin()));
00118 YAT_ASSERT(!c(rhs.end(), rhs.begin()));
00119
00120 if (!c(lhs.begin(), lhs.end()) && !c(rhs.begin(), rhs.end())) {
00121 return c(lhs.begin(), rhs.begin());
00122 }
00123
00124 return ! c(rhs.begin(), lhs.end());
00125 }
00126
00137 template<typename T, class Compare>
00138 int compare_3way(const Segment<T, Compare>& lhs,
00139 const Segment<T, Compare>& rhs)
00140 {
00141 if (compare(lhs, rhs))
00142 return -1;
00143 if (compare(rhs, lhs))
00144 return 1;
00145 return 0;
00146 }
00147
00156 template<typename T, class Compare>
00157 int compare_3way(const T& element,
00158 const Segment<T, Compare>& segment)
00159 {
00160 Compare comp;
00161 if (comp(element, segment.begin()))
00162 return -1;
00163 if (comp(element, segment.end()))
00164 return 0;
00165 return 1;
00166 }
00167
00177 template<typename T, class Compare>
00178 Segment<T, Compare> intersection(const Segment<T, Compare>& a,
00179 const Segment<T, Compare>& b)
00180 {
00181 Compare comp;
00182 Segment<T, Compare> result;
00183
00184 result.begin() = std::max(a.begin(), b.begin(), comp);
00185
00186 result.end() = std::max(result.begin(),
00187 std::min(a.end(), b.end(), comp),
00188 comp);
00189 return result;
00190 }
00191
00195 template<typename T, class Compare>
00196 struct SegmentCompare :
00197 public std::binary_function<Segment<T,Compare>, Segment<T,Compare>, bool>
00198 {
00202 bool operator()(const Segment<T, Compare>& lhs,
00203 const Segment<T, Compare>& rhs) const
00204 { return compare(lhs, rhs); }
00205 };
00206
00207 }}}
00208 #endif