2 interval.hh -- part of flowerlib
4 (c) 1996 Han-Wen Nienhuys
11 #include "flower-proto.hh"
13 #include "drul-array.hh"
15 /** a T interval. this represents the closed interval [left,right].
16 No invariants. T must be a totally ordered ring (with division, anyway ..)
17 At instantiation, the function infinity () has to be defined explicitely.
21 struct Interval_t
: public Drul_array
<T
> {
25 static T
infinity () ;
26 static String
T_to_str (T arg
);
29 return (elem (LEFT
) + elem (RIGHT
)) / T (2);
39 *this and h are comparable
41 void unite (Interval_t
<T
> h
);
42 void intersect (Interval_t
<T
> h
);
46 bool empty_b () const { return elem (LEFT
) > elem (RIGHT
); }
47 bool contains_b (Interval_t
<T
> const&) const;
51 Interval_t (T m
, T M
) : Drul_array
<T
> (m
,M
)
53 Interval_t
<T
> &operator -= (T r
) {
58 Interval_t
<T
> &operator += (T r
) {
63 Interval_t
<T
> &operator *= (T r
) {
70 elem (LEFT
) = elem (RIGHT
);
77 Real
linear_combination (Real x
) const {
78 return ((1.0 - x
) * Real (elem (LEFT
)) + (x
+ 1.0) * Real (elem (RIGHT
))) * 0.5;
93 inclusion ordering. Crash if not comparable.
96 int Interval__compare (const Interval_t
<T
>&,Interval_t
<T
> const&);
99 Inclusion ordering. return -2 if not comparable
103 _Interval__compare (const Interval_t
<T
>&a
,Interval_t
<T
> const&b
);
110 #include "compare.hh"
112 TEMPLATE_INSTANTIATE_COMPARE (Interval_t
<T
>&, Interval__compare
, template<class T
>);
117 intersection (Interval_t
<T
> a
, Interval_t
<T
> const&b
)
126 Interval_t
<T
> operator + (T a
,Interval_t
<T
> i
)
134 Interval_t
<T
> operator - (Interval_t
<T
> i
, T a
)
142 Interval_t
<T
> operator - (T a
,Interval_t
<T
> i
)
151 Interval_t
<T
> operator + (Interval_t
<T
> i
,T a
){
157 Interval_t
<T
> operator * (T a
,Interval_t
<T
> i
)
165 Interval_t
<T
> operator * (Interval_t
<T
> i
,T a
){
169 // again? see flower-proto.hh
170 typedef Interval_t
<Real
> Interval
;
171 typedef Interval_t
<int> Slice
; // weird name
174 #endif // INTERVAL_HH