#ifndef _gpuCacheTimeInterval_h_
#define _gpuCacheTimeInterval_h_
namespace GPUCache {
class TimeInterval
{
public:
    enum InfiniteTag {
        kInfinite
    };
    enum InvalidTag{
        kInvalid
    };
    
    TimeInterval(InfiniteTag)
        : fStartTime(-std::numeric_limits<double>::max()),
        fEndTime(+std::numeric_limits<double>::max())
    {}
    
    TimeInterval(InvalidTag)
        : fStartTime(+std::numeric_limits<double>::max()),
        fEndTime(-std::numeric_limits<double>::max())
    {}
    
    TimeInterval(double startTime, double endTime)
        : fStartTime(startTime),
        fEndTime(endTime)
    {}
    bool contains(double time) const
    {
        return fStartTime <= time && time < fEndTime;
    }
    bool contains(const TimeInterval& other) const
    {
        return fStartTime <= other.fStartTime && other.fEndTime <= fEndTime;
    }
    
    void operator&=(const TimeInterval& other) 
    {
        fStartTime = std::max(fStartTime, other.fStartTime);
        fEndTime   = std::min(fEndTime,   other.fEndTime);
    }
    TimeInterval operator&(const TimeInterval& other) 
    {
        TimeInterval res = *this;
        res &= other;
        return res;
    }
    
    void operator|=(const TimeInterval& other)
    {
        fStartTime = std::min(fStartTime, other.fStartTime);
        fEndTime   = std::max(fEndTime,   other.fEndTime);
    }
    TimeInterval 
operator|(
const TimeInterval& other)
    {
        TimeInterval res = *this;
        res |= other;
        return res;
    }
    double startTime() const
    {
        return fStartTime;
    }
    double endTime() const
    {
        return fEndTime;
    }
    bool valid() const
    {
        return fStartTime < fEndTime;
    }
    bool operator==(const TimeInterval& other) 
    {
        return fStartTime == other.fStartTime && fEndTime == other.fEndTime;
    }
    bool operator!=(const TimeInterval& other) 
    {
        return fStartTime != other.fStartTime || fEndTime != other.fEndTime;
    }
private:
    double fStartTime;
    double fEndTime;
};
} 
#endif