8 #ifndef Sawyer_Interval_H
9 #define Sawyer_Interval_H
11 #include <Sawyer/Assert.h>
12 #include <Sawyer/Sawyer.h>
13 #include <boost/integer_traits.hpp>
14 #include <boost/iterator/iterator_facade.hpp>
15 #include <boost/range/iterator_range.hpp>
16 #include <boost/serialization/access.hpp>
17 #include <boost/serialization/nvp.hpp>
40 friend class boost::serialization::access;
43 void serialize(S &s,
const unsigned ) {
44 s & BOOST_SERIALIZATION_NVP(lo_);
45 s & BOOST_SERIALIZATION_NVP(hi_);
62 class ConstIterator:
public boost::iterator_facade<ConstIterator, const Value, boost::bidirectional_traversal_tag,
65 friend class boost::iterator_core_access;
67 T first_, cur_, last_;
70 ConstIterator(T first, T last, T cur): first_(first), cur_(cur), last_(last), atEnd_(
false) {}
89 Value dereference()
const {
90 ASSERT_forbid(
atEnd());
95 if (
atEnd() || other.atEnd())
96 return atEnd() && other.atEnd();
97 return cur_ == other.cur_;
108 ASSERT_require(cur_ == first_);
116 if (cur_ == first_) {
119 ASSERT_require(cur_ == last_);
137 #if 0 // [Robb Matzke 2014-05-14]: too much confusion with "hi" vs. "size". Use either baseSize or hull instead.
142 Interval(T lo, T hi): lo_(lo), hi_(hi) {
143 ASSERT_require(lo <= hi);
152 retval.lo_ = std::min(v1, v2);
153 retval.hi_ = std::max(v1, v2);
162 ASSERT_require2(lo + size >= lo,
"overflow");
168 return hull(boost::integer_traits<T>::const_min, boost::integer_traits<T>::const_max);
197 bool isEmpty()
const {
return 1==lo_ && 0==hi_; }
203 bool isWhole()
const {
return lo_==boost::integer_traits<T>::const_min && hi_==boost::integer_traits<T>::const_max; }
266 return lo_==other.lo_ && hi_==other.hi_;
269 return lo_!=other.lo_ || hi_!=other.hi_;
320 std::pair<Interval, Interval>
split(T splitPoint)
const {
323 }
else if (splitPoint <
least()) {
324 return std::make_pair(
Interval(), *
this);
325 }
else if (splitPoint <
greatest()) {
328 return std::make_pair(*
this,
Interval());
351 typedef ConstIterator const_iterator;
352 typedef ConstIterator iterator;
372 ConstIterator
end()
const {
377 boost::iterator_range<ConstIterator>
values()
const {
378 return boost::iterator_range<ConstIterator>(
begin(),
end());
384 typedef void(
Interval::*unspecified_bool)()
const;
385 void this_type_does_not_support_comparisons()
const {}
399 operator unspecified_bool()
const {
400 return isEmpty() ? 0 : &Interval::this_type_does_not_support_comparisons;
std::pair< Interval, Interval > split(T splitPoint) const
Split interval in two.
Interval operator&(const Interval &other) const
Intersection.
bool isOverlapping(const Interval &other) const
True if two intervals overlap.
Value size() const
Size of interval.
Interval & operator=(const Interval &other)
Assignment from an interval.
Bidirectional forward iterator.
Interval intersection(const Interval &other) const
Intersection.
ConstIterator end() const
Iterator positioned one past the greatest value.
bool isEmpty() const
True if interval is empty.
bool isWhole() const
True if interval covers entire space.
bool isLeftAdjacent(const Interval &right) const
Adjacency predicate.
bool isSingleton() const
True if interval is a singleton.
bool isContaining(const Interval &other) const
Containment predicate.
Interval hull(T value) const
Hull.
Interval join(const Interval &right) const
Creates an interval by joining two adjacent intervals.
Name space for the entire library.
bool isAdjacent(const Interval &other) const
Adjacency predicate.
static Interval baseSize(T lo, T size)
Construct an interval from one endpoint and a size.
Interval(T value)
Constructs a singleton interval.
ConstIterator begin() const
Iterator positioned at the least value.
bool isLeftOf(const Interval &right) const
Relative position predicate.
T least() const
Returns lower limit.
static Interval whole()
Construct an interval that covers the entire domain.
static Interval hull(T v1, T v2)
Construct an interval from two endpoints.
Interval & operator=(T value)
Assignment from a scalar.
bool operator!=(const Interval &other) const
Equality test.
bool atEnd() const
Predicate to determine if an iterator is at one of its end positions.
boost::iterator_range< ConstIterator > values() const
Iterator range for values.
ConstIterator()
Create an empty iterator.
Interval hull(const Interval &other) const
Hull.
Range of values delimited by endpoints.
bool operator==(const Interval &other) const
Equality test.
T greatest() const
Returns upper limit.
Interval(const Interval &other)
Copy-constructs an interval.
bool isRightOf(const Interval &left) const
Relative position predicate.
Interval()
Constructs an empty interval.
bool isRightAdjacent(const Interval &left) const
Adjacency predicate.