thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
Interval.h
1 // -*- C++ -*-
2 //
3 // Interval.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2019 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef ThePEG_Interval_H
10 #define ThePEG_Interval_H
11 // This is the declaration of the Interval class.
12 
13 #include <utility>
14 #include <vector>
15 #include "Interval.fh"
16 #include "ThePEG/Utilities/UnitIO.h"
17 
18 namespace ThePEG {
19 
20 template <typename T, typename CMP>
27 class Interval {
28 
29 public:
30 
34  Interval() : theLimits(pair<T,T>()) {}
35 
39  Interval(T dn, T up) : theLimits(pair<T,T>(dn, up)) {}
40 
44  bool operator==(const Interval & i) const {
45  return lower() == i.lower() && upper() == i.upper();
46  }
47 
53  bool operator<(const Interval & i) const {
54  return cmp(lower(), i.lower()) ||
55  ( lower() == i.lower() && cmp(upper(), i.upper()) );
56  }
57 
61  bool check() const { return cmp(lower(), upper()); }
62 
66  bool operator()(T x) const { return includes(x); }
67 
71  bool includes(T x) const { return !cmp(x, lower()) && cmp(x, upper()); }
72 
76  bool includes(const Interval<T,CMP> & i) const {
77  return includes(i.lower()) && !cmp(upper(), i.upper());
78  }
79 
87  if ( includes(x) ) {
88  r.lower(x);
89  r.upper(upper());
90  upper(x);
91  }
92  return r;
93  }
94 
101  Interval<T,CMP> r;
102  if ( includes(x) ) {
103  r.lower(lower());
104  r.upper(x);
105  lower(x);
106  }
107  return r;
108  }
109 
113  Interval<T,CMP> overlap(const Interval & i) const {
114  Interval<T,CMP> res;
115  if ( operator==(i) ) res = i;
116  if ( includes(i.upper()) || includes(i.lower()) )
117  res = Interval<T,CMP>(max(lower(),i.lower()), min(upper(), i.upper()));
118  return res;
119  }
120 
125  Interval<T,CMP> sum(const Interval & i) const {
126  Interval<T,CMP> res;
127  if ( operator==(i) ) res = i;
128  if ( includes(i.upper()) || includes(i.lower()) )
129  res = Interval<T,CMP>(min(lower(),i.lower()), max(upper(), i.upper()));
130  return res;
131  }
132 
136  T upper() const { return theLimits.second; }
137 
141  T lower() const { return theLimits.first; }
142 
146  void upper(T up) { theLimits.second = up; }
147 
151  void lower(T dn) { theLimits.first = dn; }
152 
157  template <typename Iterator>
158  bool check(Iterator first, Iterator last);
159 
164  template <typename Iterator>
165  bool checkAll(Iterator first, Iterator last);
166 
171  std::vector< Interval<T,CMP> > split(Interval<T,CMP>, T x);
172 
177  template<typename Iterator>
178  std::vector< Interval<T,CMP> > split(Interval<T,CMP>,
179  Iterator first, Iterator last);
180 
181 private:
182 
184  std::pair<T,T> theLimits;
185 
187  static CMP cmp;
188 
189 };
190 
193 
196 template <typename T, typename CMP>
197 inline Interval<T,CMP> makeInterval(T dn, T up) { return Interval<T,CMP>(dn, up); }
198 
200 template <typename OStream, typename T, typename CMP>
201 inline OStream & operator<<(OStream & os, const Interval<T,CMP> & i) {
202  os << i.lower() << i.upper();
203  return os;
204 }
205 
207 template <typename IStream, typename T, typename CMP>
208 inline IStream & operator>>(IStream & is, Interval<T,CMP> & i) {
209  T up, dn;
210  is >> dn >> up;
211  i.lower(dn);
212  i.upper(up);
213  return is;
214 }
215 
221 template <typename OStream, typename T, typename CMP, typename UT>
222 void ounitstream(OStream & os, const Interval<T,CMP> & i, UT & u) {
223  os << ounit(i.lower(), u) << ounit(i.upper(), u);
224 }
225 
231 template <typename IStream, typename T, typename CMP, typename UT>
232 void iunitstream(IStream & is, Interval<T,CMP> & i, UT & u) {
233  T low, upp;
234  is >> iunit(low, u) >> iunit(upp, u);
235  i = Interval<T,CMP>(low, upp);
236 }
237 
238 }
239 
240 #ifndef ThePEG_TEMPLATES_IN_CC_FILE
241 #include "Interval.tcc"
242 #endif
243 
244 #endif /* ThePEG_Interval_H */
Interval< T, CMP > chopUpper(T x)
If x is in the interval return the interval [x,upper()) and change this interval to [lower()...
Definition: Interval.h:85
void ounitstream(OStream &os, const vector< T, Alloc > &v, UT &u)
Ouput a vector of objects with the specified unit.
Definition: Containers.h:275
bool checkAll(Iterator first, Iterator last)
Check if all of the values in the given iterator range is included in this interval.
Interval< T, CMP > chopLower(T x)
If x is in the interval return the interval [lower(),x) and change this interval to [x...
Definition: Interval.h:100
void lower(T dn)
Set the lower limit of the interval.
Definition: Interval.h:151
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
std::vector< Interval< T, CMP > > split(Interval< T, CMP >, T x)
If x is in the given interval, split the given interval in two, otherwise return an empty list...
T lower() const
Return the lower limit of the interval.
Definition: Interval.h:141
void iunitstream(IStream &is, vector< T, Alloc > &v, UT &u)
Input a vector of objects with the specified unit.
Definition: Containers.h:289
Interval()
Construct an empty interval.
Definition: Interval.h:34
An Interval object is used to represent an interval [ lower(), upper() ) where the ordering is define...
Definition: Interval.h:27
Interval< double > DInterval
An interval of doubles.
Definition: Interval.h:192
OUnit< T, UT > ounit(const T &t, const UT &ut)
Helper function creating a OUnit object given an object and a unit.
Definition: UnitIO.h:84
vector< T > & operator>>(vector< T > &tv, U &u)
Overload the right shift operator for vector to pop objects from a vector.
Definition: Containers.h:192
std::pair< T, T > theLimits
The lower and upper limit of this interval.
Definition: Interval.h:184
bool includes(const Interval< T, CMP > &i) const
Returns true if the whole of i is within the interval.
Definition: Interval.h:76
static CMP cmp
The object used for comparisons.
Definition: Interval.h:187
Interval< T, CMP > sum(const Interval &i) const
If this interval operlaps with i return the union of the two intervals.
Definition: Interval.h:125
bool operator()(T x) const
Returns true if x is within the interval.
Definition: Interval.h:66
bool check() const
Check consistency ie.
Definition: Interval.h:61
bool operator==(const Interval &i) const
Test for equality.
Definition: Interval.h:44
bool operator<(const Interval &i) const
Test for ordering.
Definition: Interval.h:53
T upper() const
Return the upper limit of the interval.
Definition: Interval.h:136
Interval< T, CMP > makeInterval(T dn, T up)
Helper function to create an interval of a type determined by the parameters.
Definition: Interval.h:197
Interval(T dn, T up)
Construct interval [dn,up).
Definition: Interval.h:39
Interval< T, CMP > overlap(const Interval &i) const
If this interval operlaps with i return the overlapping interval.
Definition: Interval.h:113
bool includes(T x) const
Returns true if x is within the interval.
Definition: Interval.h:71
IUnit< T, UT > iunit(T &t, const UT &ut)
Helper function creating a IUnit object given an object and a unit.
Definition: UnitIO.h:91
void upper(T up)
Set the upper limit of the interval.
Definition: Interval.h:146