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.
28 * Authors: Nathan Binkert
32 #ifndef __BASE_RANGE_HH__
33 #define __BASE_RANGE_HH__
40 * @param s range string
41 * EndExclusive Ranges are in the following format:
43 * <range> := {<start_val>}:{<end>}
44 * <start> := <end_val> | +<delta>
48 bool __parse_range(const std::string &s, T &start, T &end);
56 Range() { invalidate(); }
59 Range(const std::pair<U, U> &r)
60 : start(r.first), end(r.second)
64 Range(const Range<U> &r)
65 : start(r.start), end(r.end)
68 Range(const std::string &s)
70 if (!__parse_range(s, start, end))
75 const Range<T> &operator=(const Range<U> &r)
83 const Range<T> &operator=(const std::pair<U, U> &r)
90 const Range &operator=(const std::string &s)
92 if (!__parse_range(s, start, end))
97 void invalidate() { start = 1; end = 0; }
98 T size() const { return end - start + 1; }
99 bool valid() const { return start < end; }
103 inline std::ostream &
104 operator<<(std::ostream &o, const Range<T> &r)
106 o << '[' << r.start << "," << r.end << ']';
112 RangeEx(T start, T end)
113 { return std::make_pair(start, end - 1); }
117 RangeIn(T start, T end)
118 { return std::make_pair(start, end); }
120 template <class T, class U>
122 RangeSize(T start, U size)
123 { return std::make_pair(start, start + size - 1); }
125 ////////////////////////////////////////////////////////////////////////
127 // Range to Range Comparisons
131 * @param range1 is a range.
132 * @param range2 is a range.
133 * @return if range1 and range2 are identical.
135 template <class T, class U>
137 operator==(const Range<T> &range1, const Range<U> &range2)
139 return range1.start == range2.start && range1.end == range2.end;
143 * @param range1 is a range.
144 * @param range2 is a range.
145 * @return if range1 and range2 are not identical.
147 template <class T, class U>
149 operator!=(const Range<T> &range1, const Range<U> &range2)
151 return range1.start != range2.start || range1.end != range2.end;
155 * @param range1 is a range.
156 * @param range2 is a range.
157 * @return if range1 is less than range2 and does not overlap range1.
159 template <class T, class U>
161 operator<(const Range<T> &range1, const Range<U> &range2)
163 return range1.start < range2.start;
167 * @param range1 is a range.
168 * @param range2 is a range.
169 * @return if range1 is less than range2. range1 may overlap range2,
170 * but not extend beyond the end of range2.
172 template <class T, class U>
174 operator<=(const Range<T> &range1, const Range<U> &range2)
176 return range1.start <= range2.start;
180 * @param range1 is a range.
181 * @param range2 is a range.
182 * @return if range1 is greater than range2 and does not overlap range2.
184 template <class T, class U>
186 operator>(const Range<T> &range1, const Range<U> &range2)
188 return range1.start > range2.start;
192 * @param range1 is a range.
193 * @param range2 is a range.
194 * @return if range1 is greater than range2. range1 may overlap range2,
195 * but not extend beyond the beginning of range2.
197 template <class T, class U>
199 operator>=(const Range<T> &range1, const Range<U> &range2)
201 return range1.start >= range2.start;
204 ////////////////////////////////////////////////////////////////////////
206 // Position to Range Comparisons
210 * @param pos position compared to the range.
211 * @param range range compared against.
212 * @return indicates that position pos is within the range.
214 template <class T, class U>
216 operator==(const T &pos, const Range<U> &range)
218 return pos >= range.start && pos <= range.end;
222 * @param pos position compared to the range.
223 * @param range range compared against.
224 * @return indicates that position pos is not within the range.
226 template <class T, class U>
228 operator!=(const T &pos, const Range<U> &range)
230 return pos < range.start || pos > range.end;
234 * @param pos position compared to the range.
235 * @param range range compared against.
236 * @return indicates that position pos is below the range.
238 template <class T, class U>
240 operator<(const T &pos, const Range<U> &range)
242 return pos < range.start;
246 * @param pos position compared to the range.
247 * @param range range compared against.
248 * @return indicates that position pos is below or in the range.
250 template <class T, class U>
252 operator<=(const T &pos, const Range<U> &range)
254 return pos <= range.end;
258 * @param pos position compared to the range.
259 * @param range range compared against.
260 * @return indicates that position pos is above the range.
262 template <class T, class U>
264 operator>(const T &pos, const Range<U> &range)
266 return pos > range.end;
270 * @param pos position compared to the range.
271 * @param range range compared against.
272 * @return indicates that position pos is above or in the range.
274 template <class T, class U>
276 operator>=(const T &pos, const Range<U> &range)
278 return pos >= range.start;
281 ////////////////////////////////////////////////////////////////////////
283 // Range to Position Comparisons (for symmetry)
287 * @param range range compared against.
288 * @param pos position compared to the range.
289 * @return indicates that position pos is within the range.
291 template <class T, class U>
293 operator==(const Range<T> &range, const U &pos)
295 return pos >= range.start && pos <= range.end;
299 * @param range range compared against.
300 * @param pos position compared to the range.
301 * @return indicates that position pos is not within the range.
303 template <class T, class U>
305 operator!=(const Range<T> &range, const U &pos)
307 return pos < range.start || pos > range.end;
311 * @param range range compared against.
312 * @param pos position compared to the range.
313 * @return indicates that position pos is above the range.
315 template <class T, class U>
317 operator<(const Range<T> &range, const U &pos)
319 return range.end < pos;
323 * @param range range compared against.
324 * @param pos position compared to the range.
325 * @return indicates that position pos is above or in the range.
327 template <class T, class U>
329 operator<=(const Range<T> &range, const U &pos)
331 return range.start <= pos;
335 * @param range range compared against.
336 * @param pos position compared to the range.
337 * 'range > pos' indicates that position pos is below the range.
339 template <class T, class U>
341 operator>(const Range<T> &range, const U &pos)
343 return range.start > pos;
347 * @param range range compared against.
348 * @param pos position compared to the range.
349 * 'range >= pos' indicates that position pos is below or in the range.
351 template <class T, class U>
353 operator>=(const Range<T> &range, const U &pos)
355 return range.end >= pos;
358 #endif // __BASE_RANGE_HH__