The ext::range utility class template describe a range of consecutive values through its lowest and highest (first and last) bounding value (inclusive).
template <typename T>
class ext::range {
public:
typedef T value_type;
public:
const T & first;
const T & last;
const T static epsilon;
public:
range (const T &, const T &);
range (const range &);
range & operator = (range);
T size () const;
bool contains (const T &) const;
bool contains (const range &) const;
bool intersects (const range &) const;
bool touch (const range &) const;
std::pair <range, range> split (const T &) const;
std::pair <range, range> split (const range &) const;
std::pair <range, range> cutoff (const T &) const;
std::pair <range, range> cutoff (const range &) const;
void extend (const T &);
void extend (const range &);
range & operator += (const range &);
};
template <typename T>
bool ext::operator ==
(const ext::range <T> &, const ext::range <T> &);
template <typename T>
bool ext::operator !=
(const ext::range <T> &, const ext::range <T> &);
template <typename T>
bool ext::operator <
(const ext::range <T> &, const ext::range <T> &);
template <typename T>
bool ext::operator <=
(const ext::range <T> &, const ext::range <T> &);
template <typename T>
bool ext::operator >=
(const ext::range <T> &, const ext::range <T> &);
template <typename T>
bool ext::operator >
(const ext::range <T> &, const ext::range <T> &);
template <typename T>
ext::range <T> ext::operator +
(const ext::range <T> &, const ext::range <T> &);
#include <ext/c++>
ext::range <int> read_input () {
int a, b;
std::scanf ("%i %i", &a, &b);
return ext::range <int> (a, b);
};
int main () {
std::printf ("Range to poll: ");
ext::range <int> r = read_input ();
for (int i = r.first; i <= r.last; ++i) {
std::printf ("%d [%d/%d]: %d\n",
i, i - r.first + 1, r.size (), /*poll*/ (i));
};
return 0;
};
Note that the ext::range is intended to provide a common functionality that is often neccessary for common operations over ranges of integral or floating-point numbers. For complete (and mathematicaly correct) interval arithmetics see other libraries.
Note that the ext::range is implemented in terms of std::pair. In most cases you can assume the same behavior. Provided comparison operators (< and ==) only forward the calls to the appropriate std::pair operators.
The value of first and last members cannot be directly changed to avoid unintentional corruption of the range. Replace existing range with a new one instead.
Use the ext::range_set class template to handle non-continuous value ranges. That is ranges that need to be represented by multiple ext::range objects.
[index]