2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef __BASE_RANGE_HH__
30 #define __BASE_RANGE_HH__
37 * @param s range string
38 * EndExclusive Ranges are in the following format:
40 * <range> := {<start_val>}:{<end>}
41 * <start> := <end_val> | +<delta>
45 bool __parse_range(const std::string &s, T &start, T &end);
53 Range() { invalidate(); }
56 Range(const std::pair<U, U> &r)
57 : start(r.first), end(r.second)
61 Range(const Range<U> &r)
62 : start(r.start), end(r.end)
65 Range(const std::string &s)
67 if (!__parse_range(s, start, end))
72 const Range<T> &operator=(const Range<U> &r)
80 const Range<T> &operator=(const std::pair<U, U> &r)
87 const Range &operator=(const std::string &s)
89 if (!__parse_range(s, start, end))
94 void invalidate() { start = 1; end = 0; }
95 T size() const { return end - start + 1; }
96 bool valid() const { return start < end; }
100 inline std::ostream &
101 operator<<(std::ostream &o, const Range<T> &r)
103 o << '[' << r.start << "," << r.end << ']';
109 RangeEx(T start, T end)
110 { return std::make_pair(start, end - 1); }
114 RangeIn(T start, T end)
115 { return std::make_pair(start, end); }
117 template <class T, class U>
119 RangeSize(T start, U size)
120 { return std::make_pair(start, start + size - 1); }
122 ////////////////////////////////////////////////////////////////////////
124 // Range to Range Comparisons
128 * @param range1 is a range.
129 * @param range2 is a range.
130 * @return if range1 and range2 are identical.
132 template <class T, class U>
134 operator==(const Range<T> &range1, const Range<U> &range2)
136 return range1.start == range2.start && range1.end == range2.end;
140 * @param range1 is a range.
141 * @param range2 is a range.
142 * @return if range1 and range2 are not identical.
144 template <class T, class U>
146 operator!=(const Range<T> &range1, const Range<U> &range2)
148 return range1.start != range2.start || range1.end != range2.end;
152 * @param range1 is a range.
153 * @param range2 is a range.
154 * @return if range1 is less than range2 and does not overlap range1.
156 template <class T, class U>
158 operator<(const Range<T> &range1, const Range<U> &range2)
160 return range1.start < range2.start;
164 * @param range1 is a range.
165 * @param range2 is a range.
166 * @return if range1 is less than range2. range1 may overlap range2,
167 * but not extend beyond the end of range2.
169 template <class T, class U>
171 operator<=(const Range<T> &range1, const Range<U> &range2)
173 return range1.start <= range2.start;
177 * @param range1 is a range.
178 * @param range2 is a range.
179 * @return if range1 is greater than range2 and does not overlap range2.
181 template <class T, class U>
183 operator>(const Range<T> &range1, const Range<U> &range2)
185 return range1.start > range2.start;
189 * @param range1 is a range.
190 * @param range2 is a range.
191 * @return if range1 is greater than range2. range1 may overlap range2,
192 * but not extend beyond the beginning of range2.
194 template <class T, class U>
196 operator>=(const Range<T> &range1, const Range<U> &range2)
198 return range1.start >= range2.start;
201 ////////////////////////////////////////////////////////////////////////
203 // Position to Range Comparisons
207 * @param pos position compared to the range.
208 * @param range range compared against.
209 * @return indicates that position pos is within the range.
211 template <class T, class U>
213 operator==(const T &pos, const Range<U> &range)
215 return pos >= range.start && pos <= range.end;
219 * @param pos position compared to the range.
220 * @param range range compared against.
221 * @return indicates that position pos is not within the range.
223 template <class T, class U>
225 operator!=(const T &pos, const Range<U> &range)
227 return pos < range.start || pos > range.end;
231 * @param pos position compared to the range.
232 * @param range range compared against.
233 * @return indicates that position pos is below the range.
235 template <class T, class U>
237 operator<(const T &pos, const Range<U> &range)
239 return pos < range.start;
243 * @param pos position compared to the range.
244 * @param range range compared against.
245 * @return indicates that position pos is below or in the range.
247 template <class T, class U>
249 operator<=(const T &pos, const Range<U> &range)
251 return pos <= range.end;
255 * @param pos position compared to the range.
256 * @param range range compared against.
257 * @return indicates that position pos is above the range.
259 template <class T, class U>
261 operator>(const T &pos, const Range<U> &range)
263 return pos > range.end;
267 * @param pos position compared to the range.
268 * @param range range compared against.
269 * @return indicates that position pos is above or in the range.
271 template <class T, class U>
273 operator>=(const T &pos, const Range<U> &range)
275 return pos >= range.start;
278 ////////////////////////////////////////////////////////////////////////
280 // Range to Position Comparisons (for symmetry)
284 * @param range range compared against.
285 * @param pos position compared to the range.
286 * @return indicates that position pos is within the range.
288 template <class T, class U>
290 operator==(const Range<T> &range, const U &pos)
292 return pos >= range.start && pos <= range.end;
296 * @param range range compared against.
297 * @param pos position compared to the range.
298 * @return indicates that position pos is not within the range.
300 template <class T, class U>
302 operator!=(const Range<T> &range, const U &pos)
304 return pos < range.start || pos > range.end;
308 * @param range range compared against.
309 * @param pos position compared to the range.
310 * @return indicates that position pos is above the range.
312 template <class T, class U>
314 operator<(const Range<T> &range, const U &pos)
316 return range.end < pos;
320 * @param range range compared against.
321 * @param pos position compared to the range.
322 * @return indicates that position pos is above or in the range.
324 template <class T, class U>
326 operator<=(const Range<T> &range, const U &pos)
328 return range.start <= pos;
332 * @param range range compared against.
333 * @param pos position compared to the range.
334 * 'range > pos' indicates that position pos is below the range.
336 template <class T, class U>
338 operator>(const Range<T> &range, const U &pos)
340 return range.start > pos;
344 * @param range range compared against.
345 * @param pos position compared to the range.
346 * 'range >= pos' indicates that position pos is below or in the range.
348 template <class T, class U>
350 operator>=(const Range<T> &range, const U &pos)
352 return range.end >= pos;
355 #endif // __BASE_RANGE_HH__