1 | #ifndef theplu_yat_utility_segment |
---|
2 | #define theplu_yat_utility_segment |
---|
3 | |
---|
4 | // $Id: Segment.h 2293 2010-07-07 16:33:46Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2010 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 3 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include <functional> |
---|
26 | |
---|
27 | namespace theplu { |
---|
28 | namespace yat { |
---|
29 | namespace utility { |
---|
30 | |
---|
31 | /** |
---|
32 | \brief a class for a Segment or Interval |
---|
33 | |
---|
34 | A class for a Segment [begin, end). |
---|
35 | |
---|
36 | \since new in yat 0.7 |
---|
37 | */ |
---|
38 | template<typename T, class Compare = std::less<T> > |
---|
39 | class Segment |
---|
40 | { |
---|
41 | public: |
---|
42 | /** |
---|
43 | type the Segment holds |
---|
44 | */ |
---|
45 | typedef T value_type; |
---|
46 | |
---|
47 | /** |
---|
48 | \brief default constructor |
---|
49 | */ |
---|
50 | Segment(void) {} |
---|
51 | |
---|
52 | /** |
---|
53 | \brief Constructor |
---|
54 | |
---|
55 | Creates a segment [begin, end) |
---|
56 | */ |
---|
57 | Segment(const T& begin, const T& end) |
---|
58 | : begin_(begin), end_(end) {} |
---|
59 | |
---|
60 | /** |
---|
61 | \return reference to first element in Segment |
---|
62 | */ |
---|
63 | T& begin(void) { return begin_; } |
---|
64 | |
---|
65 | /** |
---|
66 | \return const reference to first element in Segment |
---|
67 | */ |
---|
68 | const T& begin(void) const { return begin_; } |
---|
69 | |
---|
70 | /** |
---|
71 | \return reference to first element greater than Segment |
---|
72 | */ |
---|
73 | T& end(void) { return end_; } |
---|
74 | |
---|
75 | /** |
---|
76 | \return const reference to first element greater than Segment |
---|
77 | */ |
---|
78 | const T& end(void) const { return end_; } |
---|
79 | |
---|
80 | private: |
---|
81 | T begin_; |
---|
82 | T end_; |
---|
83 | |
---|
84 | // using compiler generated copying |
---|
85 | //Segment(const Segment&); |
---|
86 | //Segment& operator=(const Segment&); |
---|
87 | }; |
---|
88 | |
---|
89 | /** |
---|
90 | \return true if all elements in \a lhs is less than all elements |
---|
91 | in \a rhs, i.e., returns false if rhs.begin() is less than |
---|
92 | lhs.end() |
---|
93 | |
---|
94 | \relates Segment |
---|
95 | |
---|
96 | \since new in yat 0.7 |
---|
97 | */ |
---|
98 | template<typename T, class Compare> |
---|
99 | bool compare(const Segment<T, Compare>& lhs, const Segment<T, Compare>& rhs) |
---|
100 | { |
---|
101 | Compare c; |
---|
102 | return ! c(rhs.begin(), lhs.end()); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | \return -1 if all elements in lhs is less than all elements in |
---|
107 | rhs; +1 if all elements in rhs is less than all elements in lhs; |
---|
108 | and 0 if lhs and rhs overlap. |
---|
109 | |
---|
110 | \relates Segment |
---|
111 | |
---|
112 | \since new in yat 0.7 |
---|
113 | */ |
---|
114 | template<typename T, class Compare> |
---|
115 | bool compare_3way(const Segment<T, Compare>& lhs, |
---|
116 | const Segment<T, Compare>& rhs) |
---|
117 | { |
---|
118 | if (compare(lhs, rhs)) |
---|
119 | return -1; |
---|
120 | if (compare(rhs, lhs)) |
---|
121 | return 1; |
---|
122 | return 0; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | \return -1 if lhs is less than all elements in rhs; +1 if all |
---|
127 | elements in rhs is less than lhs; and 0 if lhs and rhs overlap |
---|
128 | |
---|
129 | \relates Segment |
---|
130 | |
---|
131 | \since new in yat 0.7 |
---|
132 | */ |
---|
133 | template<typename T, class Compare> |
---|
134 | bool compare_3way(const T& lhs, |
---|
135 | const Segment<T, Compare>& rhs) |
---|
136 | { |
---|
137 | Compare comp; |
---|
138 | if (comp(lhs, rhs.begin())) |
---|
139 | return -1; |
---|
140 | if (comp(lhs, rhs.end())) |
---|
141 | return 0; |
---|
142 | return 1; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | \return intersection of \a a and \a b. If \a a and \b do not |
---|
147 | overlap an empty Section is returned (begin==end), but the exact |
---|
148 | value of begin is undefined. |
---|
149 | |
---|
150 | \relates Segment |
---|
151 | |
---|
152 | \since new in yat 0.7 |
---|
153 | */ |
---|
154 | template<typename T, class Compare> |
---|
155 | Segment<T, Compare> intersection(const Segment<T, Compare>& a, |
---|
156 | const Segment<T, Compare>& b) |
---|
157 | { |
---|
158 | Compare comp; |
---|
159 | Segment<T, Compare> result; |
---|
160 | |
---|
161 | result.begin() = std::max(a.begin(), b.begin(), comp); |
---|
162 | result.end() = std::max(result.begin(), |
---|
163 | std::min(a.end(), b.end(), comp), |
---|
164 | comp); |
---|
165 | return result; |
---|
166 | } |
---|
167 | |
---|
168 | }}} |
---|
169 | #endif |
---|