#pragma once
#include <limits.h>
#include <wtf/MathExtras.h>
#include <wtf/PrintStream.h>
namespace WTF {
template<typename PassedType>
class Range {
public:
typedef PassedType Type;
Range()
: m_begin(0)
, m_end(0)
{
}
explicit Range(Type value)
: m_begin(value)
, m_end(value + 1)
{
ASSERT(m_end >= m_begin);
}
Range(Type begin, Type end)
: m_begin(begin)
, m_end(end)
{
ASSERT(m_end >= m_begin);
if (m_begin == m_end) {
m_begin = 0;
m_end = 0;
}
}
static Range top()
{
return Range(std::numeric_limits<Type>::min(), std::numeric_limits<Type>::max());
}
bool operator==(const Range& other) const
{
return m_begin == other.m_begin
&& m_end == other.m_end;
}
bool operator!=(const Range& other) const
{
return !(*this == other);
}
explicit operator bool() const { return m_begin != m_end; }
Range operator|(const Range& other) const
{
if (!*this)
return other;
if (!other)
return *this;
return Range(
std::min(m_begin, other.m_begin),
std::max(m_end, other.m_end));
}
Range& operator|=(const Range& other)
{
return *this = *this | other;
}
Type begin() const { return m_begin; }
Type end() const { return m_end; }
bool overlaps(const Range& other) const
{
return WTF::rangesOverlap(m_begin, m_end, other.m_begin, other.m_end);
}
void dump(PrintStream& out) const
{
if (*this == Range()) {
out.print("Bottom");
return;
}
if (*this == top()) {
out.print("Top");
return;
}
if (m_begin + 1 == m_end) {
out.print(m_begin);
return;
}
out.print(m_begin, "...", m_end);
}
private:
Type m_begin;
Type m_end;
};
}
using WTF::Range;