/*
interval.hh -- part of flowerlib
-
- (c) 1996 Han-Wen Nienhuys
+
+ (c) 1996--2006 Han-Wen Nienhuys
*/
#ifndef INTERVAL_HH
#define INTERVAL_HH
-#include <assert.h>
-#include "fproto.hh"
-#include "real.hh"
+#include <math.h>
+#include "flower-proto.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 {
- T left, right;
-
- /* ************** */
-
- static T infinity() ;
- static String T_to_str (T arg);
-
- // ugh, egcs 1.02 ices on this
-// T center() { return (left + right) / T(2);}
- // and can't handle this either
- // anyone want to make a bug report?
- T center() {
- T two (2);
- return (left + right) / two;
+struct Interval_t : public Drul_array<T>
+{
+ Drul_array<T>::elem;
+ Drul_array<T>::elem_ref;
+
+ static T infinity ();
+ static String T_to_string (T arg);
+ T center () const;
+ void translate (T t)
+ {
+ elem_ref (LEFT) += t;
+ elem_ref (RIGHT) += t;
}
- void translate (T t) {
- left += t;
- right += t;
+ void widen (T t)
+ {
+ elem_ref (LEFT) -= t;
+ elem_ref (RIGHT) += t;
}
- T& idx (int j) {
- if (j==-1)
- return left;
- else if (j==1)
- return right;
+
+ 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
- assert (false);
- return left;
- }
- T& operator[](int j) {
- return idx (j);
+ return T (0);
}
- T operator[](int j) const {
- return ((Interval_t<T> *)this)->idx (j);
- }
- T &max() { return right;}
- T max() const { return right;}
- T min() const{ return left; }
- T &min(){ return left; }
/**
- PRE
- *this and h are comparable
- */
+ PRE
+ *this and h are comparable
+ */
void unite (Interval_t<T> h);
void intersect (Interval_t<T> h);
+ void add_point (T p)
+ {
+ elem_ref (LEFT) = min (elem (LEFT), p);
+ elem_ref (RIGHT) = max (elem (RIGHT), p);
+ }
+ T length () const;
+ T delta () const;
+ void set_empty ();
+ void set_full ();
- T length() const;
- void set_empty() ;
- bool empty_b() const { return left > right; }
- bool contains_b (Interval_t<T> const&) const;
- Interval_t() {
- set_empty();
+ /*
+ 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) {
- left =m;
- right = M;
+ Interval_t (Drul_array<T> const &src)
+ : Drul_array<T> (src)
+ {
}
- Interval_t<T> &operator += (T r) {
- left += r;
- right +=r;
+
+ Interval_t (T m, T M) : Drul_array<T> (m, M)
+ {
+ }
+ Interval_t<T> &operator -= (T r)
+ {
+ *this += -r;
+ return *this;
+ }
+
+ Interval_t<T> &operator += (T r)
+ {
+ elem_ref (LEFT) += r;
+ elem_ref (RIGHT) += r;
return *this;
}
- Interval_t<T> &operator *=(T r) {
- left *= r;
- right *= r;
- if (r < T(0)) {
- T t = left;
- left = right;
- right = t;
- }
+ Interval_t<T> &operator *= (T r)
+ {
+ if (!is_empty ())
+ {
+ elem_ref (LEFT) *= r;
+ elem_ref (RIGHT) *= r;
+ if (r < T (0))
+ swap ();
+ }
return *this;
}
- String str() const;
- void print () const;
- bool elt_b (T r);
- void negate () {
- T r = -left;
- T l = -right;
- left = l;
- right =r;
+
+ Real linear_combination (Real x) const
+ {
+ Drul_array<Real> da (elem (LEFT), elem (RIGHT));
+ return ::linear_combination (da, x);
+ }
+ String to_string () const;
+
+ bool contains (T r) const;
+ void negate ()
+ {
+ T r = -elem (LEFT);
+ T l = -elem (RIGHT);
+ elem_ref (LEFT) = l;
+ elem_ref (RIGHT) = r;
+ }
+
+ void swap ()
+ {
+ T t = elem (LEFT);
+ elem_ref (LEFT) = elem (RIGHT);
+ elem_ref (RIGHT) = t;
+ }
+
+ static int left_comparison (Interval_t<T> const &a, Interval_t<T> const &b)
+ {
+ return sign (a[LEFT] - b[RIGHT]);
}
};
+/**
+ inclusion ordering. Crash if not comparable.
+*/
+template<class T>
+int Interval__compare (const Interval_t<T> &, Interval_t<T> const &);
/**
- inclusion ordering. Crash if not comparable.
- */
+ Inclusion ordering. return -2 if not comparable
+*/
template<class T>
-int Interval__compare (const Interval_t<T>&,Interval_t<T> const&);
+int
+_Interval__compare (const Interval_t<T> &a, Interval_t<T> const &b);
/*
INLINE
- */
+*/
#include "compare.hh"
-TEMPLATE_INSTANTIATE_COMPARE(Interval_t<T>&, Interval__compare, template<class T>);
-
+TEMPLATE_INSTANTIATE_COMPARE (Interval_t<T> &, Interval__compare, template<class T>);
template<class T>
inline Interval_t<T>
-intersection (Interval_t<T> a, Interval_t<T> const&b)
+intersection (Interval_t<T> a, Interval_t<T> const &b)
{
a.intersect (b);
return a;
-
}
template<class T>
inline
-Interval_t<T> operator +(T a,Interval_t<T> i)
+Interval_t<T> operator + (T a, Interval_t<T> i)
{
i += a;
return i;
template<class T>
inline
-Interval_t<T> operator - (T a,Interval_t<T> i)
+Interval_t<T> operator - (T a, Interval_t<T> i)
{
i.negate ();
i += a;
template<class T>
inline
-Interval_t<T> operator +(Interval_t<T> i,T a){
- return a+i;
+Interval_t<T> operator + (Interval_t<T> i, T a)
+{
+ return a + i;
}
template<class T>
inline
-Interval_t<T> operator *(T a,Interval_t<T> i)
+Interval_t<T> operator * (T a, Interval_t<T> i)
{
i *= a;
return i;
template<class T>
inline
-Interval_t<T> operator *(Interval_t<T> i,T a){
- return a*i;
+Interval_t<T> operator * (Interval_t<T> i, T a)
+{
+ 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 fproto.hh
typedef Interval_t<Real> Interval;
-typedef Interval_t<int> Slice;
+typedef Interval_t<int> Slice; // weird name
#endif // INTERVAL_HH