Home | History | Annotate | Download | only in range
      1 // Copyright 2015 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef UI_GFX_RANGE_RANGE_F_H_
      6 #define UI_GFX_RANGE_RANGE_F_H_
      7 
      8 #include <limits>
      9 #include <ostream>
     10 #include <string>
     11 
     12 #include "ui/gfx/range/gfx_range_export.h"
     13 #include "ui/gfx/range/range.h"
     14 
     15 namespace gfx {
     16 
     17 // A float version of Range. RangeF is made of a start and end position; when
     18 // they are the same, the range is empty. Note that |start_| can be greater
     19 // than |end_| to respect the directionality of the range.
     20 class GFX_RANGE_EXPORT RangeF {
     21  public:
     22   // Creates an empty range {0,0}.
     23   constexpr RangeF() : RangeF(0.f) {}
     24 
     25   // Initializes the range with a start and end.
     26   constexpr RangeF(float start, float end) : start_(start), end_(end) {}
     27 
     28   // Initializes the range with the same start and end positions.
     29   constexpr explicit RangeF(float position) : RangeF(position, position) {}
     30 
     31   // Returns a range that is invalid, which is {float_max,float_max}.
     32   static constexpr RangeF InvalidRange() {
     33     return RangeF(std::numeric_limits<float>::max());
     34   }
     35 
     36   // Checks if the range is valid through comparison to InvalidRange().
     37   constexpr bool IsValid() const { return *this != InvalidRange(); }
     38 
     39   // Getters and setters.
     40   constexpr float start() const { return start_; }
     41   void set_start(float start) { start_ = start; }
     42 
     43   constexpr float end() const { return end_; }
     44   void set_end(float end) { end_ = end; }
     45 
     46   // Returns the absolute value of the length.
     47   constexpr float length() const { return GetMax() - GetMin(); }
     48 
     49   constexpr bool is_reversed() const { return start() > end(); }
     50   constexpr bool is_empty() const { return start() == end(); }
     51 
     52   // Returns the minimum and maximum values.
     53   constexpr float GetMin() const { return start() < end() ? start() : end(); }
     54   constexpr float GetMax() const { return start() > end() ? start() : end(); }
     55 
     56   constexpr bool operator==(const RangeF& other) const {
     57     return start() == other.start() && end() == other.end();
     58   }
     59   constexpr bool operator!=(const RangeF& other) const {
     60     return !(*this == other);
     61   }
     62   constexpr bool EqualsIgnoringDirection(const RangeF& other) const {
     63     return GetMin() == other.GetMin() && GetMax() == other.GetMax();
     64   }
     65 
     66   // Returns true if this range intersects the specified |range|.
     67   constexpr bool Intersects(const RangeF& range) const {
     68     return IsValid() && range.IsValid() &&
     69            !(range.GetMax() < GetMin() || range.GetMin() >= GetMax());
     70   }
     71 
     72   // Returns true if this range contains the specified |range|.
     73   constexpr bool Contains(const RangeF& range) const {
     74     return IsValid() && range.IsValid() && GetMin() <= range.GetMin() &&
     75            range.GetMax() <= GetMax();
     76   }
     77 
     78   // Computes the intersection of this range with the given |range|.
     79   // If they don't intersect, it returns an InvalidRange().
     80   // The returned range is always empty or forward (never reversed).
     81   RangeF Intersect(const RangeF& range) const;
     82   RangeF Intersect(const Range& range) const;
     83 
     84   // Floor/Ceil/Round the start and end values of the given RangeF.
     85   Range Floor() const;
     86   Range Ceil() const;
     87   Range Round() const;
     88 
     89   std::string ToString() const;
     90 
     91  private:
     92   float start_;
     93   float end_;
     94 };
     95 
     96 GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os,
     97                                           const RangeF& range);
     98 
     99 }  // namespace gfx
    100 
    101 #endif  // UI_GFX_RANGE_RANGE_F_H_
    102