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
This commit is contained in:
Nathan Binkert 2004-02-07 01:16:50 -05:00
parent 6d6c91f756
commit de285ff2a0
4 changed files with 386 additions and 225 deletions

83
base/range.cc Normal file
View file

@ -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 <class T>
bool
__x_parse_range(const std::string &str, T &start, T &end)
{
std::vector<std::string> 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);

View file

@ -29,232 +29,340 @@
#ifndef __RANGE_HH__ #ifndef __RANGE_HH__
#define __RANGE_HH__ #define __RANGE_HH__
#include <assert.h> #include <cassert>
#include <string>
#include "base/intmath.hh" template <class T>
#include "base/str.hh" bool __parse_range(const std::string &s, T &start, T &end);
template<class T> template <class T>
class Range struct Range
{ {
private: private:
bool valid; /**
* @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: public:
T start; T start;
T end; T end;
public: public:
Range() {} Range()
Range(const Range &r) { operator=(r); }
Range(const T &s, const T &e)
: start(s), end(e)
{ {
valid = (start <= end); invalidate();
} }
Range(const std::string &s) { valid = parse(s); } template <class U>
Range(const Range<U> &r)
: start(r.start), end(r.end)
{}
~Range() {} template <class U>
Range(const std::pair<U, U> &r)
: start(r.first), end(r.second)
{}
int compare(const T &p); Range(const std::string &s)
bool parse(const std::string &s); {
const Range &operator=(const Range &r); parse(s);
}
bool isValid() const { return valid; } template <class U>
}; const Range<T> &operator=(const Range<U> &r)
{
template<class T>
inline int
Range<T>::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:
// <range> := {<start_val>}:{<end>}
// <end> := <end_val> | +<delta>
template<class T>
inline bool
Range<T>::parse(const std::string &str)
{
std::vector<std::string> 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<class T>
inline const Range<T> &
Range<T>::operator=(const Range<T> &r)
{
if (this != &r) {
start = r.start; start = r.start;
end = r.end; end = r.end;
return *this;
valid = r.valid;
}
else {
valid = false;
} }
return *this; template <class U>
} const Range<T> &operator=(const std::pair<U, U> &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<class T> template<class T>
inline std::ostream & inline std::ostream &
operator<<(std::ostream &o, const Range<T> &r) operator<<(std::ostream &o, const Range<T> &r)
{ {
// don't currently support output of invalid ranges // don't currently support output of invalid ranges
assert(r.isValid()); assert(r.valid());
o << r.start << ":" << r.end; o << r.start << ":" << r.end;
return o; return o;
} }
////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// //
// Compare two ranges // Range to Range Comparisons
// //
template<class T>
/**
* @param range1 is a range.
* @param range2 is a range.
* @return if range1 and range2 are identical.
*/
template<class T, class U>
inline bool inline bool
operator==(const Range<T> &l, const Range<T> &r) operator==(const Range<T> &range1, const Range<U> &range2)
{ {
// ranges must both be valid to be equal assert(range1.valid() && range2.valid());
return (l.isValid() && r.isValid() && return range1.start == range2.start && range1.end == range2.end;
(l.start == r.start) && (l.end == r.end));
} }
template<class T> /**
* @param range1 is a range.
* @param range2 is a range.
* @return if range1 and range2 are not identical.
*/
template<class T, class U>
inline bool inline bool
operator!=(const Range<T> &l, const Range<T> &r) operator!=(const Range<T> &range1, const Range<U> &range2)
{ {
// for symmetry with ==, an invalid range is not equal to any other assert(range1.valid() && range2.valid());
return (!l.isValid() || !r.isValid() || return range1.start != range2.start || range1.end != range2.end;
(l.start != r.start) || (l.end != r.end));
} }
////////////////////////////////////////// /**
// * @param range1 is a range.
// Compare position to a range * @param range2 is a range.
// * @return if range1 is less than range2 and does not overlap range1.
// - 'pos == range' indicates that position pos is within the given range. */
// This test always returns false if the range is invalid. template<class T, class U>
// inline bool
// - 'pos < range' and 'pos > range' indicate that the position is operator<(const Range<T> &range1, const Range<U> &range2)
// before the start of or after the end of the range, respectively. {
// The range must be valid for these comparisons to be made. assert(range1.valid() && range2.valid());
// return range1.end <= range2.start;
// 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. range1 may overlap range2,
* but not extend beyond the end of range2.
*/
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;
}
/**
* @param range1 is a range.
* @param range2 is a range.
* @return if range1 is greater than range2 and does not overlap range2.
*/
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;
}
/**
* @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<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;
}
////////////////////////////////////////////////////////////////////////
// //
// 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<class T, class U>
inline bool
operator==(const T &pos, const Range<U> &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<class T, class U>
inline bool
operator!=(const T &pos, const Range<U> &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<class T, class U>
inline bool
operator<(const T &pos, const Range<U> &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<class T, class U>
inline bool
operator<=(const T &pos, const Range<U> &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<class T, class U>
inline bool
operator>(const T &pos, const Range<U> &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<class T, class U>
inline bool
operator>=(const T &pos, const Range<U> &range)
{
assert(range.valid());
return pos >= range.start;
}
////////////////////////////////////////////////////////////////////////
// //
// Basic comparisons // Range to Position Comparisons (for symmetry)
// //
template<class T>
inline bool
operator==(const T &pos, const Range<T> &range)
{ return range.isValid() && pos >= range.start && pos <= range.end; }
template<class T> /**
* @param range range compared against.
* @param pos position compared to the range.
* @return indicates that position pos is within the range.
*/
template<class T, class U>
inline bool inline bool
operator<(const T &pos, const Range<T> &range) operator==(const Range<T> &range, const U &pos)
{ assert(range.isValid()); return pos < range.start; } {
assert(range.valid());
return pos >= range.start && pos < range.end;
}
template<class T> /**
* @param range range compared against.
* @param pos position compared to the range.
* @return indicates that position pos is not within the range.
*/
template<class T, class U>
inline bool inline bool
operator>(const T &pos, const Range<T> &range) operator!=(const Range<T> &range, const U &pos)
{ assert(range.isValid()); return pos > range.end; } {
assert(range.valid());
return pos < range.start || pos >= range.end;
}
// /**
// Derived comparisons * @param range range compared against.
// * @param pos position compared to the range.
template<class T> * @return indicates that position pos is above the range.
*/
template<class T, class U>
inline bool inline bool
operator<=(const T &pos, const Range<T> &range) operator<(const Range<T> &range, const U &pos)
{ assert(range.isValid()); return pos <= range.end; } {
assert(range.valid());
return range.end <= pos;
}
template<class T> /**
* @param range range compared against.
* @param pos position compared to the range.
* @return indicates that position pos is above or in the range.
*/
template<class T, class U>
inline bool inline bool
operator>=(const T &pos, const Range<T> &range) operator<=(const Range<T> &range, const U &pos)
{ assert(range.isValid()); return pos >= range.start; } {
assert(range.valid());
return range.start <= pos;
}
template<class T> /**
* @param range range compared against.
* @param pos position compared to the range.
* 'range > pos' indicates that position pos is below the range.
*/
template<class T, class U>
inline bool inline bool
operator!=(const T &pos, const Range<T> &range) operator>(const Range<T> &range, const U &pos)
{ return !(pos == range); } {
assert(range.valid());
return range.start > pos;
}
// /**
// Define symmetric comparisons based on above * @param range range compared against.
// * @param pos position compared to the range.
template<class T> * 'range >= pos' indicates that position pos is below or in the range.
*/
template<class T, class U>
inline bool inline bool
operator>(const Range<T> &range, const T &pos) operator>=(const Range<T> &range, const U &pos)
{ return pos < range; } {
assert(range.valid());
template<class T> return range.end > pos;
inline bool }
operator<(const Range<T> &range, const T &pos)
{ return pos > range; }
template<class T>
inline bool
operator<=(const Range<T> &range, const T &pos)
{ return pos >= range; }
template<class T>
inline bool
operator>=(const Range<T> &range, const T &pos)
{ return pos <= range; }
template<class T>
inline bool
operator==(const Range<T> &range, const T &pos)
{ return (pos == range); }
template<class T>
inline bool
operator!=(const Range<T> &range, const T &pos)
{ return (pos != range); }
#endif // __RANGE_HH__ #endif // __RANGE_HH__

View file

@ -49,7 +49,7 @@ nmtest: nmtest.o object_file.o symtab.o misc.o str.o
offtest: offtest.o offtest: offtest.o
$(CXX) $(LFLAGS) -o $@ $^ $(CXX) $(LFLAGS) -o $@ $^
rangetest: rangetest.o str.o rangetest: rangetest.o range.o str.o
$(CXX) $(LFLAGS) -o $@ $^ $(CXX) $(LFLAGS) -o $@ $^
stattest: cprintf.o hostinfo.o misc.o python.o statistics.o stattest.o \ stattest: cprintf.o hostinfo.o misc.o python.o statistics.o stattest.o \

View file

@ -31,11 +31,12 @@
#include "base/range.hh" #include "base/range.hh"
using namespace std;
int int
main() main()
{ {
Range<int> r1(9, 28); Range<int> r1(make_pair(9, 28));
Range<unsigned> r2("0x1000:+0x100"); Range<unsigned> r2("0x1000:+0x100");
cout << r1 << "\n" cout << r1 << "\n"
@ -44,61 +45,30 @@ main()
#define RANGETEST(X, C, Y) \ #define RANGETEST(X, C, Y) \
cout << X << " "#C" " << Y << " => " << ((X C Y) ? "true" : "false") << "\n" cout << X << " "#C" " << Y << " => " << ((X C Y) ? "true" : "false") << "\n"
int i1 = 10; #define TESTEM(X, Y) do { \
int i2 = 0x1001; RANGETEST(X, < , Y); \
RANGETEST(i1, < , r1); RANGETEST(X, <=, Y); \
RANGETEST(i1, <=, r1); RANGETEST(X, > , Y); \
RANGETEST(i1, > , r1); RANGETEST(X, >=, Y); \
RANGETEST(i1, >=, r1); RANGETEST(X, ==, Y); \
RANGETEST(i1, ==, r1); RANGETEST(X, !=, Y); \
RANGETEST(i1, !=, r1); RANGETEST(Y, < , X); \
RANGETEST(r1, < , i1); RANGETEST(Y, <=, X); \
RANGETEST(r1, <=, i1); RANGETEST(Y, > , X); \
RANGETEST(r1, > , i1); RANGETEST(Y, >=, X); \
RANGETEST(r1, >=, i1); RANGETEST(Y, ==, X); \
RANGETEST(r1, ==, i1); RANGETEST(Y, !=, X); \
RANGETEST(r1, !=, i1); } while (0)
RANGETEST(i2, < , r1); TESTEM(8, r1);
RANGETEST(i2, <=, r1); TESTEM(9, r1);
RANGETEST(i2, > , r1); TESTEM(27, r1);
RANGETEST(i2, >=, r1); TESTEM(28, 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);
unsigned u1 = 10; TESTEM(0x0fff, r2);
unsigned u2 = 0x1001; TESTEM(0x1000, r2);
RANGETEST(u1, < , r2); TESTEM(0x10ff, r2);
RANGETEST(u1, <=, r2); TESTEM(0x1100, 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);
return 0; return 0;
} }