Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:32

0001 // -*- C++ -*-
0002 //
0003 // Interval.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef ThePEG_Interval_H
0010 #define ThePEG_Interval_H
0011 // This is the declaration of the Interval class.
0012 
0013 #include <utility>
0014 #include <vector>
0015 #include "Interval.fh"
0016 #include "ThePEG/Utilities/UnitIO.h"
0017 
0018 namespace ThePEG {
0019 
0020 template <typename T, typename CMP>
0021 /**
0022  * An <code>Interval</code> object is used to represent an interval
0023  * <code>[ lower(), upper() )</code> where the ordering is defined by
0024  * the <code>bool operator()(const T &, const T &) const</code> member
0025  * of the CMP class (by defaut less<T>).
0026  */
0027 class Interval {
0028 
0029 public:
0030 
0031   /**
0032    * Construct an empty interval.
0033    */
0034   Interval() : theLimits(pair<T,T>()) {}
0035 
0036   /**
0037    * Construct interval [dn,up).
0038    */
0039   Interval(T dn, T up) : theLimits(pair<T,T>(dn, up)) {}
0040 
0041   /**
0042    * Test for equality.
0043    */
0044   bool operator==(const Interval & i) const {
0045     return lower() == i.lower() && upper() == i.upper();
0046   }
0047 
0048   /**
0049    * Test for ordering.
0050    * @return true if <code>lower() < i.lower()</code> or <code>lower()
0051    * == i.lower()</code> and <code>upper() < i.upper()</code>.
0052    */
0053   bool operator<(const Interval & i) const {
0054     return cmp(lower(), i.lower()) ||
0055       ( lower() == i.lower() && cmp(upper(), i.upper()) );
0056   }
0057 
0058   /**
0059    * Check consistency ie. that lower() < upper().
0060    */
0061   bool check() const { return cmp(lower(), upper()); }
0062 
0063   /**
0064    * Returns true if x is within the interval.
0065    */
0066   bool operator()(T x) const { return includes(x); }
0067 
0068   /**
0069    * Returns true if x is within the interval.
0070    */
0071   bool includes(T x) const { return !cmp(x, lower()) && cmp(x, upper()); }
0072 
0073   /**
0074    * Returns true if the whole of i is within the interval.
0075    */
0076   bool includes(const Interval<T,CMP> & i) const {
0077     return includes(i.lower()) && !cmp(upper(), i.upper());
0078   }
0079 
0080   /**
0081    * If x is in the interval return the interval [x,upper()) and
0082    * change this interval to [lower(),x). If x is not within the
0083    * interval, return [0,0) and leave this interval as it is.
0084    */
0085   Interval<T,CMP> chopUpper(T x) {
0086     Interval<T,CMP> r;
0087     if ( includes(x) ) {
0088       r.lower(x);
0089       r.upper(upper());
0090       upper(x);
0091     }
0092     return r;
0093   }
0094 
0095   /**
0096    * If x is in the interval return the interval [lower(),x) and
0097    * change this interval to [x,upper()). If x is not within the
0098    * interval, return [0,0) and leave this interval as it is.
0099    */
0100   Interval<T,CMP> chopLower(T x) {
0101     Interval<T,CMP> r;
0102     if ( includes(x) ) {
0103       r.lower(lower());
0104       r.upper(x);
0105       lower(x);
0106     }
0107     return r;
0108   }
0109 
0110   /**
0111    * If this interval operlaps with i return the overlapping interval.
0112    */
0113   Interval<T,CMP> overlap(const Interval & i) const {
0114     Interval<T,CMP> res;
0115     if ( operator==(i) ) res = i;
0116     if ( includes(i.upper()) || includes(i.lower()) )
0117       res = Interval<T,CMP>(max(lower(),i.lower()), min(upper(), i.upper()));
0118     return res;
0119   }
0120 
0121   /**
0122    * If this interval operlaps with i return the union of the two
0123    * intervals.
0124    */
0125   Interval<T,CMP> sum(const Interval & i) const {
0126     Interval<T,CMP> res;
0127     if ( operator==(i) ) res = i;
0128     if ( includes(i.upper()) || includes(i.lower()) )
0129       res = Interval<T,CMP>(min(lower(),i.lower()), max(upper(), i.upper()));
0130     return res;
0131   }
0132 
0133   /**
0134    * Return the upper limit of the interval.
0135    */
0136   T upper() const { return theLimits.second; }
0137 
0138   /**
0139    * Return the lower limit of the interval.
0140    */
0141   T lower() const { return theLimits.first; }
0142 
0143   /**
0144    * Set the upper limit of the interval.
0145    */
0146   void upper(T up) { theLimits.second = up; }
0147 
0148   /**
0149    * Set the lower limit of the interval.
0150    */
0151   void lower(T dn) { theLimits.first = dn; }
0152 
0153   /**
0154    * Check if any of the values in the iterator given range is
0155    * included in this interval.
0156    */
0157   template <typename Iterator>
0158   bool check(Iterator first, Iterator last);
0159 
0160   /**
0161    * Check if all of the values in the given iterator range is
0162    * included in this interval.
0163    */
0164   template <typename Iterator>
0165   bool checkAll(Iterator first, Iterator last);
0166 
0167   /**
0168    * If x is in the given interval, split the given interval in two,
0169    * otherwise return an empty list.
0170    */
0171   std::vector< Interval<T,CMP> > split(Interval<T,CMP>, T x);
0172   
0173   /**
0174    * For each value in the given range is in the given interval, split
0175    * the interval in two, otherwise return an empty list.
0176    */
0177   template<typename Iterator>
0178   std::vector< Interval<T,CMP> > split(Interval<T,CMP>,
0179                        Iterator first, Iterator last);
0180 
0181 private:
0182 
0183   /** The lower and upper limit of this interval */
0184   std::pair<T,T> theLimits;
0185 
0186   /** The object used for comparisons. */
0187   static CMP cmp;
0188 
0189 };
0190 
0191 /** An interval of doubles. */
0192 typedef Interval<double> DInterval;
0193 
0194 /** Helper function to create an interval of a type determined by the
0195  *  parameters. */
0196 template <typename T, typename CMP>
0197 inline Interval<T,CMP> makeInterval(T dn, T up) { return Interval<T,CMP>(dn, up); }
0198 
0199 /** Ouptut an interval to a stream. */
0200 template <typename OStream, typename T, typename CMP>
0201 inline OStream & operator<<(OStream & os, const Interval<T,CMP> & i) {
0202   os << i.lower() << i.upper();
0203   return os;
0204 }
0205 
0206 /** Input an interval from a stream. */
0207 template <typename IStream, typename T, typename CMP>
0208 inline IStream & operator>>(IStream & is, Interval<T,CMP> & i) {
0209   T up, dn;
0210   is >> dn >> up;
0211   i.lower(dn);
0212   i.upper(up);
0213   return is;
0214 }
0215 
0216 /** Output an interval of a diminsionful type to a stream using the
0217  *  given unit.
0218  * @param os the output stream.
0219  * @param i the interval.
0220  * @param u the unit. */
0221 template <typename OStream, typename T, typename CMP, typename UT>
0222 void ounitstream(OStream & os, const Interval<T,CMP> & i, UT & u) {
0223   os << ounit(i.lower(), u) << ounit(i.upper(), u);
0224 }
0225 
0226 /** Input an interval of a diminsionful type from a stream using the
0227  *  given unit.
0228  * @param is the input stream.
0229  * @param i the interval.
0230  * @param u the unit. */
0231 template <typename IStream, typename T, typename CMP, typename UT>
0232 void iunitstream(IStream & is, Interval<T,CMP> & i, UT & u) {
0233   T low, upp;
0234   is >> iunit(low, u) >> iunit(upp, u);
0235   i = Interval<T,CMP>(low, upp);
0236 }
0237 
0238 }
0239 
0240 #ifndef ThePEG_TEMPLATES_IN_CC_FILE
0241 #include "Interval.tcc"
0242 #endif
0243 
0244 #endif /* ThePEG_Interval_H */