File: range_f.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,312 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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 {

// A float version of Range. RangeF is made of a start and end position; when
// they are the same, the range is empty. Note that |start_| can be greater
// than |end_| to respect the directionality of the range.
class GFX_RANGE_EXPORT RangeF {
 public:
  // Creates an empty range {0,0}.
  constexpr RangeF() : RangeF(0.f) {}

  // Initializes the range with a start and end.
  constexpr RangeF(float start, float end) : start_(start), end_(end) {}

  // Initializes the range with the same start and end positions.
  constexpr explicit RangeF(float position) : RangeF(position, position) {}

  // Returns a range that is invalid, which is {float_max,float_max}.
  static constexpr RangeF InvalidRange() {
    return RangeF(std::numeric_limits<float>::max());
  }

  // Checks if the range is valid through comparison to InvalidRange().
  constexpr bool IsValid() const { return *this != InvalidRange(); }

  // Ensures that the direction of this range matches the direction of the
  // provided range, reversing this range if necessary. Returns a reference to
  // `this` to allow method chaining.
  RangeF& MatchDirection(const RangeF& other) {
    if (is_reversed() != other.is_reversed()) {
      std::swap(start_, end_);
    }
    return *this;
  }

  // Getters and setters.
  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; }

  // Returns the absolute value of the length.
  constexpr float length() const { return GetMax() - GetMin(); }

  constexpr bool is_reversed() const { return start() > end(); }
  constexpr bool is_empty() const { return start() == end(); }

  // Returns the minimum and maximum values.
  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();
  }

  // Returns true if this range intersects the specified |range|.
  constexpr bool Intersects(const RangeF& range) const {
    return Intersect(range).IsValid();
  }

  // Returns true if this range is contained by the specified |range| or it is
  // an empty range and ending the range |range|. (copied from gfx::Range)
  constexpr bool IsBoundedBy(const RangeF& range) const {
    return IsValid() && range.IsValid() && GetMin() >= range.GetMin() &&
           GetMax() <= range.GetMax();
  }

  // Returns true if this range contains the specified |range|.
  constexpr bool Contains(const RangeF& range) const {
    return range.IsBoundedBy(*this) &&
           // A non-empty range doesn't contain the range [max, max).
           (range.GetMax() != GetMax() || range.is_empty() == is_empty());
  }

  // Computes the intersection of this range with the given |range|.
  // If they don't intersect, it returns an InvalidRange().
  // The returned range is always empty or forward (never reversed).
  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;

  // Floor/Ceil/Round the start and end values of the given RangeF.
  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);

}  // namespace gfx

#endif  // UI_GFX_RANGE_RANGE_F_H_