Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: ui/gfx/range/range_f.cc

Issue 2734663002: Make much of gfx::Range[F] constexpr. (Closed)
Patch Set: size_t Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/range/range_f.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/range/range_f.h" 5 #include "ui/gfx/range/range_f.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
11 #include <limits>
12 11
13 #include "base/format_macros.h" 12 #include "base/format_macros.h"
14 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
15 14
16 namespace gfx { 15 namespace gfx {
17 16
18 RangeF::RangeF()
19 : start_(0.0f),
20 end_(0.0f) {
21 }
22
23 RangeF::RangeF(float start, float end)
24 : start_(start),
25 end_(end) {
26 }
27
28 RangeF::RangeF(float position)
29 : start_(position),
30 end_(position) {
31 }
32
33 // static
34 const RangeF RangeF::InvalidRange() {
35 return RangeF(std::numeric_limits<float>::max());
36 }
37
38 bool RangeF::IsValid() const {
39 return *this != InvalidRange();
40 }
41
42 float RangeF::GetMin() const {
43 return std::min(start(), end());
44 }
45
46 float RangeF::GetMax() const {
47 return std::max(start(), end());
48 }
49
50 bool RangeF::operator==(const RangeF& other) const {
51 return start() == other.start() && end() == other.end();
52 }
53
54 bool RangeF::operator!=(const RangeF& other) const {
55 return !(*this == other);
56 }
57
58 bool RangeF::EqualsIgnoringDirection(const RangeF& other) const {
59 return GetMin() == other.GetMin() && GetMax() == other.GetMax();
60 }
61
62 bool RangeF::Intersects(const RangeF& range) const {
63 return IsValid() && range.IsValid() &&
64 !(range.GetMax() < GetMin() || range.GetMin() >= GetMax());
65 }
66
67 bool RangeF::Contains(const RangeF& range) const {
68 return IsValid() && range.IsValid() &&
69 GetMin() <= range.GetMin() && range.GetMax() <= GetMax();
70 }
71
72 RangeF RangeF::Intersect(const RangeF& range) const { 17 RangeF RangeF::Intersect(const RangeF& range) const {
73 float min = std::max(GetMin(), range.GetMin()); 18 float min = std::max(GetMin(), range.GetMin());
74 float max = std::min(GetMax(), range.GetMax()); 19 float max = std::min(GetMax(), range.GetMax());
75 20
76 if (min >= max) // No intersection. 21 if (min >= max) // No intersection.
77 return InvalidRange(); 22 return InvalidRange();
78 23
79 return RangeF(min, max); 24 return RangeF(min, max);
80 } 25 }
81 26
82 RangeF RangeF::Intersect(const Range& range) const { 27 RangeF RangeF::Intersect(const Range& range) const {
83 RangeF range_f(range.start(), range.end()); 28 RangeF range_f(range.start(), range.end());
84 return Intersect(range_f); 29 return Intersect(range_f);
85 } 30 }
86 31
87 Range RangeF::Floor() const { 32 Range RangeF::Floor() const {
88 size_t start = start_ > 0.0f ? static_cast<size_t>(std::floor(start_)) : 0; 33 uint32_t start = start_ > 0 ? static_cast<uint32_t>(std::floor(start_)) : 0;
89 size_t end = end_ > 0.0f ? static_cast<size_t>(std::floor(end_)) : 0; 34 uint32_t end = end_ > 0 ? static_cast<uint32_t>(std::floor(end_)) : 0;
90 return Range(start, end); 35 return Range(start, end);
91 } 36 }
92 37
93 Range RangeF::Ceil() const { 38 Range RangeF::Ceil() const {
94 size_t start = start_ > 0.0f ? static_cast<size_t>(std::ceil(start_)) : 0; 39 uint32_t start = start_ > 0 ? static_cast<uint32_t>(std::ceil(start_)) : 0;
95 size_t end = end_ > 0.0f ? static_cast<size_t>(std::ceil(end_)) : 0; 40 uint32_t end = end_ > 0 ? static_cast<uint32_t>(std::ceil(end_)) : 0;
96 return Range(start, end); 41 return Range(start, end);
97 } 42 }
98 43
99 Range RangeF::Round() const { 44 Range RangeF::Round() const {
100 size_t start = 45 uint32_t start = start_ > 0 ? static_cast<uint32_t>(std::round(start_)) : 0;
101 start_ > 0.0f ? static_cast<size_t>(std::floor(start_ + 0.5f)) : 0; 46 uint32_t end = end_ > 0 ? static_cast<uint32_t>(std::round(end_)) : 0;
102 size_t end = end_ > 0.0f ? static_cast<size_t>(std::floor(end_ + 0.5f)) : 0;
103 return Range(start, end); 47 return Range(start, end);
104 } 48 }
105 49
106 std::string RangeF::ToString() const { 50 std::string RangeF::ToString() const {
107 return base::StringPrintf("{%f,%f}", start(), end()); 51 return base::StringPrintf("{%f,%f}", start(), end());
108 } 52 }
109 53
110 std::ostream& operator<<(std::ostream& os, const RangeF& range) { 54 std::ostream& operator<<(std::ostream& os, const RangeF& range) {
111 return os << range.ToString(); 55 return os << range.ToString();
112 } 56 }
113 57
114 } // namespace gfx 58 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/range/range_f.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698