From de285ff2a0c49fdf61dba7c4fcc6b3804e045e56 Mon Sep 17 00:00:00 2001 From: Nathan Binkert Date: Sat, 7 Feb 2004 01:16:50 -0500 Subject: [PATCH] Totally rework the Range class. Now the range is from [start, end) to be consistent with the way that the stl works. It also makes lots of other stuff easier. (Maybe those guys were smart?) Overload the various comparison operators so that you can test for overlapping of ranges, etc. base/range.hh: Totally rework the Range class. Now the range is from [start, end) to be consistent with the way that the stl works. It also makes lots of other stuff easier. (Maybe those guys were smart?) Overload the various comparison operators so that you can test for overlapping of ranges, etc. make parse function private and offer operator= instead isValid -> valid and for you erik, I add comments test/Makefile: add range.o test/rangetest.cc: better tests --HG-- extra : convert_revision : dd58720aa3d02f20b03e933f2eaa3320c82bb27a --- base/range.cc | 83 +++++++++ base/range.hh | 448 ++++++++++++++++++++++++++++------------------ test/Makefile | 2 +- test/rangetest.cc | 78 +++----- 4 files changed, 386 insertions(+), 225 deletions(-) create mode 100644 base/range.cc diff --git a/base/range.cc b/base/range.cc new file mode 100644 index 000000000..0a4876e89 --- /dev/null +++ b/base/range.cc @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2003 The Regents of The University of Michigan + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "base/intmath.hh" +#include "base/range.hh" +#include "base/str.hh" + +using namespace std; + +template +bool +__x_parse_range(const std::string &str, T &start, T &end) +{ + std::vector values; + tokenize(values, str, ':'); + + T thestart, theend; + + if (values.size() != 2) + return false; + + std::string s = values[0]; + std::string e = values[1]; + + if (!to_number(s, thestart)) + return false; + + bool increment = (e[0] == '+'); + if (increment) + e = e.substr(1); + + if (!to_number(e, theend)) + return false; + + if (increment) + theend += thestart; + + start = thestart; + end = theend; + + return true; +} + +#define RANGE_PARSE(type) \ +template<> bool \ +__parse_range(const std::string &s, type &start, type &end) \ +{ return __x_parse_range(s, start, end); } + +RANGE_PARSE(unsigned long long); +RANGE_PARSE(signed long long); +RANGE_PARSE(unsigned long); +RANGE_PARSE(signed long); +RANGE_PARSE(unsigned int); +RANGE_PARSE(signed int); +RANGE_PARSE(unsigned short); +RANGE_PARSE(signed short); +RANGE_PARSE(unsigned char); +RANGE_PARSE(signed char); diff --git a/base/range.hh b/base/range.hh index 3443bf246..34bd34136 100644 --- a/base/range.hh +++ b/base/range.hh @@ -29,232 +29,340 @@ #ifndef __RANGE_HH__ #define __RANGE_HH__ -#include +#include +#include -#include "base/intmath.hh" -#include "base/str.hh" +template +bool __parse_range(const std::string &s, T &start, T &end); -template -class Range +template +struct Range { private: - bool valid; + /** + * @param s range string + * Ranges are in the following format: + * := {}:{} + * := | + + */ + void + parse(const std::string &s) + { + if (!__parse_range(s, start, end)) + invalidate(); + } public: T start; T end; public: - Range() {} - - Range(const Range &r) { operator=(r); } - - Range(const T &s, const T &e) - : start(s), end(e) + Range() { - valid = (start <= end); + invalidate(); } - Range(const std::string &s) { valid = parse(s); } + template + Range(const Range &r) + : start(r.start), end(r.end) + {} - ~Range() {} + template + Range(const std::pair &r) + : start(r.first), end(r.second) + {} - int compare(const T &p); - bool parse(const std::string &s); - const Range &operator=(const Range &r); + Range(const std::string &s) + { + parse(s); + } - bool isValid() const { return valid; } -}; - - -template -inline int -Range::compare(const T &p) -{ - assert(isValid()); - - if (p < start) - return -1; - else if (p > end) - return 1; - else - return 0; -} - -// Parse a range string -// -// Ranges are in the following format: -// := {}:{} -// := | + -template -inline bool -Range::parse(const std::string &str) -{ - std::vector values; - tokenize(values, str, ':'); - - T thestart, theend; - - if (values.size() != 2) - return false; - - std::string s = values[0]; - std::string e = values[1]; - - if (!to_number(s, thestart)) - return false; - - bool increment = (e[0] == '+'); - if (increment) - e = e.substr(1); - - if (!to_number(e, theend)) - return false; - - if (increment) - theend += thestart; - - start = thestart; - end = theend; - - if (start > end) - return false; - - return true; -} - - -template -inline const Range & -Range::operator=(const Range &r) -{ - if (this != &r) { + template + const Range &operator=(const Range &r) + { start = r.start; end = r.end; - - valid = r.valid; - } - else { - valid = false; + return *this; } - return *this; -} + template + const Range &operator=(const std::pair &r) + { + start = r.first; + end = r.second; + return *this; + } + + const Range &operator=(const std::string &s) + { + parse(s); + return *this; + } + + void invalidate() { start = 0; end = 0; } + bool size() const { return end - start; } + bool valid() const { return start < end; } +}; template inline std::ostream & operator<<(std::ostream &o, const Range &r) { // don't currently support output of invalid ranges - assert(r.isValid()); + assert(r.valid()); o << r.start << ":" << r.end; return o; } -////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////// // -// Compare two ranges +// Range to Range Comparisons // -template + +/** + * @param range1 is a range. + * @param range2 is a range. + * @return if range1 and range2 are identical. + */ +template inline bool -operator==(const Range &l, const Range &r) +operator==(const Range &range1, const Range &range2) { - // ranges must both be valid to be equal - return (l.isValid() && r.isValid() && - (l.start == r.start) && (l.end == r.end)); + assert(range1.valid() && range2.valid()); + return range1.start == range2.start && range1.end == range2.end; } -template +/** + * @param range1 is a range. + * @param range2 is a range. + * @return if range1 and range2 are not identical. + */ +template inline bool -operator!=(const Range &l, const Range &r) +operator!=(const Range &range1, const Range &range2) { - // for symmetry with ==, an invalid range is not equal to any other - return (!l.isValid() || !r.isValid() || - (l.start != r.start) || (l.end != r.end)); + assert(range1.valid() && range2.valid()); + return range1.start != range2.start || range1.end != range2.end; } -////////////////////////////////////////// -// -// Compare position to a range -// -// - 'pos == range' indicates that position pos is within the given range. -// This test always returns false if the range is invalid. -// -// - 'pos < range' and 'pos > range' indicate that the position is -// before the start of or after the end of the range, respectively. -// The range must be valid for these comparisons to be made. -// -// All other comparisons do the obvious thing based on these definitions. +/** + * @param range1 is a range. + * @param range2 is a range. + * @return if range1 is less than range2 and does not overlap range1. + */ +template +inline bool +operator<(const Range &range1, const Range &range2) +{ + assert(range1.valid() && range2.valid()); + return range1.end <= range2.start; +} + +/** + * @param range1 is a range. + * @param range2 is a range. + * @return if range1 is less than range2. range1 may overlap range2, + * but not extend beyond the end of range2. + */ +template +inline bool +operator<=(const Range &range1, const Range &range2) +{ + assert(range1.valid() && range2.valid()); + return range1.start <= range2.start && range1.end <= range2.end; +} + +/** + * @param range1 is a range. + * @param range2 is a range. + * @return if range1 is greater than range2 and does not overlap range2. + */ +template +inline bool +operator>(const Range &range1, const Range &range2) +{ + assert(range1.valid() && range2.valid()); + return range1.start >= range2.end; +} + +/** + * @param range1 is a range. + * @param range2 is a range. + * @return if range1 is greater than range2. range1 may overlap range2, + * but not extend beyond the beginning of range2. + */ +template +inline bool +operator>=(const Range &range1, const Range &range2) +{ + assert(range1.valid() && range2.valid()); + return range1.start >= range2.start && range1.end >= range2.end; +} + +//////////////////////////////////////////////////////////////////////// // +// Position to Range Comparisons // +/** + * @param pos position compared to the range. + * @param range range compared against. + * @return indicates that position pos is within the range. + */ +template +inline bool +operator==(const T &pos, const Range &range) +{ + assert(range.valid()); + return pos >= range.start && pos < range.end; +} + +/** + * @param pos position compared to the range. + * @param range range compared against. + * @return indicates that position pos is not within the range. + */ +template +inline bool +operator!=(const T &pos, const Range &range) +{ + assert(range.valid()); + return pos < range.start || pos >= range.end; +} + +/** + * @param pos position compared to the range. + * @param range range compared against. + * @return indicates that position pos is below the range. + */ +template +inline bool +operator<(const T &pos, const Range &range) +{ + assert(range.valid()); + return pos < range.start; +} + +/** + * @param pos position compared to the range. + * @param range range compared against. + * @return indicates that position pos is below or in the range. + */ +template +inline bool +operator<=(const T &pos, const Range &range) +{ + assert(range.valid()); + return pos < range.end; +} + +/** + * @param pos position compared to the range. + * @param range range compared against. + * @return indicates that position pos is above the range. + */ +template +inline bool +operator>(const T &pos, const Range &range) +{ + assert(range.valid()); + return pos >= range.end; +} + +/** + * @param pos position compared to the range. + * @param range range compared against. + * @return indicates that position pos is above or in the range. + */ +template +inline bool +operator>=(const T &pos, const Range &range) +{ + assert(range.valid()); + return pos >= range.start; +} + +//////////////////////////////////////////////////////////////////////// // -// Basic comparisons +// Range to Position Comparisons (for symmetry) // -template -inline bool -operator==(const T &pos, const Range &range) -{ return range.isValid() && pos >= range.start && pos <= range.end; } -template +/** + * @param range range compared against. + * @param pos position compared to the range. + * @return indicates that position pos is within the range. + */ +template inline bool -operator<(const T &pos, const Range &range) -{ assert(range.isValid()); return pos < range.start; } +operator==(const Range &range, const U &pos) +{ + assert(range.valid()); + return pos >= range.start && pos < range.end; +} -template +/** + * @param range range compared against. + * @param pos position compared to the range. + * @return indicates that position pos is not within the range. + */ +template inline bool -operator>(const T &pos, const Range &range) -{ assert(range.isValid()); return pos > range.end; } +operator!=(const Range &range, const U &pos) +{ + assert(range.valid()); + return pos < range.start || pos >= range.end; +} -// -// Derived comparisons -// -template +/** + * @param range range compared against. + * @param pos position compared to the range. + * @return indicates that position pos is above the range. + */ +template inline bool -operator<=(const T &pos, const Range &range) -{ assert(range.isValid()); return pos <= range.end; } +operator<(const Range &range, const U &pos) +{ + assert(range.valid()); + return range.end <= pos; +} -template +/** + * @param range range compared against. + * @param pos position compared to the range. + * @return indicates that position pos is above or in the range. + */ +template inline bool -operator>=(const T &pos, const Range &range) -{ assert(range.isValid()); return pos >= range.start; } +operator<=(const Range &range, const U &pos) +{ + assert(range.valid()); + return range.start <= pos; +} -template +/** + * @param range range compared against. + * @param pos position compared to the range. + * 'range > pos' indicates that position pos is below the range. + */ +template inline bool -operator!=(const T &pos, const Range &range) -{ return !(pos == range); } +operator>(const Range &range, const U &pos) +{ + assert(range.valid()); + return range.start > pos; +} -// -// Define symmetric comparisons based on above -// -template +/** + * @param range range compared against. + * @param pos position compared to the range. + * 'range >= pos' indicates that position pos is below or in the range. + */ +template inline bool -operator>(const Range &range, const T &pos) -{ return pos < range; } - -template -inline bool -operator<(const Range &range, const T &pos) -{ return pos > range; } - -template -inline bool -operator<=(const Range &range, const T &pos) -{ return pos >= range; } - -template -inline bool -operator>=(const Range &range, const T &pos) -{ return pos <= range; } - -template -inline bool -operator==(const Range &range, const T &pos) -{ return (pos == range); } - -template -inline bool -operator!=(const Range &range, const T &pos) -{ return (pos != range); } +operator>=(const Range &range, const U &pos) +{ + assert(range.valid()); + return range.end > pos; +} #endif // __RANGE_HH__ diff --git a/test/Makefile b/test/Makefile index 2c3780c93..d62dba64a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -49,7 +49,7 @@ nmtest: nmtest.o object_file.o symtab.o misc.o str.o offtest: offtest.o $(CXX) $(LFLAGS) -o $@ $^ -rangetest: rangetest.o str.o +rangetest: rangetest.o range.o str.o $(CXX) $(LFLAGS) -o $@ $^ stattest: cprintf.o hostinfo.o misc.o python.o statistics.o stattest.o \ diff --git a/test/rangetest.cc b/test/rangetest.cc index f44d835b7..c38c13fd1 100644 --- a/test/rangetest.cc +++ b/test/rangetest.cc @@ -31,11 +31,12 @@ #include "base/range.hh" +using namespace std; int main() { - Range r1(9, 28); + Range r1(make_pair(9, 28)); Range r2("0x1000:+0x100"); cout << r1 << "\n" @@ -44,61 +45,30 @@ main() #define RANGETEST(X, C, Y) \ cout << X << " "#C" " << Y << " => " << ((X C Y) ? "true" : "false") << "\n" - int i1 = 10; - int i2 = 0x1001; - RANGETEST(i1, < , r1); - RANGETEST(i1, <=, r1); - RANGETEST(i1, > , r1); - RANGETEST(i1, >=, r1); - RANGETEST(i1, ==, r1); - RANGETEST(i1, !=, r1); - RANGETEST(r1, < , i1); - RANGETEST(r1, <=, i1); - RANGETEST(r1, > , i1); - RANGETEST(r1, >=, i1); - RANGETEST(r1, ==, i1); - RANGETEST(r1, !=, i1); +#define TESTEM(X, Y) do { \ + RANGETEST(X, < , Y); \ + RANGETEST(X, <=, Y); \ + RANGETEST(X, > , Y); \ + RANGETEST(X, >=, Y); \ + RANGETEST(X, ==, Y); \ + RANGETEST(X, !=, Y); \ + RANGETEST(Y, < , X); \ + RANGETEST(Y, <=, X); \ + RANGETEST(Y, > , X); \ + RANGETEST(Y, >=, X); \ + RANGETEST(Y, ==, X); \ + RANGETEST(Y, !=, X); \ +} while (0) - RANGETEST(i2, < , r1); - RANGETEST(i2, <=, r1); - RANGETEST(i2, > , r1); - RANGETEST(i2, >=, r1); - RANGETEST(i2, ==, r1); - RANGETEST(i2, !=, r1); - RANGETEST(r1, < , i2); - RANGETEST(r1, <=, i2); - RANGETEST(r1, > , i2); - RANGETEST(r1, >=, i2); - RANGETEST(r1, ==, i2); - RANGETEST(r1, !=, i2); + TESTEM(8, r1); + TESTEM(9, r1); + TESTEM(27, r1); + TESTEM(28, r1); - unsigned u1 = 10; - unsigned u2 = 0x1001; - RANGETEST(u1, < , r2); - RANGETEST(u1, <=, r2); - RANGETEST(u1, > , r2); - RANGETEST(u1, >=, r2); - RANGETEST(u1, ==, r2); - RANGETEST(u1, !=, r2); - RANGETEST(r2, < , u1); - RANGETEST(r2, <=, u1); - RANGETEST(r2, > , u1); - RANGETEST(r2, >=, u1); - RANGETEST(r2, ==, u1); - RANGETEST(r2, !=, u1); - - RANGETEST(u2, < , r2); - RANGETEST(u2, <=, r2); - RANGETEST(u2, > , r2); - RANGETEST(u2, >=, r2); - RANGETEST(u2, ==, r2); - RANGETEST(u2, !=, r2); - RANGETEST(r2, < , u2); - RANGETEST(r2, <=, u2); - RANGETEST(r2, > , u2); - RANGETEST(r2, >=, u2); - RANGETEST(r2, ==, u2); - RANGETEST(r2, !=, u2); + TESTEM(0x0fff, r2); + TESTEM(0x1000, r2); + TESTEM(0x10ff, r2); + TESTEM(0x1100, r2); return 0; }