First steps toward getting full system to work with
[gem5.git] / base / range.hh
index 34bd34136d376ba4ee5aa6decc4d4a3b5bd2a591..4e3e0fd6e28557ec149948472c60c40435816522 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003 The Regents of The University of Michigan
+ * Copyright (c) 2002-2005 The Regents of The University of Michigan
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef __RANGE_HH__
-#define __RANGE_HH__
+#ifndef __BASE_RANGE_HH__
+#define __BASE_RANGE_HH__
 
 #include <cassert>
+#include <iostream>
 #include <string>
 
+/**
+ * @param s range string
+ * EndExclusive Ranges are in the following format:
+ * @verbatim
+ *    <range> := {<start_val>}:{<end>}
+ *    <start>   := <end_val> | +<delta>
+ * @endverbatim
+ */
 template <class T>
 bool __parse_range(const std::string &s, T &start, T &end);
 
 template <class T>
 struct Range
 {
-  private:
-    /**
-     * @param s range string
-     * Ranges are in the following format:
-     *    <range> := {<start_val>}:{<end>}
-     *    <end>   := <end_val> | +<delta>
-     */
-    void
-    parse(const std::string &s)
-    {
-        if (!__parse_range(s, start, end))
-            invalidate();
-    }
-
-  public:
     T start;
     T end;
 
-  public:
-    Range()
-    {
-        invalidate();
-    }
+    Range() { invalidate(); }
 
     template <class U>
-    Range(const Range<U> &r)
-        : start(r.start), end(r.end)
+    Range(const std::pair<U, U> &r)
+        : start(r.first), end(r.second)
     {}
 
     template <class U>
-    Range(const std::pair<U, U> &r)
-        : start(r.first), end(r.second)
+    Range(const Range<U> &r)
+        : start(r.start), end(r.end)
     {}
 
     Range(const std::string &s)
     {
-        parse(s);
+        if (!__parse_range(s, start, end))
+            invalidate();
     }
 
     template <class U>
@@ -95,25 +86,39 @@ struct Range
 
     const Range &operator=(const std::string &s)
     {
-        parse(s);
+        if (!__parse_range(s, start, end))
+            invalidate();
         return *this;
     }
 
-    void invalidate() { start = 0; end = 0; }
-    bool size() const { return end - start; }
+    void invalidate() { start = 1; end = 0; }
+    T size() const { return end - start + 1; }
     bool valid() const { return start < end; }
 };
 
-template<class T>
+template <class T>
 inline std::ostream &
 operator<<(std::ostream &o, const Range<T> &r)
 {
-    // don't currently support output of invalid ranges
-    assert(r.valid());
-    o << r.start << ":" << r.end;
+    o << '[' << r.start << "," << r.end << ']';
     return o;
 }
 
+template <class T>
+inline Range<T>
+RangeEx(T start, T end)
+{ return std::make_pair(start, end - 1); }
+
+template <class T>
+inline Range<T>
+RangeIn(T start, T end)
+{ return std::make_pair(start, end); }
+
+template <class T, class U>
+inline Range<T>
+RangeSize(T start, U size)
+{ return std::make_pair(start, start + size - 1); }
+
 ////////////////////////////////////////////////////////////////////////
 //
 // Range to Range Comparisons
@@ -124,11 +129,10 @@ operator<<(std::ostream &o, const Range<T> &r)
  * @param range2 is a range.
  * @return if range1 and range2 are identical.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator==(const Range<T> &range1, const Range<U> &range2)
 {
-    assert(range1.valid() && range2.valid());
     return range1.start == range2.start && range1.end == range2.end;
 }
 
@@ -137,11 +141,10 @@ operator==(const Range<T> &range1, const Range<U> &range2)
  * @param range2 is a range.
  * @return if range1 and range2 are not identical.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator!=(const Range<T> &range1, const Range<U> &range2)
 {
-    assert(range1.valid() && range2.valid());
     return range1.start != range2.start || range1.end != range2.end;
 }
 
@@ -150,12 +153,11 @@ operator!=(const Range<T> &range1, const Range<U> &range2)
  * @param range2 is a range.
  * @return if range1 is less than range2 and does not overlap range1.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator<(const Range<T> &range1, const Range<U> &range2)
 {
-    assert(range1.valid() && range2.valid());
-    return range1.end <= range2.start;
+    return range1.start < range2.start;
 }
 
 /**
@@ -164,12 +166,11 @@ operator<(const Range<T> &range1, const Range<U> &range2)
  * @return if range1 is less than range2.  range1 may overlap range2,
  * but not extend beyond the end of range2.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator<=(const Range<T> &range1, const Range<U> &range2)
 {
-    assert(range1.valid() && range2.valid());
-    return range1.start <= range2.start && range1.end <= range2.end;
+    return range1.start <= range2.start;
 }
 
 /**
@@ -177,12 +178,11 @@ operator<=(const Range<T> &range1, const Range<U> &range2)
  * @param range2 is a range.
  * @return if range1 is greater than range2 and does not overlap range2.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator>(const Range<T> &range1, const Range<U> &range2)
 {
-    assert(range1.valid() && range2.valid());
-    return range1.start >= range2.end;
+    return range1.start > range2.start;
 }
 
 /**
@@ -191,12 +191,11 @@ operator>(const Range<T> &range1, const Range<U> &range2)
  * @return if range1 is greater than range2.  range1 may overlap range2,
  * but not extend beyond the beginning of range2.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator>=(const Range<T> &range1, const Range<U> &range2)
 {
-    assert(range1.valid() && range2.valid());
-    return range1.start >= range2.start && range1.end >= range2.end;
+    return range1.start >= range2.start;
 }
 
 ////////////////////////////////////////////////////////////////////////
@@ -209,12 +208,11 @@ operator>=(const Range<T> &range1, const Range<U> &range2)
  * @param range range compared against.
  * @return indicates that position pos is within the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator==(const T &pos, const Range<U> &range)
 {
-    assert(range.valid());
-    return pos >= range.start && pos < range.end;
+    return pos >= range.start && pos <= range.end;
 }
 
 /**
@@ -222,12 +220,11 @@ operator==(const T &pos, const Range<U> &range)
  * @param range range compared against.
  * @return indicates that position pos is not within the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator!=(const T &pos, const Range<U> &range)
 {
-    assert(range.valid());
-    return pos < range.start || pos >= range.end;
+    return pos < range.start || pos > range.end;
 }
 
 /**
@@ -235,11 +232,10 @@ operator!=(const T &pos, const Range<U> &range)
  * @param range range compared against.
  * @return indicates that position pos is below the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator<(const T &pos, const Range<U> &range)
 {
-    assert(range.valid());
     return pos < range.start;
 }
 
@@ -248,12 +244,11 @@ operator<(const T &pos, const Range<U> &range)
  * @param range range compared against.
  * @return indicates that position pos is below or in the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator<=(const T &pos, const Range<U> &range)
 {
-    assert(range.valid());
-    return pos < range.end;
+    return pos <= range.end;
 }
 
 /**
@@ -261,12 +256,11 @@ operator<=(const T &pos, const Range<U> &range)
  * @param range range compared against.
  * @return indicates that position pos is above the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator>(const T &pos, const Range<U> &range)
 {
-    assert(range.valid());
-    return pos >= range.end;
+    return pos > range.end;
 }
 
 /**
@@ -274,11 +268,10 @@ operator>(const T &pos, const Range<U> &range)
  * @param range range compared against.
  * @return indicates that position pos is above or in the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator>=(const T &pos, const Range<U> &range)
 {
-    assert(range.valid());
     return pos >= range.start;
 }
 
@@ -292,12 +285,11 @@ operator>=(const T &pos, const Range<U> &range)
  * @param pos position compared to the range.
  * @return indicates that position pos is within the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator==(const Range<T> &range, const U &pos)
 {
-    assert(range.valid());
-    return pos >= range.start && pos < range.end;
+    return pos >= range.start && pos <= range.end;
 }
 
 /**
@@ -305,12 +297,11 @@ operator==(const Range<T> &range, const U &pos)
  * @param pos position compared to the range.
  * @return indicates that position pos is not within the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator!=(const Range<T> &range, const U &pos)
 {
-    assert(range.valid());
-    return pos < range.start || pos >= range.end;
+    return pos < range.start || pos > range.end;
 }
 
 /**
@@ -318,12 +309,11 @@ operator!=(const Range<T> &range, const U &pos)
  * @param pos position compared to the range.
  * @return indicates that position pos is above the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator<(const Range<T> &range, const U &pos)
 {
-    assert(range.valid());
-    return range.end <= pos;
+    return range.end < pos;
 }
 
 /**
@@ -331,11 +321,10 @@ operator<(const Range<T> &range, const U &pos)
  * @param pos position compared to the range.
  * @return indicates that position pos is above or in the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator<=(const Range<T> &range, const U &pos)
 {
-    assert(range.valid());
     return range.start <= pos;
 }
 
@@ -344,11 +333,10 @@ operator<=(const Range<T> &range, const U &pos)
  * @param pos position compared to the range.
  * 'range > pos' indicates that position pos is below the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator>(const Range<T> &range, const U &pos)
 {
-    assert(range.valid());
     return range.start > pos;
 }
 
@@ -357,12 +345,11 @@ operator>(const Range<T> &range, const U &pos)
  * @param pos position compared to the range.
  * 'range >= pos' indicates that position pos is below or in the range.
  */
-template<class T, class U>
+template <class T, class U>
 inline bool
 operator>=(const Range<T> &range, const U &pos)
 {
-    assert(range.valid());
-    return range.end > pos;
+    return range.end >= pos;
 }
 
-#endif // __RANGE_HH__
+#endif // __BASE_RANGE_HH__