#pragma once
#include <wtf/Vector.h>
namespace WebCore {
template <typename T>
class ShapeInterval {
WTF_MAKE_FAST_ALLOCATED;
public:
ShapeInterval()
: m_x1(-1)
, m_x2(-2)
{
ASSERT(isUndefined());
}
ShapeInterval(T x1, T x2)
: m_x1(x1)
, m_x2(x2)
{
ASSERT(x2 >= x1);
}
bool isUndefined() const { return m_x2 < m_x1; }
T x1() const { return isUndefined() ? 0 : m_x1; }
T x2() const { return isUndefined() ? 0 : m_x2; }
T width() const { return isUndefined() ? 0 : m_x2 - m_x1; }
bool isEmpty() const { return isUndefined() ? true : m_x1 == m_x2; }
void set(T x1, T x2)
{
ASSERT(x2 >= x1);
m_x1 = x1;
m_x2 = x2;
}
bool overlaps(const ShapeInterval<T>& interval) const
{
if (isUndefined() || interval.isUndefined())
return false;
return x2() >= interval.x1() && x1() <= interval.x2();
}
bool contains(const ShapeInterval<T>& interval) const
{
if (isUndefined() || interval.isUndefined())
return false;
return x1() <= interval.x1() && x2() >= interval.x2();
}
void unite(const ShapeInterval<T>& interval)
{
if (interval.isUndefined())
return;
if (isUndefined())
set(interval.x1(), interval.x2());
else
set(std::min<T>(x1(), interval.x1()), std::max<T>(x2(), interval.x2()));
}
bool operator==(const ShapeInterval<T>& other) const { return x1() == other.x1() && x2() == other.x2(); }
bool operator!=(const ShapeInterval<T>& other) const { return !operator==(other); }
private:
T m_x1;
T m_x2;
};
typedef ShapeInterval<int> IntShapeInterval;
typedef ShapeInterval<float> FloatShapeInterval;
typedef Vector<IntShapeInterval> IntShapeIntervals;
typedef Vector<FloatShapeInterval> FloatShapeIntervals;
}