00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef ThePEG_Interval_H
00010 #define ThePEG_Interval_H
00011
00012
00013 #include <utility>
00014 #include <vector>
00015 #include "Interval.fh"
00016 #include "ThePEG/Utilities/UnitIO.h"
00017
00018 namespace ThePEG {
00019
00020 template <typename T, typename CMP>
00027 class Interval {
00028
00029 public:
00030
00034 Interval() : theLimits(pair<T,T>()) {}
00035
00039 Interval(T dn, T up) : theLimits(pair<T,T>(dn, up)) {}
00040
00044 bool operator==(const Interval & i) const {
00045 return lower() == i.lower() && upper() == i.upper();
00046 }
00047
00053 bool operator<(const Interval & i) const {
00054 return cmp(lower(), i.lower()) ||
00055 ( lower() == i.lower() && cmp(upper(), i.upper()) );
00056 }
00057
00061 bool check() const { return cmp(lower(), upper()); }
00062
00066 bool operator()(T x) const { return includes(x); }
00067
00071 bool includes(T x) const { return !cmp(x, lower()) && cmp(x, upper()); }
00072
00076 bool includes(const Interval<T,CMP> & i) const {
00077 return includes(i.lower()) && !cmp(upper(), i.upper());
00078 }
00079
00085 Interval<T,CMP> chopUpper(T x) {
00086 Interval<T,CMP> r;
00087 if ( includes(x) ) {
00088 r.lower(x);
00089 r.upper(upper());
00090 upper(x);
00091 }
00092 return r;
00093 }
00094
00100 Interval<T,CMP> chopLower(T x) {
00101 Interval<T,CMP> r;
00102 if ( includes(x) ) {
00103 r.lower(lower());
00104 r.upper(x);
00105 lower(x);
00106 }
00107 return r;
00108 }
00109
00113 Interval<T,CMP> overlap(const Interval & i) const {
00114 Interval<T,CMP> res;
00115 if ( operator==(i) ) res = i;
00116 if ( includes(i.upper()) || includes(i.lower()) )
00117 res = Interval<T,CMP>(max(lower(),i.lower()), min(upper(), i.upper()));
00118 return res;
00119 }
00120
00125 Interval<T,CMP> sum(const Interval & i) const {
00126 Interval<T,CMP> res;
00127 if ( operator==(i) ) res = i;
00128 if ( includes(i.upper()) || includes(i.lower()) )
00129 res = Interval<T,CMP>(min(lower(),i.lower()), max(upper(), i.upper()));
00130 return res;
00131 }
00132
00136 T upper() const { return theLimits.second; }
00137
00141 T lower() const { return theLimits.first; }
00142
00146 void upper(T up) { theLimits.second = up; }
00147
00151 void lower(T dn) { theLimits.first = dn; }
00152
00157 template <typename Iterator>
00158 static bool check(Iterator first, Iterator last);
00159
00164 template <typename Iterator>
00165 static bool checkAll(Iterator first, Iterator last);
00166
00171 static std::vector< Interval<T,CMP> > split(Interval<T,CMP>, T x);
00172
00177 template<typename Iterator>
00178 static std::vector< Interval<T,CMP> > split(Interval<T,CMP>,
00179 Iterator first, Iterator last);
00180
00181 private:
00182
00184 std::pair<T,T> theLimits;
00185
00187 static CMP cmp;
00188
00189 };
00190
00192 typedef Interval<double> DInterval;
00193
00196 template <typename T, typename CMP>
00197 inline Interval<T,CMP> makeInterval(T dn, T up) { return Interval<T,CMP>(dn, up); }
00198
00200 template <typename OStream, typename T, typename CMP>
00201 inline OStream & operator<<(OStream & os, const Interval<T,CMP> & i) {
00202 os << i.lower() << i.upper();
00203 return os;
00204 }
00205
00207 template <typename IStream, typename T, typename CMP>
00208 inline IStream & operator>>(IStream & is, Interval<T,CMP> & i) {
00209 T up, dn;
00210 is >> dn >> up;
00211 i.lower(dn);
00212 i.upper(up);
00213 return is;
00214 }
00215
00221 template <typename OStream, typename T, typename CMP, typename UT>
00222 void ounitstream(OStream & os, const Interval<T,CMP> & i, UT & u) {
00223 os << ounit(i.lower(), u) << ounit(i.upper(), u);
00224 }
00225
00231 template <typename IStream, typename T, typename CMP, typename UT>
00232 void iunitstream(IStream & is, Interval<T,CMP> & i, UT & u) {
00233 T low, upp;
00234 is >> iunit(low, u) >> iunit(upp, u);
00235 i = Interval<T,CMP>(low, upp);
00236 }
00237
00238 }
00239
00240 #ifndef ThePEG_TEMPLATES_IN_CC_FILE
00241 #include "Interval.tcc"
00242 #endif
00243
00244 #endif