#ifndef UI_GFX_RANGE_RANGE_F_H_
#define UI_GFX_RANGE_RANGE_F_H_
#include <iosfwd>
#include <limits>
#include <string>
#include "ui/gfx/range/gfx_range_export.h"
#include "ui/gfx/range/range.h"
namespace gfx {
class GFX_RANGE_EXPORT RangeF {
public:
constexpr RangeF() : RangeF(0.f) {}
constexpr RangeF(float start, float end) : start_(start), end_(end) {}
constexpr explicit RangeF(float position) : RangeF(position, position) {}
static constexpr RangeF InvalidRange() {
return RangeF(std::numeric_limits<float>::max());
}
constexpr bool IsValid() const { return *this != InvalidRange(); }
RangeF& MatchDirection(const RangeF& other) {
if (is_reversed() != other.is_reversed()) {
std::swap(start_, end_);
}
return *this;
}
constexpr float start() const { return start_; }
void set_start(float start) { start_ = start; }
constexpr float end() const { return end_; }
void set_end(float end) { end_ = end; }
constexpr float length() const { return GetMax() - GetMin(); }
constexpr bool is_reversed() const { return start() > end(); }
constexpr bool is_empty() const { return start() == end(); }
constexpr float GetMin() const { return start() < end() ? start() : end(); }
constexpr float GetMax() const { return start() > end() ? start() : end(); }
constexpr bool operator==(const RangeF& other) const = default;
constexpr auto operator<=>(const RangeF& other) const = default;
constexpr bool EqualsIgnoringDirection(const RangeF& other) const {
return GetMin() == other.GetMin() && GetMax() == other.GetMax();
}
constexpr bool Intersects(const RangeF& range) const {
return Intersect(range).IsValid();
}
constexpr bool IsBoundedBy(const RangeF& range) const {
return IsValid() && range.IsValid() && GetMin() >= range.GetMin() &&
GetMax() <= range.GetMax();
}
constexpr bool Contains(const RangeF& range) const {
return range.IsBoundedBy(*this) &&
(range.GetMax() != GetMax() || range.is_empty() == is_empty());
}
constexpr RangeF Intersect(const RangeF& range) const {
const float min = std::max(GetMin(), range.GetMin());
const float max = std::min(GetMax(), range.GetMax());
return (min < max || Contains(range) || range.Contains(*this))
? RangeF(min, max)
: InvalidRange();
}
RangeF Intersect(const Range& range) const;
Range Floor() const;
Range Ceil() const;
Range Round() const;
std::string ToString() const;
private:
float start_;
float end_;
};
GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os,
const RangeF& range);
}
#endif