CMS 3D CMS Logo

Interval.h

Go to the documentation of this file.
00001 #ifndef CondTools_L1Trigger_Interval_h
00002 #define CondTools_L1Trigger_Interval_h
00003 
00004 #include <map>
00005 
00006 namespace l1t
00007 {
00008     /* Template class that will be used to represnt interval from one value to another.
00009      * In general this class is not interested in what interval mark means, most of the time
00010      * it should be number, time or something similar
00011      *
00012      * This class requires that TimeType should have defined operator == and < as defined in STL.
00013      * It is enforced via sserts that start time is less then end time under provided operator <.
00014      * TimeType requires empty constructor.
00015      *
00016      * Payload should have defined copy constructor and assigment operator.
00017      */
00018     template<typename TimeType, typename PayloadType>
00019     class Interval
00020     {
00021     public:
00022         /* Construncts the class with provided start and end times. Payload is created
00023          * with default constructor.
00024          */
00025         Interval (const TimeType& start, const TimeType& end)
00026             : m_start (start), m_end (end), isInvalid (false)
00027         { assert (m_start <= m_end); }
00028 
00029         /* Constructs the class with given start and end times, as well as given payload.
00030          */
00031         Interval (const TimeType& start, const TimeType& end, const PayloadType& payload)
00032             : m_start(start), m_end (end), _payload (payload), isInvalid (false) {}
00033 
00034         /* Sets the payload to the given one. */
00035         void setPayload (const PayloadType& payload) { this->_payload = payload; }
00036         /* Returns the payload */
00037         const PayloadType& payload () const { return this->_payload; }
00038 
00039         /* Returns start time */
00040         const TimeType & start () const { return this->m_start; }
00041         /* Returns end time */
00042         const TimeType & end () const { return this->m_end; }
00043 
00044         /* Static member that will define an invalid interval. Two invalid interfaces are
00045          * always considered equal.
00046          */
00047         static Interval & invalid ();
00048 
00049         // Operator overloading
00050         bool operator== (const Interval<TimeType, PayloadType> & other) const
00051         { return (this->isInvalid == true && other.isInvalid == true ) ||
00052             (this->start () == other.start ()) && (this->end () == other.end () &&
00053                     this->isInvalid == other.isInvalid); }
00054 
00055         bool operator!= (const Interval<TimeType, PayloadType> & other) const
00056         { return ! (*this == other); }
00057 
00058     protected:
00059         /* Private data */
00060         TimeType m_start;
00061         TimeType m_end;
00062         PayloadType _payload;
00063 
00064         /* flag that will check if this interval is invalid */
00065         bool isInvalid;
00066     };
00067 
00068     /* Manages a list of intervals and provides method to find interval that contains
00069      * some value.
00070      *
00071      * Template parameters are used to manage Interval class, so all requirements to these
00072      * parameters comes from Interval class
00073      */
00074     template<typename TimeType, typename PayloadType>
00075     class IntervalManager
00076     {
00077     public:
00078         /* Adds one given interval to the list of intervals.
00079          */
00080         void addInterval (const Interval<TimeType, PayloadType> & interval)
00081         { intervalMap.insert (std::make_pair (interval.start (), interval)); }
00082 
00083         /* Removes all stored intervals from the list
00084          */
00085         void clear () { intervalMap.clear (); }
00086 
00087         /* Returns interval that contaisn given time. If multiple intervals exists
00088          * any of them is returned
00089          */
00090         const Interval<TimeType, PayloadType> & find (const TimeType & time) const;
00091 
00092     protected:
00093         /* Information to store list of intervals */
00094         typedef std::map<TimeType, Interval<TimeType, PayloadType> > IntervalMap;
00095         IntervalMap intervalMap;
00096     };
00097 
00098 } // namespace
00099 
00100 // implementation
00101 #include "CondTools/L1Trigger/src/Interval.icc"
00102 
00103 #endif

Generated on Tue Jun 9 17:26:54 2009 for CMSSW by  doxygen 1.5.4