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_string (T arg
);
29 return (elem (LEFT
) + elem (RIGHT
)) / T (2);
44 *this and h are comparable
46 void unite (Interval_t
<T
> h
);
47 void intersect (Interval_t
<T
> h
);
48 void add_point (T p
) {
49 elem(LEFT
) = elem (LEFT
) <? p
;
50 elem(RIGHT
) = elem (RIGHT
) >? p
;
56 bool empty_b () const { return elem (LEFT
) > elem (RIGHT
); }
57 bool contains_b (Interval_t
<T
> const&) const;
61 Interval_t (T m
, T M
) : Drul_array
<T
> (m
,M
)
64 Interval_t
<T
> &operator -= (T r
) {
69 Interval_t
<T
> &operator += (T r
) {
74 Interval_t
<T
> &operator *= (T r
) {
86 Real
linear_combination (Real x
) const {
87 return ((1.0 - x
) * Real (elem (LEFT
)) + (x
+ 1.0) * Real (elem (RIGHT
))) * 0.5;
89 String
to_string () const;
102 elem (LEFT
) = elem (RIGHT
);
109 inclusion ordering. Crash if not comparable.
112 int Interval__compare (const Interval_t
<T
>&,Interval_t
<T
> const&);
115 Inclusion ordering. return -2 if not comparable
119 _Interval__compare (const Interval_t
<T
>&a
,Interval_t
<T
> const&b
);
126 #include "compare.hh"
128 TEMPLATE_INSTANTIATE_COMPARE (Interval_t
<T
>&, Interval__compare
, template<class T
>);
133 intersection (Interval_t
<T
> a
, Interval_t
<T
> const&b
)
142 Interval_t
<T
> operator + (T a
,Interval_t
<T
> i
)
150 Interval_t
<T
> operator - (Interval_t
<T
> i
, T a
)
158 Interval_t
<T
> operator - (T a
,Interval_t
<T
> i
)
167 Interval_t
<T
> operator + (Interval_t
<T
> i
,T a
){
173 Interval_t
<T
> operator * (T a
,Interval_t
<T
> i
)
181 Interval_t
<T
> operator * (Interval_t
<T
> i
,T a
){
185 // again? see flower-proto.hh
186 typedef Interval_t
<Real
> Interval
;
187 typedef Interval_t
<int> Slice
; // weird name
190 #endif // INTERVAL_HH