/*
interval.hh -- part of flowerlib
- (c) 1996--2003 Han-Wen Nienhuys
+ (c) 1996--2005 Han-Wen Nienhuys
*/
#ifndef INTERVAL_HH
#define INTERVAL_HH
-#include <assert.h>
#include "flower-proto.hh"
-#include "real.hh"
#include "drul-array.hh"
-/** a T interval. this represents the closed interval [left,right].
- No invariants. T must be a totally ordered ring (with division, anyway ..)
- At instantiation, the function infinity () has to be defined explicitely.
-
- */
+/* A T interval. This represents the closed interval [left,right].
+ No invariants. T must be a totally ordered ring (with division, anyway ..)
+ At instantiation, the function infinity () has to be defined explicitely. */
template<class T>
-struct Interval_t : public Drul_array<T> {
+struct Interval_t : public Drul_array<T>
+{
+ Drul_array<T>::elem;
+ Drul_array<T>::elem_ref;
- /* ************** */
-
- static T infinity () ;
+ static T infinity ();
static String T_to_string (T arg);
- T center () const {
- assert (!empty_b ());
- return (elem (LEFT) + elem (RIGHT)) / T (2);
- }
+ T center () const;
void translate (T t)
{
- elem (LEFT) += t;
- elem (RIGHT) += t;
+ elem_ref (LEFT) += t;
+ elem_ref (RIGHT) += t;
}
void widen (T t)
{
- elem (LEFT) -= t;
- elem (RIGHT) += t;
+ elem_ref (LEFT) -= t;
+ elem_ref (RIGHT) += t;
}
+ T distance (T t) const
+ {
+ if (t > elem (RIGHT))
+ return T (t - elem (RIGHT));
+ else if (t < elem (LEFT))
+ return T (elem (LEFT) - t);
+ else
+ return T (0);
+ }
/**
PRE
*this and h are comparable
*/
void unite (Interval_t<T> h);
void intersect (Interval_t<T> h);
- void add_point (T p) {
- elem(LEFT) = elem (LEFT) <? p;
- elem(RIGHT) = elem (RIGHT) >? p;
+ void add_point (T p)
+ {
+ elem_ref(LEFT) = elem (LEFT) <? p;
+ elem_ref(RIGHT) = elem (RIGHT) >? p;
}
T length () const;
T delta () const;
- void set_empty () ;
+ void set_empty ();
void set_full ();
- bool empty_b () const { return elem (LEFT) > elem (RIGHT); }
- bool contains_b (Interval_t<T> const&) const;
- Interval_t () {
+
+ /*
+ TODO: strip hungarian suffix.
+ */
+ bool is_empty () const
+ {
+ return elem (LEFT) > elem (RIGHT);
+ }
+ bool superset (Interval_t<T> const&) const;
+ Interval_t ()
+ {
set_empty ();
}
Interval_t (T m, T M) : Drul_array<T> (m,M)
}
Interval_t<T> &operator += (T r) {
- elem (LEFT) += r;
- elem (RIGHT) +=r;
+ elem_ref (LEFT) += r;
+ elem_ref (RIGHT) +=r;
return *this;
}
Interval_t<T> &operator *= (T r) {
- if (!empty_b ())
+ if (!is_empty ())
{
- elem (LEFT) *= r;
- elem (RIGHT) *= r;
+ elem_ref (LEFT) *= r;
+ elem_ref (RIGHT) *= r;
if (r < T (0))
swap();
return *this;
}
- Real linear_combination (Real x) const {
- return ((1.0 - x) * Real (elem (LEFT)) + (x + 1.0) * Real (elem (RIGHT))) * 0.5;
+ Real linear_combination (Real x) const
+ {
+ Drul_array<Real> da (elem (LEFT), elem (RIGHT));
+ return ::linear_combination (da, x);
}
- String to_string () const;
+ String to_string () const;
- bool elem_b (T r);
- void negate () {
+ bool contains (T r);
+ void negate ()
+ {
T r = -elem (LEFT);
T l = -elem (RIGHT);
- elem (LEFT) = l;
- elem (RIGHT) =r;
+ elem_ref (LEFT) = l;
+ elem_ref (RIGHT) =r;
}
-
+
void swap ()
{
T t = elem (LEFT);
- elem (LEFT) = elem (RIGHT);
- elem (RIGHT) = t;
+ elem_ref (LEFT) = elem (RIGHT);
+ elem_ref (RIGHT) = t;
}
};
return a*i;
}
+
+template<class T>
+inline T
+Interval_t<T>::center () const
+{
+ assert (!is_empty ());
+ return (elem (LEFT) + elem (RIGHT)) / T (2);
+}
+
// again? see flower-proto.hh
typedef Interval_t<Real> Interval;
typedef Interval_t<int> Slice; // weird name